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