This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
forked from blikoon/QtQREncoder
-
Notifications
You must be signed in to change notification settings - Fork 4
/
widget.cpp
147 lines (134 loc) · 3.05 KB
/
widget.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <QMessageBox>
#include <QPainter>
#include <QImage>
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
qr = NULL;
setString("ALINE AND BOKO TEAM!");
}
Widget::~Widget()
{
delete ui;
if(qr != NULL)
{
QRcode_free(qr);
}
}
//IMPLEMENT
void Widget::setString(QString str)
{
string = str;
if(qr != NULL)
{
QRcode_free(qr);
}
qr = QRcode_encodeString(string.toStdString().c_str(),
1,
QR_ECLEVEL_L,
QR_MODE_8,
1);
update();
}
//IMPLEMENT
int Widget::getQRWidth() const
{
if(qr != NULL)
{
return qr->width;
}
else
{
return 0;
}
}
//IMPLEMENT
bool Widget::saveImage(QString fileName, int size)
{
if(size != 0 && !fileName.isEmpty())
{
QImage image(size, size, QImage::Format_Mono);
QPainter painter(&image);
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, size, size);
if(qr != NULL)
{
draw(painter, size, size);
}
return image.save(fileName);
}
else
{
return false;
}
}
//IMPLEMENT
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, width(), height());
if(qr != NULL)
{
draw(painter, width(), height());
}
}
//IMPLEMENT
QSize Widget::sizeHint() const
{
QSize s;
if(qr != NULL)
{
int qr_width = qr->width > 0 ? qr->width : 1;
s = QSize(qr_width * 4, qr_width * 4);
}
else
{
s = QSize(50, 50);
}
return s;
}
//IMPLEMENT
QSize Widget::minimumSizeHint() const
{
QSize s;
if(qr != NULL)
{
int qr_width = qr->width > 0 ? qr->width : 1;
s = QSize(qr_width, qr_width);
}
else
{
s = QSize(50, 50);
}
return s;
}
//IMPLEMENT
void Widget::draw(QPainter &painter, int width, int height)
{
QColor foreground(Qt::black);
painter.setBrush(foreground);
const int qr_width = qr->width > 0 ? qr->width : 1;
double scale_x = width / qr_width;
double scale_y = height / qr_width;
for( int y = 0; y < qr_width; y ++)
{
for(int x = 0; x < qr_width; x++)
{
unsigned char b = qr->data[y * qr_width + x];
if(b & 0x01)
{
QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
painter.drawRects(&r, 1);
}
}
}
}