| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <[email protected]> |
| 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 Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "KeySequenceLineEdit.h" |
| 21 | |
| 22 | KeySequenceLineEdit::KeySequenceLineEdit(QWidget *widget, const QList<Qt::Key> &allowedKeys, const QSharedPointer<IPlatformChecker> &platformChecker) : |
| 23 | QLineEdit(widget), |
| 24 | mPlatformChecker(platformChecker) |
| 25 | { |
| 26 | mAllowedKeys = allowedKeys; |
| 27 | } |
| 28 | |
| 29 | KeySequenceLineEdit::~KeySequenceLineEdit() |
| 30 | { |
| 31 | mSpecialKeyFilters.clear(); |
| 32 | } |
| 33 | |
| 34 | void KeySequenceLineEdit::keyPressEvent(QKeyEvent *event) |
| 35 | { |
| 36 | mModifiers = event->modifiers(); |
| 37 | mKey = getAllowedKey(event); |
| 38 | updateKeySequence(); |
| 39 | } |
| 40 | |
| 41 | Qt::Key KeySequenceLineEdit::getAllowedKey(const QKeyEvent *event) const |
| 42 | { |
| 43 | return mAllowedKeys.contains(static_cast<Qt::Key>(event->key())) ? static_cast<Qt::Key>(event->key()) : Qt::Key_unknown; |
| 44 | } |
| 45 | |
| 46 | void KeySequenceLineEdit::updateKeySequence() |
| 47 | { |
| 48 | mKeySequence = mKey == Qt::Key_unknown ? QKeySequence(mModifiers) : QKeySequence(mModifiers | mKey); |
| 49 | updateText(); |
| 50 | } |
| 51 | |
| 52 | void KeySequenceLineEdit::keyReleaseEvent(QKeyEvent *event) |
| 53 | { |
| 54 | mModifiers = Qt::NoModifier; |
| 55 | mKey = Qt::Key_unknown; |
| 56 | QWidget::keyReleaseEvent(event); |
| 57 | } |
| 58 | |
| 59 | void KeySequenceLineEdit::updateText() |
| 60 | { |
| 61 | setText(mKeySequence.toString()); |
| 62 | } |
| 63 | |
| 64 | QKeySequence KeySequenceLineEdit::value() const |
| 65 | { |
| 66 | return mKeySequence; |
| 67 | } |
| 68 | |
| 69 | void KeySequenceLineEdit::clear() |
| 70 | { |
| 71 | mKeySequence = QKeySequence(); |
| 72 | updateText(); |
| 73 | } |
| 74 | |
| 75 | void KeySequenceLineEdit::setValue(const QKeySequence &keySequence) |
| 76 | { |
| 77 | mKeySequence = keySequence; |
| 78 | updateText(); |
| 79 | } |
| 80 | |
| 81 | void KeySequenceLineEdit::focusInEvent(QFocusEvent *event) |
| 82 | { |
| 83 | setupSpecialKeyHandling(); |
| 84 | QLineEdit::focusInEvent(event); |
| 85 | } |
| 86 | |
| 87 | void KeySequenceLineEdit::focusOutEvent(QFocusEvent *event) |
| 88 | { |
| 89 | removeSpecialKeyHandler(); |
| 90 | QLineEdit::focusOutEvent(event); |
| 91 | } |
| 92 | |
| 93 | void KeySequenceLineEdit::removeSpecialKeyHandler() |
| 94 | { |
| 95 | for(const auto& keyFilter : mSpecialKeyFilters) { |
| 96 | QApplication::instance()->removeNativeEventFilter(keyFilter.data()); |
| 97 | } |
| 98 | mSpecialKeyFilters.clear(); |
| 99 | } |
| 100 | |
| 101 | void KeySequenceLineEdit::keyPressed(Qt::Key key) |
| 102 | { |
| 103 | mKey = key; |
| 104 | updateKeySequence(); |
| 105 | } |
| 106 | |
| 107 | void KeySequenceLineEdit::setupSpecialKeyHandling() |
| 108 | { |
| 109 | addSpecialKeyHandler(Qt::Key_Print, Qt::Key_Print); |
| 110 | addSpecialKeyHandler(Qt::CTRL | Qt::Key_Print, Qt::Key_Print); |
| 111 | addSpecialKeyHandler(Qt::ALT | Qt::Key_Print, Qt::Key_Print); |
| 112 | addSpecialKeyHandler(Qt::SHIFT | Qt::Key_Print, Qt::Key_Print); |
| 113 | addSpecialKeyHandler(Qt::CTRL | Qt::ALT | Qt::Key_Print, Qt::Key_Print); |
| 114 | addSpecialKeyHandler(Qt::CTRL | Qt::SHIFT | Qt::Key_Print, Qt::Key_Print); |
| 115 | addSpecialKeyHandler(Qt::ALT | Qt::SHIFT | Qt::Key_Print, Qt::Key_Print); |
| 116 | addSpecialKeyHandler(Qt::CTRL | Qt::ALT | Qt::SHIFT | Qt::Key_Print, Qt::Key_Print); |
| 117 | } |
| 118 | |
| 119 | void KeySequenceLineEdit::addSpecialKeyHandler(const QKeySequence &keySequence, Qt::Key key) |
| 120 | { |
| 121 | auto keyHandler = KeyHandlerFactory::create(mPlatformChecker); |
| 122 | keyHandler->registerKey(keySequence); |
| 123 | auto keyFilter = QSharedPointer<NativeKeyEventFilter>(new NativeKeyEventFilter(keyHandler)); |
| 124 | connect(keyFilter.data(), &NativeKeyEventFilter::triggered, [this, key]() { keyPressed(key); }); |
| 125 | mSpecialKeyFilters.append(keyFilter); |
| 126 | QApplication::instance()->installNativeEventFilter(keyFilter.data()); |
| 127 | } |