1 // This module implements the QsciCommand class.
2 //
3 // Copyright (c) 2019 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 
20 
21 #include "Qsci/qscicommand.h"
22 
23 #include <qnamespace.h>
24 #include <qapplication.h>
25 
26 #include "Qsci/qsciscintilla.h"
27 #include "Qsci/qsciscintillabase.h"
28 
29 
30 static int convert(int key);
31 
32 
33 // The ctor.
QsciCommand(QsciScintilla * qs,QsciCommand::Command cmd,int key,int altkey,const char * desc)34 QsciCommand::QsciCommand(QsciScintilla *qs, QsciCommand::Command cmd, int key,
35         int altkey, const char *desc)
36     : qsCmd(qs), scicmd(cmd), qkey(key), qaltkey(altkey), descCmd(desc)
37 {
38     scikey = convert(qkey);
39 
40     if (scikey)
41         qsCmd->SendScintilla(QsciScintillaBase::SCI_ASSIGNCMDKEY, scikey,
42                 scicmd);
43 
44     scialtkey = convert(qaltkey);
45 
46     if (scialtkey)
47         qsCmd->SendScintilla(QsciScintillaBase::SCI_ASSIGNCMDKEY, scialtkey,
48                 scicmd);
49 }
50 
51 
52 // Execute the command.
execute()53 void QsciCommand::execute()
54 {
55     qsCmd->SendScintilla(scicmd);
56 }
57 
58 
59 // Bind a key to a command.
setKey(int key)60 void QsciCommand::setKey(int key)
61 {
62     bindKey(key,qkey,scikey);
63 }
64 
65 
66 // Bind an alternate key to a command.
setAlternateKey(int altkey)67 void QsciCommand::setAlternateKey(int altkey)
68 {
69     bindKey(altkey,qaltkey,scialtkey);
70 }
71 
72 
73 // Do the hard work of binding a key.
bindKey(int key,int & qk,int & scik)74 void QsciCommand::bindKey(int key,int &qk,int &scik)
75 {
76     int new_scikey;
77 
78     // Ignore if it is invalid, allowing for the fact that we might be
79     // unbinding it.
80     if (key)
81     {
82         new_scikey = convert(key);
83 
84         if (!new_scikey)
85             return;
86     }
87     else
88         new_scikey = 0;
89 
90     if (scik)
91         qsCmd->SendScintilla(QsciScintillaBase::SCI_CLEARCMDKEY, scik);
92 
93     qk = key;
94     scik = new_scikey;
95 
96     if (scik)
97         qsCmd->SendScintilla(QsciScintillaBase::SCI_ASSIGNCMDKEY, scik, scicmd);
98 }
99 
100 
101 // See if a key is valid.
validKey(int key)102 bool QsciCommand::validKey(int key)
103 {
104     return convert(key);
105 }
106 
107 
108 // Convert a Qt character to the Scintilla equivalent.  Return zero if it is
109 // invalid.
convert(int key)110 static int convert(int key)
111 {
112     // Convert the modifiers.
113     int sci_mod = 0;
114 
115     if (key & Qt::SHIFT)
116         sci_mod |= QsciScintillaBase::SCMOD_SHIFT;
117 
118     if (key & Qt::CTRL)
119         sci_mod |= QsciScintillaBase::SCMOD_CTRL;
120 
121     if (key & Qt::ALT)
122         sci_mod |= QsciScintillaBase::SCMOD_ALT;
123 
124     if (key & Qt::META)
125         sci_mod |= QsciScintillaBase::SCMOD_META;
126 
127     key &= ~Qt::MODIFIER_MASK;
128 
129     // Convert the key.
130     int sci_key = QsciScintillaBase::commandKey(key, sci_mod);
131 
132     if (sci_key)
133         sci_key |= (sci_mod << 16);
134 
135     return sci_key;
136 }
137 
138 
139 // Return the translated user friendly description.
description() const140 QString QsciCommand::description() const
141 {
142     return qApp->translate("QsciCommand", descCmd);
143 }
144