2012-11-20 13:18:01 -08:00
|
|
|
"""
|
|
|
|
A PyQt4 dialog to show a message and let the user check a box
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
2014-03-23 10:30:23 -07:00
|
|
|
Copyright 2012-2014 Anthony Beville
|
2012-11-20 13:18:01 -08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
2015-02-12 14:57:46 -08:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-20 13:18:01 -08:00
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
example usage:
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
checked = OptionalMessageDialog.msg(self, "Disclaimer",
|
2015-02-12 14:57:46 -08:00
|
|
|
"This is beta software, and you are using it at your own risk!",
|
2015-02-13 15:08:07 -08:00
|
|
|
)
|
2012-11-20 13:18:01 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
said_yes, checked = OptionalMessageDialog.question(self, "Question",
|
2015-02-12 14:57:46 -08:00
|
|
|
"Are you sure you wish to do this?",
|
2015-02-13 15:08:07 -08:00
|
|
|
)
|
2012-11-20 13:18:01 -08:00
|
|
|
"""
|
|
|
|
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
|
2012-11-20 13:18:01 -08:00
|
|
|
StyleMessage = 0
|
|
|
|
StyleQuestion = 1
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
|
2012-11-20 13:18:01 -08:00
|
|
|
class OptionalMessageDialog(QDialog):
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-15 03:44:09 -08:00
|
|
|
def __init__(self, parent, style, title, msg,
|
|
|
|
check_state=Qt.Unchecked, check_text=None):
|
2015-02-12 14:57:46 -08:00
|
|
|
QDialog.__init__(self, parent)
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
self.setWindowTitle(title)
|
2015-02-12 14:57:46 -08:00
|
|
|
self.was_accepted = False
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
l = QVBoxLayout(self)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
self.theLabel = QLabel(msg)
|
2015-02-12 14:57:46 -08:00
|
|
|
self.theLabel.setWordWrap(True)
|
2015-02-13 15:08:07 -08:00
|
|
|
self.theLabel.setTextFormat(Qt.RichText)
|
2015-02-12 14:57:46 -08:00
|
|
|
self.theLabel.setOpenExternalLinks(True)
|
2015-02-15 02:44:00 -08:00
|
|
|
self.theLabel.setTextInteractionFlags(
|
|
|
|
Qt.TextSelectableByMouse | Qt.LinksAccessibleByMouse | Qt.LinksAccessibleByKeyboard)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
l.addWidget(self.theLabel)
|
2015-02-15 02:44:00 -08:00
|
|
|
l.insertSpacing(-1, 10)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
if check_text is None:
|
|
|
|
if style == StyleQuestion:
|
|
|
|
check_text = "Remember this answer"
|
|
|
|
else:
|
|
|
|
check_text = "Don't show this message again"
|
|
|
|
|
|
|
|
self.theCheckBox = QCheckBox(check_text)
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
self.theCheckBox.setCheckState(check_state)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
l.addWidget(self.theCheckBox)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
btnbox_style = QDialogButtonBox.Ok
|
|
|
|
if style == StyleQuestion:
|
2015-02-15 02:44:00 -08:00
|
|
|
btnbox_style = QDialogButtonBox.Yes | QDialogButtonBox.No
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
self.theButtonBox = QDialogButtonBox(
|
2015-02-15 02:44:00 -08:00
|
|
|
btnbox_style,
|
|
|
|
parent=self,
|
|
|
|
accepted=self.accept,
|
|
|
|
rejected=self.reject)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
l.addWidget(self.theButtonBox)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def accept(self):
|
2015-02-12 14:57:46 -08:00
|
|
|
self.was_accepted = True
|
|
|
|
QDialog.accept(self)
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def reject(self):
|
2015-02-12 14:57:46 -08:00
|
|
|
self.was_accepted = False
|
|
|
|
QDialog.reject(self)
|
|
|
|
|
|
|
|
@staticmethod
|
2015-02-13 15:08:07 -08:00
|
|
|
def msg(parent, title, msg, check_state=Qt.Unchecked, check_text=None):
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-15 02:44:00 -08:00
|
|
|
d = OptionalMessageDialog(
|
2015-02-15 03:55:04 -08:00
|
|
|
parent,
|
|
|
|
StyleMessage,
|
|
|
|
title,
|
|
|
|
msg,
|
|
|
|
check_state=check_state,
|
|
|
|
check_text=check_text)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
d.exec_()
|
2015-02-15 02:44:00 -08:00
|
|
|
return d.theCheckBox.isChecked()
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
@staticmethod
|
2015-02-15 03:44:09 -08:00
|
|
|
def question(
|
|
|
|
parent, title, msg, check_state=Qt.Unchecked, check_text=None):
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-15 02:44:00 -08:00
|
|
|
d = OptionalMessageDialog(
|
2015-02-15 03:55:04 -08:00
|
|
|
parent,
|
|
|
|
StyleQuestion,
|
|
|
|
title,
|
|
|
|
msg,
|
|
|
|
check_state=check_state,
|
|
|
|
check_text=check_text)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
d.exec_()
|
|
|
|
|
|
|
|
return d.was_accepted, d.theCheckBox.isChecked()
|