6866686ad78deb101a519d1acfec0251cf0ad513
[electrum-nvc.git] / gui / qt / qrcodewidget.py
1 from PyQt4.QtGui import *
2 from PyQt4.QtCore import *
3 import PyQt4.QtCore as QtCore
4 import PyQt4.QtGui as QtGui
5
6 from electrum import bmp, pyqrnative
7
8
9 class QRCodeWidget(QWidget):
10
11     def __init__(self, data = None):
12         QWidget.__init__(self)
13         self.addr = None
14         self.qr = None
15         if data:
16             self.set_addr(data)
17             self.update_qr()
18
19     def set_addr(self, addr):
20         if self.addr != addr:
21             if len(addr) < 128:
22                 MinSize = 210
23             else:
24                 MinSize = 500
25             self.setMinimumSize(MinSize, MinSize)
26             self.addr = addr
27             self.qr = None
28             self.update()
29
30     def update_qr(self):
31         if self.addr and not self.qr:
32             for size in range(len(pyqrnative.QRUtil.PATTERN_POSITION_TABLE)): # [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32]:
33                 try:
34                     self.qr = pyqrnative.QRCode(size, pyqrnative.QRErrorCorrectLevel.L)
35                     self.qr.addData(self.addr)
36                     self.qr.make()
37                     break
38                 except Exception:
39                     self.qr=None
40                     continue
41             self.update()
42
43     def paintEvent(self, e):
44
45         if not self.addr:
46             return
47
48         black = QColor(0, 0, 0, 255)
49         white = QColor(255, 255, 255, 255)
50
51         if not self.qr:
52             qp = QtGui.QPainter()
53             qp.begin(self)
54             qp.setBrush(white)
55             qp.setPen(white)
56             qp.drawRect(0, 0, 198, 198)
57             qp.end()
58             return
59  
60         k = self.qr.getModuleCount()
61         qp = QtGui.QPainter()
62         qp.begin(self)
63         r = qp.viewport()
64         boxsize = min(r.width(), r.height())*0.8/k
65         size = k*boxsize
66         left = (r.width() - size)/2
67         top = (r.height() - size)/2         
68
69         # Make a white margin around the QR in case of dark theme use:
70         margin = 10
71         qp.setBrush(white)
72         qp.drawRect(left-margin, top-margin, size+(margin*2), size+(margin*2))
73
74         for r in range(k):
75             for c in range(k):
76                 if self.qr.isDark(r, c):
77                     qp.setBrush(black)
78                     qp.setPen(black)
79                 else:
80                     qp.setBrush(white)
81                     qp.setPen(white)
82                 qp.drawRect(left+c*boxsize, top+r*boxsize, boxsize, boxsize)
83         qp.end()
84         
85
86 import os
87 from electrum.i18n import _
88
89 class QRDialog(QDialog):
90
91     def __init__(self, data, parent=None, title = "", show_text=False):
92         QDialog.__init__(self, parent)
93
94         d = self
95         d.setModal(1)
96         d.setWindowTitle(title)
97         d.setMinimumSize(270, 300)
98         vbox = QVBoxLayout()
99         qrw = QRCodeWidget(data)
100         vbox.addWidget(qrw, 1)
101         if show_text:
102             text = QTextEdit()
103             text.setText(data)
104             text.setReadOnly(True)
105             vbox.addWidget(text)
106         hbox = QHBoxLayout()
107         hbox.addStretch(1)
108
109         if parent:
110             self.config = parent.config
111             filename = os.path.join(self.config.path, "qrcode.bmp")
112
113             def print_qr():
114                 bmp.save_qrcode(qrw.qr, filename)
115                 QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK'))
116
117             def copy_to_clipboard():
118                 bmp.save_qrcode(qrw.qr, filename)
119                 self.parent().app.clipboard().setImage(QImage(filename))
120                 QMessageBox.information(None, _('Message'), _("QR code saved to clipboard"), _('OK'))
121
122                 b = QPushButton(_("Copy"))
123                 hbox.addWidget(b)
124                 b.clicked.connect(copy_to_clipboard)
125
126                 b = QPushButton(_("Save"))
127                 hbox.addWidget(b)
128                 b.clicked.connect(print_qr)
129
130         b = QPushButton(_("Close"))
131         hbox.addWidget(b)
132         b.clicked.connect(d.accept)
133         b.setDefault(True)
134
135         vbox.addLayout(hbox)
136         d.setLayout(vbox)
137