diff --git a/electrum/gui/qml/components/controls/QRImage.qml b/electrum/gui/qml/components/controls/QRImage.qml index 638773aea..b9a4d900d 100644 --- a/electrum/gui/qml/components/controls/QRImage.qml +++ b/electrum/gui/qml/components/controls/QRImage.qml @@ -1,4 +1,5 @@ import QtQuick 2.6 +import QtQuick.Controls 2.15 Item { id: root @@ -17,10 +18,10 @@ Item { } Image { - source: qrdata && render ? 'image://qrgen/' + qrdata : '' + source: qrdata && render && qrprops.modules > 0 ? 'image://qrgen/' + qrdata : '' Rectangle { - visible: root.render + visible: root.render && qrprops.valid color: 'white' x: (parent.width - width) / 2 y: (parent.height - height) / 2 @@ -28,6 +29,7 @@ Item { height: qrprops.icon_modules * qrprops.box_size Image { + visible: qrprops.valid source: '../../../icons/electrum.png' x: 1 y: 1 @@ -36,5 +38,10 @@ Item { scale: 0.9 } } + Label { + visible: !qrprops.valid + text: qsTr('Data too big for QR') + anchors.centerIn: parent + } } } diff --git a/electrum/gui/qml/qeqr.py b/electrum/gui/qml/qeqr.py index 1bd9b1e35..cc3d8a4f0 100644 --- a/electrum/gui/qml/qeqr.py +++ b/electrum/gui/qml/qeqr.py @@ -1,12 +1,14 @@ import asyncio import qrcode +from qrcode.exceptions import DataOverflowError + import math import urllib from PIL import Image, ImageQt from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QRect, QPoint -from PyQt5.QtGui import QImage,QColor +from PyQt5.QtGui import QImage, QColor from PyQt5.QtQuick import QQuickImageProvider from electrum.logging import get_logger @@ -143,13 +145,20 @@ class QEQRImageProvider(QQuickImageProvider): # calculate best box_size pixelsize = min(self._max_size, 400) - modules = 17 + 4 * qr.best_fit() + qr.border * 2 - qr.box_size = math.floor(pixelsize/modules) - - qr.make(fit=True) - - pimg = qr.make_image(fill_color='black', back_color='white') - self.qimg = ImageQt.ImageQt(pimg) + try: + modules = 17 + 4 * qr.best_fit() + qr.border * 2 + qr.box_size = math.floor(pixelsize/modules) + + qr.make(fit=True) + + pimg = qr.make_image(fill_color='black', back_color='white') + self.qimg = ImageQt.ImageQt(pimg) + except DataOverflowError: + # fake it + modules = 17 + qr.border * 2 + box_size = math.floor(pixelsize/modules) + self.qimg = QImage(box_size * modules, box_size * modules, QImage.Format_RGB32) + self.qimg.fill(QColor('gray')) return self.qimg, self.qimg.size() # helper for placing icon exactly where it should go on the QR code @@ -167,12 +176,17 @@ class QEQRImageProviderHelper(QObject): # calculate best box_size pixelsize = min(self._max_size, 400) - modules = 17 + 4 * qr.best_fit() + qr.border * 2 - qr.box_size = math.floor(pixelsize/modules) + try: + modules = 17 + 4 * qr.best_fit() + qr.border * 2 + valid = True + except DataOverflowError: + # fake it + modules = 17 + qr.border * 2 + valid = False + qr.box_size = math.floor(pixelsize/modules) # calculate icon width in modules icon_modules = int(modules / 5) icon_modules += (icon_modules+1)%2 # force odd - return { 'modules': modules, 'box_size': qr.box_size, 'icon_modules': icon_modules } - + return { 'modules': modules, 'box_size': qr.box_size, 'icon_modules': icon_modules, 'valid' : valid }