E5Gui/E5Action.py

changeset 945
8cd4d08fa9f6
parent 791
9ec2ac20e54e
child 1131
7781e396c903
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
9 This extension is necessary in order to support alternate keyboard 9 This extension is necessary in order to support alternate keyboard
10 shortcuts. 10 shortcuts.
11 """ 11 """
12 12
13 from PyQt4.QtGui import QAction, QActionGroup, QIcon, QKeySequence, qApp 13 from PyQt4.QtGui import QAction, QActionGroup, QIcon, QKeySequence, qApp
14
14 15
15 class ArgumentsError(RuntimeError): 16 class ArgumentsError(RuntimeError):
16 """ 17 """
17 Class implementing an exception, which is raised, if the wrong number of arguments 18 Class implementing an exception, which is raised, if the wrong number of arguments
18 are given. 19 are given.
37 38
38 @return string representing the error message 39 @return string representing the error message
39 """ 40 """
40 return str(self.errorMessage) 41 return str(self.errorMessage)
41 42
43
42 class E5Action(QAction): 44 class E5Action(QAction):
43 """ 45 """
44 Class implementing an Action class extending QAction. 46 Class implementing an Action class extending QAction.
45 """ 47 """
46 def __init__(self, *args): 48 def __init__(self, *args):
47 """ 49 """
48 Constructor 50 Constructor
49 51
50 @param args argument list of the constructor. This list is one of 52 @param args argument list of the constructor. This list is one of
51 <ul> 53 <ul>
52 <li>text (string), icon (QIcon), menu text (string), 54 <li>text (string), icon (QIcon), menu text (string),
53 accelarator (QKeySequence), alternative accelerator (QKeySequence), 55 accelarator (QKeySequence), alternative accelerator (QKeySequence),
54 parent (QObject), name (string), toggle (boolean)</li> 56 parent (QObject), name (string), toggle (boolean)</li>
55 <li>text (string), icon (QIcon), menu text (string), 57 <li>text (string), icon (QIcon), menu text (string),
56 accelarator (QKeySequence), alternative accelerator (QKeySequence), 58 accelarator (QKeySequence), alternative accelerator (QKeySequence),
57 parent (QObject), name (string)</li> 59 parent (QObject), name (string)</li>
58 <li>text (string), menu text (string), 60 <li>text (string), menu text (string),
59 accelarator (QKeySequence), alternative accelerator (QKeySequence), 61 accelarator (QKeySequence), alternative accelerator (QKeySequence),
60 parent (QObject), name (string), toggle (boolean)</li> 62 parent (QObject), name (string), toggle (boolean)</li>
61 <li>text (string), menu text (string), 63 <li>text (string), menu text (string),
62 accelarator (QKeySequence), alternative accelerator (QKeySequence), 64 accelarator (QKeySequence), alternative accelerator (QKeySequence),
63 parent (QObject), name (string)</li> 65 parent (QObject), name (string)</li>
64 </ul> 66 </ul>
65 """ 67 """
66 if isinstance(args[1], QIcon): 68 if isinstance(args[1], QIcon):
67 icon = args[1] 69 icon = args[1]
68 incr = 1 70 incr = 1
69 else: 71 else:
70 icon = None 72 icon = None
71 incr = 0 73 incr = 0
72 if len(args) < 6+incr: 74 if len(args) < 6 + incr:
73 raise ArgumentsError( 75 raise ArgumentsError(
74 "Not enough arguments, {0:d} expected, got {1:d}".format( 76 "Not enough arguments, {0:d} expected, got {1:d}".format(
75 6 + incr, len(args))) 77 6 + incr, len(args)))
76 elif len(args) > 7+incr: 78 elif len(args) > 7 + incr:
77 raise ArgumentsError( 79 raise ArgumentsError(
78 "Too many arguments, max. {0:d} expected, got {1:d}".format( 80 "Too many arguments, max. {0:d} expected, got {1:d}".format(
79 7 + incr, len(args))) 81 7 + incr, len(args)))
80 82
81 parent = args[4+incr] 83 parent = args[4 + incr]
82 QAction.__init__(self, parent) 84 QAction.__init__(self, parent)
83 name = args[5+incr] 85 name = args[5 + incr]
84 if name: 86 if name:
85 self.setObjectName(name) 87 self.setObjectName(name)
86 88
87 if args[1+incr]: 89 if args[1 + incr]:
88 self.setText(args[1+incr]) 90 self.setText(args[1 + incr])
89 91
90 if args[0]: 92 if args[0]:
91 self.setIconText(args[0]) 93 self.setIconText(args[0])
92 if args[2+incr]: 94 if args[2 + incr]:
93 self.setShortcut(QKeySequence(args[2+incr])) 95 self.setShortcut(QKeySequence(args[2 + incr]))
94 96
95 if args[3+incr]: 97 if args[3 + incr]:
96 self.setAlternateShortcut(QKeySequence(args[3+incr])) 98 self.setAlternateShortcut(QKeySequence(args[3 + incr]))
97 99
98 if icon: 100 if icon:
99 self.setIcon(icon) 101 self.setIcon(icon)
100 102
101 if len(args) == 7+incr: 103 if len(args) == 7 + incr:
102 self.setCheckable(args[6+incr]) 104 self.setCheckable(args[6 + incr])
103 105
104 self.__ammendToolTip() 106 self.__ammendToolTip()
105 107
106 def setAlternateShortcut(self, shortcut): 108 def setAlternateShortcut(self, shortcut):
107 """ 109 """
142 def setShortcuts(self, shortcuts): 144 def setShortcuts(self, shortcuts):
143 """ 145 """
144 Public slot to set the list of keyboard shortcuts. 146 Public slot to set the list of keyboard shortcuts.
145 147
146 @param shortcuts list of keyboard accelerators (list of QKeySequence) 148 @param shortcuts list of keyboard accelerators (list of QKeySequence)
147 or key for a platform dependent list of accelerators 149 or key for a platform dependent list of accelerators
148 (QKeySequence.StandardKey) 150 (QKeySequence.StandardKey)
149 """ 151 """
150 QAction.setShortcuts(self, shortcuts) 152 QAction.setShortcuts(self, shortcuts)
151 self.__ammendToolTip() 153 self.__ammendToolTip()
152 154
159 if qApp.isLeftToRight(): 161 if qApp.isLeftToRight():
160 fmt = "{0} ({1})" 162 fmt = "{0} ({1})"
161 else: 163 else:
162 fmt = "({1}) {0}" 164 fmt = "({1}) {0}"
163 self.setToolTip(fmt.format(self.iconText(), shortcut)) 165 self.setToolTip(fmt.format(self.iconText(), shortcut))
166
164 167
165 def addActions(target, actions): 168 def addActions(target, actions):
166 """ 169 """
167 Module function to add a list of actions to a widget. 170 Module function to add a list of actions to a widget.
168 171
177 if action is None: 180 if action is None:
178 target.addSeparator() 181 target.addSeparator()
179 else: 182 else:
180 target.addAction(action) 183 target.addAction(action)
181 184
182 def createActionGroup(parent, name = None, exclusive = False): 185
186 def createActionGroup(parent, name=None, exclusive=False):
183 """ 187 """
184 Module function to create an action group. 188 Module function to create an action group.
185 189
186 @param parent parent object of the action group (QObject) 190 @param parent parent object of the action group (QObject)
187 @param name name of the action group object (string) 191 @param name name of the action group object (string)

eric ide

mercurial