E5Gui/E5MessageBox.py

changeset 553
5af61623ae3c
parent 550
22b951013193
child 562
8bf0dbc1ca6a
equal deleted inserted replaced
551:e1074b63ff08 553:5af61623ae3c
7 Module implementing QMessageBox replacements and more convenience function. 7 Module implementing QMessageBox replacements and more convenience function.
8 """ 8 """
9 9
10 from PyQt4.QtCore import Qt 10 from PyQt4.QtCore import Qt
11 from PyQt4.QtGui import QMessageBox, QApplication 11 from PyQt4.QtGui import QMessageBox, QApplication
12
13 ################################################################################
14 ## Mappings to standard QMessageBox ##
15 ################################################################################
16
17 # QMessageBox.Icon
18 Critical = QMessageBox.Critical
19 Information = QMessageBox.Information
20 Question = QMessageBox.Question
21 Warning = QMessageBox.Warning
22
23 StandardButtons = QMessageBox.StandardButtons
24
25 # QMessageBox.StandardButton
26 Abort = QMessageBox.Abort
27 Apply = QMessageBox.Apply
28 Cancel = QMessageBox.Cancel
29 Close = QMessageBox.Close
30 Discard = QMessageBox.Discard
31 Help = QMessageBox.Help
32 Ignore = QMessageBox.Ignore
33 No = QMessageBox.No
34 NoToAll = QMessageBox.NoToAll
35 Ok = QMessageBox.Ok
36 Open = QMessageBox.Open
37 Reset = QMessageBox.Reset
38 RestoreDefaults = QMessageBox.RestoreDefaults
39 Retry = QMessageBox.Retry
40 Save = QMessageBox.Save
41 SaveAll = QMessageBox.SaveAll
42 Yes = QMessageBox.Yes
43 YesToAll = QMessageBox.YesToAll
44 NoButton = QMessageBox.NoButton
45
46 # QMessageBox.ButtonRole
47 AcceptRole = QMessageBox.AcceptRole
48 ActionRole = QMessageBox.ActionRole
49 ApplyRole = QMessageBox.ApplyRole
50 DestructiveRole = QMessageBox.DestructiveRole
51 InvalidRole = QMessageBox.InvalidRole
52 HelpRole = QMessageBox.HelpRole
53 NoRole = QMessageBox.NoRole
54 RejectRole = QMessageBox.RejectRole
55 ResetRole = QMessageBox.ResetRole
56 YesRole = QMessageBox.YesRole
57
58 ################################################################################
59 ## Replacement for the QMessageBox class ##
60 ################################################################################
61
62 class E5MessageBox(QMessageBox):
63 """
64 Class implementing a replacement for QMessageBox.
65 """
66 def __init__(self, icon, title, text, modal = False,
67 buttons = QMessageBox.StandardButtons(QMessageBox.NoButton),
68 parent = None):
69 """
70 Constructor
71
72 @param icon type of icon to be shown (QMessageBox.Icon)
73 @param title caption of the message box (string)
74 @param text text to be shown by the message box (string)
75 @keyparam modal flag indicating a modal dialog (boolean)
76 @keyparam buttons set of standard buttons to generate (StandardButtons)
77 @keyparam parent parent widget of the message box (QWidget)
78 """
79 QMessageBox.__init__(self, parent)
80 self.setIcon(icon)
81 if modal and parent is not None:
82 self.setWindowModality(Qt.WindowModal)
83 self.setWindowTitle("{0} - {1}".format(
84 QApplication.applicationName(), title))
85 self.setText(text)
86 if buttons != QMessageBox.NoButton:
87 self.setStandardButtons(buttons)
88
89 ################################################################################
90 ## Replacements for QMessageBox static methods ##
91 ################################################################################
12 92
13 def __messageBox(parent, title, text, icon, 93 def __messageBox(parent, title, text, icon,
14 buttons = QMessageBox.Ok, defaultButton = QMessageBox.NoButton): 94 buttons = QMessageBox.Ok, defaultButton = QMessageBox.NoButton):
15 """ 95 """
16 Private module function to show a modal message box. 96 Private module function to show a modal message box.
116 (QMessageBox.StandardButton) 196 (QMessageBox.StandardButton)
117 """ 197 """
118 return __messageBox(parent, title, text, QMessageBox.Warning, 198 return __messageBox(parent, title, text, QMessageBox.Warning,
119 buttons, defaultButton) 199 buttons, defaultButton)
120 200
121 Critical = 0 201 ################################################################################
122 Information = 1 202 ## Additional convenience functions ##
123 Question = 2 203 ################################################################################
124 Warning = 3 204
125 205 def yesNo(parent, title, text, icon = Question, yesDefault = False):
126 def yesNo(parent, title, text, type_ = Question, yesDefault = False):
127 """ 206 """
128 Function to show a model yes/no message box. 207 Function to show a model yes/no message box.
129 208
130 @param parent parent widget of the message box (QWidget) 209 @param parent parent widget of the message box (QWidget)
131 @param title caption of the message box (string) 210 @param title caption of the message box (string)
132 @param text text to be shown by the message box (string) 211 @param text text to be shown by the message box (string)
133 @keyparam type_ type of the dialog (Critical, Information, Question or Warning) 212 @keyparam icon icon for the dialog (Critical, Information, Question or Warning)
134 @keyparam yesDefault flag indicating that the Yes button should be the default 213 @keyparam yesDefault flag indicating that the Yes button should be the default
135 button (boolean) 214 button (boolean)
136 @return flag indicating the selection of the Yes button (boolean) 215 @return flag indicating the selection of the Yes button (boolean)
137 """ 216 """
138 assert type_ in [Critical, Information, Question, Warning] 217 assert icon in [Critical, Information, Question, Warning]
139
140 if type_ == Question:
141 icon = QMessageBox.Question
142 elif type_ == Warning:
143 icon = QMessageBox.Warning
144 elif type_ == Critical:
145 icon = QMessageBox.Critical
146 elif type_ == Information:
147 icon = QMessageBox.Information
148 218
149 res = __messageBox(parent, title, text, icon, 219 res = __messageBox(parent, title, text, icon,
150 QMessageBox.StandardButtons(QMessageBox.Yes | QMessageBox.No), 220 QMessageBox.StandardButtons(QMessageBox.Yes | QMessageBox.No),
151 yesDefault and QMessageBox.Yes or QMessageBox.No) 221 yesDefault and QMessageBox.Yes or QMessageBox.No)
152 return res == QMessageBox.Yes 222 return res == QMessageBox.Yes
153 223
154 def retryAbort(parent, title, text, type_ = Question): 224 def retryAbort(parent, title, text, icon = Question):
155 """ 225 """
156 Function to show a model abort/retry message box. 226 Function to show a model abort/retry message box.
157 227
158 @param parent parent widget of the message box (QWidget) 228 @param parent parent widget of the message box (QWidget)
159 @param title caption of the message box (string) 229 @param title caption of the message box (string)
160 @param text text to be shown by the message box (string) 230 @param text text to be shown by the message box (string)
161 @keyparam type_ type of the dialog (Critical, Information, Question or Warning) 231 @keyparam icon icon for the dialog (Critical, Information, Question or Warning)
162 @return flag indicating the selection of the Retry button (boolean) 232 @return flag indicating the selection of the Retry button (boolean)
163 """ 233 """
164 assert type_ in [Critical, Information, Question, Warning] 234 assert icon in [Critical, Information, Question, Warning]
165
166 if type_ == Question:
167 icon = QMessageBox.Question
168 elif type_ == Warning:
169 icon = QMessageBox.Warning
170 elif type_ == Critical:
171 icon = QMessageBox.Critical
172 elif type_ == Information:
173 icon = QMessageBox.Information
174 235
175 res = __messageBox(parent, title, text, icon, 236 res = __messageBox(parent, title, text, icon,
176 QMessageBox.StandardButtons(QMessageBox.Retry | QMessageBox.Abort), 237 QMessageBox.StandardButtons(QMessageBox.Retry | QMessageBox.Abort),
177 QMessageBox.Retry) 238 QMessageBox.Retry)
178 return res == QMessageBox.Retry 239 return res == QMessageBox.Retry

eric ide

mercurial