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