qscicommand.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // This module implements the QsciCommand class.
  2. //
  3. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of QScintilla.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. #include "Qsci/qscicommand.h"
  20. #include <qnamespace.h>
  21. #include <qapplication.h>
  22. #include "Qsci/qsciscintilla.h"
  23. #include "Qsci/qsciscintillabase.h"
  24. static int convert(int key);
  25. // The ctor.
  26. QsciCommand::QsciCommand(QsciScintilla *qs, QsciCommand::Command cmd, int key,
  27. int altkey, const char *desc)
  28. : qsCmd(qs), scicmd(cmd), qkey(key), qaltkey(altkey), descCmd(desc)
  29. {
  30. scikey = convert(qkey);
  31. if (scikey)
  32. qsCmd->SendScintilla(QsciScintillaBase::SCI_ASSIGNCMDKEY, scikey,
  33. scicmd);
  34. scialtkey = convert(qaltkey);
  35. if (scialtkey)
  36. qsCmd->SendScintilla(QsciScintillaBase::SCI_ASSIGNCMDKEY, scialtkey,
  37. scicmd);
  38. }
  39. // Execute the command.
  40. void QsciCommand::execute()
  41. {
  42. qsCmd->SendScintilla(scicmd);
  43. }
  44. // Bind a key to a command.
  45. void QsciCommand::setKey(int key)
  46. {
  47. bindKey(key,qkey,scikey);
  48. }
  49. // Bind an alternate key to a command.
  50. void QsciCommand::setAlternateKey(int altkey)
  51. {
  52. bindKey(altkey,qaltkey,scialtkey);
  53. }
  54. // Do the hard work of binding a key.
  55. void QsciCommand::bindKey(int key,int &qk,int &scik)
  56. {
  57. int new_scikey;
  58. // Ignore if it is invalid, allowing for the fact that we might be
  59. // unbinding it.
  60. if (key)
  61. {
  62. new_scikey = convert(key);
  63. if (!new_scikey)
  64. return;
  65. }
  66. else
  67. new_scikey = 0;
  68. if (scik)
  69. qsCmd->SendScintilla(QsciScintillaBase::SCI_CLEARCMDKEY, scik);
  70. qk = key;
  71. scik = new_scikey;
  72. if (scik)
  73. qsCmd->SendScintilla(QsciScintillaBase::SCI_ASSIGNCMDKEY, scik, scicmd);
  74. }
  75. // See if a key is valid.
  76. bool QsciCommand::validKey(int key)
  77. {
  78. return convert(key);
  79. }
  80. // Convert a Qt character to the Scintilla equivalent. Return zero if it is
  81. // invalid.
  82. static int convert(int key)
  83. {
  84. // Convert the modifiers.
  85. int sci_mod = 0;
  86. if (key & Qt::SHIFT)
  87. sci_mod |= QsciScintillaBase::SCMOD_SHIFT;
  88. if (key & Qt::CTRL)
  89. sci_mod |= QsciScintillaBase::SCMOD_CTRL;
  90. if (key & Qt::ALT)
  91. sci_mod |= QsciScintillaBase::SCMOD_ALT;
  92. if (key & Qt::META)
  93. sci_mod |= QsciScintillaBase::SCMOD_META;
  94. key &= ~Qt::MODIFIER_MASK;
  95. // Convert the key.
  96. int sci_key = QsciScintillaBase::commandKey(key, sci_mod);
  97. if (sci_key)
  98. sci_key |= (sci_mod << 16);
  99. return sci_key;
  100. }
  101. // Return the translated user friendly description.
  102. QString QsciCommand::description() const
  103. {
  104. return qApp->translate("QsciCommand", descCmd);
  105. }