mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Refactor message box to not use uic to generate the UI
Also fixes incorrect resize of message box when toggling detailed message.
This commit is contained in:
parent
8e2abc008f
commit
7b44654e20
@ -12,15 +12,45 @@ from PyQt5.Qt import (QDialog, QIcon, QApplication, QSize, QKeySequence,
|
|||||||
QLabel, QPlainTextEdit, QTextDocument, QCheckBox, pyqtSignal)
|
QLabel, QPlainTextEdit, QTextDocument, QCheckBox, pyqtSignal)
|
||||||
|
|
||||||
from calibre.constants import __version__, isfrozen
|
from calibre.constants import __version__, isfrozen
|
||||||
from calibre.gui2.dialogs.message_box_ui import Ui_Dialog
|
|
||||||
|
|
||||||
class MessageBox(QDialog, Ui_Dialog): # {{{
|
class MessageBox(QDialog): # {{{
|
||||||
|
|
||||||
ERROR = 0
|
ERROR = 0
|
||||||
WARNING = 1
|
WARNING = 1
|
||||||
INFO = 2
|
INFO = 2
|
||||||
QUESTION = 3
|
QUESTION = 3
|
||||||
|
|
||||||
|
resize_needed = pyqtSignal()
|
||||||
|
|
||||||
|
def setup_ui(self):
|
||||||
|
self.setObjectName("Dialog")
|
||||||
|
self.resize(497, 235)
|
||||||
|
self.gridLayout = l = QGridLayout(self)
|
||||||
|
l.setObjectName("gridLayout")
|
||||||
|
self.icon_label = la = QLabel('')
|
||||||
|
la.setMaximumSize(QSize(68, 68))
|
||||||
|
la.setScaledContents(True)
|
||||||
|
la.setObjectName("icon_label")
|
||||||
|
l.addWidget(la)
|
||||||
|
self.msg = la = QLabel(self)
|
||||||
|
la.setWordWrap(True)
|
||||||
|
la.setOpenExternalLinks(True)
|
||||||
|
la.setObjectName("msg")
|
||||||
|
l.addWidget(la, 0, 1, 1, 1)
|
||||||
|
self.det_msg = dm = QPlainTextEdit(self)
|
||||||
|
dm.setReadOnly(True)
|
||||||
|
dm.setObjectName("det_msg")
|
||||||
|
l.addWidget(dm, 1, 0, 1, 2)
|
||||||
|
self.bb = bb = QDialogButtonBox(self)
|
||||||
|
bb.setStandardButtons(QDialogButtonBox.Ok)
|
||||||
|
bb.setObjectName("bb")
|
||||||
|
bb.accepted.connect(self.accept)
|
||||||
|
bb.rejected.connect(self.reject)
|
||||||
|
l.addWidget(bb, 3, 0, 1, 2)
|
||||||
|
self.toggle_checkbox = tc = QCheckBox(self)
|
||||||
|
tc.setObjectName("toggle_checkbox")
|
||||||
|
l.addWidget(tc, 2, 0, 1, 2)
|
||||||
|
|
||||||
def __init__(self, type_, title, msg,
|
def __init__(self, type_, title, msg,
|
||||||
det_msg='',
|
det_msg='',
|
||||||
q_icon=None,
|
q_icon=None,
|
||||||
@ -39,7 +69,7 @@ class MessageBox(QDialog, Ui_Dialog): # {{{
|
|||||||
self.icon = QIcon(I(icon))
|
self.icon = QIcon(I(icon))
|
||||||
else:
|
else:
|
||||||
self.icon = q_icon
|
self.icon = q_icon
|
||||||
self.setupUi(self)
|
self.setup_ui()
|
||||||
|
|
||||||
self.setWindowTitle(title)
|
self.setWindowTitle(title)
|
||||||
self.setWindowIcon(self.icon)
|
self.setWindowIcon(self.icon)
|
||||||
@ -86,20 +116,23 @@ class MessageBox(QDialog, Ui_Dialog): # {{{
|
|||||||
if not det_msg:
|
if not det_msg:
|
||||||
self.det_msg_toggle.setVisible(False)
|
self.det_msg_toggle.setVisible(False)
|
||||||
|
|
||||||
|
self.resize_needed.connect(self.do_resize, type=Qt.QueuedConnection)
|
||||||
self.do_resize()
|
self.do_resize()
|
||||||
|
|
||||||
|
def sizeHint(self):
|
||||||
|
ans = QDialog.sizeHint(self)
|
||||||
|
ans.setWidth(max(min(ans.width(), 500), self.bb.sizeHint().width() + 100))
|
||||||
|
ans.setHeight(min(ans.height(), 500))
|
||||||
|
return ans
|
||||||
|
|
||||||
def toggle_det_msg(self, *args):
|
def toggle_det_msg(self, *args):
|
||||||
vis = unicode(self.det_msg_toggle.text()) == self.hide_det_msg
|
vis = self.det_msg.isVisible()
|
||||||
self.det_msg_toggle.setText(self.show_det_msg if vis else
|
|
||||||
self.hide_det_msg)
|
|
||||||
self.det_msg.setVisible(not vis)
|
self.det_msg.setVisible(not vis)
|
||||||
self.do_resize()
|
self.det_msg_toggle.setText(self.show_det_msg if vis else self.hide_det_msg)
|
||||||
|
self.resize_needed.emit()
|
||||||
|
|
||||||
def do_resize(self):
|
def do_resize(self):
|
||||||
sz = self.sizeHint() + QSize(100, 0)
|
self.resize(self.sizeHint())
|
||||||
sz.setWidth(min(500, sz.width()))
|
|
||||||
sz.setHeight(min(500, sz.height()))
|
|
||||||
self.resize(sz)
|
|
||||||
|
|
||||||
def copy_to_clipboard(self, *args):
|
def copy_to_clipboard(self, *args):
|
||||||
QApplication.clipboard().setText(
|
QApplication.clipboard().setText(
|
||||||
@ -129,7 +162,7 @@ class MessageBox(QDialog, Ui_Dialog): # {{{
|
|||||||
self.det_msg_toggle.setText(self.show_det_msg)
|
self.det_msg_toggle.setText(self.show_det_msg)
|
||||||
self.det_msg_toggle.setVisible(bool(msg))
|
self.det_msg_toggle.setVisible(bool(msg))
|
||||||
self.det_msg.setVisible(False)
|
self.det_msg.setVisible(False)
|
||||||
self.do_resize()
|
self.resize_needed.emit()
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
class ViewLog(QDialog): # {{{
|
class ViewLog(QDialog): # {{{
|
||||||
@ -394,24 +427,8 @@ class JobError(QDialog): # {{{
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = QApplication([])
|
app = QApplication([])
|
||||||
d = JobError(None)
|
from calibre.gui2 import question_dialog
|
||||||
d.show_error('test title', 'some long meaningless test message', 'det msg')
|
print question_dialog(None, 'title', 'msg <a href="http://google.com">goog</a> ',
|
||||||
d.show_error('test title', 'some long meaningless test message', 'det msg')
|
det_msg='det '*1000,
|
||||||
d.show_error('test title', 'some long meaningless test message', 'det msg')
|
show_copy_button=True)
|
||||||
d.show_error('test title', 'some long meaningless test message', 'det msg')
|
|
||||||
d.show_error('test title', 'some long meaningless test message', 'det msg')
|
|
||||||
d.show_error('test title', 'some long meaningless test message', 'det msg')
|
|
||||||
app.setQuitOnLastWindowClosed(False)
|
|
||||||
def checkd():
|
|
||||||
if not d.queue:
|
|
||||||
app.quit()
|
|
||||||
app.lastWindowClosed.connect(checkd)
|
|
||||||
app.exec_()
|
|
||||||
|
|
||||||
# if __name__ == '__main__':
|
|
||||||
# app = QApplication([])
|
|
||||||
# from calibre.gui2 import question_dialog
|
|
||||||
# print question_dialog(None, 'title', 'msg <a href="http://google.com">goog</a> ',
|
|
||||||
# det_msg='det '*1000,
|
|
||||||
# show_copy_button=True)
|
|
||||||
|
|
||||||
|
@ -1,112 +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>497</width>
|
|
||||||
<height>235</height>
|
|
||||||
</rect>
|
|
||||||
</property>
|
|
||||||
<property name="windowTitle">
|
|
||||||
<string>Dialog</string>
|
|
||||||
</property>
|
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
|
||||||
<item row="0" column="0">
|
|
||||||
<widget class="QLabel" name="icon_label">
|
|
||||||
<property name="maximumSize">
|
|
||||||
<size>
|
|
||||||
<width>68</width>
|
|
||||||
<height>68</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
|
||||||
<string/>
|
|
||||||
</property>
|
|
||||||
<property name="pixmap">
|
|
||||||
<pixmap resource="../../../../resources/images.qrc">:/images/dialog_warning.png</pixmap>
|
|
||||||
</property>
|
|
||||||
<property name="scaledContents">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="0" column="1">
|
|
||||||
<widget class="QLabel" name="msg">
|
|
||||||
<property name="text">
|
|
||||||
<string/>
|
|
||||||
</property>
|
|
||||||
<property name="wordWrap">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
<property name="openExternalLinks">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="1" column="0" colspan="2">
|
|
||||||
<widget class="QPlainTextEdit" name="det_msg">
|
|
||||||
<property name="readOnly">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="3" column="0" colspan="2">
|
|
||||||
<widget class="QDialogButtonBox" name="bb">
|
|
||||||
<property name="orientation">
|
|
||||||
<enum>Qt::Horizontal</enum>
|
|
||||||
</property>
|
|
||||||
<property name="standardButtons">
|
|
||||||
<set>QDialogButtonBox::Ok</set>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="2" column="0" colspan="2">
|
|
||||||
<widget class="QCheckBox" name="toggle_checkbox">
|
|
||||||
<property name="text">
|
|
||||||
<string/>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</widget>
|
|
||||||
<resources>
|
|
||||||
<include location="../../../../resources/images.qrc"/>
|
|
||||||
</resources>
|
|
||||||
<connections>
|
|
||||||
<connection>
|
|
||||||
<sender>bb</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>bb</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>
|
|
Loading…
x
Reference in New Issue
Block a user