1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2003 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the message box wizard dialog.
8"""
9
10import os
11
12from PyQt5.QtCore import pyqtSlot
13from PyQt5.QtWidgets import QMessageBox, QDialog, QDialogButtonBox
14
15from .Ui_MessageBoxWizardDialog import Ui_MessageBoxWizardDialog
16
17
18class MessageBoxWizardDialog(QDialog, Ui_MessageBoxWizardDialog):
19    """
20    Class implementing the message box wizard dialog.
21
22    It displays a dialog for entering the parameters
23    for the QMessageBox code generator.
24    """
25    def __init__(self, parent=None):
26        """
27        Constructor
28
29        @param parent parent widget (QWidget)
30        """
31        super().__init__(parent)
32        self.setupUi(self)
33
34        # keep the following three lists in sync
35        self.buttonsList = [
36            self.tr("No button"),
37            self.tr("Abort"),
38            self.tr("Apply"),
39            self.tr("Cancel"),
40            self.tr("Close"),
41            self.tr("Discard"),
42            self.tr("Help"),
43            self.tr("Ignore"),
44            self.tr("No"),
45            self.tr("No to all"),
46            self.tr("Ok"),
47            self.tr("Open"),
48            self.tr("Reset"),
49            self.tr("Restore defaults"),
50            self.tr("Retry"),
51            self.tr("Save"),
52            self.tr("Save all"),
53            self.tr("Yes"),
54            self.tr("Yes to all"),
55        ]
56        self.buttonsCodeListBinary = [
57            QMessageBox.StandardButton.NoButton,
58            QMessageBox.StandardButton.Abort,
59            QMessageBox.StandardButton.Apply,
60            QMessageBox.StandardButton.Cancel,
61            QMessageBox.StandardButton.Close,
62            QMessageBox.StandardButton.Discard,
63            QMessageBox.StandardButton.Help,
64            QMessageBox.StandardButton.Ignore,
65            QMessageBox.StandardButton.No,
66            QMessageBox.StandardButton.NoToAll,
67            QMessageBox.StandardButton.Ok,
68            QMessageBox.StandardButton.Open,
69            QMessageBox.StandardButton.Reset,
70            QMessageBox.StandardButton.RestoreDefaults,
71            QMessageBox.StandardButton.Retry,
72            QMessageBox.StandardButton.Save,
73            QMessageBox.StandardButton.SaveAll,
74            QMessageBox.StandardButton.Yes,
75            QMessageBox.StandardButton.YesToAll,
76        ]
77        self.buttonsCodeListText = [
78            "QMessageBox.StandardButton.NoButton",
79            "QMessageBox.StandardButton.Abort",
80            "QMessageBox.StandardButton.Apply",
81            "QMessageBox.StandardButton.Cancel",
82            "QMessageBox.StandardButton.Close",
83            "QMessageBox.StandardButton.Discard",
84            "QMessageBox.StandardButton.Help",
85            "QMessageBox.StandardButton.Ignore",
86            "QMessageBox.StandardButton.No",
87            "QMessageBox.StandardButton.NoToAll",
88            "QMessageBox.StandardButton.Ok",
89            "QMessageBox.StandardButton.Open",
90            "QMessageBox.StandardButton.Reset",
91            "QMessageBox.StandardButton.RestoreDefaults",
92            "QMessageBox.StandardButton.Retry",
93            "QMessageBox.StandardButton.Save",
94            "QMessageBox.StandardButton.SaveAll",
95            "QMessageBox.StandardButton.Yes",
96            "QMessageBox.StandardButton.YesToAll",
97        ]
98
99        self.defaultCombo.addItems(self.buttonsList)
100
101        self.bTest = self.buttonBox.addButton(
102            self.tr("Test"), QDialogButtonBox.ButtonRole.ActionRole)
103
104    def __testSelectedOptions(self):
105        """
106        Private method to test the selected options.
107        """
108        buttons = QMessageBox.StandardButton.NoButton
109        if self.abortCheck.isChecked():
110            buttons |= QMessageBox.StandardButton.Abort
111        if self.applyCheck.isChecked():
112            buttons |= QMessageBox.StandardButton.Apply
113        if self.cancelCheck.isChecked():
114            buttons |= QMessageBox.StandardButton.Cancel
115        if self.closeCheck.isChecked():
116            buttons |= QMessageBox.StandardButton.Close
117        if self.discardCheck.isChecked():
118            buttons |= QMessageBox.StandardButton.Discard
119        if self.helpCheck.isChecked():
120            buttons |= QMessageBox.StandardButton.Help
121        if self.ignoreCheck.isChecked():
122            buttons |= QMessageBox.StandardButton.Ignore
123        if self.noCheck.isChecked():
124            buttons |= QMessageBox.StandardButton.No
125        if self.notoallCheck.isChecked():
126            buttons |= QMessageBox.StandardButton.NoToAll
127        if self.okCheck.isChecked():
128            buttons |= QMessageBox.StandardButton.Ok
129        if self.openCheck.isChecked():
130            buttons |= QMessageBox.StandardButton.Open
131        if self.resetCheck.isChecked():
132            buttons |= QMessageBox.StandardButton.Reset
133        if self.restoreCheck.isChecked():
134            buttons |= QMessageBox.StandardButton.RestoreDefaults
135        if self.retryCheck.isChecked():
136            buttons |= QMessageBox.StandardButton.Retry
137        if self.saveCheck.isChecked():
138            buttons |= QMessageBox.StandardButton.Save
139        if self.saveallCheck.isChecked():
140            buttons |= QMessageBox.StandardButton.SaveAll
141        if self.yesCheck.isChecked():
142            buttons |= QMessageBox.StandardButton.Yes
143        if self.yestoallCheck.isChecked():
144            buttons |= QMessageBox.StandardButton.YesToAll
145        if buttons == QMessageBox.StandardButton.NoButton:
146            buttons = QMessageBox.StandardButton.Ok
147
148        defaultButton = self.buttonsCodeListBinary[
149            self.defaultCombo.currentIndex()]
150
151        if self.rInformation.isChecked():
152            QMessageBox.information(
153                self,
154                self.eCaption.text(),
155                self.eMessage.toPlainText(),
156                QMessageBox.StandardButtons(buttons),
157                defaultButton
158            )
159        elif self.rQuestion.isChecked():
160            QMessageBox.question(
161                self,
162                self.eCaption.text(),
163                self.eMessage.toPlainText(),
164                QMessageBox.StandardButtons(buttons),
165                defaultButton
166            )
167        elif self.rWarning.isChecked():
168            QMessageBox.warning(
169                self,
170                self.eCaption.text(),
171                self.eMessage.toPlainText(),
172                QMessageBox.StandardButtons(buttons),
173                defaultButton
174            )
175        elif self.rCritical.isChecked():
176            QMessageBox.critical(
177                self,
178                self.eCaption.text(),
179                self.eMessage.toPlainText(),
180                QMessageBox.StandardButtons(buttons),
181                defaultButton
182            )
183
184    def on_buttonBox_clicked(self, button):
185        """
186        Private slot called by a button of the button box clicked.
187
188        @param button button that was clicked (QAbstractButton)
189        """
190        if button == self.bTest:
191            self.on_bTest_clicked()
192
193    @pyqtSlot()
194    def on_bTest_clicked(self):
195        """
196        Private method to test the selected options.
197        """
198        if self.rAbout.isChecked():
199            QMessageBox.about(
200                None,
201                self.eCaption.text(),
202                self.eMessage.toPlainText()
203            )
204        elif self.rAboutQt.isChecked():
205            QMessageBox.aboutQt(
206                None,
207                self.eCaption.text()
208            )
209        else:
210            self.__testSelectedOptions()
211
212    def __enabledGroups(self):
213        """
214        Private method to enable/disable some group boxes.
215        """
216        enable = not self.rAbout.isChecked() and not self.rAboutQt.isChecked()
217        self.standardButtons.setEnabled(enable)
218        self.lResultVar.setEnabled(enable)
219        self.eResultVar.setEnabled(enable)
220
221        self.eMessage.setEnabled(not self.rAboutQt.isChecked())
222
223    def on_rAbout_toggled(self, on):
224        """
225        Private slot to handle the toggled signal of the rAbout radio button.
226
227        @param on toggle state (boolean) (ignored)
228        """
229        self.__enabledGroups()
230
231    def on_rAboutQt_toggled(self, on):
232        """
233        Private slot to handle the toggled signal of the rAboutQt radio button.
234
235        @param on toggle state (boolean) (ignored)
236        """
237        self.__enabledGroups()
238
239    def __getButtonCode(self, istring, indString):
240        """
241        Private method to generate the button code.
242
243        @param istring indentation string (string)
244        @param indString string used for indentation (space or tab) (string)
245        @return the button code (string)
246        """
247        buttons = []
248        if self.abortCheck.isChecked():
249            buttons.append("QMessageBox.StandardButton.Abort")
250        if self.applyCheck.isChecked():
251            buttons.append("QMessageBox.StandardButton.Apply")
252        if self.cancelCheck.isChecked():
253            buttons.append("QMessageBox.StandardButton.Cancel")
254        if self.closeCheck.isChecked():
255            buttons.append("QMessageBox.StandardButton.Close")
256        if self.discardCheck.isChecked():
257            buttons.append("QMessageBox.StandardButton.Discard")
258        if self.helpCheck.isChecked():
259            buttons.append("QMessageBox.StandardButton.Help")
260        if self.ignoreCheck.isChecked():
261            buttons.append("QMessageBox.StandardButton.Ignore")
262        if self.noCheck.isChecked():
263            buttons.append("QMessageBox.StandardButton.No")
264        if self.notoallCheck.isChecked():
265            buttons.append("QMessageBox.StandardButton.NoToAll")
266        if self.okCheck.isChecked():
267            buttons.append("QMessageBox.StandardButton.Ok")
268        if self.openCheck.isChecked():
269            buttons.append("QMessageBox.StandardButton.Open")
270        if self.resetCheck.isChecked():
271            buttons.append("QMessageBox.StandardButton.Reset")
272        if self.restoreCheck.isChecked():
273            buttons.append("QMessageBox.StandardButton.RestoreDefaults")
274        if self.retryCheck.isChecked():
275            buttons.append("QMessageBox.StandardButton.Retry")
276        if self.saveCheck.isChecked():
277            buttons.append("QMessageBox.StandardButton.Save")
278        if self.saveallCheck.isChecked():
279            buttons.append("QMessageBox.StandardButton.SaveAll")
280        if self.yesCheck.isChecked():
281            buttons.append("QMessageBox.StandardButton.Yes")
282        if self.yestoallCheck.isChecked():
283            buttons.append("QMessageBox.StandardButton.YesToAll")
284        if len(buttons) == 0:
285            return ""
286
287        istring2 = istring + indString
288        joinstring = ' |{0}{1}'.format(os.linesep, istring2)
289        btnCode = ',{0}{1}QMessageBox.StandardButtons('.format(
290            os.linesep, istring)
291        btnCode += '{0}{1}{2})'.format(
292            os.linesep, istring2, joinstring.join(buttons))
293        defaultIndex = self.defaultCombo.currentIndex()
294        if defaultIndex:
295            btnCode += ',{0}{1}{2}'.format(
296                os.linesep, istring,
297                self.buttonsCodeListText[defaultIndex])
298        return btnCode
299
300    def getCode(self, indLevel, indString):
301        """
302        Public method to get the source code.
303
304        @param indLevel indentation level (int)
305        @param indString string used for indentation (space or tab) (string)
306        @return generated code (string)
307        """
308        # calculate our indentation level and the indentation string
309        il = indLevel + 1
310        istring = il * indString
311        estring = os.linesep + indLevel * indString
312
313        # now generate the code
314        if self.parentSelf.isChecked():
315            parent = "self"
316        elif self.parentNone.isChecked():
317            parent = "None"
318        elif self.parentOther.isChecked():
319            parent = self.parentEdit.text()
320            if parent == "":
321                parent = "None"
322
323        resvar = self.eResultVar.text()
324        if not resvar:
325            resvar = "res"
326
327        if self.rAbout.isChecked():
328            msgdlg = "QMessageBox.about("
329        elif self.rAboutQt.isChecked():
330            msgdlg = "QMessageBox.aboutQt("
331        elif self.rInformation.isChecked():
332            msgdlg = "{0} = QMessageBox.information(".format(resvar)
333        elif self.rQuestion.isChecked():
334            msgdlg = "{0} = QMessageBox.question(".format(resvar)
335        elif self.rWarning.isChecked():
336            msgdlg = "{0} = QMessageBox.warning(".format(resvar)
337        else:
338            msgdlg = "{0} = QMessageBox.critical(".format(resvar)
339
340        if self.rAboutQt.isChecked():
341            if self.eCaption.text():
342                msgdlg += '{0}{1}{2}'.format(os.linesep, istring, parent)
343                msgdlg += ',{0}{1}self.tr("{2}")'.format(
344                    os.linesep, istring, self.eCaption.text())
345            else:
346                msgdlg += parent
347        else:
348            msgdlg += '{0}{1}{2}'.format(os.linesep, istring, parent)
349            msgdlg += ',{0}{1}self.tr("{2}")'.format(
350                os.linesep, istring, self.eCaption.text())
351            msgdlg += ',{0}{1}self.tr("""{2}""")'.format(
352                os.linesep, istring, self.eMessage.toPlainText())
353            if not self.rAbout.isChecked() and not self.rAboutQt.isChecked():
354                msgdlg += self.__getButtonCode(istring, indString)
355        msgdlg += '){0}'.format(estring)
356        return msgdlg
357