eric7/E5Gui/E5MessageBox.py

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

eric ide

mercurial