eric6/E5Gui/E5Action.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) 2006 - 2019 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 __future__ import unicode_literals
14
15 from PyQt5.QtGui import QIcon, QKeySequence
16 from PyQt5.QtWidgets import QAction, QActionGroup, qApp
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(E5Action, self).__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(E5Action, self).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(E5Action, self).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(E5Action, self).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(QKeySequence.NativeText)
184 if shortcut:
185 if qApp.isLeftToRight():
186 fmt = "{0} ({1})"
187 else:
188 fmt = "({1}) {0}"
189 self.setToolTip(fmt.format(self.iconText(), shortcut))
190
191
192 def addActions(target, actions):
193 """
194 Module function to add a list of actions to a widget.
195
196 @param target reference to the target widget (QWidget)
197 @param actions list of actions to be added to the target. A
198 None indicates a separator (list of QActions)
199 """
200 if target is None:
201 return
202
203 for action in actions:
204 if action is None:
205 target.addSeparator()
206 else:
207 target.addAction(action)
208
209
210 def createActionGroup(parent, name=None, exclusive=False):
211 """
212 Module function to create an action group.
213
214 @param parent parent object of the action group (QObject)
215 @param name name of the action group object (string)
216 @param exclusive flag indicating an exclusive action group (boolean)
217 @return reference to the created action group (QActionGroup)
218 """
219 actGrp = QActionGroup(parent)
220 if name:
221 actGrp.setObjectName(name)
222 actGrp.setExclusive(exclusive)
223 return actGrp

eric ide

mercurial