Browse Source

qt QRDialog: make dialog usefully resizeable

In commit 9bba65199e,
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.
patch-4
SomberNight 3 years ago
parent
commit
1e97491124
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 20
      electrum/gui/qt/qrcodewidget.py

20
electrum/gui/qt/qrcodewidget.py

@ -1,8 +1,10 @@
from typing import Optional
import qrcode import qrcode
from PyQt5.QtGui import QColor, QPen from PyQt5.QtGui import QColor, QPen
import PyQt5.QtGui as QtGui import PyQt5.QtGui as QtGui
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt, QRect
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QApplication, QVBoxLayout, QTextEdit, QHBoxLayout, QPushButton, QWidget, QApplication, QVBoxLayout, QTextEdit, QHBoxLayout, QPushButton, QWidget,
QFileDialog, QFileDialog,
@ -20,6 +22,7 @@ class QRCodeWidget(QWidget):
QWidget.__init__(self) QWidget.__init__(self)
self.data = None self.data = None
self.qr = None self.qr = None
self._framesize = None # type: Optional[int]
self.setData(data) self.setData(data)
@ -64,6 +67,7 @@ class QRCodeWidget(QWidget):
qp.begin(self) qp.begin(self)
r = qp.viewport() r = qp.viewport()
framesize = min(r.width(), r.height()) framesize = min(r.width(), r.height())
self._framesize = framesize
boxsize = int(framesize/(k + 2)) boxsize = int(framesize/(k + 2))
if boxsize < 2: if boxsize < 2:
qp.drawText(0, 20, 'Cannot draw QR code:') qp.drawText(0, 20, 'Cannot draw QR code:')
@ -87,6 +91,15 @@ class QRCodeWidget(QWidget):
boxsize - 1, boxsize - 1) boxsize - 1, boxsize - 1)
qp.end() 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): class QRDialog(WindowModalDialog):
@ -109,10 +122,7 @@ class QRDialog(WindowModalDialog):
qrw = QRCodeWidget(data) qrw = QRCodeWidget(data)
qrw.setMinimumSize(250, 250) qrw.setMinimumSize(250, 250)
qr_hbox = QHBoxLayout() vbox.addWidget(qrw, 1)
qr_hbox.addWidget(qrw)
qr_hbox.addStretch(1)
vbox.addLayout(qr_hbox)
help_text = data if show_text else help_text help_text = data if show_text else help_text
if help_text: if help_text:

Loading…
Cancel
Save