Cleanup message box implementation

Fixes continuous resize loop when clicking show details under sway.
Bloody wayland waste of time
This commit is contained in:
Kovid Goyal 2024-03-29 11:35:28 +05:30
parent bc0b5fdcaf
commit 3f0a747730
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -5,6 +5,7 @@ __license__ = 'GPL v3'
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
__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'<li>{prepare_string_for_xml(title)}</li>')
lines.append('</ol>')
print(question_dialog(None, 'title', 'msg <a href="http://google.com">goog</a> ',
w = QMainWindow()
w.show()
def doit():
print(question_dialog(w, 'title', 'msg <a href="http://google.com">goog</a> ',
det_msg='\n'.join(lines),
show_copy_button=True))
w.close()
QTimer.singleShot(100, doit)
app.exec()