2017-03-01 15:46:13 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from PyQt4 import QtGui, QtCore
|
|
|
|
import qrcode
|
|
|
|
|
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
|
|
|
|
class Image(qrcode.image.base.BaseImage):
|
|
|
|
def __init__(self, border, width, box_size):
|
|
|
|
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):
|
|
|
|
return QtGui.QPixmap.fromImage(self._image)
|
|
|
|
|
|
|
|
def drawrect(self, row, col):
|
|
|
|
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)
|
|
|
|
|
|
|
|
def save(self, stream, kind=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-03-07 14:34:45 +01:00
|
|
|
class QRCodeDialog(QtGui.QDialog):
|
|
|
|
|
|
|
|
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(
|
|
|
|
QtCore.Qt.AlignCenter | 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):
|
|
|
|
self.setWindowTitle(_translate("QRCodeDialog", "QR-code"))
|
2017-03-01 15:46:13 +01:00
|
|
|
|
|
|
|
def render(self, text):
|
|
|
|
self.label.setText(text)
|
|
|
|
self.image.setPixmap(
|
|
|
|
qrcode.make(text, image_factory=Image).pixmap())
|
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):
|
|
|
|
def on_action_ShowQR():
|
2017-03-07 14:34:45 +01:00
|
|
|
try:
|
|
|
|
dialog = form.qrcode_dialog
|
|
|
|
except AttributeError:
|
|
|
|
form.qrcode_dialog = dialog = QRCodeDialog(form)
|
|
|
|
dialog.render(str(form.getCurrentAccount()))
|
|
|
|
dialog.exec_()
|
|
|
|
|
2017-03-07 14:44:48 +01:00
|
|
|
return on_action_ShowQR, _translate("MainWindow", "Show QR-code")
|