src/eric7/EricGui/EricAction.py

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

eric ide

mercurial