From 95ed09f1bdef88b706ca1176b92fee9c8d38345a Mon Sep 17 00:00:00 2001 From: Vincent Texier <vit@free.fr> Date: Thu, 23 Feb 2023 17:48:41 +0100 Subject: [PATCH] [feat] display qrcode of address in account widget --- pyproject.toml | 1 + tikka/slots/pyqt/entities/qrcode_image.py | 79 +++++++++++++++++++ .../pyqt/resources/gui/widgets/account.ui | 2 +- tikka/slots/pyqt/widgets/account.py | 10 +++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tikka/slots/pyqt/entities/qrcode_image.py diff --git a/pyproject.toml b/pyproject.toml index c00dd27f..3efaa6ba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ PyQt5 = "5.15.9" PyYAML = "^6.0" substrate-interface = "^1.4.2" python-sql = "^1.4.0" +qrcode = "^7.4.2" [tool.poetry.group.dev.dependencies] black = "22.3.0" diff --git a/tikka/slots/pyqt/entities/qrcode_image.py b/tikka/slots/pyqt/entities/qrcode_image.py new file mode 100644 index 00000000..6b13e8a1 --- /dev/null +++ b/tikka/slots/pyqt/entities/qrcode_image.py @@ -0,0 +1,79 @@ +# Copyright 2021 Vincent Texier <vit@free.fr> +# +# This software is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Image class for QR code +from typing import Any + +from PyQt5.QtCore import Qt +from PyQt5.QtGui import QImage, QPainter, QPixmap +from qrcode import QRCode +from qrcode.image.base import BaseImage + + +class QRCodeImage(BaseImage): + + # constructor + def __init__(self, border, width, box_size, *args, **kwargs): + + # assigning border + super().__init__(border, width, box_size, *args, **kwargs) + self.border = border + + # assigning width + self.width = width + + # assigning box size + self.box_size = box_size + + # creating size + size = (width + border * 2) * box_size + + # image + self._image = QImage(size, size, QImage.Format_RGB16) + + # initial image as white + self._image.fill(Qt.white) + + # pixmap method + def pixmap(self): + + # returns image + return QPixmap.fromImage(self._image) + + def drawrect_context(self, row: int, col: int, qr: QRCode): + pass + + # drawrect method for drawing rectangle + def drawrect(self, row, col): + # creating painter object + painter = QPainter(self._image) + + # drawing rectangle + painter.fillRect( + (col + self.border) * self.box_size, + (row + self.border) * self.box_size, + self.box_size, + self.box_size, + Qt.black, + ) + + def save(self, stream, kind=None): + pass + + def new_image(self, **kwargs) -> Any: + pass + + def process(self): + pass diff --git a/tikka/slots/pyqt/resources/gui/widgets/account.ui b/tikka/slots/pyqt/resources/gui/widgets/account.ui index c1b3a69a..950ba735 100644 --- a/tikka/slots/pyqt/resources/gui/widgets/account.ui +++ b/tikka/slots/pyqt/resources/gui/widgets/account.ui @@ -20,7 +20,7 @@ <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> - <widget class="QLabel" name="safeImageLabel"> + <widget class="QLabel" name="QRCodeAddressLabel"> <property name="maximumSize"> <size> <width>100</width> diff --git a/tikka/slots/pyqt/widgets/account.py b/tikka/slots/pyqt/widgets/account.py index 4b5d3b4d..1bc56954 100644 --- a/tikka/slots/pyqt/widgets/account.py +++ b/tikka/slots/pyqt/widgets/account.py @@ -15,6 +15,7 @@ import sys from typing import Optional +import qrcode from PyQt5.QtCore import QMutex, QPoint from PyQt5.QtGui import QFont, QPixmap from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget @@ -31,6 +32,7 @@ from tikka.slots.pyqt.entities.constants import ( ICON_ACCOUNT_WALLET_LOCKED, ICON_ACCOUNT_WALLET_UNLOCKED, ) +from tikka.slots.pyqt.entities.qrcode_image import QRCodeImage from tikka.slots.pyqt.entities.worker import AsyncQWorker from tikka.slots.pyqt.resources.gui.widgets.account_rc import Ui_AccountWidget from tikka.slots.pyqt.widgets.account_menu import AccountPopupMenu @@ -71,6 +73,14 @@ class AccountWidget(QWidget, Ui_AccountWidget): self.addressValueLabel.setFont(self.monospace_font) self.addressValueLabel.setText(self.account.address) + # creating a pix map of qr code + qr_code_pixmap = qrcode.make( + self.account.address, image_factory=QRCodeImage + ).pixmap() + + # set qrcode to the label + self.QRCodeAddressLabel.setPixmap(qr_code_pixmap) + self._update_ui() ############################## -- GitLab