1 /*
2  * Stellarium
3  * Copyright (C) 2012 Anton Samoylov
4  * Copyright (C) 2012 Bogdan Marinov
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
19  */
20 #ifndef SHORTCUTLINEEDIT_H
21 #define SHORTCUTLINEEDIT_H
22 
23 #include <QLineEdit>
24 #include <QKeySequence>
25 
26 //! Specialized GUI control for entering keyboard shortcut combinations.
27 //! Allows Emacs-style key sequences (for example "Ctrl+E, Ctrl+2")
28 //! no longer than four combinations. See the documentation of
29 //! QKeySequence for details.
30 //!
31 //! When ShortcutLineEdit receives the focus, its whole contents get
32 //! selected. On a key press, if any of the contents are selected,
33 //! the new key replaces @em all the previous contents. Otherwise, it's
34 //! appended to the sequence.
35 class ShortcutLineEdit : public QLineEdit
36 {
37 	Q_OBJECT
38 
39 public:
40 	ShortcutLineEdit(QWidget* parent = Q_NULLPTR);
41 
42 	QKeySequence getKeySequence() const;
43 	bool isEmpty() const;
44 
45 public slots:
46 	//! Clear contents and stored keys.
47 	void clear();
48 	//! Remove the last key from the key sequence.
49 	void backspace();
50 	//! Emits contentsChanged() if the new sequence is not the same.
51 	void setContents(QKeySequence ks);
52 
53 signals:
54 	//! Needed for enabling/disabling buttons in ShortcutsDialog.
55 	//! @param[out] focus @b false if the widget has the focus,
56 	//! true otherwise.
57 	void focusChanged(bool focus);
58 	void contentsChanged();
59 
60 protected:
61 	virtual void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
62 	virtual void focusInEvent(QFocusEvent *e) Q_DECL_OVERRIDE;
63 	virtual void focusOutEvent(QFocusEvent *e) Q_DECL_OVERRIDE;
64 
65 private:
66 	//! transform modifiers to int.
67 	static int getModifiers(Qt::KeyboardModifiers state, const QString &text);
68 
69 	//! Codes of the keys in the manipulated key sequence.
70 	QList<int> keys;
71 };
72 
73 #endif // SHORTCUTLINEEDIT_H
74