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