eric6/E5Gui/E5MessageBox.py

changeset 8143
2c730d5fd177
parent 7923
91e843545d9a
child 8176
31965986ecd1
child 8218
7c09585bd960
equal deleted inserted replaced
8141:27f636beebad 8143:2c730d5fd177
13 ############################################################################### 13 ###############################################################################
14 ## Mappings to standard QMessageBox ## 14 ## Mappings to standard QMessageBox ##
15 ############################################################################### 15 ###############################################################################
16 16
17 # QMessageBox.Icon 17 # QMessageBox.Icon
18 NoIcon = QMessageBox.NoIcon 18 NoIcon = QMessageBox.Icon.NoIcon
19 Critical = QMessageBox.Critical 19 Critical = QMessageBox.Icon.Critical
20 Information = QMessageBox.Information 20 Information = QMessageBox.Icon.Information
21 Question = QMessageBox.Question 21 Question = QMessageBox.Icon.Question
22 Warning = QMessageBox.Warning # __IGNORE_WARNING_M131__ 22 Warning = QMessageBox.Icon.Warning # __IGNORE_WARNING_M131__
23 23
24 StandardButtons = QMessageBox.StandardButtons 24 StandardButtons = QMessageBox.StandardButtons
25 25
26 # QMessageBox.StandardButton 26 # QMessageBox.StandardButton
27 Abort = QMessageBox.Abort 27 Abort = QMessageBox.StandardButton.Abort
28 Apply = QMessageBox.Apply 28 Apply = QMessageBox.StandardButton.Apply
29 Cancel = QMessageBox.Cancel 29 Cancel = QMessageBox.StandardButton.Cancel
30 Close = QMessageBox.Close 30 Close = QMessageBox.StandardButton.Close
31 Discard = QMessageBox.Discard 31 Discard = QMessageBox.StandardButton.Discard
32 Help = QMessageBox.Help 32 Help = QMessageBox.StandardButton.Help
33 Ignore = QMessageBox.Ignore 33 Ignore = QMessageBox.StandardButton.Ignore
34 No = QMessageBox.No 34 No = QMessageBox.StandardButton.No
35 NoToAll = QMessageBox.NoToAll 35 NoToAll = QMessageBox.StandardButton.NoToAll
36 Ok = QMessageBox.Ok 36 Ok = QMessageBox.StandardButton.Ok
37 Open = QMessageBox.Open 37 Open = QMessageBox.StandardButton.Open
38 Reset = QMessageBox.Reset 38 Reset = QMessageBox.StandardButton.Reset
39 RestoreDefaults = QMessageBox.RestoreDefaults 39 RestoreDefaults = QMessageBox.StandardButton.RestoreDefaults
40 Retry = QMessageBox.Retry 40 Retry = QMessageBox.StandardButton.Retry
41 Save = QMessageBox.Save 41 Save = QMessageBox.StandardButton.Save
42 SaveAll = QMessageBox.SaveAll 42 SaveAll = QMessageBox.StandardButton.SaveAll
43 Yes = QMessageBox.Yes 43 Yes = QMessageBox.StandardButton.Yes
44 YesToAll = QMessageBox.YesToAll 44 YesToAll = QMessageBox.StandardButton.YesToAll
45 NoButton = QMessageBox.NoButton 45 NoButton = QMessageBox.StandardButton.NoButton
46 46
47 # QMessageBox.ButtonRole 47 # QMessageBox.ButtonRole
48 AcceptRole = QMessageBox.AcceptRole 48 AcceptRole = QMessageBox.ButtonRole.AcceptRole
49 ActionRole = QMessageBox.ActionRole 49 ActionRole = QMessageBox.ButtonRole.ActionRole
50 ApplyRole = QMessageBox.ApplyRole 50 ApplyRole = QMessageBox.ButtonRole.ApplyRole
51 DestructiveRole = QMessageBox.DestructiveRole 51 DestructiveRole = QMessageBox.ButtonRole.DestructiveRole
52 InvalidRole = QMessageBox.InvalidRole 52 InvalidRole = QMessageBox.ButtonRole.InvalidRole
53 HelpRole = QMessageBox.HelpRole 53 HelpRole = QMessageBox.ButtonRole.HelpRole
54 NoRole = QMessageBox.NoRole 54 NoRole = QMessageBox.ButtonRole.NoRole
55 RejectRole = QMessageBox.RejectRole 55 RejectRole = QMessageBox.ButtonRole.RejectRole
56 ResetRole = QMessageBox.ResetRole 56 ResetRole = QMessageBox.ButtonRole.ResetRole
57 YesRole = QMessageBox.YesRole 57 YesRole = QMessageBox.ButtonRole.YesRole
58 58
59 ############################################################################### 59 ###############################################################################
60 ## Replacement for the QMessageBox class ## 60 ## Replacement for the QMessageBox class ##
61 ############################################################################### 61 ###############################################################################
62 62
64 class E5MessageBox(QMessageBox): 64 class E5MessageBox(QMessageBox):
65 """ 65 """
66 Class implementing a replacement for QMessageBox. 66 Class implementing a replacement for QMessageBox.
67 """ 67 """
68 def __init__(self, icon, title, text, modal=False, 68 def __init__(self, icon, title, text, modal=False,
69 buttons=QMessageBox.NoButton, 69 buttons=QMessageBox.StandardButton.NoButton,
70 parent=None): 70 parent=None):
71 """ 71 """
72 Constructor 72 Constructor
73 73
74 @param icon type of icon to be shown (QMessageBox.Icon) 74 @param icon type of icon to be shown (QMessageBox.Icon)
80 """ 80 """
81 super(E5MessageBox, self).__init__(parent) 81 super(E5MessageBox, self).__init__(parent)
82 self.setIcon(icon) 82 self.setIcon(icon)
83 if modal: 83 if modal:
84 if parent is not None: 84 if parent is not None:
85 self.setWindowModality(Qt.WindowModal) 85 self.setWindowModality(Qt.WindowModality.WindowModal)
86 else: 86 else:
87 self.setWindowModality(Qt.ApplicationModal) 87 self.setWindowModality(Qt.WindowModality.ApplicationModal)
88 else: 88 else:
89 self.setWindowModality(Qt.NonModal) 89 self.setWindowModality(Qt.WindowModality.NonModal)
90 if title == "": 90 if title == "":
91 self.setWindowTitle("{0}".format( 91 self.setWindowTitle("{0}".format(
92 QApplication.applicationName())) 92 QApplication.applicationName()))
93 else: 93 else:
94 self.setWindowTitle("{0} - {1}".format( 94 self.setWindowTitle("{0} - {1}".format(
100 ## Replacements for QMessageBox static methods ## 100 ## Replacements for QMessageBox static methods ##
101 ############################################################################### 101 ###############################################################################
102 102
103 103
104 def __messageBox(parent, title, text, icon, 104 def __messageBox(parent, title, text, icon,
105 buttons=QMessageBox.Ok, defaultButton=QMessageBox.NoButton, 105 buttons=QMessageBox.StandardButton.Ok,
106 textFormat=Qt.AutoText): 106 defaultButton=QMessageBox.StandardButton.NoButton,
107 textFormat=Qt.TextFormat.AutoText):
107 """ 108 """
108 Private module function to show a modal message box. 109 Private module function to show a modal message box.
109 110
110 @param parent parent widget of the message box (QWidget) 111 @param parent parent widget of the message box (QWidget)
111 @param title caption of the message box (string) 112 @param title caption of the message box (string)
119 @return button pressed by the user (QMessageBox.StandardButton) 120 @return button pressed by the user (QMessageBox.StandardButton)
120 """ 121 """
121 messageBox = QMessageBox(parent) 122 messageBox = QMessageBox(parent)
122 messageBox.setIcon(icon) 123 messageBox.setIcon(icon)
123 if parent is not None: 124 if parent is not None:
124 messageBox.setWindowModality(Qt.WindowModal) 125 messageBox.setWindowModality(Qt.WindowModality.WindowModal)
125 if title == "": 126 if title == "":
126 messageBox.setWindowTitle("{0}".format( 127 messageBox.setWindowTitle("{0}".format(
127 QApplication.applicationName())) 128 QApplication.applicationName()))
128 else: 129 else:
129 messageBox.setWindowTitle("{0} - {1}".format( 130 messageBox.setWindowTitle("{0} - {1}".format(
133 messageBox.setStandardButtons(buttons) 134 messageBox.setStandardButtons(buttons)
134 messageBox.setDefaultButton(defaultButton) 135 messageBox.setDefaultButton(defaultButton)
135 messageBox.exec() 136 messageBox.exec()
136 clickedButton = messageBox.clickedButton() 137 clickedButton = messageBox.clickedButton()
137 if clickedButton is None: 138 if clickedButton is None:
138 return QMessageBox.NoButton 139 return QMessageBox.StandardButton.NoButton
139 else: 140 else:
140 return messageBox.standardButton(clickedButton) 141 return messageBox.standardButton(clickedButton)
141 142
142 # the about functions are here for consistancy 143 # the about functions are here for consistancy
143 about = QMessageBox.about 144 about = QMessageBox.about
144 aboutQt = QMessageBox.aboutQt 145 aboutQt = QMessageBox.aboutQt
145 146
146 147
147 def critical(parent, title, text, 148 def critical(parent, title, text,
148 buttons=QMessageBox.Ok, defaultButton=QMessageBox.NoButton): 149 buttons=QMessageBox.StandardButton.Ok,
150 defaultButton=QMessageBox.StandardButton.NoButton):
149 """ 151 """
150 Function to show a modal critical message box. 152 Function to show a modal critical message box.
151 153
152 @param parent parent widget of the message box (QWidget) 154 @param parent parent widget of the message box (QWidget)
153 @param title caption of the message box (string) 155 @param title caption of the message box (string)
156 (QMessageBox.StandardButtons) 158 (QMessageBox.StandardButtons)
157 @param defaultButton flag indicating the default button 159 @param defaultButton flag indicating the default button
158 (QMessageBox.StandardButton) 160 (QMessageBox.StandardButton)
159 @return button pressed by the user (QMessageBox.StandardButton) 161 @return button pressed by the user (QMessageBox.StandardButton)
160 """ 162 """
161 return __messageBox(parent, title, text, QMessageBox.Critical, 163 return __messageBox(parent, title, text, QMessageBox.Icon.Critical,
162 buttons, defaultButton) 164 buttons, defaultButton)
163 165
164 166
165 def information(parent, title, text, 167 def information(parent, title, text,
166 buttons=QMessageBox.Ok, defaultButton=QMessageBox.NoButton): 168 buttons=QMessageBox.StandardButton.Ok,
169 defaultButton=QMessageBox.StandardButton.NoButton):
167 """ 170 """
168 Function to show a modal information message box. 171 Function to show a modal information message box.
169 172
170 @param parent parent widget of the message box (QWidget) 173 @param parent parent widget of the message box (QWidget)
171 @param title caption of the message box (string) 174 @param title caption of the message box (string)
174 (QMessageBox.StandardButtons) 177 (QMessageBox.StandardButtons)
175 @param defaultButton flag indicating the default button 178 @param defaultButton flag indicating the default button
176 (QMessageBox.StandardButton) 179 (QMessageBox.StandardButton)
177 @return button pressed by the user (QMessageBox.StandardButton) 180 @return button pressed by the user (QMessageBox.StandardButton)
178 """ 181 """
179 return __messageBox(parent, title, text, QMessageBox.Information, 182 return __messageBox(parent, title, text, QMessageBox.Icon.Information,
180 buttons, defaultButton) 183 buttons, defaultButton)
181 184
182 185
183 def question(parent, title, text, 186 def question(parent, title, text,
184 buttons=QMessageBox.Ok, defaultButton=QMessageBox.NoButton): 187 buttons=QMessageBox.StandardButton.Ok,
188 defaultButton=QMessageBox.StandardButton.NoButton):
185 """ 189 """
186 Function to show a modal question message box. 190 Function to show a modal question message box.
187 191
188 @param parent parent widget of the message box (QWidget) 192 @param parent parent widget of the message box (QWidget)
189 @param title caption of the message box (string) 193 @param title caption of the message box (string)
192 (QMessageBox.StandardButtons) 196 (QMessageBox.StandardButtons)
193 @param defaultButton flag indicating the default button 197 @param defaultButton flag indicating the default button
194 (QMessageBox.StandardButton) 198 (QMessageBox.StandardButton)
195 @return button pressed by the user (QMessageBox.StandardButton) 199 @return button pressed by the user (QMessageBox.StandardButton)
196 """ 200 """
197 return __messageBox(parent, title, text, QMessageBox.Question, 201 return __messageBox(parent, title, text, QMessageBox.Icon.Question,
198 buttons, defaultButton) 202 buttons, defaultButton)
199 203
200 204
201 def warning(parent, title, text, 205 def warning(parent, title, text,
202 buttons=QMessageBox.Ok, defaultButton=QMessageBox.NoButton): 206 buttons=QMessageBox.StandardButton.Ok,
207 defaultButton=QMessageBox.StandardButton.NoButton):
203 """ 208 """
204 Function to show a modal warning message box. 209 Function to show a modal warning message box.
205 210
206 @param parent parent widget of the message box (QWidget) 211 @param parent parent widget of the message box (QWidget)
207 @param title caption of the message box (string) 212 @param title caption of the message box (string)
210 (QMessageBox.StandardButtons) 215 (QMessageBox.StandardButtons)
211 @param defaultButton flag indicating the default button 216 @param defaultButton flag indicating the default button
212 (QMessageBox.StandardButton) 217 (QMessageBox.StandardButton)
213 @return button pressed by the user (QMessageBox.StandardButton) 218 @return button pressed by the user (QMessageBox.StandardButton)
214 """ 219 """
215 return __messageBox(parent, title, text, QMessageBox.Warning, 220 return __messageBox(parent, title, text, QMessageBox.Icon.Warning,
216 buttons, defaultButton) 221 buttons, defaultButton)
217 222
218 ############################################################################### 223 ###############################################################################
219 ## Additional convenience functions ## 224 ## Additional convenience functions ##
220 ############################################################################### 225 ###############################################################################
221 226
222 227
223 def yesNo(parent, title, text, icon=Question, yesDefault=False, 228 def yesNo(parent, title, text, icon=Question, yesDefault=False,
224 textFormat=Qt.AutoText): 229 textFormat=Qt.TextFormat.AutoText):
225 """ 230 """
226 Function to show a model yes/no message box. 231 Function to show a model yes/no message box.
227 232
228 @param parent parent widget of the message box (QWidget) 233 @param parent parent widget of the message box (QWidget)
229 @param title caption of the message box (string) 234 @param title caption of the message box (string)
239 if icon not in [Critical, Information, Question, Warning]: 244 if icon not in [Critical, Information, Question, Warning]:
240 raise ValueError("Bad value for 'icon' parameter.") 245 raise ValueError("Bad value for 'icon' parameter.")
241 246
242 res = __messageBox( 247 res = __messageBox(
243 parent, title, text, icon, 248 parent, title, text, icon,
244 QMessageBox.StandardButtons(QMessageBox.Yes | QMessageBox.No), 249 QMessageBox.StandardButtons(QMessageBox.StandardButton.Yes |
245 yesDefault and QMessageBox.Yes or QMessageBox.No, 250 QMessageBox.StandardButton.No),
251 yesDefault and QMessageBox.StandardButton.Yes or
252 QMessageBox.StandardButton.No,
246 textFormat) 253 textFormat)
247 return res == QMessageBox.Yes 254 return res == QMessageBox.StandardButton.Yes
248 255
249 256
250 def retryAbort(parent, title, text, icon=Question, textFormat=Qt.AutoText): 257 def retryAbort(parent, title, text, icon=Question,
258 textFormat=Qt.TextFormat.AutoText):
251 """ 259 """
252 Function to show a model abort/retry message box. 260 Function to show a model abort/retry message box.
253 261
254 @param parent parent widget of the message box (QWidget) 262 @param parent parent widget of the message box (QWidget)
255 @param title caption of the message box (string) 263 @param title caption of the message box (string)
263 if icon not in [Critical, Information, Question, Warning]: 271 if icon not in [Critical, Information, Question, Warning]:
264 raise ValueError("Bad value for 'icon' parameter.") 272 raise ValueError("Bad value for 'icon' parameter.")
265 273
266 res = __messageBox( 274 res = __messageBox(
267 parent, title, text, icon, 275 parent, title, text, icon,
268 QMessageBox.StandardButtons(QMessageBox.Retry | QMessageBox.Abort), 276 QMessageBox.StandardButtons(QMessageBox.StandardButton.Retry |
269 QMessageBox.Retry, 277 QMessageBox.StandardButton.Abort),
278 QMessageBox.StandardButton.Retry,
270 textFormat) 279 textFormat)
271 return res == QMessageBox.Retry 280 return res == QMessageBox.StandardButton.Retry
272 281
273 282
274 def okToClearData(parent, title, text, saveFunc, textFormat=Qt.AutoText): 283 def okToClearData(parent, title, text, saveFunc,
284 textFormat=Qt.TextFormat.AutoText):
275 """ 285 """
276 Function to show a model message box to ask for clearing the data. 286 Function to show a model message box to ask for clearing the data.
277 287
278 @param parent parent widget of the message box (QWidget) 288 @param parent parent widget of the message box (QWidget)
279 @param title caption of the message box (string) 289 @param title caption of the message box (string)
282 must be a parameterless function returning a flag indicating success. 292 must be a parameterless function returning a flag indicating success.
283 @param textFormat format of the text (Qt.TextFormat) 293 @param textFormat format of the text (Qt.TextFormat)
284 @return flag indicating that it is ok to clear the data (boolean) 294 @return flag indicating that it is ok to clear the data (boolean)
285 """ 295 """
286 res = __messageBox( 296 res = __messageBox(
287 parent, title, text, QMessageBox.Warning, 297 parent, title, text, QMessageBox.Icon.Warning,
288 QMessageBox.StandardButtons( 298 QMessageBox.StandardButtons(
289 QMessageBox.Abort | QMessageBox.Discard | QMessageBox.Save), 299 QMessageBox.StandardButton.Abort |
290 QMessageBox.Save, 300 QMessageBox.StandardButton.Discard |
301 QMessageBox.StandardButton.Save),
302 QMessageBox.StandardButton.Save,
291 textFormat) 303 textFormat)
292 if res == QMessageBox.Abort: 304 if res == QMessageBox.StandardButton.Abort:
293 return False 305 return False
294 if res == QMessageBox.Save: 306 if res == QMessageBox.StandardButton.Save:
295 return saveFunc() 307 return saveFunc()
296 return True 308 return True

eric ide

mercurial