forked from gabutakut/gabutdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QrcodePaint.vala
executable file
·64 lines (62 loc) · 2.43 KB
/
QrcodePaint.vala
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
/*
* Copyright (c) {2024} torikulhabib (https://github.com/gabutakut)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: torikulhabib <torik.habib@Gmail.com>
*/
namespace Gabut {
public class QrcodePaint : GdmPaint {
private string _qrstr;
public string qrstr {
get {
return _qrstr;
}
set {
_qrstr = value;
queue_draw ();
}
}
protected override void on_snapshot (Gtk.Snapshot snapshot, double width, double height) {
var qrencode = new Qrencode.QRcode.encodeData (qrstr.length, qrstr.data, 1, Qrencode.EcLevel.M);
int qrenwidth = qrencode.width;
var limitsize = (qrenwidth < 29? 1.3 : 0);
var grect = Graphene.Rect ();
grect.init (0, 0, (float) width, (float)height);
Cairo.Context cr = snapshot.append_cairo (grect);
cr.set_source_rgb (1.0, 1.0, 1.0);
cr.rectangle (0, 0, width, height);
cr.fill ();
char* qrentdata = qrencode.data;
for (int y = 0; y < qrenwidth; y++) {
for (int x = 0; x < qrenwidth; x++) {
double rectx = 13 + x * (8 + limitsize);
double recty = 13 + y * (8 + limitsize);
int digit_ornot = 0;
digit_ornot += (*qrentdata & 1);
if (digit_ornot == 1) {
cr.set_source_rgb (0.0, 0.0, 0.0);
} else {
cr.set_source_rgb (1.0, 1.0, 1.0);
}
cr.rectangle (rectx, recty, (8 + limitsize), (8 + limitsize));
cr.fill ();
qrentdata++;
}
}
}
}
}