1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2009,2011 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #ifndef SHORTCUTEDIT_H
21 #define SHORTCUTEDIT_H
22 
23 #include <config.h>
24 
25 #ifdef USE_KDE
26 #include <KDE/KKeySequenceWidget>
27 #define SHORTCUTEDIT_BASE KKeySequenceWidget
28 #else
29 #include <QToolButton>
30 #include <QKeySequence>
31 #include <QWidget>
32 #define SHORTCUTEDIT_BASE QWidget
33 #endif
34 
35 namespace LicqQtGui
36 {
37 
38 #ifndef USE_KDE
39 /**
40  * Button used for performing the actual capture
41  * Captures key when pressed
42  */
43 class ShortcutButton : public QToolButton
44 {
45   Q_OBJECT
46 
47 public:
48   /**
49    * Constructor
50    *
51    * @parent Parent widget
52    */
53   ShortcutButton(QWidget* parent = NULL);
54 
55   /**
56    * Get shortcut
57    *
58    * @return Current shortcut
59    */
60   QKeySequence shortcut() const;
61 
62 public slots:
63   /**
64    * Set shortcut
65    *
66    * @param sequence New shortcut
67    */
68   void setShortcut(const QKeySequence& shortcut);
69 
70   /**
71    * Clear current shortcut
72    */
73   void clearShortcut();
74 
75 signals:
76   /**
77    * Emitted when shortcut is changed
78    *
79    * @param shortcut New shortcut
80    */
81   void shortcutChanged(const QKeySequence& shortcut);
82 
83 protected:
84   /**
85    * Main event handler for widget
86    * Overridden to stop dialog from getting shortcuts while capturing
87    *
88    * @param event Event
89    * @return True if event was handled
90    */
91   bool event(QEvent* event);
92 
93   /**
94    * A key was pressed
95    * Overloaded to capture key presses when getting new shortcut key
96    *
97    * @param event Key press event
98    */
99   virtual void keyPressEvent(QKeyEvent* event);
100 
101   /**
102    * A key was released
103    * Overloaded to capture key presses when getting new shortcut key
104    *
105    * @param event Key release event
106    */
107   virtual void keyReleaseEvent(QKeyEvent* event);
108 
109   /**
110    * Button lost focus
111    * If a capture was ongoing, abort it
112    *
113    * @param event Focus out event
114    */
115   virtual void focusOutEvent(QFocusEvent* event);
116 
117 private slots:
118   /**
119    * Start key capture to listen for new shortcut key
120    */
121   void startCapture();
122 
123   /**
124    * Stop key capture
125    *
126    * @param change True to save changes, false to revert
127    */
128   void stopCapture(bool change = true);
129 
130 private:
131   /**
132    * Update button text for current shortcut
133    */
134   void updateText();
135 
136   /**
137    * Check if a key can be allowed as shortcut with no modifier active
138    *
139    * @param key Qt key code of the key to check
140    * @return True if this key requires a modifier
141    */
142   bool keyMustHaveModifier(int key);
143 
144   QKeySequence myShortcut;
145   QKeySequence myNewShortcut;
146   bool myCapturing;
147   Qt::KeyboardModifiers myModifiers;
148 };
149 #endif
150 
151 
152 /**
153  * Control for selecting key shortcuts.
154  * Uses KKeySequenceWidget when building with KDE support.
155  */
156 class ShortcutEdit : public SHORTCUTEDIT_BASE
157 {
158   Q_OBJECT
159 
160 public:
161   /**
162    * Constructor
163    *
164    * @parent Parent widget
165    */
166   ShortcutEdit(QWidget* parent = NULL);
167 
168   /**
169    * Destructor
170    */
~ShortcutEdit()171   virtual ~ShortcutEdit() {}
172 
173 #ifndef USE_KDE
174   /**
175    * Get shortcut
176    *
177    * @return Current shortcut
178    */
keySequence()179   QKeySequence keySequence() const
180   { return myKeyButton->shortcut(); }
181 
182 public slots:
183   /**
184    * Set shortcut
185    *
186    * @param sequence New shortcut
187    */
setKeySequence(const QKeySequence & sequence)188   void setKeySequence(const QKeySequence& sequence)
189   { myKeyButton->setShortcut(sequence); }
190 
191 signals:
192   /**
193    * Emitted with shortcut is changed
194    *
195    * @param sequence New shortcut
196    */
197   void keySequenceChanged(const QKeySequence& sequence);
198 
199 private:
200   ShortcutButton* myKeyButton;
201   QToolButton* myClearButton;
202 #endif
203 };
204 
205 } // namespace LicqQtGui
206 
207 #endif
208