1 from PyQt4.QtGui import *
2 from PyQt4.QtCore import *
3 import PyQt4.QtCore as QtCore
4 import PyQt4.QtGui as QtGui
9 from electrum import bmp
10 from electrum.i18n import _
13 class QRCodeWidget(QWidget):
15 def __init__(self, data = None):
16 QWidget.__init__(self)
22 def setData(self, data):
26 self.qr = qrcode.QRCode()
27 self.qr.add_data(self.data)
28 k = len(self.qr.get_matrix())
29 self.setMinimumSize(k*5,k*5)
36 def paintEvent(self, e):
40 black = QColor(0, 0, 0, 255)
41 white = QColor(255, 255, 255, 255)
49 qp.drawRect(0, 0, r.width(), r.height())
53 matrix = self.qr.get_matrix()
60 framesize = min(r.width(), r.height())
61 boxsize = int( (framesize - 2*margin)/k )
63 left = (r.width() - size)/2
64 top = (r.height() - size)/2
66 # Make a white margin around the QR in case of dark theme use
69 qp.drawRect(left-margin, top-margin, size+(margin*2), size+(margin*2))
79 qp.drawRect(left+c*boxsize, top+r*boxsize, boxsize, boxsize)
84 class QRDialog(QDialog):
86 def __init__(self, data, parent=None, title = "", show_text=False):
87 QDialog.__init__(self, parent)
90 d.setWindowTitle(title)
92 qrw = QRCodeWidget(data)
93 vbox.addWidget(qrw, 1)
97 text.setReadOnly(True)
103 self.config = parent.config
104 filename = os.path.join(self.config.path, "qrcode.bmp")
107 bmp.save_qrcode(qrw.qr, filename)
108 QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK'))
110 def copy_to_clipboard():
111 bmp.save_qrcode(qrw.qr, filename)
112 self.parent().app.clipboard().setImage(QImage(filename))
113 QMessageBox.information(None, _('Message'), _("QR code saved to clipboard"), _('OK'))
115 b = QPushButton(_("Copy"))
117 b.clicked.connect(copy_to_clipboard)
119 b = QPushButton(_("Save"))
121 b.clicked.connect(print_qr)
123 b = QPushButton(_("Close"))
125 b.clicked.connect(d.accept)