eric6/Preferences/ShortcutDialog.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2003 - 2019 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 __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSignal, QEvent, Qt
13 from PyQt5.QtGui import QKeySequence
14 from PyQt5.QtWidgets import QDialog, QDialogButtonBox
15
16 from .Ui_ShortcutDialog import Ui_ShortcutDialog
17
18
19 class ShortcutDialog(QDialog, Ui_ShortcutDialog):
20 """
21 Class implementing a dialog for the configuration of a keyboard shortcut.
22
23 @signal shortcutChanged(QKeySequence, QKeySequence, bool, string) emitted
24 after the OK button was pressed
25 """
26 shortcutChanged = pyqtSignal(QKeySequence, QKeySequence, bool, str)
27
28 def __init__(self, parent=None, name=None, modal=False):
29 """
30 Constructor
31
32 @param parent The parent widget of this dialog. (QWidget)
33 @param name The name of this dialog. (string)
34 @param modal Flag indicating a modal dialog. (boolean)
35 """
36 super(ShortcutDialog, self).__init__(parent)
37 if name:
38 self.setObjectName(name)
39 self.setModal(modal)
40 self.setupUi(self)
41 self.setWindowFlags(Qt.Window)
42
43 self.keyIndex = 0
44 self.keys = [0, 0, 0, 0]
45 self.noCheck = False
46 self.objectType = ""
47
48 self.primaryClearButton.clicked.connect(self.__clear)
49 self.alternateClearButton.clicked.connect(self.__clear)
50 self.primaryButton.clicked.connect(self.__typeChanged)
51 self.alternateButton.clicked.connect(self.__typeChanged)
52
53 self.shortcutsGroup.installEventFilter(self)
54 self.primaryButton.installEventFilter(self)
55 self.alternateButton.installEventFilter(self)
56 self.primaryClearButton.installEventFilter(self)
57 self.alternateClearButton.installEventFilter(self)
58 self.keyEdit.installEventFilter(self)
59 self.alternateKeyEdit.installEventFilter(self)
60
61 self.buttonBox.button(QDialogButtonBox.Ok).installEventFilter(self)
62 self.buttonBox.button(QDialogButtonBox.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.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 [Qt.Key_Control, Qt.Key_Meta, Qt.Key_Shift, Qt.Key_Alt,
142 Qt.Key_Menu, Qt.Key_Hyper_L, Qt.Key_Hyper_R,
143 Qt.Key_Super_L, Qt.Key_Super_R]:
144 return
145
146 if self.keyIndex == 4:
147 self.keyIndex = 0
148 self.keys = [0, 0, 0, 0]
149
150 if evt.key() == Qt.Key_Backtab and evt.modifiers() & Qt.ShiftModifier:
151 self.keys[self.keyIndex] = Qt.Key_Tab
152 else:
153 self.keys[self.keyIndex] = evt.key()
154
155 if evt.modifiers() & Qt.ShiftModifier:
156 self.keys[self.keyIndex] += Qt.SHIFT
157 if evt.modifiers() & Qt.ControlModifier:
158 self.keys[self.keyIndex] += Qt.CTRL
159 if evt.modifiers() & Qt.AltModifier:
160 self.keys[self.keyIndex] += Qt.ALT
161 if evt.modifiers() & Qt.MetaModifier:
162 self.keys[self.keyIndex] += Qt.META
163
164 self.keyIndex += 1
165
166 if self.keyIndex == 1:
167 self.__setKeyEditText(QKeySequence(self.keys[0]).toString())
168 elif self.keyIndex == 2:
169 self.__setKeyEditText(
170 QKeySequence(self.keys[0], self.keys[1]).toString())
171 elif self.keyIndex == 3:
172 self.__setKeyEditText(QKeySequence(
173 self.keys[0], self.keys[1],
174 self.keys[2]).toString())
175 elif self.keyIndex == 4:
176 self.__setKeyEditText(QKeySequence(
177 self.keys[0], self.keys[1],
178 self.keys[2], self.keys[3]).toString())

eric ide

mercurial