Package node_manager_fkie :: Module detailed_msg_box
[frames] | no frames]

Source Code for Module node_manager_fkie.detailed_msg_box

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Fraunhofer nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  from python_qt_binding.QtCore import Qt 
 34  from python_qt_binding.QtGui import QPixmap 
 35   
 36  try: 
 37      from python_qt_binding.QtGui import QCheckBox, QPushButton, QSpacerItem, QSizePolicy, QTextEdit, QDialog 
 38      from python_qt_binding.QtGui import QDialogButtonBox, QVBoxLayout, QHBoxLayout, QLabel, QStyle, QStyleOption, QApplication 
 39  except: 
 40      from python_qt_binding.QtWidgets import QCheckBox, QPushButton, QSpacerItem, QSizePolicy, QTextEdit, QDialog 
 41      from python_qt_binding.QtWidgets import QDialogButtonBox, QVBoxLayout, QHBoxLayout, QLabel, QStyle, QStyleOption, QApplication 
 42   
 43   
 44  IGNORED_ERRORS = [] 
45 46 47 -class DetailedError(Exception):
48 ''' ''' 49
50 - def __init__(self, title, text, detailed_text=""):
51 self.title = title 52 self.value = text 53 self.detailed_text = detailed_text
54
55 - def __str__(self):
56 return repr(self.text) + ":::" + self.detailed_text
57
58 59 -class MessageBox(QDialog):
60 61 NoIcon = 0 62 Information = 1 63 Warning = 2 64 Critical = 3 65 Question = 4 66 67 NoButton = 0 68 Ok = 1 # An "OK" button defined with the AcceptRole . 69 Open = 2 # A "Open" button defined with the AcceptRole . 70 Save = 4 # A "Save" button defined with the AcceptRole . 71 Cancel = 8 # A "Cancel" button defined with the RejectRole . 72 Close = 16 # A "Close" button defined with the RejectRole . 73 Discard = 32 # A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole . 74 Apply = 64 # An "Apply" button defined with the ApplyRole . 75 Reset = 128 # A "Reset" button defined with the ResetRole . 76 RestoreDefaults = 256 # A "Restore Defaults" button defined with the ResetRole . 77 Help = 512 # A "Help" button defined with the HelpRole . 78 SaveAll = 1024 # A "Save All" button defined with the AcceptRole . 79 Yes = 2048 # A "Yes" button defined with the YesRole . 80 YesToAll = 4096 # A "Yes to All" button defined with the YesRole . 81 No = 8192 # A "No" button defined with the NoRole . 82 NoToAll = 16384 # A "No to All" button defined with the NoRole . 83 Abort = 32768 # An "Abort" button defined with the RejectRole . 84 Retry = 65536 # A "Retry" button defined with the AcceptRole . 85 Ignore = 131072 # An "Ignore" button defined with the AcceptRole . 86 Avoid = 262144 # An "'Don't show again'" button defined with the HelpRole, returns a default AcceptButton . 87
88 - def __init__(self, icon, title, text, detailed_text="", buttons=Cancel | Ok, parent=None):
89 QDialog.__init__(self, parent=parent) 90 self.setWindowFlags(self.windowFlags() & ~Qt.WindowTitleHint) 91 self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint & ~Qt.WindowMinimizeButtonHint) 92 self.setObjectName('MessageBox') 93 self._use_checkbox = True 94 self.text = text 95 self.verticalLayout = QVBoxLayout(self) 96 self.verticalLayout.setObjectName("verticalLayout") 97 self.verticalLayout.setContentsMargins(1, 1, 1, 1) 98 99 self.horizontalLayout = QHBoxLayout() 100 self.horizontalLayout.setObjectName("horizontalLayout") 101 self.horizontalLayout.setContentsMargins(1, 1, 1, 1) 102 # create icon 103 pixmap = None 104 style_option = QStyleOption() 105 if icon == self.NoIcon: 106 pass 107 elif icon == self.Question: 108 pixmap = QApplication.style().standardPixmap(QStyle.SP_MessageBoxQuestion, style_option) 109 elif icon == self.Information: 110 pixmap = QApplication.style().standardPixmap(QStyle.SP_MessageBoxInformation, style_option) 111 elif icon == self.Warning: 112 pixmap = QPixmap(":icons/crystal_clear_warning_56.png") 113 elif icon == self.Critical: 114 pixmap = QApplication.style().standardPixmap(QStyle.SP_MessageBoxCritical, style_option) 115 spacerItem = QSpacerItem(10, 60, QSizePolicy.Minimum, QSizePolicy.Minimum) 116 self.horizontalLayout.addItem(spacerItem) 117 self.icon_label = QLabel() 118 if pixmap is not None: 119 self.icon_label.setPixmap(pixmap) 120 self.icon_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) 121 self.horizontalLayout.addWidget(self.icon_label) 122 spacerItem = QSpacerItem(10, 60, QSizePolicy.Minimum, QSizePolicy.Minimum) 123 self.horizontalLayout.addItem(spacerItem) 124 # add message 125 self.message_label = QLabel(text) 126 self.message_label.setWordWrap(True) 127 self.message_label.setScaledContents(True) 128 self.message_label.setOpenExternalLinks(True) 129 self.horizontalLayout.addWidget(self.message_label) 130 self.verticalLayout.addLayout(self.horizontalLayout) 131 132 # create buttons 133 self.buttonBox = QDialogButtonBox(self) 134 self.buttonBox.setObjectName("buttonBox") 135 self.buttonBox.setOrientation(Qt.Horizontal) 136 137 self._accept_button = None 138 self._reject_button = None 139 self._buttons = buttons 140 self._create_buttons(buttons) 141 self.buttonBox.accepted.connect(self.accept) 142 self.buttonBox.rejected.connect(self.reject) 143 self.verticalLayout.addWidget(self.buttonBox) 144 145 if detailed_text: 146 self.btn_show_details = QPushButton(self.tr('Details...')) 147 self.btn_show_details.setCheckable(True) 148 self.btn_show_details.setChecked(True) 149 self.btn_show_details.toggled.connect(self.on_toggled_details) 150 self.buttonBox.addButton(self.btn_show_details, QDialogButtonBox.ActionRole) 151 # create area for detailed text 152 self.textEdit = textEdit = QTextEdit(self) 153 textEdit.setObjectName("textEdit") 154 textEdit.setReadOnly(True) 155 textEdit.setText(detailed_text) 156 # textEdit.setVisible(False) 157 self.verticalLayout.addWidget(self.textEdit) 158 self.resize(480, self.verticalLayout.totalSizeHint().height()) 159 buttons_in_box = self.buttonBox.buttons() 160 if buttons_in_box: 161 self.buttonBox.buttons()[0].setFocus()
162
163 - def setAcceptButton(self, button):
164 ''' 165 Sets the button with given ID to accept button if more then one button with AcceptRole was added to this dialog. 166 Adds the buttton to the box if is not already in. 167 :param button: int 168 ''' 169 if not button & self._buttons: 170 self._create_buttons(button) 171 self._accept_button = button
172
173 - def setRejectButton(self, button):
174 ''' 175 Sets the button with given ID to reject button if more then one button with RejectRole was added to this dialog. 176 Adds the buttton to the box if is not already in. 177 :param button: int 178 ''' 179 if not button & self._buttons: 180 self._create_buttons(button) 181 self._reject_button = button
182
183 - def on_toggled_details(self, checked):
184 if checked: 185 self.verticalLayout.addWidget(self.textEdit) 186 else: 187 self.verticalLayout.removeWidget(self.textEdit) 188 self.textEdit.setVisible(checked) 189 if not self.isMaximized(): 190 self.setMinimumSize(self.verticalLayout.totalMinimumSize()) 191 self.resize(self._current_size.width(), self.verticalLayout.totalSizeHint().height())
192 193 @staticmethod
194 - def about(parent, title, text, detailed_text='', buttons=Close):
195 box = MessageBox(MessageBox.Information, title, text, detailed_text=detailed_text, buttons=buttons, parent=parent) 196 if MessageBox.Yes & buttons: 197 box.setAcceptButton(MessageBox.Yes) 198 if MessageBox.Cancel & buttons: 199 box.setRejectButton(MessageBox.Cancel) 200 elif MessageBox.No & buttons: 201 box.setRejectButton(MessageBox.No) 202 return box.exec_()
203 204 @staticmethod
205 - def information(parent, title, text, detailed_text='', buttons=Close):
206 box = MessageBox(MessageBox.Information, title, text, detailed_text=detailed_text, buttons=buttons, parent=parent) 207 if MessageBox.Yes & buttons: 208 box.setAcceptButton(MessageBox.Yes) 209 if MessageBox.Cancel & buttons: 210 box.setRejectButton(MessageBox.Cancel) 211 elif MessageBox.No & buttons: 212 box.setRejectButton(MessageBox.No) 213 return box.exec_()
214 215 @staticmethod
216 - def question(parent, title, text, detailed_text='', buttons=Yes | No | Cancel):
217 box = MessageBox(MessageBox.Question, title, text, detailed_text=detailed_text, buttons=buttons, parent=parent) 218 if MessageBox.Yes & buttons: 219 box.setAcceptButton(MessageBox.Yes) 220 if MessageBox.Cancel & buttons: 221 box.setRejectButton(MessageBox.Cancel) 222 elif MessageBox.No & buttons: 223 box.setRejectButton(MessageBox.No) 224 return box.exec_()
225 226 @staticmethod
227 - def warning(parent, title, text, detailed_text='', buttons=Ok):
228 box = MessageBox(MessageBox.Warning, title, text, detailed_text=detailed_text, buttons=buttons, parent=parent) 229 if MessageBox.Yes & buttons: 230 box.setAcceptButton(MessageBox.Yes) 231 if MessageBox.Cancel & buttons: 232 box.setRejectButton(MessageBox.Cancel) 233 elif MessageBox.No & buttons: 234 box.setRejectButton(MessageBox.No) 235 return box.exec_()
236 237 @staticmethod
238 - def critical(parent, title, text, detailed_text='', buttons=Ok):
239 box = MessageBox(MessageBox.Critical, title, text, detailed_text=detailed_text, buttons=buttons, parent=parent) 240 if MessageBox.Yes & buttons: 241 box.setAcceptButton(MessageBox.Yes) 242 if MessageBox.Cancel & buttons: 243 box.setRejectButton(MessageBox.Cancel) 244 elif MessageBox.No & buttons: 245 box.setRejectButton(MessageBox.No) 246 return box.exec_()
247
248 - def resizeEvent(self, event):
249 if not self.isMaximized(): 250 self._current_size = event.size()
251 252 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 253 # %%%%%%%%%%%%%%%%%% close handling %%%%%%%%%%%%%%%%%%%%% 254 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 255
256 - def exec_(self):
257 if self.text in IGNORED_ERRORS: 258 self.accept() 259 return self.result() 260 return QDialog.exec_(self)
261
262 - def accept(self):
263 if self.result() == 0: 264 if self._accept_button is not None: 265 self.setResult(self._accept_button) 266 else: 267 self.setResult(1) 268 self.accepted.emit() 269 if self.isModal(): 270 self.hide()
271
272 - def reject(self):
273 if self.result() == 0: 274 if self._reject_button is not None: 275 self.setResult(self._reject_button) 276 self.rejected.emit() 277 self.hide()
278
279 - def hideEvent(self, event):
280 # event.ignore() 281 self.close()
282
283 - def closeEvent(self, event):
284 self.setAttribute(Qt.WA_DeleteOnClose, True) 285 QDialog.closeEvent(self, event)
286 287 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 288 # %%%%%%%%%%%%%%%%%% create buttons %%%%%%%%%%%%%%%%%%%%% 289 # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 290
291 - def _create_buttons(self, buttons):
292 if MessageBox.Ok & buttons: 293 self._accept_button = MessageBox.Ok 294 bt = QPushButton(self.tr("&ok")) 295 bt.clicked.connect(self._on_ok_clicked) 296 self.buttonBox.addButton(bt, QDialogButtonBox.AcceptRole) 297 if MessageBox.Open & buttons: 298 self._accept_button = MessageBox.Open 299 bt = QPushButton(self.tr("&Open")) 300 bt.clicked.connect(self._on_open_clicked) 301 self.buttonBox.addButton(bt, QDialogButtonBox.AcceptRole) 302 if MessageBox.Save & buttons: 303 self._accept_button = MessageBox.Save 304 bt = QPushButton(self.tr("&Save")) 305 bt.clicked.connect(self._on_save_clicked) 306 self.buttonBox.addButton(bt, QDialogButtonBox.AcceptRole) 307 if MessageBox.Cancel & buttons: 308 self._reject_button = MessageBox.Cancel 309 bt = QPushButton(self.tr("&Cancel")) 310 bt.clicked.connect(self._on_cancel_clicked) 311 self.buttonBox.addButton(bt, QDialogButtonBox.RejectRole) 312 if MessageBox.Close & buttons: 313 self._reject_button = MessageBox.Close 314 bt = QPushButton(self.tr("&Close")) 315 bt.clicked.connect(self._on_close_clicked) 316 self.buttonBox.addButton(bt, QDialogButtonBox.RejectRole) 317 if MessageBox.Discard & buttons: 318 bt = QPushButton(self.tr("&Discard")) 319 bt.clicked.connect(self._on_discard_clicked) 320 self.buttonBox.addButton(bt, QDialogButtonBox.DestructiveRole) 321 if MessageBox.Apply & buttons: 322 bt = QPushButton(self.tr("&Apply")) 323 bt.clicked.connect(self._on_apply_clicked) 324 self.buttonBox.addButton(bt, QDialogButtonBox.ApplyRole) 325 if MessageBox.Reset & buttons: 326 bt = QPushButton(self.tr("&Reset")) 327 bt.clicked.connect(self._on_reset_clicked) 328 self.buttonBox.addButton(bt, QDialogButtonBox.ResetRole) 329 if MessageBox.RestoreDefaults & buttons: 330 bt = QPushButton(self.tr("&RestoreDefaults")) 331 bt.clicked.connect(self._on_restore_defaults_clicked) 332 self.buttonBox.addButton(bt, QDialogButtonBox.ResetRole) 333 if MessageBox.Help & buttons: 334 bt = QPushButton(self.tr("&Help")) 335 bt.clicked.connect(self._on_help_clicked) 336 self.buttonBox.addButton(bt, QDialogButtonBox.HelpRole) 337 if MessageBox.SaveAll & buttons: 338 self._accept_button = MessageBox.SaveAll 339 bt = QPushButton(self.tr("&SaveAll")) 340 bt.clicked.connect(self._on_saveall_clicked) 341 self.buttonBox.addButton(bt, QDialogButtonBox.AcceptRole) 342 if MessageBox.Yes & buttons: 343 bt = QPushButton(self.tr("&Yes")) 344 bt.clicked.connect(self._on_yes_clicked) 345 self.buttonBox.addButton(bt, QDialogButtonBox.YesRole) 346 if MessageBox.YesToAll & buttons: 347 bt = QPushButton(self.tr("&YesToAll")) 348 bt.clicked.connect(self._on_yestoall_clicked) 349 self.buttonBox.addButton(bt, QDialogButtonBox.YesRole) 350 if MessageBox.No & buttons: 351 bt = QPushButton(self.tr("&No")) 352 bt.clicked.connect(self._on_no_clicked) 353 self.buttonBox.addButton(bt, QDialogButtonBox.NoRole) 354 if MessageBox.NoToAll & buttons: 355 bt = QPushButton(self.tr("&NoToAll")) 356 bt.clicked.connect(self._on_notoall_clicked) 357 self.buttonBox.addButton(bt, QDialogButtonBox.NoRole) 358 if MessageBox.Abort & buttons: 359 self._reject_button = MessageBox.Abort 360 bt = QPushButton(self.tr("&Abort")) 361 bt.clicked.connect(self._on_abort_clicked) 362 self.buttonBox.addButton(bt, QDialogButtonBox.RejectRole) 363 if MessageBox.Retry & buttons: 364 self._accept_button = MessageBox.Retry 365 bt = QPushButton(self.tr("&Retry")) 366 bt.clicked.connect(self._on_retry_clicked) 367 self.buttonBox.addButton(bt, QDialogButtonBox.AcceptRole) 368 if MessageBox.Ignore & buttons: 369 self._accept_button = MessageBox.Ignore 370 bt = QPushButton(self.tr("&Ignore")) 371 bt.clicked.connect(self._on_ignore_clicked) 372 self.buttonBox.addButton(bt, QDialogButtonBox.AcceptRole) 373 if MessageBox.Avoid & buttons: 374 if self._use_checkbox: 375 checkbox = QCheckBox("&Don't show again", self) 376 checkbox.stateChanged.connect(self._check_ignore) 377 self.buttonBox.addButton(checkbox, QDialogButtonBox.HelpRole) 378 else: 379 bt = QPushButton(self.tr("&Don't show again")) 380 bt.setMaximumHeight(24) 381 bt.clicked.connect(self._add_to_ignore) 382 self.buttonBox.addButton(bt, QDialogButtonBox.HelpRole)
383
384 - def _on_ok_clicked(self):
385 self.done(MessageBox.Ok)
386
387 - def _on_open_clicked(self):
388 self.done(MessageBox.Open)
389
390 - def _on_save_clicked(self):
391 self.done(MessageBox.Save)
392
393 - def _on_cancel_clicked(self):
394 self.done(MessageBox.Cancel)
395
396 - def _on_close_clicked(self):
397 self.done(MessageBox.Close)
398
399 - def _on_discard_clicked(self):
400 self.done(MessageBox.Discard)
401
402 - def _on_apply_clicked(self):
403 self.done(MessageBox.Apply)
404
405 - def _on_reset_clicked(self):
406 self.done(MessageBox.Reset)
407
409 self.done(MessageBox.RestoreDefaults)
410
411 - def _on_help_clicked(self):
412 self.done(MessageBox.Help)
413
414 - def _on_saveall_clicked(self):
415 self.done(MessageBox.SaveAll)
416
417 - def _on_yes_clicked(self):
418 self.done(MessageBox.Yes)
419
420 - def _on_yestoall_clicked(self):
421 self.done(MessageBox.YesToAll)
422
423 - def _on_no_clicked(self):
424 self.done(MessageBox.No)
425
426 - def _on_notoall_clicked(self):
427 self.done(MessageBox.NoToAll)
428
429 - def _on_abort_clicked(self):
430 self.done(MessageBox.Abort)
431
432 - def _on_retry_clicked(self):
433 self.done(MessageBox.Retry)
434
435 - def _on_ignore_clicked(self):
436 self.done(MessageBox.Ignore)
437
438 - def _add_to_ignore(self):
439 IGNORED_ERRORS.append(self.text) 440 self.accept()
441
442 - def _check_ignore(self, state):
443 if state: 444 IGNORED_ERRORS.append(self.text) 445 else: 446 try: 447 IGNORED_ERRORS.remove(self.text) 448 except Exception: 449 pass
450