1 from PyQt4.QtGui import *
2 from PyQt4.QtCore import *
3 import PyQt4.QtCore as QtCore
4 import PyQt4.QtGui as QtGui
10 from electrum import bmp
11 from electrum.i18n import _
14 class QRCodeWidget(QWidget):
16 def __init__(self, data = None):
17 QWidget.__init__(self)
23 def setData(self, data):
27 self.qr = qrcode.QRCode()
28 self.qr.add_data(self.data)
29 k = len(self.qr.get_matrix())
30 self.setMinimumSize(k*5,k*5)
37 def paintEvent(self, e):
41 black = QColor(0, 0, 0, 255)
42 white = QColor(255, 255, 255, 255)
50 qp.drawRect(0, 0, r.width(), r.height())
54 matrix = self.qr.get_matrix()
61 framesize = min(r.width(), r.height())
62 boxsize = int( (framesize - 2*margin)/k )
64 left = (r.width() - size)/2
65 top = (r.height() - size)/2
67 # Make a white margin around the QR in case of dark theme use
70 qp.drawRect(left-margin, top-margin, size+(margin*2), size+(margin*2))
80 qp.drawRect(left+c*boxsize, top+r*boxsize, boxsize, boxsize)
85 class QRDialog(QDialog):
87 def __init__(self, data, parent=None, title = "", show_text=False):
88 QDialog.__init__(self, parent)
91 d.setWindowTitle(title)
93 qrw = QRCodeWidget(data)
94 vbox.addWidget(qrw, 1)
98 text.setReadOnly(True)
103 config = electrum.get_config()
105 filename = os.path.join(config.path, "qrcode.bmp")
108 bmp.save_qrcode(qrw.qr, filename)
109 QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK'))
111 def copy_to_clipboard():
112 bmp.save_qrcode(qrw.qr, filename)
113 self.parent().app.clipboard().setImage(QImage(filename))
114 QMessageBox.information(None, _('Message'), _("QR code saved to clipboard"), _('OK'))
116 b = QPushButton(_("Copy"))
118 b.clicked.connect(copy_to_clipboard)
120 b = QPushButton(_("Save"))
122 b.clicked.connect(print_qr)
124 b = QPushButton(_("Close"))
126 b.clicked.connect(d.accept)