eric7/E5Gui/E5Action.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) 2006 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing an Action class extending QAction.
8
9 This extension is necessary in order to support alternate keyboard
10 shortcuts.
11 """
12
13 from PyQt5.QtGui import QIcon, QKeySequence
14 from PyQt5.QtWidgets import QAction, QActionGroup
15
16 from E5Gui.E5Application import e5App
17
18
19 class ArgumentsError(RuntimeError):
20 """
21 Class implementing an exception, which is raised, if the wrong number of
22 arguments are given.
23 """
24 def __init__(self, error):
25 """
26 Constructor
27
28 @param error error message of the exception (string)
29 """
30 self.errorMessage = str(error)
31
32 def __repr__(self):
33 """
34 Special method returning a representation of the exception.
35
36 @return string representing the error message
37 """
38 return str(self.errorMessage)
39
40 def __str__(self):
41 """
42 Special method returning a string representation of the exception.
43
44 @return string representing the error message
45 """
46 return str(self.errorMessage)
47
48
49 class E5Action(QAction):
50 """
51 Class implementing an Action class extending QAction.
52 """
53 def __init__(self, *args):
54 """
55 Constructor
56
57 @param args argument list of the constructor. This list is one of
58 <ul>
59 <li>text (string), icon (QIcon), menu text (string),
60 accelarator (QKeySequence), alternative accelerator
61 (QKeySequence), parent (QObject), name (string), toggle
62 (boolean)</li>
63 <li>text (string), icon (QIcon), menu text (string),
64 accelarator (QKeySequence), alternative accelerator
65 (QKeySequence), parent (QObject), name (string)</li>
66 <li>text (string), menu text (string),
67 accelarator (QKeySequence), alternative accelerator
68 (QKeySequence), parent (QObject), name (string), toggle
69 (boolean)</li>
70 <li>text (string), menu text (string),
71 accelarator (QKeySequence), alternative accelerator
72 (QKeySequence), parent (QObject), name (string)</li>
73 </ul>
74 @exception ArgumentsError raised to indicate invalid arguments
75 """
76 if isinstance(args[1], QIcon):
77 icon = args[1]
78 incr = 1
79 else:
80 icon = None
81 incr = 0
82 if len(args) < 6 + incr:
83 raise ArgumentsError(
84 "Not enough arguments, {0:d} expected, got {1:d}".format(
85 6 + incr, len(args)))
86 elif len(args) > 7 + incr:
87 raise ArgumentsError(
88 "Too many arguments, max. {0:d} expected, got {1:d}".format(
89 7 + incr, len(args)))
90
91 parent = args[4 + incr]
92 super().__init__(parent)
93 name = args[5 + incr]
94 if name:
95 self.setObjectName(name)
96
97 if args[1 + incr]:
98 self.setText(args[1 + incr])
99
100 if args[0]:
101 self.setIconText(args[0])
102 if args[2 + incr]:
103 self.setShortcut(QKeySequence(args[2 + incr]))
104
105 if args[3 + incr]:
106 self.setAlternateShortcut(QKeySequence(args[3 + incr]))
107
108 if icon:
109 self.setIcon(icon)
110
111 if len(args) == 7 + incr:
112 self.setCheckable(args[6 + incr])
113
114 self.__ammendToolTip()
115
116 def setAlternateShortcut(self, shortcut, removeEmpty=False):
117 """
118 Public slot to set the alternative keyboard shortcut.
119
120 @param shortcut the alternative accelerator (QKeySequence)
121 @param removeEmpty flag indicating to remove the alternate shortcut,
122 if it is empty (boolean)
123 """
124 if not shortcut.isEmpty():
125 shortcuts = self.shortcuts()
126 if len(shortcuts) > 0:
127 if len(shortcuts) == 1:
128 shortcuts.append(shortcut)
129 else:
130 shortcuts[1] = shortcut
131 self.setShortcuts(shortcuts)
132 elif removeEmpty:
133 shortcuts = self.shortcuts()
134 if len(shortcuts) == 2:
135 del shortcuts[1]
136 self.setShortcuts(shortcuts)
137
138 def alternateShortcut(self):
139 """
140 Public method to retrieve the alternative keyboard shortcut.
141
142 @return the alternative accelerator (QKeySequence)
143 """
144 shortcuts = self.shortcuts()
145 if len(shortcuts) < 2:
146 return QKeySequence()
147 else:
148 return shortcuts[1]
149
150 def setShortcut(self, shortcut):
151 """
152 Public slot to set the keyboard shortcut.
153
154 @param shortcut the accelerator (QKeySequence)
155 """
156 super().setShortcut(shortcut)
157 self.__ammendToolTip()
158
159 def setShortcuts(self, shortcuts):
160 """
161 Public slot to set the list of keyboard shortcuts.
162
163 @param shortcuts list of keyboard accelerators (list of QKeySequence)
164 or key for a platform dependent list of accelerators
165 (QKeySequence.StandardKey)
166 """
167 super().setShortcuts(shortcuts)
168 self.__ammendToolTip()
169
170 def setIconText(self, text):
171 """
172 Public slot to set the icon text of the action.
173
174 @param text new icon text (string)
175 """
176 super().setIconText(text)
177 self.__ammendToolTip()
178
179 def __ammendToolTip(self):
180 """
181 Private slot to add the primary keyboard accelerator to the tooltip.
182 """
183 shortcut = self.shortcut().toString(
184 QKeySequence.SequenceFormat.NativeText)
185 if shortcut:
186 if e5App().isLeftToRight():
187 fmt = "{0} ({1})"
188 else:
189 fmt = "({1}) {0}"
190 self.setToolTip(fmt.format(self.iconText(), shortcut))
191
192
193 def addActions(target, actions):
194 """
195 Module function to add a list of actions to a widget.
196
197 @param target reference to the target widget (QWidget)
198 @param actions list of actions to be added to the target. A
199 None indicates a separator (list of QActions)
200 """
201 if target is None:
202 return
203
204 for action in actions:
205 if action is None:
206 target.addSeparator()
207 else:
208 target.addAction(action)
209
210
211 def createActionGroup(parent, name=None, exclusive=False):
212 """
213 Module function to create an action group.
214
215 @param parent parent object of the action group (QObject)
216 @param name name of the action group object (string)
217 @param exclusive flag indicating an exclusive action group (boolean)
218 @return reference to the created action group (QActionGroup)
219 """
220 actGrp = QActionGroup(parent)
221 if name:
222 actGrp.setObjectName(name)
223 actGrp.setExclusive(exclusive)
224 return actGrp

eric ide

mercurial