Confirmation dialog: Rewrite getting rid of the use of Qt Designer. Now works with any sized icon images.

This commit is contained in:
Kovid Goyal 2014-10-25 09:00:40 +05:30
parent e85f51f0ef
commit a07788a74a
2 changed files with 40 additions and 131 deletions

View File

@ -3,24 +3,54 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
from PyQt5.Qt import QDialog, Qt, QPixmap, QIcon
from PyQt5.Qt import (
QDialog, Qt, QPixmap, QIcon, QSize, QVBoxLayout, QHBoxLayout, QLabel,
QCheckBox, QDialogButtonBox)
from calibre import confirm_config_name
from calibre.gui2 import dynamic
from calibre.gui2.dialogs.confirm_delete_ui import Ui_Dialog
class Dialog(QDialog, Ui_Dialog):
class Dialog(QDialog):
def __init__(self, msg, name, parent, config_set=dynamic):
def __init__(self, msg, name, parent, config_set=dynamic, icon='dialog_warning.png',
title=None, confirm_msg=None, show_cancel_button=True):
QDialog.__init__(self, parent)
self.setupUi(self)
self.setWindowTitle(title or _("Are you sure?"))
self.setWindowIcon(QIcon(I(icon)))
self.l = l = QVBoxLayout(self)
self.h = h = QHBoxLayout()
l.addLayout(h)
self.label = la = QLabel(self)
la.setScaledContents(True), la.setMaximumSize(QSize(96, 96)), la.setMinimumSize(QSize(96, 96))
la.setPixmap(QPixmap(I(icon)))
la.setObjectName("label")
self.msg = m = QLabel(self)
m.setMinimumWidth(300), m.setWordWrap(True), m.setObjectName("msg")
m.setText(msg)
h.addWidget(la), h.addSpacing(10), h.addWidget(m)
self.again = a = QCheckBox((confirm_msg or _("&Show this warning again")), self)
a.setChecked(True), a.setObjectName("again")
a.stateChanged.connect(self.toggle)
l.addWidget(a)
buttons = QDialogButtonBox.Ok
if show_cancel_button:
buttons |= QDialogButtonBox.Cancel
self.buttonBox = bb = QDialogButtonBox(buttons, self)
bb.setObjectName("buttonBox")
bb.setFocus(Qt.OtherFocusReason)
bb.accepted.connect(self.accept), bb.rejected.connect(self.reject)
l.addWidget(bb)
self.msg.setText(msg)
self.name = name
self.again.stateChanged.connect(self.toggle)
self.buttonBox.setFocus(Qt.OtherFocusReason)
self.config_set = config_set
self.resize(self.sizeHint())
def toggle(self, *args):
self.config_set[confirm_config_name(self.name)] = self.again.isChecked()
@ -30,14 +60,6 @@ def confirm(msg, name, parent=None, pixmap='dialog_warning.png', title=None,
config_set = config_set or dynamic
if not config_set.get(confirm_config_name(name), True):
return True
d = Dialog(msg, name, parent, config_set=config_set)
d.label.setPixmap(QPixmap(I(pixmap)))
d.setWindowIcon(QIcon(I(pixmap)))
if title is not None:
d.setWindowTitle(title)
if not show_cancel_button:
d.buttonBox.button(d.buttonBox.Cancel).setVisible(False)
if confirm_msg is not None:
d.again.setText(confirm_msg)
d.resize(d.sizeHint())
d = Dialog(msg, name, parent, config_set=config_set, icon=pixmap,
title=title, confirm_msg=confirm_msg, show_cancel_button=show_cancel_button)
return d.exec_() == d.Accepted

View File

@ -1,113 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>439</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Are you sure?</string>
</property>
<property name="windowIcon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/dialog_warning.png</normaloff>:/images/dialog_warning.png</iconset>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="pixmap">
<pixmap resource="../../../../resources/images.qrc">:/images/dialog_warning.png</pixmap>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="msg">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>TextLabel</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="again">
<property name="text">
<string>&amp;Show this warning again</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../../../../resources/images.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>