From 3f0a7477301d735e8386b14c23bd3bdd009810de Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 29 Mar 2024 11:35:28 +0530 Subject: [PATCH] Cleanup message box implementation Fixes continuous resize loop when clicking show details under sway. Bloody wayland waste of time --- src/calibre/gui2/dialogs/message_box.py | 29 ++++++++++++++----------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/calibre/gui2/dialogs/message_box.py b/src/calibre/gui2/dialogs/message_box.py index f286e257a7..700355a5af 100644 --- a/src/calibre/gui2/dialogs/message_box.py +++ b/src/calibre/gui2/dialogs/message_box.py @@ -5,6 +5,7 @@ __license__ = 'GPL v3' __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' +from contextlib import suppress import sys from qt.core import ( QAction, QApplication, QCheckBox, QDialog, QDialogButtonBox, QGridLayout, QIcon, @@ -84,7 +85,7 @@ class MessageBox(QDialog): # {{{ add_abort_button=False, only_copy_details=False ): - QDialog.__init__(self, parent) + super().__init__(parent) self.only_copy_details = only_copy_details self.aborted = False if q_icon is None: @@ -151,18 +152,12 @@ class MessageBox(QDialog): # {{{ if not det_msg: self.det_msg_toggle.setVisible(False) - self.resize_needed.connect(self.do_resize, type=Qt.ConnectionType.QueuedConnection) + self.resize_needed.connect(self.do_resize) self.do_resize() def on_abort(self): self.aborted = True - 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): vis = self.det_msg.isVisible() self.det_msg.setVisible(not vis) @@ -170,6 +165,10 @@ class MessageBox(QDialog): # {{{ self.resize_needed.emit() def do_resize(self): + sz = self.sizeHint() + sz.setWidth(max(min(sz.width(), 500), self.bb.sizeHint().width() + 100)) + sz.setHeight(min(sz.height(), 500)) + self.setMaximumSize(sz) self.resize(self.sizeHint()) def copy_to_clipboard(self, *args): @@ -183,11 +182,9 @@ class MessageBox(QDialog): # {{{ def showEvent(self, ev): ret = QDialog.showEvent(self, ev) if self.is_question: - try: + with suppress(Exception): self.bb.button(QDialogButtonBox.StandardButton.Yes if self.default_yes else QDialogButtonBox.StandardButton.No ).setFocus(Qt.FocusReason.OtherFocusReason) - except: - pass # Buttons were changed else: self.bb.button(QDialogButtonBox.StandardButton.Ok).setFocus(Qt.FocusReason.OtherFocusReason) return ret @@ -494,6 +491,7 @@ class JobError(QDialog): # {{{ if __name__ == '__main__': from calibre import prepare_string_for_xml from calibre.gui2 import Application, question_dialog + from qt.core import QMainWindow, QTimer app = Application([]) merged = {'Kovid Goyal': ['Waterloo', 'Doomed'], 'Someone Else': ['Some other book ' * 1000]} lines = [] @@ -502,7 +500,12 @@ if __name__ == '__main__': for title in sorted(merged[author]): lines.append(f'
  • {prepare_string_for_xml(title)}
  • ') lines.append('') - - print(question_dialog(None, 'title', 'msg goog ', + w = QMainWindow() + w.show() + def doit(): + print(question_dialog(w, 'title', 'msg goog ', det_msg='\n'.join(lines), show_copy_button=True)) + w.close() + QTimer.singleShot(100, doit) + app.exec()