-
Notifications
You must be signed in to change notification settings - Fork 5
/
QRDialog.cpp
100 lines (84 loc) · 3.17 KB
/
QRDialog.cpp
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/******************************************************************************
* wxVcashGUI: a GUI for Vcash, a decentralized currency
* for the internet (https://vcash.info).
*
* Copyright (c) The Vcash Developers
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
******************************************************************************/
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/button.h>
#include <wx/dcclient.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#endif
#include "QRDialog.h"
namespace wxGUI {
class QRImage : public wxWindow {
private:
QRcode *qrcode;
public:
QRImage(wxWindow *parent, const std::string str, const int dimension) : wxWindow(parent, wxID_ANY) {
SetMinSize(wxSize(dimension, dimension));
SetMaxSize(wxSize(dimension, dimension));
qrcode = QRcode_encodeString(str.c_str(), 0, QR_ECLEVEL_M, QR_MODE_8, 1);
Bind(wxEVT_PAINT, [this](wxPaintEvent &event) {
paintEvent(event);
});
}
~QRImage() {
if(qrcode!=NULL)
QRcode_free(qrcode);
}
bool AcceptsFocus() const {
return false;
}
bool AcceptsFocusFromKeyboard() const {
return false;
}
void paintEvent(wxPaintEvent &evt) {
wxPaintDC dc(this);
render(dc);
}
void render(wxDC &dc) {
if(qrcode==NULL)
return;
wxSize sz = GetClientSize();
int width = qrcode->width;
double dim = wxMin(sz.x, sz.y) / width;
double marginX = (sz.x - (dim * width)) / 2;
double marginY = (sz.y - (dim * width)) / 2;
dc.SetPen(*wxWHITE_PEN);
dc.SetBrush(*wxWHITE_BRUSH);
wxRect rectToDraw(marginX, marginY, dim*width, dim*width);
if (IsExposed(rectToDraw))
dc.DrawRectangle(rectToDraw);
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxBLACK_BRUSH);
for (int pos = 0, i = 0; i < width; i++) {
for (int j = 0; j < width; j++, pos++) {
if (qrcode->data[pos] & 1) {
wxRect rectToDraw(marginX + dim*j, marginY + dim*i, dim, dim);
if (IsExposed(rectToDraw))
dc.DrawRectangle(rectToDraw);
}
}
}
}
};
}
using namespace wxGUI;
wxGUI::QRDialog::QRDialog(wxWindow &parent, const wxString &title, const wxString &str)
: ShowInfoDialog(parent, title, [this, str]() {
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
wxStaticText *header = new wxStaticText(this, wxID_ANY, wxString(str));
const int dimension = 128;
QRImage *qr = new QRImage(this, str.ToStdString(), dimension);
vbox->Add(header, 0, wxALIGN_CENTER | wxALL, 10);
vbox->Add(qr, 0, wxALIGN_CENTER | wxALL, 10);
return vbox;
}) { }