src/eric7/Preferences/ShortcutDialog.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
15 15
16 16
17 class ShortcutDialog(QDialog, Ui_ShortcutDialog): 17 class ShortcutDialog(QDialog, Ui_ShortcutDialog):
18 """ 18 """
19 Class implementing a dialog for the configuration of a keyboard shortcut. 19 Class implementing a dialog for the configuration of a keyboard shortcut.
20 20
21 @signal shortcutChanged(QKeySequence, QKeySequence, bool, string) emitted 21 @signal shortcutChanged(QKeySequence, QKeySequence, bool, string) emitted
22 after the OK button was pressed 22 after the OK button was pressed
23 """ 23 """
24
24 shortcutChanged = pyqtSignal(QKeySequence, QKeySequence, bool, str) 25 shortcutChanged = pyqtSignal(QKeySequence, QKeySequence, bool, str)
25 26
26 def __init__(self, parent=None, name=None, modal=False): 27 def __init__(self, parent=None, name=None, modal=False):
27 """ 28 """
28 Constructor 29 Constructor
29 30
30 @param parent The parent widget of this dialog. (QWidget) 31 @param parent The parent widget of this dialog. (QWidget)
31 @param name The name of this dialog. (string) 32 @param name The name of this dialog. (string)
32 @param modal Flag indicating a modal dialog. (boolean) 33 @param modal Flag indicating a modal dialog. (boolean)
33 """ 34 """
34 super().__init__(parent) 35 super().__init__(parent)
35 if name: 36 if name:
36 self.setObjectName(name) 37 self.setObjectName(name)
37 self.setModal(modal) 38 self.setModal(modal)
38 self.setupUi(self) 39 self.setupUi(self)
39 self.setWindowFlags(Qt.WindowType.Window) 40 self.setWindowFlags(Qt.WindowType.Window)
40 41
41 self.__clearKeys() 42 self.__clearKeys()
42 43
43 self.noCheck = False 44 self.noCheck = False
44 self.objectType = "" 45 self.objectType = ""
45 46
46 self.primaryClearButton.clicked.connect(self.__clear) 47 self.primaryClearButton.clicked.connect(self.__clear)
47 self.alternateClearButton.clicked.connect(self.__clear) 48 self.alternateClearButton.clicked.connect(self.__clear)
48 self.primaryButton.clicked.connect(self.__typeChanged) 49 self.primaryButton.clicked.connect(self.__typeChanged)
49 self.alternateButton.clicked.connect(self.__typeChanged) 50 self.alternateButton.clicked.connect(self.__typeChanged)
50 51
51 self.shortcutsGroup.installEventFilter(self) 52 self.shortcutsGroup.installEventFilter(self)
52 self.primaryButton.installEventFilter(self) 53 self.primaryButton.installEventFilter(self)
53 self.alternateButton.installEventFilter(self) 54 self.alternateButton.installEventFilter(self)
54 self.primaryClearButton.installEventFilter(self) 55 self.primaryClearButton.installEventFilter(self)
55 self.alternateClearButton.installEventFilter(self) 56 self.alternateClearButton.installEventFilter(self)
56 self.keyEdit.installEventFilter(self) 57 self.keyEdit.installEventFilter(self)
57 self.alternateKeyEdit.installEventFilter(self) 58 self.alternateKeyEdit.installEventFilter(self)
58 59
60 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).installEventFilter(
61 self
62 )
59 self.buttonBox.button( 63 self.buttonBox.button(
60 QDialogButtonBox.StandardButton.Ok).installEventFilter(self) 64 QDialogButtonBox.StandardButton.Cancel
61 self.buttonBox.button( 65 ).installEventFilter(self)
62 QDialogButtonBox.StandardButton.Cancel).installEventFilter(self) 66
63
64 msh = self.minimumSizeHint() 67 msh = self.minimumSizeHint()
65 self.resize(max(self.width(), msh.width()), msh.height()) 68 self.resize(max(self.width(), msh.width()), msh.height())
66 69
67 def __clearKeys(self): 70 def __clearKeys(self):
68 """ 71 """
69 Private method to clear the list of recorded keys. 72 Private method to clear the list of recorded keys.
70 """ 73 """
71 self.keyIndex = 0 74 self.keyIndex = 0
72 self.keys = [ 75 self.keys = [
73 QKeyCombination(), 76 QKeyCombination(),
74 QKeyCombination(), 77 QKeyCombination(),
75 QKeyCombination(), 78 QKeyCombination(),
76 QKeyCombination() 79 QKeyCombination(),
77 ] 80 ]
78 81
79 def setKeys(self, key, alternateKey, noCheck, objectType): 82 def setKeys(self, key, alternateKey, noCheck, objectType):
80 """ 83 """
81 Public method to set the key to be configured. 84 Public method to set the key to be configured.
82 85
83 @param key key sequence to be changed (QKeySequence) 86 @param key key sequence to be changed (QKeySequence)
84 @param alternateKey alternate key sequence to be changed (QKeySequence) 87 @param alternateKey alternate key sequence to be changed (QKeySequence)
85 @param noCheck flag indicating that no uniqueness check should 88 @param noCheck flag indicating that no uniqueness check should
86 be performed (boolean) 89 be performed (boolean)
87 @param objectType type of the object (string). 90 @param objectType type of the object (string).
88 """ 91 """
89 self.__clearKeys() 92 self.__clearKeys()
90 93
91 self.keyEdit.setText(key.toString()) 94 self.keyEdit.setText(key.toString())
92 self.alternateKeyEdit.setText(alternateKey.toString()) 95 self.alternateKeyEdit.setText(alternateKey.toString())
93 self.primaryButton.setChecked(True) 96 self.primaryButton.setChecked(True)
94 self.noCheck = noCheck 97 self.noCheck = noCheck
95 self.objectType = objectType 98 self.objectType = objectType
96 99
97 def on_buttonBox_accepted(self): 100 def on_buttonBox_accepted(self):
98 """ 101 """
99 Private slot to handle the OK button press. 102 Private slot to handle the OK button press.
100 """ 103 """
101 self.hide() 104 self.hide()
102 self.shortcutChanged.emit( 105 self.shortcutChanged.emit(
103 QKeySequence(self.keyEdit.text()), 106 QKeySequence(self.keyEdit.text()),
104 QKeySequence(self.alternateKeyEdit.text()), 107 QKeySequence(self.alternateKeyEdit.text()),
105 self.noCheck, self.objectType) 108 self.noCheck,
109 self.objectType,
110 )
106 111
107 def __clear(self): 112 def __clear(self):
108 """ 113 """
109 Private slot to handle the Clear button press. 114 Private slot to handle the Clear button press.
110 """ 115 """
111 self.__clearKeys() 116 self.__clearKeys()
112 self.__setKeyEditText("") 117 self.__setKeyEditText("")
113 118
114 def __typeChanged(self): 119 def __typeChanged(self):
115 """ 120 """
116 Private slot to handle the change of the shortcuts type. 121 Private slot to handle the change of the shortcuts type.
117 """ 122 """
118 self.__clearKeys() 123 self.__clearKeys()
119 124
120 def __setKeyEditText(self, txt): 125 def __setKeyEditText(self, txt):
121 """ 126 """
122 Private method to set the text of a key edit. 127 Private method to set the text of a key edit.
123 128
124 @param txt text to be set (string) 129 @param txt text to be set (string)
125 """ 130 """
126 if self.primaryButton.isChecked(): 131 if self.primaryButton.isChecked():
127 self.keyEdit.setText(txt) 132 self.keyEdit.setText(txt)
128 else: 133 else:
129 self.alternateKeyEdit.setText(txt) 134 self.alternateKeyEdit.setText(txt)
130 135
131 def eventFilter(self, watched, event): 136 def eventFilter(self, watched, event):
132 """ 137 """
133 Public method called to filter the event queue. 138 Public method called to filter the event queue.
134 139
135 @param watched the QObject being watched 140 @param watched the QObject being watched
136 @param event the event that occurred 141 @param event the event that occurred
137 @return always False 142 @return always False
138 """ 143 """
139 if event.type() == QEvent.Type.KeyPress: 144 if event.type() == QEvent.Type.KeyPress:
140 self.keyPressEvent(event) 145 self.keyPressEvent(event)
141 return True 146 return True
142 147
143 return False 148 return False
144 149
145 def keyPressEvent(self, evt): 150 def keyPressEvent(self, evt):
146 """ 151 """
147 Protected method to handle a key press event. 152 Protected method to handle a key press event.
148 153
149 @param evt the key event (QKeyEvent) 154 @param evt the key event (QKeyEvent)
150 """ 155 """
151 if evt.key() in [ 156 if evt.key() in [
152 Qt.Key.Key_Control, Qt.Key.Key_Meta, Qt.Key.Key_Shift, 157 Qt.Key.Key_Control,
153 Qt.Key.Key_Alt, Qt.Key.Key_Menu, 158 Qt.Key.Key_Meta,
154 Qt.Key.Key_Hyper_L, Qt.Key.Key_Hyper_R, 159 Qt.Key.Key_Shift,
155 Qt.Key.Key_Super_L, Qt.Key.Key_Super_R 160 Qt.Key.Key_Alt,
161 Qt.Key.Key_Menu,
162 Qt.Key.Key_Hyper_L,
163 Qt.Key.Key_Hyper_R,
164 Qt.Key.Key_Super_L,
165 Qt.Key.Key_Super_R,
156 ]: 166 ]:
157 return 167 return
158 168
159 if self.keyIndex == 4: 169 if self.keyIndex == 4:
160 self.__clearKeys() 170 self.__clearKeys()
161 171
162 if ( 172 if (
163 evt.key() == Qt.Key.Key_Backtab and 173 evt.key() == Qt.Key.Key_Backtab
164 evt.modifiers() & Qt.KeyboardModifier.ShiftModifier 174 and evt.modifiers() & Qt.KeyboardModifier.ShiftModifier
165 ): 175 ):
166 self.keys[self.keyIndex] = QKeyCombination(evt.modifiers(), 176 self.keys[self.keyIndex] = QKeyCombination(evt.modifiers(), Qt.Key.Key_Tab)
167 Qt.Key.Key_Tab)
168 else: 177 else:
169 self.keys[self.keyIndex] = QKeyCombination(evt.modifiers(), 178 self.keys[self.keyIndex] = QKeyCombination(
170 Qt.Key(evt.key())) 179 evt.modifiers(), Qt.Key(evt.key())
171 180 )
181
172 self.keyIndex += 1 182 self.keyIndex += 1
173 183
174 if self.keyIndex == 1: 184 if self.keyIndex == 1:
175 ks = QKeySequence(self.keys[0]) 185 ks = QKeySequence(self.keys[0])
176 elif self.keyIndex == 2: 186 elif self.keyIndex == 2:
177 ks = QKeySequence(self.keys[0], self.keys[1]) 187 ks = QKeySequence(self.keys[0], self.keys[1])
178 elif self.keyIndex == 3: 188 elif self.keyIndex == 3:
179 ks = QKeySequence(self.keys[0], self.keys[1], self.keys[2]) 189 ks = QKeySequence(self.keys[0], self.keys[1], self.keys[2])
180 elif self.keyIndex == 4: 190 elif self.keyIndex == 4:
181 ks = QKeySequence( 191 ks = QKeySequence(self.keys[0], self.keys[1], self.keys[2], self.keys[3])
182 self.keys[0], self.keys[1], self.keys[2], self.keys[3]) 192 self.__setKeyEditText(ks.toString(QKeySequence.SequenceFormat.NativeText))
183 self.__setKeyEditText(
184 ks.toString(QKeySequence.SequenceFormat.NativeText)
185 )

eric ide

mercurial