eric7/Preferences/ShortcutDialog.py

branch
eric7
changeset 8312
800c432b34c8
parent 8218
7c09585bd960
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2003 - 2021 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 PyQt5.QtCore import pyqtSignal, QEvent, Qt
11 from PyQt5.QtGui import QKeySequence
12 from PyQt5.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.keyIndex = 0
42 self.keys = [0, 0, 0, 0]
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 setKeys(self, key, alternateKey, noCheck, objectType):
68 """
69 Public method to set the key to be configured.
70
71 @param key key sequence to be changed (QKeySequence)
72 @param alternateKey alternate key sequence to be changed (QKeySequence)
73 @param noCheck flag indicating that no uniqueness check should
74 be performed (boolean)
75 @param objectType type of the object (string).
76 """
77 self.keyIndex = 0
78 self.keys = [0, 0, 0, 0]
79 self.keyEdit.setText(key.toString())
80 self.alternateKeyEdit.setText(alternateKey.toString())
81 self.primaryButton.setChecked(True)
82 self.noCheck = noCheck
83 self.objectType = objectType
84
85 def on_buttonBox_accepted(self):
86 """
87 Private slot to handle the OK button press.
88 """
89 self.hide()
90 self.shortcutChanged.emit(
91 QKeySequence(self.keyEdit.text()),
92 QKeySequence(self.alternateKeyEdit.text()),
93 self.noCheck, self.objectType)
94
95 def __clear(self):
96 """
97 Private slot to handle the Clear button press.
98 """
99 self.keyIndex = 0
100 self.keys = [0, 0, 0, 0]
101 self.__setKeyEditText("")
102
103 def __typeChanged(self):
104 """
105 Private slot to handle the change of the shortcuts type.
106 """
107 self.keyIndex = 0
108 self.keys = [0, 0, 0, 0]
109
110 def __setKeyEditText(self, txt):
111 """
112 Private method to set the text of a key edit.
113
114 @param txt text to be set (string)
115 """
116 if self.primaryButton.isChecked():
117 self.keyEdit.setText(txt)
118 else:
119 self.alternateKeyEdit.setText(txt)
120
121 def eventFilter(self, watched, event):
122 """
123 Public method called to filter the event queue.
124
125 @param watched the QObject being watched
126 @param event the event that occurred
127 @return always False
128 """
129 if event.type() == QEvent.Type.KeyPress:
130 self.keyPressEvent(event)
131 return True
132
133 return False
134
135 def keyPressEvent(self, evt):
136 """
137 Protected method to handle a key press event.
138
139 @param evt the key event (QKeyEvent)
140 """
141 if evt.key() in [
142 Qt.Key.Key_Control, Qt.Key.Key_Meta, Qt.Key.Key_Shift,
143 Qt.Key.Key_Alt, Qt.Key.Key_Menu,
144 Qt.Key.Key_Hyper_L, Qt.Key.Key_Hyper_R,
145 Qt.Key.Key_Super_L, Qt.Key.Key_Super_R
146 ]:
147 return
148
149 if self.keyIndex == 4:
150 self.keyIndex = 0
151 self.keys = [0, 0, 0, 0]
152
153 if (
154 evt.key() == Qt.Key.Key_Backtab and
155 evt.modifiers() & Qt.KeyboardModifier.ShiftModifier
156 ):
157 self.keys[self.keyIndex] = Qt.Key.Key_Tab
158 else:
159 self.keys[self.keyIndex] = evt.key()
160
161 if evt.modifiers() & Qt.KeyboardModifier.ShiftModifier:
162 self.keys[self.keyIndex] += Qt.Modifier.SHIFT
163 if evt.modifiers() & Qt.KeyboardModifier.ControlModifier:
164 self.keys[self.keyIndex] += Qt.Modifier.CTRL
165 if evt.modifiers() & Qt.KeyboardModifier.AltModifier:
166 self.keys[self.keyIndex] += Qt.Modifier.ALT
167 if evt.modifiers() & Qt.KeyboardModifier.MetaModifier:
168 self.keys[self.keyIndex] += Qt.Modifier.META
169
170 self.keyIndex += 1
171
172 if self.keyIndex == 1:
173 self.__setKeyEditText(QKeySequence(self.keys[0]).toString())
174 elif self.keyIndex == 2:
175 self.__setKeyEditText(
176 QKeySequence(self.keys[0], self.keys[1]).toString())
177 elif self.keyIndex == 3:
178 self.__setKeyEditText(QKeySequence(
179 self.keys[0], self.keys[1],
180 self.keys[2]).toString())
181 elif self.keyIndex == 4:
182 self.__setKeyEditText(QKeySequence(
183 self.keys[0], self.keys[1],
184 self.keys[2], self.keys[3]).toString())

eric ide

mercurial