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