src/eric7/EricWidgets/EricMessageBox.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
17 # QMessageBox.Icon 17 # QMessageBox.Icon
18 NoIcon = QMessageBox.Icon.NoIcon 18 NoIcon = QMessageBox.Icon.NoIcon
19 Critical = QMessageBox.Icon.Critical 19 Critical = QMessageBox.Icon.Critical
20 Information = QMessageBox.Icon.Information 20 Information = QMessageBox.Icon.Information
21 Question = QMessageBox.Icon.Question 21 Question = QMessageBox.Icon.Question
22 Warning = QMessageBox.Icon.Warning # __IGNORE_WARNING_M131__ 22 Warning = QMessageBox.Icon.Warning # __IGNORE_WARNING_M131__
23 23
24 # QMessageBox.StandardButton 24 # QMessageBox.StandardButton
25 Abort = QMessageBox.StandardButton.Abort 25 Abort = QMessageBox.StandardButton.Abort
26 Apply = QMessageBox.StandardButton.Apply 26 Apply = QMessageBox.StandardButton.Apply
27 Cancel = QMessageBox.StandardButton.Cancel 27 Cancel = QMessageBox.StandardButton.Cancel
61 61
62 class EricMessageBox(QMessageBox): 62 class EricMessageBox(QMessageBox):
63 """ 63 """
64 Class implementing a replacement for QMessageBox. 64 Class implementing a replacement for QMessageBox.
65 """ 65 """
66 def __init__(self, icon, title, text, modal=False, 66
67 buttons=QMessageBox.StandardButton.NoButton, 67 def __init__(
68 parent=None): 68 self,
69 icon,
70 title,
71 text,
72 modal=False,
73 buttons=QMessageBox.StandardButton.NoButton,
74 parent=None,
75 ):
69 """ 76 """
70 Constructor 77 Constructor
71 78
72 @param icon type of icon to be shown (QMessageBox.Icon) 79 @param icon type of icon to be shown (QMessageBox.Icon)
73 @param title caption of the message box (string) 80 @param title caption of the message box (string)
74 @param text text to be shown by the message box (string) 81 @param text text to be shown by the message box (string)
75 @param modal flag indicating a modal dialog (boolean) 82 @param modal flag indicating a modal dialog (boolean)
76 @param buttons set of standard buttons to generate (StandardButtons) 83 @param buttons set of standard buttons to generate (StandardButtons)
84 else: 91 else:
85 self.setWindowModality(Qt.WindowModality.ApplicationModal) 92 self.setWindowModality(Qt.WindowModality.ApplicationModal)
86 else: 93 else:
87 self.setWindowModality(Qt.WindowModality.NonModal) 94 self.setWindowModality(Qt.WindowModality.NonModal)
88 if title == "": 95 if title == "":
89 self.setWindowTitle("{0}".format( 96 self.setWindowTitle("{0}".format(QApplication.applicationName()))
90 QApplication.applicationName()))
91 else: 97 else:
92 self.setWindowTitle("{0} - {1}".format( 98 self.setWindowTitle(
93 QApplication.applicationName(), title)) 99 "{0} - {1}".format(QApplication.applicationName(), title)
100 )
94 self.setText(text) 101 self.setText(text)
95 self.setStandardButtons(buttons) 102 self.setStandardButtons(buttons)
96 103
104
97 ############################################################################### 105 ###############################################################################
98 ## Replacements for QMessageBox static methods ## 106 ## Replacements for QMessageBox static methods ##
99 ############################################################################### 107 ###############################################################################
100 108
101 109
102 def __messageBox(parent, title, text, icon, 110 def __messageBox(
103 buttons=QMessageBox.StandardButton.Ok, 111 parent,
104 defaultButton=QMessageBox.StandardButton.NoButton, 112 title,
105 textFormat=Qt.TextFormat.AutoText): 113 text,
114 icon,
115 buttons=QMessageBox.StandardButton.Ok,
116 defaultButton=QMessageBox.StandardButton.NoButton,
117 textFormat=Qt.TextFormat.AutoText,
118 ):
106 """ 119 """
107 Private module function to show a modal message box. 120 Private module function to show a modal message box.
108 121
109 @param parent parent widget of the message box (QWidget) 122 @param parent parent widget of the message box (QWidget)
110 @param title caption of the message box (string) 123 @param title caption of the message box (string)
111 @param text text to be shown by the message box (string) 124 @param text text to be shown by the message box (string)
112 @param icon type of icon to be shown (QMessageBox.Icon) 125 @param icon type of icon to be shown (QMessageBox.Icon)
113 @param buttons flags indicating which buttons to show 126 @param buttons flags indicating which buttons to show
120 messageBox = QMessageBox(parent) 133 messageBox = QMessageBox(parent)
121 messageBox.setIcon(icon) 134 messageBox.setIcon(icon)
122 if parent is not None: 135 if parent is not None:
123 messageBox.setWindowModality(Qt.WindowModality.WindowModal) 136 messageBox.setWindowModality(Qt.WindowModality.WindowModal)
124 if title == "": 137 if title == "":
125 messageBox.setWindowTitle("{0}".format( 138 messageBox.setWindowTitle("{0}".format(QApplication.applicationName()))
126 QApplication.applicationName()))
127 else: 139 else:
128 messageBox.setWindowTitle("{0} - {1}".format( 140 messageBox.setWindowTitle(
129 QApplication.applicationName(), title)) 141 "{0} - {1}".format(QApplication.applicationName(), title)
142 )
130 messageBox.setTextFormat(textFormat) 143 messageBox.setTextFormat(textFormat)
131 messageBox.setText(text) 144 messageBox.setText(text)
132 messageBox.setStandardButtons(buttons) 145 messageBox.setStandardButtons(buttons)
133 messageBox.setDefaultButton(defaultButton) 146 messageBox.setDefaultButton(defaultButton)
134 messageBox.exec() 147 messageBox.exec()
136 if clickedButton is None: 149 if clickedButton is None:
137 return QMessageBox.StandardButton.NoButton 150 return QMessageBox.StandardButton.NoButton
138 else: 151 else:
139 return messageBox.standardButton(clickedButton) 152 return messageBox.standardButton(clickedButton)
140 153
154
141 # the about functions are here for consistancy 155 # the about functions are here for consistancy
142 about = QMessageBox.about 156 about = QMessageBox.about
143 aboutQt = QMessageBox.aboutQt 157 aboutQt = QMessageBox.aboutQt
144 158
145 159
146 def critical(parent, title, text, 160 def critical(
147 buttons=QMessageBox.StandardButton.Ok, 161 parent,
148 defaultButton=QMessageBox.StandardButton.NoButton): 162 title,
163 text,
164 buttons=QMessageBox.StandardButton.Ok,
165 defaultButton=QMessageBox.StandardButton.NoButton,
166 ):
149 """ 167 """
150 Function to show a modal critical message box. 168 Function to show a modal critical message box.
151 169
152 @param parent parent widget of the message box (QWidget) 170 @param parent parent widget of the message box (QWidget)
153 @param title caption of the message box (string) 171 @param title caption of the message box (string)
154 @param text text to be shown by the message box (string) 172 @param text text to be shown by the message box (string)
155 @param buttons flags indicating which buttons to show 173 @param buttons flags indicating which buttons to show
156 (QMessageBox.StandardButtons) 174 (QMessageBox.StandardButtons)
157 @param defaultButton flag indicating the default button 175 @param defaultButton flag indicating the default button
158 (QMessageBox.StandardButton) 176 (QMessageBox.StandardButton)
159 @return button pressed by the user (QMessageBox.StandardButton) 177 @return button pressed by the user (QMessageBox.StandardButton)
160 """ 178 """
161 return __messageBox(parent, title, text, QMessageBox.Icon.Critical, 179 return __messageBox(
162 buttons, defaultButton) 180 parent, title, text, QMessageBox.Icon.Critical, buttons, defaultButton
163 181 )
164 182
165 def information(parent, title, text, 183
166 buttons=QMessageBox.StandardButton.Ok, 184 def information(
167 defaultButton=QMessageBox.StandardButton.NoButton): 185 parent,
186 title,
187 text,
188 buttons=QMessageBox.StandardButton.Ok,
189 defaultButton=QMessageBox.StandardButton.NoButton,
190 ):
168 """ 191 """
169 Function to show a modal information message box. 192 Function to show a modal information message box.
170 193
171 @param parent parent widget of the message box (QWidget) 194 @param parent parent widget of the message box (QWidget)
172 @param title caption of the message box (string) 195 @param title caption of the message box (string)
173 @param text text to be shown by the message box (string) 196 @param text text to be shown by the message box (string)
174 @param buttons flags indicating which buttons to show 197 @param buttons flags indicating which buttons to show
175 (QMessageBox.StandardButtons) 198 (QMessageBox.StandardButtons)
176 @param defaultButton flag indicating the default button 199 @param defaultButton flag indicating the default button
177 (QMessageBox.StandardButton) 200 (QMessageBox.StandardButton)
178 @return button pressed by the user (QMessageBox.StandardButton) 201 @return button pressed by the user (QMessageBox.StandardButton)
179 """ 202 """
180 return __messageBox(parent, title, text, QMessageBox.Icon.Information, 203 return __messageBox(
181 buttons, defaultButton) 204 parent, title, text, QMessageBox.Icon.Information, buttons, defaultButton
182 205 )
183 206
184 def question(parent, title, text, 207
185 buttons=QMessageBox.StandardButton.Ok, 208 def question(
186 defaultButton=QMessageBox.StandardButton.NoButton): 209 parent,
210 title,
211 text,
212 buttons=QMessageBox.StandardButton.Ok,
213 defaultButton=QMessageBox.StandardButton.NoButton,
214 ):
187 """ 215 """
188 Function to show a modal question message box. 216 Function to show a modal question message box.
189 217
190 @param parent parent widget of the message box (QWidget) 218 @param parent parent widget of the message box (QWidget)
191 @param title caption of the message box (string) 219 @param title caption of the message box (string)
192 @param text text to be shown by the message box (string) 220 @param text text to be shown by the message box (string)
193 @param buttons flags indicating which buttons to show 221 @param buttons flags indicating which buttons to show
194 (QMessageBox.StandardButtons) 222 (QMessageBox.StandardButtons)
195 @param defaultButton flag indicating the default button 223 @param defaultButton flag indicating the default button
196 (QMessageBox.StandardButton) 224 (QMessageBox.StandardButton)
197 @return button pressed by the user (QMessageBox.StandardButton) 225 @return button pressed by the user (QMessageBox.StandardButton)
198 """ 226 """
199 return __messageBox(parent, title, text, QMessageBox.Icon.Question, 227 return __messageBox(
200 buttons, defaultButton) 228 parent, title, text, QMessageBox.Icon.Question, buttons, defaultButton
201 229 )
202 230
203 def warning(parent, title, text, 231
204 buttons=QMessageBox.StandardButton.Ok, 232 def warning(
205 defaultButton=QMessageBox.StandardButton.NoButton): 233 parent,
234 title,
235 text,
236 buttons=QMessageBox.StandardButton.Ok,
237 defaultButton=QMessageBox.StandardButton.NoButton,
238 ):
206 """ 239 """
207 Function to show a modal warning message box. 240 Function to show a modal warning message box.
208 241
209 @param parent parent widget of the message box (QWidget) 242 @param parent parent widget of the message box (QWidget)
210 @param title caption of the message box (string) 243 @param title caption of the message box (string)
211 @param text text to be shown by the message box (string) 244 @param text text to be shown by the message box (string)
212 @param buttons flags indicating which buttons to show 245 @param buttons flags indicating which buttons to show
213 (QMessageBox.StandardButtons) 246 (QMessageBox.StandardButtons)
214 @param defaultButton flag indicating the default button 247 @param defaultButton flag indicating the default button
215 (QMessageBox.StandardButton) 248 (QMessageBox.StandardButton)
216 @return button pressed by the user (QMessageBox.StandardButton) 249 @return button pressed by the user (QMessageBox.StandardButton)
217 """ 250 """
218 return __messageBox(parent, title, text, QMessageBox.Icon.Warning, 251 return __messageBox(
219 buttons, defaultButton) 252 parent, title, text, QMessageBox.Icon.Warning, buttons, defaultButton
253 )
254
220 255
221 ############################################################################### 256 ###############################################################################
222 ## Additional convenience functions ## 257 ## Additional convenience functions ##
223 ############################################################################### 258 ###############################################################################
224 259
225 260
226 def yesNo(parent, title, text, icon=Question, yesDefault=False, 261 def yesNo(
227 textFormat=Qt.TextFormat.AutoText): 262 parent,
263 title,
264 text,
265 icon=Question,
266 yesDefault=False,
267 textFormat=Qt.TextFormat.AutoText,
268 ):
228 """ 269 """
229 Function to show a model yes/no message box. 270 Function to show a model yes/no message box.
230 271
231 @param parent parent widget of the message box (QWidget) 272 @param parent parent widget of the message box (QWidget)
232 @param title caption of the message box (string) 273 @param title caption of the message box (string)
233 @param text text to be shown by the message box (string) 274 @param text text to be shown by the message box (string)
234 @param icon icon for the dialog (Critical, Information, Question or 275 @param icon icon for the dialog (Critical, Information, Question or
235 Warning) 276 Warning)
239 @return flag indicating the selection of the Yes button (boolean) 280 @return flag indicating the selection of the Yes button (boolean)
240 @exception ValueError raised to indicate a bad parameter value 281 @exception ValueError raised to indicate a bad parameter value
241 """ 282 """
242 if icon not in [Critical, Information, Question, Warning]: 283 if icon not in [Critical, Information, Question, Warning]:
243 raise ValueError("Bad value for 'icon' parameter.") 284 raise ValueError("Bad value for 'icon' parameter.")
244 285
245 res = __messageBox( 286 res = __messageBox(
246 parent, title, text, icon, 287 parent,
288 title,
289 text,
290 icon,
247 QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, 291 QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
248 yesDefault and QMessageBox.StandardButton.Yes or 292 yesDefault and QMessageBox.StandardButton.Yes or QMessageBox.StandardButton.No,
249 QMessageBox.StandardButton.No, 293 textFormat,
250 textFormat) 294 )
251 return res == QMessageBox.StandardButton.Yes 295 return res == QMessageBox.StandardButton.Yes
252 296
253 297
254 def retryAbort(parent, title, text, icon=Question, 298 def retryAbort(parent, title, text, icon=Question, textFormat=Qt.TextFormat.AutoText):
255 textFormat=Qt.TextFormat.AutoText):
256 """ 299 """
257 Function to show a model abort/retry message box. 300 Function to show a model abort/retry message box.
258 301
259 @param parent parent widget of the message box (QWidget) 302 @param parent parent widget of the message box (QWidget)
260 @param title caption of the message box (string) 303 @param title caption of the message box (string)
261 @param text text to be shown by the message box (string) 304 @param text text to be shown by the message box (string)
262 @param icon icon for the dialog (Critical, Information, Question or 305 @param icon icon for the dialog (Critical, Information, Question or
263 Warning) 306 Warning)
265 @return flag indicating the selection of the Retry button (boolean) 308 @return flag indicating the selection of the Retry button (boolean)
266 @exception ValueError raised to indicate a bad parameter value 309 @exception ValueError raised to indicate a bad parameter value
267 """ 310 """
268 if icon not in [Critical, Information, Question, Warning]: 311 if icon not in [Critical, Information, Question, Warning]:
269 raise ValueError("Bad value for 'icon' parameter.") 312 raise ValueError("Bad value for 'icon' parameter.")
270 313
271 res = __messageBox( 314 res = __messageBox(
272 parent, title, text, icon, 315 parent,
316 title,
317 text,
318 icon,
273 QMessageBox.StandardButton.Retry | QMessageBox.StandardButton.Abort, 319 QMessageBox.StandardButton.Retry | QMessageBox.StandardButton.Abort,
274 QMessageBox.StandardButton.Retry, 320 QMessageBox.StandardButton.Retry,
275 textFormat) 321 textFormat,
322 )
276 return res == QMessageBox.StandardButton.Retry 323 return res == QMessageBox.StandardButton.Retry
277 324
278 325
279 def okToClearData(parent, title, text, saveFunc, 326 def okToClearData(parent, title, text, saveFunc, textFormat=Qt.TextFormat.AutoText):
280 textFormat=Qt.TextFormat.AutoText):
281 """ 327 """
282 Function to show a model message box to ask for clearing the data. 328 Function to show a model message box to ask for clearing the data.
283 329
284 @param parent parent widget of the message box (QWidget) 330 @param parent parent widget of the message box (QWidget)
285 @param title caption of the message box (string) 331 @param title caption of the message box (string)
286 @param text text to be shown by the message box (string) 332 @param text text to be shown by the message box (string)
287 @param saveFunc reference to a function performing the save action. It 333 @param saveFunc reference to a function performing the save action. It
288 must be a parameterless function returning a flag indicating success. 334 must be a parameterless function returning a flag indicating success.
289 @param textFormat format of the text (Qt.TextFormat) 335 @param textFormat format of the text (Qt.TextFormat)
290 @return flag indicating that it is ok to clear the data (boolean) 336 @return flag indicating that it is ok to clear the data (boolean)
291 """ 337 """
292 res = __messageBox( 338 res = __messageBox(
293 parent, title, text, QMessageBox.Icon.Warning, 339 parent,
294 (QMessageBox.StandardButton.Abort | 340 title,
295 QMessageBox.StandardButton.Discard | 341 text,
296 QMessageBox.StandardButton.Save), 342 QMessageBox.Icon.Warning,
343 (
344 QMessageBox.StandardButton.Abort
345 | QMessageBox.StandardButton.Discard
346 | QMessageBox.StandardButton.Save
347 ),
297 QMessageBox.StandardButton.Save, 348 QMessageBox.StandardButton.Save,
298 textFormat) 349 textFormat,
350 )
299 if res == QMessageBox.StandardButton.Abort: 351 if res == QMessageBox.StandardButton.Abort:
300 return False 352 return False
301 if res == QMessageBox.StandardButton.Save: 353 if res == QMessageBox.StandardButton.Save:
302 return saveFunc() 354 return saveFunc()
303 return True 355 return True

eric ide

mercurial