2017-03-01 15:46:13 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-03-13 12:39:35 +01:00
|
|
|
"""
|
|
|
|
A menu plugin showing QR-Code for bitmessage address in modal dialog.
|
|
|
|
"""
|
2017-03-01 15:46:13 +01:00
|
|
|
|
2018-03-14 17:57:27 +01:00
|
|
|
import urllib
|
|
|
|
|
2017-03-01 15:46:13 +01:00
|
|
|
import qrcode
|
2020-01-24 15:16:05 +01:00
|
|
|
from PyQt4 import QtCore, QtGui
|
2017-03-01 15:46:13 +01:00
|
|
|
|
2017-03-07 14:34:45 +01:00
|
|
|
from pybitmessage.tr import _translate
|
2017-03-01 15:46:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
# http://stackoverflow.com/questions/20452486
|
2019-12-21 08:14:13 +01:00
|
|
|
class Image(qrcode.image.base.BaseImage): # pylint: disable=abstract-method
|
2018-03-13 12:39:35 +01:00
|
|
|
"""Image output class for qrcode using QPainter"""
|
2019-09-23 12:37:23 +02:00
|
|
|
|
2019-12-21 08:14:13 +01:00
|
|
|
def __init__(self, border, width, box_size):
|
|
|
|
# pylint: disable=super-init-not-called
|
2017-03-01 15:46:13 +01:00
|
|
|
self.border = border
|
|
|
|
self.width = width
|
|
|
|
self.box_size = box_size
|
|
|
|
size = (width + border * 2) * box_size
|
|
|
|
self._image = QtGui.QImage(
|
|
|
|
size, size, QtGui.QImage.Format_RGB16)
|
|
|
|
self._image.fill(QtCore.Qt.white)
|
|
|
|
|
|
|
|
def pixmap(self):
|
2018-03-13 12:39:35 +01:00
|
|
|
"""Get image pixmap"""
|
2017-03-01 15:46:13 +01:00
|
|
|
return QtGui.QPixmap.fromImage(self._image)
|
|
|
|
|
|
|
|
def drawrect(self, row, col):
|
2018-03-13 12:39:35 +01:00
|
|
|
"""Draw a single rectangle - implementation"""
|
2017-03-01 15:46:13 +01:00
|
|
|
painter = QtGui.QPainter(self._image)
|
|
|
|
painter.fillRect(
|
|
|
|
(col + self.border) * self.box_size,
|
|
|
|
(row + self.border) * self.box_size,
|
|
|
|
self.box_size, self.box_size,
|
|
|
|
QtCore.Qt.black)
|
|
|
|
|
|
|
|
|
2017-03-07 14:34:45 +01:00
|
|
|
class QRCodeDialog(QtGui.QDialog):
|
2018-03-13 12:39:35 +01:00
|
|
|
"""The dialog"""
|
2017-03-07 14:34:45 +01:00
|
|
|
def __init__(self, parent):
|
|
|
|
super(QRCodeDialog, self).__init__(parent)
|
|
|
|
self.image = QtGui.QLabel(self)
|
|
|
|
self.label = QtGui.QLabel(self)
|
2017-03-01 15:46:13 +01:00
|
|
|
font = QtGui.QFont()
|
|
|
|
font.setBold(True)
|
|
|
|
font.setWeight(75)
|
|
|
|
self.label.setFont(font)
|
|
|
|
self.label.setAlignment(
|
2018-03-14 17:57:27 +01:00
|
|
|
QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
|
2017-03-07 14:34:45 +01:00
|
|
|
buttonBox = QtGui.QDialogButtonBox(self)
|
|
|
|
buttonBox.setOrientation(QtCore.Qt.Horizontal)
|
|
|
|
buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok)
|
|
|
|
buttonBox.accepted.connect(self.accept)
|
|
|
|
layout = QtGui.QVBoxLayout(self)
|
2017-03-01 15:46:13 +01:00
|
|
|
layout.addWidget(self.image)
|
|
|
|
layout.addWidget(self.label)
|
2017-03-07 14:34:45 +01:00
|
|
|
layout.addWidget(buttonBox)
|
|
|
|
self.retranslateUi()
|
2017-03-01 15:46:13 +01:00
|
|
|
|
2017-03-07 14:34:45 +01:00
|
|
|
def retranslateUi(self):
|
2018-03-13 12:39:35 +01:00
|
|
|
"""A conventional Qt Designer method for dynamic l10n"""
|
2017-03-07 14:34:45 +01:00
|
|
|
self.setWindowTitle(_translate("QRCodeDialog", "QR-code"))
|
2017-03-01 15:46:13 +01:00
|
|
|
|
|
|
|
def render(self, text):
|
2018-03-13 12:39:35 +01:00
|
|
|
"""Draw QR-code and address in labels"""
|
2018-03-14 17:57:27 +01:00
|
|
|
pixmap = qrcode.make(text, image_factory=Image).pixmap()
|
|
|
|
self.image.setPixmap(pixmap)
|
2017-03-01 15:46:13 +01:00
|
|
|
self.label.setText(text)
|
2018-03-14 17:57:27 +01:00
|
|
|
self.label.setToolTip(text)
|
|
|
|
self.label.setFixedWidth(pixmap.width())
|
2017-03-07 14:34:45 +01:00
|
|
|
self.setFixedSize(QtGui.QWidget.sizeHint(self))
|
2017-03-01 15:46:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
def connect_plugin(form):
|
2018-03-13 12:39:35 +01:00
|
|
|
"""Plugin entry point"""
|
2017-03-01 15:46:13 +01:00
|
|
|
def on_action_ShowQR():
|
2018-03-13 12:39:35 +01:00
|
|
|
"""A slot for popup menu action"""
|
2017-03-07 14:34:45 +01:00
|
|
|
try:
|
|
|
|
dialog = form.qrcode_dialog
|
|
|
|
except AttributeError:
|
|
|
|
form.qrcode_dialog = dialog = QRCodeDialog(form)
|
2019-05-29 17:31:54 +02:00
|
|
|
account = form.getContactSelected()
|
|
|
|
try:
|
|
|
|
label = account._getLabel() # pylint: disable=protected-access
|
|
|
|
except AttributeError:
|
|
|
|
try:
|
|
|
|
label = account.getLabel()
|
|
|
|
except AttributeError:
|
|
|
|
return
|
2018-03-14 17:57:27 +01:00
|
|
|
dialog.render(
|
|
|
|
'bitmessage:%s' % account.address + (
|
|
|
|
'?' + urllib.urlencode({'label': label.encode('utf-8')})
|
|
|
|
if label != account.address else '')
|
|
|
|
)
|
2017-03-07 14:34:45 +01:00
|
|
|
dialog.exec_()
|
|
|
|
|
2017-03-07 14:44:48 +01:00
|
|
|
return on_action_ShowQR, _translate("MainWindow", "Show QR-code")
|