|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the eric message box wizard dialog. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt6.QtCore import pyqtSlot |
|
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton |
|
14 |
|
15 from EricWidgets import EricMessageBox |
|
16 |
|
17 from .Ui_EricMessageBoxWizardDialog import Ui_EricMessageBoxWizardDialog |
|
18 |
|
19 |
|
20 class EricMessageBoxWizardDialog(QDialog, Ui_EricMessageBoxWizardDialog): |
|
21 """ |
|
22 Class implementing the eric message box wizard dialog. |
|
23 |
|
24 It displays a dialog for entering the parameters |
|
25 for the EricMessageBox code generator. |
|
26 """ |
|
27 def __init__(self, parent=None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param parent reference to the parent widget |
|
32 @type QWidget |
|
33 """ |
|
34 super().__init__(parent) |
|
35 self.setupUi(self) |
|
36 |
|
37 # keep the following three lists in sync |
|
38 self.buttonsList = [ |
|
39 self.tr("No button"), |
|
40 self.tr("Abort"), |
|
41 self.tr("Apply"), |
|
42 self.tr("Cancel"), |
|
43 self.tr("Close"), |
|
44 self.tr("Discard"), |
|
45 self.tr("Help"), |
|
46 self.tr("Ignore"), |
|
47 self.tr("No"), |
|
48 self.tr("No to all"), |
|
49 self.tr("Ok"), |
|
50 self.tr("Open"), |
|
51 self.tr("Reset"), |
|
52 self.tr("Restore defaults"), |
|
53 self.tr("Retry"), |
|
54 self.tr("Save"), |
|
55 self.tr("Save all"), |
|
56 self.tr("Yes"), |
|
57 self.tr("Yes to all"), |
|
58 ] |
|
59 self.buttonsCodeListBinary = [ |
|
60 EricMessageBox.NoButton, |
|
61 EricMessageBox.Abort, |
|
62 EricMessageBox.Apply, |
|
63 EricMessageBox.Cancel, |
|
64 EricMessageBox.Close, |
|
65 EricMessageBox.Discard, |
|
66 EricMessageBox.Help, |
|
67 EricMessageBox.Ignore, |
|
68 EricMessageBox.No, |
|
69 EricMessageBox.NoToAll, |
|
70 EricMessageBox.Ok, |
|
71 EricMessageBox.Open, |
|
72 EricMessageBox.Reset, |
|
73 EricMessageBox.RestoreDefaults, |
|
74 EricMessageBox.Retry, |
|
75 EricMessageBox.Save, |
|
76 EricMessageBox.SaveAll, |
|
77 EricMessageBox.Yes, |
|
78 EricMessageBox.YesToAll, |
|
79 ] |
|
80 self.buttonsCodeListText = [ |
|
81 "EricMessageBox.NoButton", |
|
82 "EricMessageBox.Abort", |
|
83 "EricMessageBox.Apply", |
|
84 "EricMessageBox.Cancel", |
|
85 "EricMessageBox.Close", |
|
86 "EricMessageBox.Discard", |
|
87 "EricMessageBox.Help", |
|
88 "EricMessageBox.Ignore", |
|
89 "EricMessageBox.No", |
|
90 "EricMessageBox.NoToAll", |
|
91 "EricMessageBox.Ok", |
|
92 "EricMessageBox.Open", |
|
93 "EricMessageBox.Reset", |
|
94 "EricMessageBox.RestoreDefaults", |
|
95 "EricMessageBox.Retry", |
|
96 "EricMessageBox.Save", |
|
97 "EricMessageBox.SaveAll", |
|
98 "EricMessageBox.Yes", |
|
99 "EricMessageBox.YesToAll", |
|
100 ] |
|
101 |
|
102 self.defaultCombo.addItems(self.buttonsList) |
|
103 |
|
104 self.bTest = self.buttonBox.addButton( |
|
105 self.tr("Test"), QDialogButtonBox.ButtonRole.ActionRole) |
|
106 |
|
107 self.__enabledGroups() |
|
108 |
|
109 def __enabledGroups(self): |
|
110 """ |
|
111 Private method to enable/disable some group boxes. |
|
112 """ |
|
113 self.standardButtons.setEnabled( |
|
114 self.rInformation.isChecked() or |
|
115 self.rQuestion.isChecked() or |
|
116 self.rWarning.isChecked() or |
|
117 self.rCritical.isChecked() or |
|
118 self.rStandard.isChecked() |
|
119 ) |
|
120 |
|
121 self.defaultButton.setEnabled( |
|
122 self.rInformation.isChecked() or |
|
123 self.rQuestion.isChecked() or |
|
124 self.rWarning.isChecked() or |
|
125 self.rCritical.isChecked() |
|
126 ) |
|
127 |
|
128 self.iconBox.setEnabled( |
|
129 self.rYesNo.isChecked() or |
|
130 self.rRetryAbort.isChecked() or |
|
131 self.rStandard.isChecked() |
|
132 ) |
|
133 |
|
134 self.bTest.setEnabled(not self.rStandard.isChecked()) |
|
135 |
|
136 self.eMessage.setEnabled(not self.rAboutQt.isChecked()) |
|
137 |
|
138 @pyqtSlot(bool) |
|
139 def on_rInformation_toggled(self, on): |
|
140 """ |
|
141 Private slot to handle the toggled signal of the rInformation |
|
142 radio button. |
|
143 |
|
144 @param on toggle state (ignored) |
|
145 @type bool |
|
146 """ |
|
147 self.__enabledGroups() |
|
148 |
|
149 @pyqtSlot(bool) |
|
150 def on_rQuestion_toggled(self, on): |
|
151 """ |
|
152 Private slot to handle the toggled signal of the rQuestion |
|
153 radio button. |
|
154 |
|
155 @param on toggle state (ignored) |
|
156 @type bool |
|
157 """ |
|
158 self.__enabledGroups() |
|
159 |
|
160 @pyqtSlot(bool) |
|
161 def on_rWarning_toggled(self, on): |
|
162 """ |
|
163 Private slot to handle the toggled signal of the rWarning |
|
164 radio button. |
|
165 |
|
166 @param on toggle state (ignored) |
|
167 @type bool |
|
168 """ |
|
169 self.__enabledGroups() |
|
170 |
|
171 @pyqtSlot(bool) |
|
172 def on_rCritical_toggled(self, on): |
|
173 """ |
|
174 Private slot to handle the toggled signal of the rCritical |
|
175 radio button. |
|
176 |
|
177 @param on toggle state (ignored) |
|
178 @type bool |
|
179 """ |
|
180 self.__enabledGroups() |
|
181 |
|
182 @pyqtSlot(bool) |
|
183 def on_rYesNo_toggled(self, on): |
|
184 """ |
|
185 Private slot to handle the toggled signal of the rYesNo |
|
186 radio button. |
|
187 |
|
188 @param on toggle state (ignored) |
|
189 @type bool |
|
190 """ |
|
191 self.__enabledGroups() |
|
192 |
|
193 @pyqtSlot(bool) |
|
194 def on_rRetryAbort_toggled(self, on): |
|
195 """ |
|
196 Private slot to handle the toggled signal of the rRetryAbort |
|
197 radio button. |
|
198 |
|
199 @param on toggle state (ignored) |
|
200 @type bool |
|
201 """ |
|
202 self.__enabledGroups() |
|
203 |
|
204 @pyqtSlot(bool) |
|
205 def on_rOkToClearData_toggled(self, on): |
|
206 """ |
|
207 Private slot to handle the toggled signal of the rOkToClearData |
|
208 radio button. |
|
209 |
|
210 @param on toggle state (ignored) |
|
211 @type bool |
|
212 """ |
|
213 self.__enabledGroups() |
|
214 |
|
215 @pyqtSlot(bool) |
|
216 def on_rAbout_toggled(self, on): |
|
217 """ |
|
218 Private slot to handle the toggled signal of the rAbout |
|
219 radio button. |
|
220 |
|
221 @param on toggle state (ignored) |
|
222 @type bool |
|
223 """ |
|
224 self.__enabledGroups() |
|
225 |
|
226 @pyqtSlot(bool) |
|
227 def on_rAboutQt_toggled(self, on): |
|
228 """ |
|
229 Private slot to handle the toggled signal of the rAboutQt |
|
230 radio button. |
|
231 |
|
232 @param on toggle state (ignored) |
|
233 @type bool |
|
234 """ |
|
235 self.__enabledGroups() |
|
236 |
|
237 @pyqtSlot(bool) |
|
238 def on_rStandard_toggled(self, on): |
|
239 """ |
|
240 Private slot to handle the toggled signal of the rStandard |
|
241 radio button. |
|
242 |
|
243 @param on toggle state (ignored) |
|
244 @type bool |
|
245 """ |
|
246 self.__enabledGroups() |
|
247 |
|
248 @pyqtSlot(QAbstractButton) |
|
249 def on_buttonBox_clicked(self, button): |
|
250 """ |
|
251 Private slot called by a button of the button box clicked. |
|
252 |
|
253 @param button button that was clicked |
|
254 @type QAbstractButton |
|
255 """ |
|
256 if button == self.bTest: |
|
257 self.on_bTest_clicked() |
|
258 |
|
259 @pyqtSlot() |
|
260 def on_bTest_clicked(self): |
|
261 """ |
|
262 Private method to test the selected options. |
|
263 """ |
|
264 if self.rAbout.isChecked(): |
|
265 EricMessageBox.about( |
|
266 None, |
|
267 self.eCaption.text(), |
|
268 self.eMessage.toPlainText() |
|
269 ) |
|
270 elif self.rAboutQt.isChecked(): |
|
271 EricMessageBox.aboutQt( |
|
272 None, self.eCaption.text() |
|
273 ) |
|
274 elif ( |
|
275 self.rInformation.isChecked() or |
|
276 self.rQuestion.isChecked() or |
|
277 self.rWarning.isChecked() or |
|
278 self.rCritical.isChecked() |
|
279 ): |
|
280 buttons = EricMessageBox.NoButton |
|
281 if self.abortCheck.isChecked(): |
|
282 buttons |= EricMessageBox.Abort |
|
283 if self.applyCheck.isChecked(): |
|
284 buttons |= EricMessageBox.Apply |
|
285 if self.cancelCheck.isChecked(): |
|
286 buttons |= EricMessageBox.Cancel |
|
287 if self.closeCheck.isChecked(): |
|
288 buttons |= EricMessageBox.Close |
|
289 if self.discardCheck.isChecked(): |
|
290 buttons |= EricMessageBox.Discard |
|
291 if self.helpCheck.isChecked(): |
|
292 buttons |= EricMessageBox.Help |
|
293 if self.ignoreCheck.isChecked(): |
|
294 buttons |= EricMessageBox.Ignore |
|
295 if self.noCheck.isChecked(): |
|
296 buttons |= EricMessageBox.No |
|
297 if self.notoallCheck.isChecked(): |
|
298 buttons |= EricMessageBox.NoToAll |
|
299 if self.okCheck.isChecked(): |
|
300 buttons |= EricMessageBox.Ok |
|
301 if self.openCheck.isChecked(): |
|
302 buttons |= EricMessageBox.Open |
|
303 if self.resetCheck.isChecked(): |
|
304 buttons |= EricMessageBox.Reset |
|
305 if self.restoreCheck.isChecked(): |
|
306 buttons |= EricMessageBox.RestoreDefaults |
|
307 if self.retryCheck.isChecked(): |
|
308 buttons |= EricMessageBox.Retry |
|
309 if self.saveCheck.isChecked(): |
|
310 buttons |= EricMessageBox.Save |
|
311 if self.saveallCheck.isChecked(): |
|
312 buttons |= EricMessageBox.SaveAll |
|
313 if self.yesCheck.isChecked(): |
|
314 buttons |= EricMessageBox.Yes |
|
315 if self.yestoallCheck.isChecked(): |
|
316 buttons |= EricMessageBox.YesToAll |
|
317 if buttons == EricMessageBox.NoButton: |
|
318 buttons = EricMessageBox.Ok |
|
319 |
|
320 defaultButton = self.buttonsCodeListBinary[ |
|
321 self.defaultCombo.currentIndex()] |
|
322 |
|
323 if self.rInformation.isChecked(): |
|
324 EricMessageBox.information( |
|
325 self, |
|
326 self.eCaption.text(), |
|
327 self.eMessage.toPlainText(), |
|
328 buttons, |
|
329 defaultButton |
|
330 ) |
|
331 elif self.rQuestion.isChecked(): |
|
332 EricMessageBox.question( |
|
333 self, |
|
334 self.eCaption.text(), |
|
335 self.eMessage.toPlainText(), |
|
336 buttons, |
|
337 defaultButton |
|
338 ) |
|
339 elif self.rWarning.isChecked(): |
|
340 EricMessageBox.warning( |
|
341 self, |
|
342 self.eCaption.text(), |
|
343 self.eMessage.toPlainText(), |
|
344 buttons, |
|
345 defaultButton |
|
346 ) |
|
347 elif self.rCritical.isChecked(): |
|
348 EricMessageBox.critical( |
|
349 self, |
|
350 self.eCaption.text(), |
|
351 self.eMessage.toPlainText(), |
|
352 buttons, |
|
353 defaultButton |
|
354 ) |
|
355 elif ( |
|
356 self.rYesNo.isChecked() or |
|
357 self.rRetryAbort.isChecked() |
|
358 ): |
|
359 if self.iconInformation.isChecked(): |
|
360 icon = EricMessageBox.Information |
|
361 elif self.iconQuestion.isChecked(): |
|
362 icon = EricMessageBox.Question |
|
363 elif self.iconWarning.isChecked(): |
|
364 icon = EricMessageBox.Warning |
|
365 elif self.iconCritical.isChecked(): |
|
366 icon = EricMessageBox.Critical |
|
367 |
|
368 if self.rYesNo.isChecked(): |
|
369 EricMessageBox.yesNo( |
|
370 self, |
|
371 self.eCaption.text(), |
|
372 self.eMessage.toPlainText(), |
|
373 icon=icon, |
|
374 yesDefault=self.yesDefaultCheck.isChecked() |
|
375 ) |
|
376 elif self.rRetryAbort.isChecked(): |
|
377 EricMessageBox.retryAbort( |
|
378 self, |
|
379 self.eCaption.text(), |
|
380 self.eMessage.toPlainText(), |
|
381 icon=icon |
|
382 ) |
|
383 elif self.rOkToClearData.isChecked(): |
|
384 EricMessageBox.okToClearData( |
|
385 self, |
|
386 self.eCaption.text(), |
|
387 self.eMessage.toPlainText(), |
|
388 lambda: True |
|
389 ) |
|
390 |
|
391 def __getStandardButtonCode(self, istring, withIntro=True): |
|
392 """ |
|
393 Private method to generate the button code for the standard buttons. |
|
394 |
|
395 @param istring indentation string |
|
396 @type str |
|
397 @param withIntro flag indicating to generate a first line |
|
398 with introductory text |
|
399 @type bool |
|
400 @return the button code |
|
401 @rtype str |
|
402 """ |
|
403 buttons = [] |
|
404 if self.abortCheck.isChecked(): |
|
405 buttons.append("EricMessageBox.Abort") |
|
406 if self.applyCheck.isChecked(): |
|
407 buttons.append("EricMessageBox.Apply") |
|
408 if self.cancelCheck.isChecked(): |
|
409 buttons.append("EricMessageBox.Cancel") |
|
410 if self.closeCheck.isChecked(): |
|
411 buttons.append("EricMessageBox.Close") |
|
412 if self.discardCheck.isChecked(): |
|
413 buttons.append("EricMessageBox.Discard") |
|
414 if self.helpCheck.isChecked(): |
|
415 buttons.append("EricMessageBox.Help") |
|
416 if self.ignoreCheck.isChecked(): |
|
417 buttons.append("EricMessageBox.Ignore") |
|
418 if self.noCheck.isChecked(): |
|
419 buttons.append("EricMessageBox.No") |
|
420 if self.notoallCheck.isChecked(): |
|
421 buttons.append("EricMessageBox.NoToAll") |
|
422 if self.okCheck.isChecked(): |
|
423 buttons.append("EricMessageBox.Ok") |
|
424 if self.openCheck.isChecked(): |
|
425 buttons.append("EricMessageBox.Open") |
|
426 if self.resetCheck.isChecked(): |
|
427 buttons.append("EricMessageBox.Reset") |
|
428 if self.restoreCheck.isChecked(): |
|
429 buttons.append("EricMessageBox.RestoreDefaults") |
|
430 if self.retryCheck.isChecked(): |
|
431 buttons.append("EricMessageBox.Retry") |
|
432 if self.saveCheck.isChecked(): |
|
433 buttons.append("EricMessageBox.Save") |
|
434 if self.saveallCheck.isChecked(): |
|
435 buttons.append("EricMessageBox.SaveAll") |
|
436 if self.yesCheck.isChecked(): |
|
437 buttons.append("EricMessageBox.Yes") |
|
438 if self.yestoallCheck.isChecked(): |
|
439 buttons.append("EricMessageBox.YesToAll") |
|
440 if len(buttons) == 0: |
|
441 return "" |
|
442 |
|
443 joinstring = ' |{0}{1}'.format(os.linesep, istring) |
|
444 intro = ',' if withIntro else '' |
|
445 btnCode = '{0}{1}{2}{3}'.format( |
|
446 intro, os.linesep, istring, joinstring.join(buttons)) |
|
447 |
|
448 return btnCode |
|
449 |
|
450 def __getDefaultButtonCode(self, istring): |
|
451 """ |
|
452 Private method to generate the button code for the default button. |
|
453 |
|
454 @param istring indentation string |
|
455 @type str |
|
456 @return the button code |
|
457 @rtype str |
|
458 """ |
|
459 btnCode = "" |
|
460 defaultIndex = self.defaultCombo.currentIndex() |
|
461 if defaultIndex: |
|
462 btnCode = ',{0}{1}{2}'.format( |
|
463 os.linesep, istring, |
|
464 self.buttonsCodeListText[defaultIndex]) |
|
465 return btnCode |
|
466 |
|
467 def getCode(self, indLevel, indString): |
|
468 """ |
|
469 Public method to get the source code. |
|
470 |
|
471 @param indLevel indentation level |
|
472 @type int |
|
473 @param indString string used for indentation (space or tab) |
|
474 @type str |
|
475 @return generated code |
|
476 @rtype str |
|
477 """ |
|
478 # calculate our indentation level and the indentation string |
|
479 il = indLevel + 1 |
|
480 istring = il * indString |
|
481 estring = os.linesep + indLevel * indString |
|
482 |
|
483 # now generate the code |
|
484 if self.parentSelf.isChecked(): |
|
485 parent = "self" |
|
486 elif self.parentNone.isChecked(): |
|
487 parent = "None" |
|
488 elif self.parentOther.isChecked(): |
|
489 parent = self.parentEdit.text() |
|
490 if parent == "": |
|
491 parent = "None" |
|
492 |
|
493 if self.iconInformation.isChecked(): |
|
494 icon = "EricMessageBox.Information" |
|
495 elif self.iconQuestion.isChecked(): |
|
496 icon = "EricMessageBox.Question" |
|
497 elif self.iconWarning.isChecked(): |
|
498 icon = "EricMessageBox.Warning" |
|
499 elif self.iconCritical.isChecked(): |
|
500 icon = "EricMessageBox.Critical" |
|
501 |
|
502 if not self.rStandard.isChecked(): |
|
503 resvar = self.eResultVar.text() |
|
504 if not resvar: |
|
505 resvar = "res" |
|
506 |
|
507 if self.rAbout.isChecked(): |
|
508 msgdlg = "EricMessageBox.about({0}".format(os.linesep) |
|
509 elif self.rAboutQt.isChecked(): |
|
510 msgdlg = "EricMessageBox.aboutQt({0}".format(os.linesep) |
|
511 elif self.rInformation.isChecked(): |
|
512 msgdlg = "{0} = EricMessageBox.information({1}".format( |
|
513 resvar, os.linesep) |
|
514 elif self.rQuestion.isChecked(): |
|
515 msgdlg = "{0} = EricMessageBox.question({1}".format( |
|
516 resvar, os.linesep) |
|
517 elif self.rWarning.isChecked(): |
|
518 msgdlg = "{0} = EricMessageBox.warning({1}".format( |
|
519 resvar, os.linesep) |
|
520 elif self.rCritical.isChecked(): |
|
521 msgdlg = "{0} = EricMessageBox.critical({1}".format( |
|
522 resvar, os.linesep) |
|
523 elif self.rYesNo.isChecked(): |
|
524 msgdlg = "{0} = EricMessageBox.yesNo({1}".format( |
|
525 resvar, os.linesep) |
|
526 elif self.rRetryAbort.isChecked(): |
|
527 msgdlg = "{0} = EricMessageBox.retryAbort({1}".format( |
|
528 resvar, os.linesep) |
|
529 elif self.rOkToClearData.isChecked(): |
|
530 msgdlg = "{0} = EricMessageBox.okToClearData({1}".format( |
|
531 resvar, os.linesep) |
|
532 |
|
533 msgdlg += '{0}{1},{2}'.format(istring, parent, os.linesep) |
|
534 msgdlg += '{0}self.tr("{1}")'.format( |
|
535 istring, self.eCaption.text()) |
|
536 |
|
537 if not self.rAboutQt.isChecked(): |
|
538 msgdlg += ',{0}{1}self.tr("""{2}""")'.format( |
|
539 os.linesep, istring, self.eMessage.toPlainText()) |
|
540 |
|
541 if ( |
|
542 self.rInformation.isChecked() or |
|
543 self.rQuestion.isChecked() or |
|
544 self.rWarning.isChecked() or |
|
545 self.rCritical.isChecked() |
|
546 ): |
|
547 msgdlg += self.__getStandardButtonCode(istring) |
|
548 msgdlg += self.__getDefaultButtonCode(istring) |
|
549 elif self.rYesNo.isChecked(): |
|
550 if not self.iconQuestion.isChecked(): |
|
551 msgdlg += ',{0}{1}icon={2}'.format( |
|
552 os.linesep, istring, icon) |
|
553 if self.yesDefaultCheck.isChecked(): |
|
554 msgdlg += ',{0}{1}yesDefault=True'.format( |
|
555 os.linesep, istring) |
|
556 elif self.rRetryAbort.isChecked(): |
|
557 if not self.iconQuestion.isChecked(): |
|
558 msgdlg += ',{0}{1}icon={2}'.format( |
|
559 os.linesep, istring, icon) |
|
560 elif self.rOkToClearData.isChecked(): |
|
561 saveFunc = self.saveFuncEdit.text() |
|
562 if saveFunc == "": |
|
563 saveFunc = "lambda: True" |
|
564 msgdlg += ',{0}{1}{2}'.format(os.linesep, istring, saveFunc) |
|
565 else: |
|
566 resvar = self.eResultVar.text() |
|
567 if not resvar: |
|
568 resvar = "dlg" |
|
569 |
|
570 msgdlg = "{0} = EricMessageBox.EricMessageBox({1}".format( |
|
571 resvar, os.linesep) |
|
572 msgdlg += '{0}{1},{2}'.format(istring, icon, os.linesep) |
|
573 msgdlg += '{0}self.tr("{1}")'.format( |
|
574 istring, self.eCaption.text()) |
|
575 msgdlg += ',{0}{1}self.tr("""{2}""")'.format( |
|
576 os.linesep, istring, self.eMessage.toPlainText()) |
|
577 if self.modalCheck.isChecked(): |
|
578 msgdlg += ',{0}{1}modal=True'.format(os.linesep, istring) |
|
579 btnCode = self.__getStandardButtonCode( |
|
580 istring, withIntro=False) |
|
581 if btnCode: |
|
582 msgdlg += ',{0}{1}buttons={2}'.format( |
|
583 os.linesep, istring, btnCode) |
|
584 if not self.parentNone.isChecked(): |
|
585 msgdlg += ',{0}{1}parent={2}'.format( |
|
586 os.linesep, istring, parent) |
|
587 |
|
588 msgdlg += '){0}'.format(estring) |
|
589 return msgdlg |