Debian Codegraph @

qt5/widgetsaddons/fcitxqtkeysequencewidget.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2013~2013 by CSSlayer *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
19
20 /* this is forked from kdelibs/kdeui/kkeysequencewidget.cpp */
21
22 /*
23 Original Copyright header
24 Copyright (C) 1998 Mark Donohoe <[email protected]>
25 Copyright (C) 2001 Ellis Whitehead <[email protected]>
26 Copyright (C) 2007 Andreas Hartmetz <[email protected]>
27
28 This library is free software; you can redistribute it and/or
29 modify it under the terms of the GNU Library General Public
30 License as published by the Free Software Foundation; either
31 version 2 of the License, or (at your option) any later version.
32
33 This library is distributed in the hope that it will be useful,
34 but WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 Library General Public License for more details.
37
38 You should have received a copy of the GNU Library General Public License
39 along with this library; see the file COPYING.LIB. If not, write to
40 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
41 Boston, MA 02110-1301, USA.
42 */
43
44 #include "fcitxqtkeysequencewidget.h"
45 #include "fcitxqtkeysequencewidget_p.h"
46
47 #include <QApplication>
48 #include <QDebug>
49 #include <QHBoxLayout>
50 #include <QHash>
51 #include <QKeyEvent>
52 #include <QMessageBox>
53 #include <QTimer>
54 #include <QToolButton>
55 #include <fcitx-utils/keysymgen.h>
56 #include <libintl.h>
57
58 #include "qtkeytrans.h"
59
60 #define _(x) QString::fromUtf8(dgettext("fcitx-qt5", x))
61
62 class FcitxQtKeySequenceWidgetPrivate {
63 public:
64 FcitxQtKeySequenceWidgetPrivate(FcitxQtKeySequenceWidget *q);
65
66 void init();
67
68 static QKeySequence appendToSequence(const QKeySequence &seq, int keyQt);
69 static bool isOkWhenModifierless(int keyQt);
70
71 void updateShortcutDisplay();
72 void startRecording();
73
74 void controlModifierlessTimout() {
75 if (nKey != 0 && !modifierKeys) {
76 // No modifier key pressed currently. Start the timout
77 modifierlessTimeout.start(600);
78 } else {
79 // A modifier is pressed. Stop the timeout
80 modifierlessTimeout.stop();
81 }
82 }
83
84 void cancelRecording() {
85 keySequence = oldKeySequence;
86 side = oldSide;
87 doneRecording();
88 }
89
90 // private slot
91 void doneRecording();
92
93 // members
94 FcitxQtKeySequenceWidget *const q;
95 QHBoxLayout *layout;
96 FcitxQtKeySequenceButton *keyButton;
97 QToolButton *clearButton;
98
99 QKeySequence keySequence;
100 QKeySequence oldKeySequence;
101 QTimer modifierlessTimeout;
102 bool allowModifierless;
103 uint nKey;
104 uint modifierKeys;
105 bool isRecording;
106 bool multiKeyShortcutsAllowed;
107 bool allowModifierOnly;
108 FcitxQtModifierSide side;
109 FcitxQtModifierSide oldSide;
110 };
111
112 FcitxQtKeySequenceWidgetPrivate::FcitxQtKeySequenceWidgetPrivate(
113 FcitxQtKeySequenceWidget *q)
114 : q(q), layout(nullptr), keyButton(nullptr), clearButton(nullptr),
115 allowModifierless(false), nKey(0), modifierKeys(0), isRecording(false),
116 multiKeyShortcutsAllowed(true), allowModifierOnly(false),
117 side(MS_Unknown) {}
118
119 FcitxQtKeySequenceWidget::FcitxQtKeySequenceWidget(QWidget *parent)
120 : QWidget(parent), d(new FcitxQtKeySequenceWidgetPrivate(this)) {
121 d->init();
122 setFocusProxy(d->keyButton);
123 connect(d->keyButton, &QPushButton::clicked, this,
124 &FcitxQtKeySequenceWidget::captureKeySequence);
125 connect(d->clearButton, &QPushButton::clicked, this,
126 &FcitxQtKeySequenceWidget::clearKeySequence);
127 connect(&d->modifierlessTimeout, &QTimer::timeout, this,
128 [this]() { d->doneRecording(); });
129 // TODO: how to adopt style changes at runtime?
130 /*QFont modFont = d->clearButton->font();
131 modFont.setStyleHint(QFont::TypeWriter);
132 d->clearButton->setFont(modFont);*/
133 d->updateShortcutDisplay();
134 }
135
136 void FcitxQtKeySequenceWidgetPrivate::init() {
137 layout = new QHBoxLayout(q);
138 layout->setMargin(0);
139
140 keyButton = new FcitxQtKeySequenceButton(this, q);
141 keyButton->setFocusPolicy(Qt::StrongFocus);
142 layout->addWidget(keyButton);
143
144 clearButton = new QToolButton(q);
145 layout->addWidget(clearButton);
146
147 #if QT_VERSION < QT_VERSION_CHECK(4, 8, 0)
148 clearButton->setText(_("Clear"));
149 #else
150 keyButton->setIcon(QIcon::fromTheme("configure"));
151 if (qApp->isLeftToRight())
152 clearButton->setIcon(QIcon::fromTheme("edit-clear-locationbar-rtl"));
153 else
154 clearButton->setIcon(QIcon::fromTheme("edit-clear-locationbar-ltr"));
155 #endif
156 }
157
158 FcitxQtKeySequenceWidget::~FcitxQtKeySequenceWidget() { delete d; }
159
160 bool FcitxQtKeySequenceWidget::multiKeyShortcutsAllowed() const {
161 return d->multiKeyShortcutsAllowed;
162 }
163
164 void FcitxQtKeySequenceWidget::setMultiKeyShortcutsAllowed(bool allowed) {
165 d->multiKeyShortcutsAllowed = allowed;
166 }
167
168 void FcitxQtKeySequenceWidget::setModifierlessAllowed(bool allow) {
169 d->allowModifierless = allow;
170 }
171
172 bool FcitxQtKeySequenceWidget::isModifierlessAllowed() {
173 return d->allowModifierless;
174 }
175
176 bool FcitxQtKeySequenceWidget::isModifierOnlyAllowed() {
177 return d->allowModifierOnly;
178 }
179
180 void FcitxQtKeySequenceWidget::setModifierOnlyAllowed(bool allow) {
181 d->allowModifierOnly = allow;
182 }
183
184 FcitxQtModifierSide FcitxQtKeySequenceWidget::modifierSide() { return d->side; }
185
186 void FcitxQtKeySequenceWidget::setClearButtonShown(bool show) {
187 d->clearButton->setVisible(show);
188 }
189
190 // slot
191 void FcitxQtKeySequenceWidget::captureKeySequence() { d->startRecording(); }
192
193 QKeySequence FcitxQtKeySequenceWidget::keySequence() const {
194 return d->keySequence;
195 }
196
197 // slot
198 void FcitxQtKeySequenceWidget::setKeySequence(
199 const QKeySequence &seq, FcitxQtModifierSide side,
200 FcitxQtKeySequenceWidget::Validation validate) {
201 Q_UNUSED(validate);
202 // oldKeySequence holds the key sequence before recording started, if
203 // setKeySequence()
204 // is called while not recording then set oldKeySequence to the existing
205 // sequence so
206 // that the keySequenceChanged() signal is emitted if the new and previous
207 // key
208 // sequences are different
209 if (!d->isRecording) {
210 d->oldKeySequence = d->keySequence;
211 d->oldSide = d->side;
212 }
213
214 d->side = side;
215 d->keySequence = seq;
216 d->doneRecording();
217 }
218
219 // slot
220 void FcitxQtKeySequenceWidget::clearKeySequence() {
221 setKeySequence(QKeySequence());
222 d->side = MS_Unknown;
223 }
224
225 void FcitxQtKeySequenceWidgetPrivate::startRecording() {
226 nKey = 0;
227 modifierKeys = 0;
228 oldKeySequence = keySequence;
229 oldSide = side;
230 keySequence = QKeySequence();
231 side = MS_Unknown;
232 isRecording = true;
233 keyButton->grabKeyboard();
234
235 if (!QWidget::keyboardGrabber()) {
236 qWarning() << "Failed to grab the keyboard! Most likely qt's nograb "
237 "option is active";
238 }
239
240 keyButton->setDown(true);
241 updateShortcutDisplay();
242 }
243
244 void FcitxQtKeySequenceWidgetPrivate::doneRecording() {
245 modifierlessTimeout.stop();
246 isRecording = false;
247 keyButton->releaseKeyboard();
248 keyButton->setDown(false);
249
250 if (keySequence == oldKeySequence &&
251 (oldSide == side || !allowModifierOnly)) {
252 // The sequence hasn't changed
253 updateShortcutDisplay();
254 return;
255 }
256
257 Q_EMIT q->keySequenceChanged(keySequence, side);
258
259 updateShortcutDisplay();
260 }
261
262 void FcitxQtKeySequenceWidgetPrivate::updateShortcutDisplay() {
263 do {
264 if (keySequence.count() != 1) {
265 break;
266 }
267 int key = keySequence[0] & (~Qt::KeyboardModifierMask);
268 if (key == Qt::Key_Shift || key == Qt::Key_Control ||
269 key == Qt::Key_Meta || key == Qt::Key_Alt) {
270 QString s;
271 int mod = keySequence[0] & (Qt::KeyboardModifierMask);
272 if (mod & Qt::META && key != Qt::Key_Meta)
273 s += "Meta+";
274 if (mod & Qt::CTRL && key != Qt::Key_Control)
275 s += "Ctrl+";
276 if (mod & Qt::ALT && key != Qt::Key_Alt)
277 s += "Alt+";
278 if (mod & Qt::SHIFT && key != Qt::Key_Shift)
279 s += "Shift+";
280
281 if (side == MS_Left) {
282 s += _("Left") + " ";
283 } else if (side == MS_Right) {
284 s += _("Right") + " ";
285 }
286
287 switch (key) {
288 case Qt::Key_Shift:
289 s += "Shift";
290 break;
291 case Qt::Key_Control:
292 s += "Ctrl";
293 break;
294 case Qt::Key_Meta:
295 s += "Meta";
296 break;
297 case Qt::Key_Alt:
298 s += "Alt";
299 break;
300 }
301 keyButton->setText(s);
302 return;
303 }
304 } while (0);
305
306 // empty string if no non-modifier was pressed
307 QString s = keySequence.toString(QKeySequence::NativeText);
308 s.replace('&', QLatin1String("&&"));
309
310 if (isRecording) {
311 if (modifierKeys) {
312 if (!s.isEmpty())
313 s.append(",");
314 if (modifierKeys & Qt::META)
315 s += "Meta+";
316 if (modifierKeys & Qt::CTRL)
317 s += "Ctrl+";
318 if (modifierKeys & Qt::ALT)
319 s += "Alt+";
320 if (modifierKeys & Qt::SHIFT)
321 s += "Shift+";
322
323 } else if (nKey == 0) {
324 s = "...";
325 }
326 // make it clear that input is still going on
327 s.append(" ...");
328 }
329
330 if (s.isEmpty()) {
331 s = _("Empty");
332 }
333
334 s.prepend(' ');
335 s.append(' ');
336 keyButton->setText(s);
337 }
338
339 FcitxQtKeySequenceButton::~FcitxQtKeySequenceButton() {}
340
341 // prevent Qt from special casing Tab and Backtab
342 bool FcitxQtKeySequenceButton::event(QEvent *e) {
343 if (d->isRecording && e->type() == QEvent::KeyPress) {
344 keyPressEvent(static_cast<QKeyEvent *>(e));
345 return true;
346 }
347
348 // The shortcut 'alt+c' ( or any other dialog local action shortcut )
349 // ended the recording and triggered the action associated with the
350 // action. In case of 'alt+c' ending the dialog. It seems that those
351 // ShortcutOverride events get sent even if grabKeyboard() is active.
352 if (d->isRecording && e->type() == QEvent::ShortcutOverride) {
353 e->accept();
354 return true;
355 }
356
357 return QPushButton::event(e);
358 }
359
360 void FcitxQtKeySequenceButton::keyPressEvent(QKeyEvent *e) {
361 int keyQt = e->key();
362 if (keyQt == -1) {
363 // Qt sometimes returns garbage keycodes, I observed -1, if it doesn't
364 // know a key.
365 // We cannot do anything useful with those (several keys have -1,
366 // indistinguishable)
367 // and QKeySequence.toString() will also yield a garbage string.
368 QMessageBox::warning(
369 this, _("The key you just pressed is not supported by Qt."),
370 _("Unsupported Key"));
371 return d->cancelRecording();
372 }
373
374 uint newModifiers =
375 e->modifiers() & (Qt::SHIFT | Qt::CTRL | Qt::ALT | Qt::META);
376
377 // don't have the return or space key appear as first key of the sequence
378 // when they
379 // were pressed to start editing - catch and them and imitate their effect
380 if (!d->isRecording &&
381 ((keyQt == Qt::Key_Return || keyQt == Qt::Key_Space))) {
382 d->startRecording();
383 d->modifierKeys = newModifiers;
384 d->updateShortcutDisplay();
385 return;
386 }
387
388 // We get events even if recording isn't active.
389 if (!d->isRecording)
390 return QPushButton::keyPressEvent(e);
391
392 e->accept();
393 d->modifierKeys = newModifiers;
394
395 switch (keyQt) {
396 case Qt::Key_AltGr: // or else we get unicode salad
397 return;
398 case Qt::Key_Shift:
399 case Qt::Key_Control:
400 case Qt::Key_Alt:
401 case Qt::Key_Meta:
402 case Qt::Key_Menu: // unused (yes, but why?)
403 d->controlModifierlessTimout();
404 d->updateShortcutDisplay();
405 break;
406 default:
407
408 if (d->nKey == 0 && !(d->modifierKeys & ~Qt::SHIFT)) {
409 // It's the first key and no modifier pressed. Check if this is
410 // allowed
411 if (!(FcitxQtKeySequenceWidgetPrivate::isOkWhenModifierless(
412 keyQt) ||
413 d->allowModifierless)) {
414 // No it's not
415 return;
416 }
417 }
418
419 // We now have a valid key press.
420 if (keyQt) {
421 if ((keyQt == Qt::Key_Backtab) && (d->modifierKeys & Qt::SHIFT)) {
422 keyQt = Qt::Key_Tab | d->modifierKeys;
423 } else {
424 keyQt |= d->modifierKeys;
425 }
426
427 if (d->nKey == 0) {
428 d->keySequence = QKeySequence(keyQt);
429 } else {
430 d->keySequence =
431 FcitxQtKeySequenceWidgetPrivate::appendToSequence(
432 d->keySequence, keyQt);
433 }
434
435 d->nKey++;
436 if ((!d->multiKeyShortcutsAllowed) || (d->nKey >= 4)) {
437 d->doneRecording();
438 return;
439 }
440 d->controlModifierlessTimout();
441 d->updateShortcutDisplay();
442 }
443 }
444 }
445
446 void FcitxQtKeySequenceButton::keyReleaseEvent(QKeyEvent *e) {
447 if (e->key() == -1) {
448 // ignore garbage, see keyPressEvent()
449 return;
450 }
451
452 if (!d->isRecording)
453 return QPushButton::keyReleaseEvent(e);
454
455 e->accept();
456
457 if (!d->multiKeyShortcutsAllowed && d->allowModifierOnly &&
458 (e->key() == Qt::Key_Shift || e->key() == Qt::Key_Control ||
459 e->key() == Qt::Key_Meta || e->key() == Qt::Key_Alt)) {
460 d->side = MS_Unknown;
461
462 if (qApp->platformName() == "xcb") {
463
464 if (e->nativeVirtualKey() == FcitxKey_Control_L ||
465 e->nativeVirtualKey() == FcitxKey_Alt_L ||
466 e->nativeVirtualKey() == FcitxKey_Shift_L ||
467 e->nativeVirtualKey() == FcitxKey_Super_L) {
468 d->side = MS_Left;
469 }
470 if (e->nativeVirtualKey() == FcitxKey_Control_R ||
471 e->nativeVirtualKey() == FcitxKey_Alt_R ||
472 e->nativeVirtualKey() == FcitxKey_Shift_R ||
473 e->nativeVirtualKey() == FcitxKey_Super_R) {
474 d->side = MS_Right;
475 }
476 }
477 int keyQt = e->key() | d->modifierKeys;
478 d->keySequence = QKeySequence(keyQt);
479 d->doneRecording();
480 return;
481 }
482
483 uint newModifiers =
484 e->modifiers() & (Qt::SHIFT | Qt::CTRL | Qt::ALT | Qt::META);
485
486 // if a modifier that belongs to the shortcut was released...
487 if ((newModifiers & d->modifierKeys) < d->modifierKeys) {
488 d->modifierKeys = newModifiers;
489 d->controlModifierlessTimout();
490 d->updateShortcutDisplay();
491 }
492 }
493
494 // static
495 QKeySequence
496 FcitxQtKeySequenceWidgetPrivate::appendToSequence(const QKeySequence &seq,
497 int keyQt) {
498 switch (seq.count()) {
499 case 0:
500 return QKeySequence(keyQt);
501 case 1:
502 return QKeySequence(seq[0], keyQt);
503 case 2:
504 return QKeySequence(seq[0], seq[1], keyQt);
505 case 3:
506 return QKeySequence(seq[0], seq[1], seq[2], keyQt);
507 default:
508 return seq;
509 }
510 }
511
512 // static
513 bool FcitxQtKeySequenceWidgetPrivate::isOkWhenModifierless(int keyQt) {
514 // this whole function is a hack, but especially the first line of code
515 if (QKeySequence(keyQt).toString().length() == 1)
516 return false;
517
518 switch (keyQt) {
519 case Qt::Key_Return:
520 case Qt::Key_Space:
521 case Qt::Key_Tab:
522 case Qt::Key_Backtab: // does this ever happen?
523 case Qt::Key_Backspace:
524 case Qt::Key_Delete:
525 return false;
526 default:
527 return true;
528 }
529 }
530
531 void FcitxQtKeySequenceWidget::keyQtToFcitx(int keyQt, FcitxQtModifierSide side,
532 int &outsym, uint &outstate) {
533 int key = keyQt & (~Qt::KeyboardModifierMask);
534 int state = keyQt & Qt::KeyboardModifierMask;
535 int sym = 0;
536 keyQtToSym(key, Qt::KeyboardModifiers(state), sym, outstate);
537 if (side == MS_Right) {
538 switch (sym) {
539 case FcitxKey_Control_L:
540 sym = FcitxKey_Control_R;
541 break;
542 case FcitxKey_Alt_L:
543 sym = FcitxKey_Alt_R;
544 break;
545 case FcitxKey_Shift_L:
546 sym = FcitxKey_Shift_R;
547 break;
548 case FcitxKey_Super_L:
549 sym = FcitxKey_Super_R;
550 break;
551 }
552 }
553
554 outsym = sym;
555 }
556
557 int FcitxQtKeySequenceWidget::keyFcitxToQt(int sym, uint state) {
558 Qt::KeyboardModifiers qstate = Qt::NoModifier;
559
560 int key;
561 symToKeyQt((int)sym, state, key, qstate);
562
563 return key | qstate;
564 }
565
566 #include "moc_fcitxqtkeysequencewidget.cpp"