fixed amount part of URI in QR-Codes / removed (no label) string if we have NO label...
[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     // don't display "(no label)" if there IS no label, as this is confusing in the QR dialog
28     if(label != tr("(no label)"))
29         ui->lnLabel->setText(label);
30
31     genCode();
32 }
33
34 QRCodeDialog::~QRCodeDialog()
35 {
36     delete ui;
37 }
38
39 void QRCodeDialog::genCode()
40 {
41     QString uri = getURI();
42     QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
43     myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
44     myImage.fill(0xffffff);
45     unsigned char *p = code->data;
46     for (int y = 0; y < code->width; y++)
47     {
48         for (int x = 0; x < code->width; x++)
49         {
50             myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
51             p++;
52         }
53     }
54     QRcode_free(code);
55     ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
56 }
57
58 QString QRCodeDialog::getURI()
59 {
60     QString ret = QString("bitcoin:%1").arg(address);
61
62     int paramCount = 0;
63     if (ui->chkReq->isChecked() && !ui->lnReqAmount->text().isEmpty())
64     {
65         bool ok = false;
66         ui->lnReqAmount->text().toDouble(&ok);
67         if (ok)
68         {
69             ret += QString("?amount=%1").arg(ui->lnReqAmount->text());
70             paramCount++;
71         }
72     }
73
74     if (!ui->lnLabel->text().isEmpty())
75     {
76         QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
77         ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
78         paramCount++;
79     }
80
81     if (!ui->lnMessage->text().isEmpty())
82     {
83         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
84         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
85         paramCount++;
86     }
87
88     return ret;
89 }
90
91 void QRCodeDialog::on_lnReqAmount_textChanged(const QString &)
92 {
93     genCode();
94 }
95
96 void QRCodeDialog::on_lnLabel_textChanged(const QString &)
97 {
98     genCode();
99 }
100
101 void QRCodeDialog::on_lnMessage_textChanged(const QString &)
102 {
103     genCode();
104 }
105
106 void QRCodeDialog::on_btnSaveAs_clicked()
107 {
108     QString fn = GUIUtil::getSaveFileName(this, tr("Save Image..."), QString(), tr("PNG Images (*.png)"));
109     if (!fn.isEmpty())
110         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
111 }
112
113 void QRCodeDialog::on_chkReq_toggled(bool)
114 {
115     genCode();
116 }