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, fixedSize=False):
17 QWidget.__init__(self)
20 self.fixedSize=fixedSize
22 self.setFixedSize(fixedSize, fixedSize)
26 def setData(self, data):
30 self.qr = qrcode.QRCode()
31 self.qr.add_data(self.data)
32 if not self.fixedSize:
33 k = len(self.qr.get_matrix())
34 self.setMinimumSize(k*5,k*5)
41 def paintEvent(self, e):
45 black = QColor(0, 0, 0, 255)
46 white = QColor(255, 255, 255, 255)
54 qp.drawRect(0, 0, r.width(), r.height())
58 matrix = self.qr.get_matrix()
65 framesize = min(r.width(), r.height())
66 boxsize = int( (framesize - 2*margin)/k )
68 left = (r.width() - size)/2
69 top = (r.height() - size)/2
71 # Make a white margin around the QR in case of dark theme use
74 qp.drawRect(left-margin, top-margin, size+(margin*2), size+(margin*2))
84 qp.drawRect(left+c*boxsize, top+r*boxsize, boxsize, boxsize)
89 class QRDialog(QDialog):
91 def __init__(self, data, parent=None, title = "", show_text=False):
92 QDialog.__init__(self, parent)
95 d.setWindowTitle(title)
97 qrw = QRCodeWidget(data)
98 vbox.addWidget(qrw, 1)
102 text.setReadOnly(True)
107 config = electrum.get_config()
109 filename = os.path.join(config.path, "qrcode.bmp")
112 bmp.save_qrcode(qrw.qr, filename)
113 QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK'))
115 def copy_to_clipboard():
116 bmp.save_qrcode(qrw.qr, filename)
117 self.parent().app.clipboard().setImage(QImage(filename))
118 QMessageBox.information(None, _('Message'), _("QR code saved to clipboard"), _('OK'))
120 b = QPushButton(_("Copy"))
122 b.clicked.connect(copy_to_clipboard)
124 b = QPushButton(_("Save"))
126 b.clicked.connect(print_qr)
128 b = QPushButton(_("Close"))
130 b.clicked.connect(d.accept)