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