From 1e9749112420e69cc9f3ed54804dba22401e30b0 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 12 Jul 2022 19:26:01 +0200 Subject: [PATCH] qt QRDialog: make dialog usefully resizeable In commit https://github.com/spesmilo/electrum/commit/9bba65199e80556de0c6efd28258344777ac5a6d, the QRCodeWidget was put inside a BoxLayout as a workaround to avoid the "copy to clipboard" and "save as file" functionality grabbing extra whitespace/stretch/padding and putting it into the exported image. However, in turn that commit introduced a bug, where making the dialog larger does not make the QRCodeWidget larger (which worked prior). This commit tries to fix the regression and also the original bug. --- electrum/gui/qt/qrcodewidget.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/electrum/gui/qt/qrcodewidget.py b/electrum/gui/qt/qrcodewidget.py index d012c8c8d..807d82af5 100644 --- a/electrum/gui/qt/qrcodewidget.py +++ b/electrum/gui/qt/qrcodewidget.py @@ -1,8 +1,10 @@ +from typing import Optional + import qrcode from PyQt5.QtGui import QColor, QPen import PyQt5.QtGui as QtGui -from PyQt5.QtCore import Qt +from PyQt5.QtCore import Qt, QRect from PyQt5.QtWidgets import ( QApplication, QVBoxLayout, QTextEdit, QHBoxLayout, QPushButton, QWidget, QFileDialog, @@ -20,6 +22,7 @@ class QRCodeWidget(QWidget): QWidget.__init__(self) self.data = None self.qr = None + self._framesize = None # type: Optional[int] self.setData(data) @@ -64,6 +67,7 @@ class QRCodeWidget(QWidget): qp.begin(self) r = qp.viewport() framesize = min(r.width(), r.height()) + self._framesize = framesize boxsize = int(framesize/(k + 2)) if boxsize < 2: qp.drawText(0, 20, 'Cannot draw QR code:') @@ -87,6 +91,15 @@ class QRCodeWidget(QWidget): boxsize - 1, boxsize - 1) qp.end() + def grab(self) -> QtGui.QPixmap: + """Overrides QWidget.grab to only include the QR code itself, + excluding horizontal/vertical stretch. + """ + fsize = self._framesize + if fsize is None: + fsize = -1 + rect = QRect(0, 0, fsize, fsize) + return QWidget.grab(self, rect) class QRDialog(WindowModalDialog): @@ -109,10 +122,7 @@ class QRDialog(WindowModalDialog): qrw = QRCodeWidget(data) qrw.setMinimumSize(250, 250) - qr_hbox = QHBoxLayout() - qr_hbox.addWidget(qrw) - qr_hbox.addStretch(1) - vbox.addLayout(qr_hbox) + vbox.addWidget(qrw, 1) help_text = data if show_text else help_text if help_text: