E5Gui/E5Action.py

changeset 55
b5c84934de9c
child 96
9624a110667d
equal deleted inserted replaced
54:31463df17fd5 55:b5c84934de9c
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2006 - 2010 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 PyQt4.QtGui import QAction, QActionGroup, QIcon, QKeySequence
14 from PyQt4.QtCore import QObject
15
16 class ArgumentsError(RuntimeError):
17 """
18 Class implementing an exception, which is raised, if the wrong number of arguments
19 are given.
20 """
21 def __init__(self, error):
22 """
23 Constructor
24 """
25 self.errorMessage = str(error)
26
27 def __repr__(self):
28 """
29 Private method returning a representation of the exception.
30
31 @return string representing the error message
32 """
33 return str(self.errorMessage)
34
35 def __str__(self):
36 """
37 Private method returning a string representation of the exception.
38
39 @return string representing the error message
40 """
41 return str(self.errorMessage)
42
43 class E5Action(QAction):
44 """
45 Class implementing an Action class extending QAction.
46 """
47 def __init__(self, *args):
48 """
49 Constructor
50
51 @param args argument list of the constructor. This list is one of
52 <ul>
53 <li>text (string), icon (QIcon), menu text (string),
54 accelarator (QKeySequence), alternative accelerator (QKeySequence),
55 parent (QObject), name (string), toggle (boolean)</li>
56 <li>text (string), icon (QIcon), menu text (string),
57 accelarator (QKeySequence), alternative accelerator (QKeySequence),
58 parent (QObject), name (string)</li>
59 <li>text (string), menu text (string),
60 accelarator (QKeySequence), alternative accelerator (QKeySequence),
61 parent (QObject), name (string), toggle (boolean)</li>
62 <li>text (string), menu text (string),
63 accelarator (QKeySequence), alternative accelerator (QKeySequence),
64 parent (QObject), name (string)</li>
65 </ul>
66 """
67 if isinstance(args[1], QIcon):
68 icon = args[1]
69 incr = 1
70 else:
71 icon = None
72 incr = 0
73 if len(args) < 6+incr:
74 raise ArgumentsError("Not enough arguments, %d expected, got %d" % \
75 (6+incr, len(args)))
76 elif len(args) > 7+incr:
77 raise ArgumentsError("Too many arguments, max. %d expected, got %d" % \
78 (7+incr, len(args)))
79
80 parent = args[4+incr]
81 QAction.__init__(self, parent)
82 name = args[5+incr]
83 if name:
84 self.setObjectName(name)
85
86 if args[1+incr]:
87 self.setText(args[1+incr])
88
89 if args[0]:
90 self.setIconText(args[0])
91 if args[2+incr]:
92 self.setShortcut(QKeySequence(args[2+incr]))
93
94 if args[3+incr]:
95 self.setAlternateShortcut(QKeySequence(args[3+incr]))
96
97 if icon:
98 self.setIcon(icon)
99
100 if len(args) == 7+incr:
101 self.setCheckable(args[6+incr])
102
103 def setAlternateShortcut(self, shortcut):
104 """
105 Public slot to set the alternative keyboard shortcut.
106
107 @param shortcut the alternative accelerator (QKeySequence)
108 """
109 if not shortcut.isEmpty():
110 shortcuts = self.shortcuts()
111 if len(shortcuts) > 0:
112 if len(shortcuts) == 1:
113 shortcuts.append(shortcut)
114 else:
115 shortcuts[1] = shortcut
116 self.setShortcuts(shortcuts)
117
118 def alternateShortcut(self):
119 """
120 Public method to retrieve the alternative keyboard shortcut.
121
122 @return the alternative accelerator (QKeySequence)
123 """
124 shortcuts = self.shortcuts()
125 if len(shortcuts) < 2:
126 return QKeySequence()
127 else:
128 return shortcuts[1]
129
130 def addActions(target, actions):
131 """
132 Module function to add a list of actions to a widget.
133
134 @param target reference to the target widget (QWidget)
135 @param actions list of actions to be added to the target. A
136 None indicates a separator (list of QActions)
137 """
138 if target is None:
139 return
140
141 for action in actions:
142 if action is None:
143 target.addSeparator()
144 else:
145 target.addAction(action)
146
147 def createActionGroup(parent, name = None, exclusive = False):
148 """
149 Module function to create an action group.
150
151 @param parent parent object of the action group (QObject)
152 @param name name of the action group object (string)
153 @param exclusive flag indicating an exclusive action group (boolean)
154 @return reference to the created action group (QActionGroup)
155 """
156 actGrp = QActionGroup(parent)
157 if name:
158 actGrp.setObjectName(name)
159 actGrp.setExclusive(exclusive)
160 return actGrp

eric ide

mercurial