|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog for the configuration of a keyboard shortcut. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 from Ui_ShortcutDialog import Ui_ShortcutDialog |
|
14 |
|
15 class ShortcutDialog(QDialog, Ui_ShortcutDialog): |
|
16 """ |
|
17 Class implementing a dialog for the configuration of a keyboard shortcut. |
|
18 |
|
19 @signal shortcutChanged(QKeySequence, QKeySequence, bool, objectType) emitted |
|
20 after the OK button was pressed |
|
21 """ |
|
22 def __init__(self, parent = None, name = None, modal = False): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param parent The parent widget of this dialog. (QWidget) |
|
27 @param name The name of this dialog. (string) |
|
28 @param modal Flag indicating a modal dialog. (boolean) |
|
29 """ |
|
30 QDialog.__init__(self, parent) |
|
31 if name: |
|
32 self.setObjectName(name) |
|
33 self.setModal(modal) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.keyIndex = 0 |
|
37 self.keys = [0, 0, 0, 0] |
|
38 self.noCheck = False |
|
39 self.objectType = None |
|
40 |
|
41 self.connect(self.primaryClearButton, SIGNAL("clicked()"), self.__clear) |
|
42 self.connect(self.alternateClearButton, SIGNAL("clicked()"), self.__clear) |
|
43 self.connect(self.primaryButton, SIGNAL("clicked()"), self.__typeChanged) |
|
44 self.connect(self.alternateButton, SIGNAL("clicked()"), self.__typeChanged) |
|
45 |
|
46 self.shortcutsGroup.installEventFilter(self) |
|
47 self.primaryButton.installEventFilter(self) |
|
48 self.alternateButton.installEventFilter(self) |
|
49 self.primaryClearButton.installEventFilter(self) |
|
50 self.alternateClearButton.installEventFilter(self) |
|
51 |
|
52 self.buttonBox.button(QDialogButtonBox.Ok).installEventFilter(self) |
|
53 self.buttonBox.button(QDialogButtonBox.Cancel).installEventFilter(self) |
|
54 |
|
55 def setKeys(self, key, alternateKey, noCheck, objectType): |
|
56 """ |
|
57 Public method to set the key to be configured. |
|
58 |
|
59 @param key key sequence to be changed (QKeySequence) |
|
60 @param alternateKey alternate key sequence to be changed (QKeySequence) |
|
61 @param noCheck flag indicating that no uniqueness check should |
|
62 be performed (boolean) |
|
63 @param objectType type of the object (string). |
|
64 """ |
|
65 self.keyIndex = 0 |
|
66 self.keys = [0, 0, 0, 0] |
|
67 self.keyLabel.setText(key.toString()) |
|
68 self.alternateKeyLabel.setText(alternateKey.toString()) |
|
69 self.primaryButton.setChecked(True) |
|
70 self.noCheck = noCheck |
|
71 self.objectType = objectType |
|
72 |
|
73 def on_buttonBox_accepted(self): |
|
74 """ |
|
75 Private slot to handle the OK button press. |
|
76 """ |
|
77 self.hide() |
|
78 self.emit(SIGNAL('shortcutChanged'), |
|
79 QKeySequence(self.keyLabel.text()), |
|
80 QKeySequence(self.alternateKeyLabel.text()), |
|
81 self.noCheck, self.objectType) |
|
82 |
|
83 def __clear(self): |
|
84 """ |
|
85 Private slot to handle the Clear button press. |
|
86 """ |
|
87 self.keyIndex = 0 |
|
88 self.keys = [0, 0, 0, 0] |
|
89 self.__setKeyLabelText("") |
|
90 |
|
91 def __typeChanged(self): |
|
92 """ |
|
93 Private slot to handle the change of the shortcuts type. |
|
94 """ |
|
95 self.keyIndex = 0 |
|
96 self.keys = [0, 0, 0, 0] |
|
97 |
|
98 def __setKeyLabelText(self, txt): |
|
99 """ |
|
100 Private method to set the text of a key label. |
|
101 |
|
102 @param txt text to be set (string) |
|
103 """ |
|
104 if self.primaryButton.isChecked(): |
|
105 self.keyLabel.setText(txt) |
|
106 else: |
|
107 self.alternateKeyLabel.setText(txt) |
|
108 |
|
109 def eventFilter(self, watched, event): |
|
110 """ |
|
111 Method called to filter the event queue. |
|
112 |
|
113 @param watched the QObject being watched |
|
114 @param event the event that occurred |
|
115 @return always False |
|
116 """ |
|
117 if event.type() == QEvent.KeyPress: |
|
118 self.keyPressEvent(event) |
|
119 return True |
|
120 |
|
121 return False |
|
122 |
|
123 def keyPressEvent(self, evt): |
|
124 """ |
|
125 Private method to handle a key press event. |
|
126 |
|
127 @param evt the key event (QKeyEvent) |
|
128 """ |
|
129 if evt.key() == Qt.Key_Control: |
|
130 return |
|
131 if evt.key() == Qt.Key_Meta: |
|
132 return |
|
133 if evt.key() == Qt.Key_Shift: |
|
134 return |
|
135 if evt.key() == Qt.Key_Alt: |
|
136 return |
|
137 if evt.key() == Qt.Key_Menu: |
|
138 return |
|
139 |
|
140 if self.keyIndex == 4: |
|
141 self.keyIndex = 0 |
|
142 self.keys = [0, 0, 0, 0] |
|
143 |
|
144 if evt.key() == Qt.Key_Backtab and evt.modifiers() & Qt.ShiftModifier: |
|
145 self.keys[self.keyIndex] = Qt.Key_Tab |
|
146 else: |
|
147 self.keys[self.keyIndex] = evt.key() |
|
148 |
|
149 if evt.modifiers() & Qt.ShiftModifier: |
|
150 self.keys[self.keyIndex] += Qt.SHIFT |
|
151 if evt.modifiers() & Qt.ControlModifier: |
|
152 self.keys[self.keyIndex] += Qt.CTRL |
|
153 if evt.modifiers() & Qt.AltModifier: |
|
154 self.keys[self.keyIndex] += Qt.ALT |
|
155 if evt.modifiers() & Qt.MetaModifier: |
|
156 self.keys[self.keyIndex] += Qt.META |
|
157 |
|
158 self.keyIndex += 1 |
|
159 |
|
160 if self.keyIndex == 1: |
|
161 self.__setKeyLabelText(QKeySequence(self.keys[0]).toString()) |
|
162 elif self.keyIndex == 2: |
|
163 self.__setKeyLabelText(QKeySequence(self.keys[0], self.keys[1]).toString()) |
|
164 elif self.keyIndex == 3: |
|
165 self.__setKeyLabelText(QKeySequence(self.keys[0], self.keys[1], |
|
166 self.keys[2]).toString()) |
|
167 elif self.keyIndex == 4: |
|
168 self.__setKeyLabelText(QKeySequence(self.keys[0], self.keys[1], |
|
169 self.keys[2], self.keys[3]).toString()) |