access to global configuration using set_config and get_config
[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 import os
7 import qrcode
8
9 import electrum
10 from electrum import bmp
11 from electrum.i18n import _
12
13
14 class QRCodeWidget(QWidget):
15
16     def __init__(self, data = None):
17         QWidget.__init__(self)
18         self.data = None
19         self.qr = None
20         self.setData(data)
21
22
23     def setData(self, data):
24         if self.data != data:
25             self.data = data
26         if 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)
31         else:
32             self.qr = None
33
34         self.update()
35  
36
37     def paintEvent(self, e):
38         if not self.data:
39             return
40
41         black = QColor(0, 0, 0, 255)
42         white = QColor(255, 255, 255, 255)
43
44         if not self.qr:
45             qp = QtGui.QPainter()
46             qp.begin(self)
47             qp.setBrush(white)
48             qp.setPen(white)
49             r = qp.viewport()
50             qp.drawRect(0, 0, r.width(), r.height())
51             qp.end()
52             return
53
54         matrix = self.qr.get_matrix()
55         k = len(matrix)
56         qp = QtGui.QPainter()
57         qp.begin(self)
58         r = qp.viewport()
59
60         margin = 10
61         framesize = min(r.width(), r.height())
62         boxsize = int( (framesize - 2*margin)/k )
63         size = k*boxsize
64         left = (r.width() - size)/2
65         top = (r.height() - size)/2
66
67         # Make a white margin around the QR in case of dark theme use
68         qp.setBrush(white)
69         qp.setPen(white)
70         qp.drawRect(left-margin, top-margin, size+(margin*2), size+(margin*2))
71
72         for r in range(k):
73             for c in range(k):
74                 if matrix[r][c]:
75                     qp.setBrush(black)
76                     qp.setPen(black)
77                 else:
78                     qp.setBrush(white)
79                     qp.setPen(white)
80                 qp.drawRect(left+c*boxsize, top+r*boxsize, boxsize, boxsize)
81         qp.end()
82         
83
84
85 class QRDialog(QDialog):
86
87     def __init__(self, data, parent=None, title = "", show_text=False):
88         QDialog.__init__(self, parent)
89
90         d = self
91         d.setWindowTitle(title)
92         vbox = QVBoxLayout()
93         qrw = QRCodeWidget(data)
94         vbox.addWidget(qrw, 1)
95         if show_text:
96             text = QTextEdit()
97             text.setText(data)
98             text.setReadOnly(True)
99             vbox.addWidget(text)
100         hbox = QHBoxLayout()
101         hbox.addStretch(1)
102
103         config = electrum.get_config()
104         if config:
105             filename = os.path.join(config.path, "qrcode.bmp")
106
107             def print_qr():
108                 bmp.save_qrcode(qrw.qr, filename)
109                 QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK'))
110
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'))
115
116             b = QPushButton(_("Copy"))
117             hbox.addWidget(b)
118             b.clicked.connect(copy_to_clipboard)
119
120             b = QPushButton(_("Save"))
121             hbox.addWidget(b)
122             b.clicked.connect(print_qr)
123
124         b = QPushButton(_("Close"))
125         hbox.addWidget(b)
126         b.clicked.connect(d.accept)
127         b.setDefault(True)
128
129         vbox.addLayout(hbox)
130         d.setLayout(vbox)
131