Bugfix: Check that QRcode_encodeString didn't return NULL (error)
[novacoin.git] / src / qt / qrcodedialog.cpp
1 #include "qrcodedialog.h"
2 #include "ui_qrcodedialog.h"
3 #include "guiutil.h"
4
5 #include <QPixmap>
6 #include <QUrl>
7 #include <QDebug>
8
9 #include <qrencode.h>
10
11 #define EXPORT_IMAGE_SIZE   256
12
13 QRCodeDialog::QRCodeDialog(const QString &title, const QString &addr, const QString &label, bool enableReq, QWidget *parent) :
14     QDialog(parent),
15     ui(new Ui::QRCodeDialog),
16     address(addr)
17 {
18     ui->setupUi(this);
19     setWindowTitle(title);
20     setAttribute(Qt::WA_DeleteOnClose);
21
22     ui->chkReq->setVisible(enableReq);
23     ui->lnReqAmount->setVisible(enableReq);
24     ui->lblAm1->setVisible(enableReq);
25     ui->lblAm2->setVisible(enableReq);
26
27     ui->lnLabel->setText(label);
28
29     genCode();
30 }
31
32 QRCodeDialog::~QRCodeDialog()
33 {
34     delete ui;
35 }
36
37 void QRCodeDialog::genCode()
38 {
39     QString uri = getURI();
40     //qDebug() << "Encoding:" << uri.toUtf8().constData();
41
42     {
43         ui->lblQRCode->setText("");
44
45         QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
46         if (!code)
47         {
48             ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
49             return;
50         }
51         myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
52         myImage.fill(0xffffff);
53         unsigned char *p = code->data;
54         for (int y = 0; y < code->width; y++)
55         {
56             for (int x = 0; x < code->width; x++)
57             {
58                 myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
59                 p++;
60             }
61         }
62         QRcode_free(code);
63         ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
64     }
65 }
66
67 QString QRCodeDialog::getURI()
68 {
69     QString ret = QString("bitcoin:%1").arg(address);
70
71     int paramCount = 0;
72     if(ui->chkReq->isChecked() && ui->lnReqAmount->text().isEmpty() == false) {
73         bool ok= false;
74         double amount = ui->lnReqAmount->text().toDouble(&ok);
75         if(ok) {
76             ret += QString("?amount=%1X8").arg(ui->lnReqAmount->text());
77             paramCount++;
78         }
79     }
80
81     if(ui->lnLabel->text().isEmpty() == false) {
82         QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
83         ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
84         paramCount++;
85     }
86
87     if(ui->lnMessage->text().isEmpty() == false) {
88         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
89         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
90         paramCount++;
91     }
92
93     return ret;
94 }
95
96 void QRCodeDialog::on_lnReqAmount_textChanged(const QString &)
97 {
98     genCode();
99 }
100
101 void QRCodeDialog::on_lnLabel_textChanged(const QString &)
102 {
103     genCode();
104 }
105
106 void QRCodeDialog::on_lnMessage_textChanged(const QString &)
107 {
108     genCode();
109 }
110
111 void QRCodeDialog::on_btnSaveAs_clicked()
112 {
113     QString fn = GUIUtil::getSaveFileName(this, tr("Save Image..."), QString(), tr("PNG Images (*.png)"));
114     if(!fn.isEmpty()) {
115         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
116     }
117 }
118
119 void QRCodeDialog::on_chkReq_toggled(bool)
120 {
121     genCode();
122 }