updated to reflect pull-request suggestions / renamed some GUI elements
[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 &addr, const QString &label, bool enableReq, QWidget *parent) :
14     QDialog(parent), ui(new Ui::QRCodeDialog), address(addr)
15 {
16     ui->setupUi(this);
17     setWindowTitle(QString("%1").arg(address));
18     setAttribute(Qt::WA_DeleteOnClose);
19
20     ui->chkReqPayment->setVisible(enableReq);
21     ui->lnReqAmount->setVisible(enableReq);
22     ui->lblAmount->setVisible(enableReq);
23     ui->lblBTC->setVisible(enableReq);
24
25     ui->lnLabel->setText(label);
26
27     genCode();
28 }
29
30 QRCodeDialog::~QRCodeDialog()
31 {
32     delete ui;
33 }
34
35 void QRCodeDialog::genCode()
36 {
37     QString uri = getURI();
38     QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
39     myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
40     myImage.fill(0xffffff);
41     unsigned char *p = code->data;
42     for (int y = 0; y < code->width; y++)
43     {
44         for (int x = 0; x < code->width; x++)
45         {
46             myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
47             p++;
48         }
49     }
50     QRcode_free(code);
51     ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
52 }
53
54 QString QRCodeDialog::getURI()
55 {
56     QString ret = QString("bitcoin:%1").arg(address);
57
58     int paramCount = 0;
59     if (ui->chkReqPayment->isChecked() && !ui->lnReqAmount->text().isEmpty())
60     {
61         bool ok = false;
62         ui->lnReqAmount->text().toDouble(&ok);
63         if (ok)
64         {
65             ret += QString("?amount=%1").arg(ui->lnReqAmount->text());
66             paramCount++;
67         }
68     }
69
70     if (!ui->lnLabel->text().isEmpty())
71     {
72         QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
73         ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
74         paramCount++;
75     }
76
77     if (!ui->lnMessage->text().isEmpty())
78     {
79         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
80         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
81         paramCount++;
82     }
83
84     return ret;
85 }
86
87 void QRCodeDialog::on_lnReqAmount_textChanged(const QString &arg1)
88 {
89     genCode();
90 }
91
92 void QRCodeDialog::on_lnLabel_textChanged(const QString &arg1)
93 {
94     genCode();
95 }
96
97 void QRCodeDialog::on_lnMessage_textChanged(const QString &arg1)
98 {
99     genCode();
100 }
101
102 void QRCodeDialog::on_btnSaveAs_clicked()
103 {
104     QString fn = GUIUtil::getSaveFileName(this, tr("Save Image..."), QString(), tr("PNG Images (*.png)"));
105     if (!fn.isEmpty())
106         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
107 }
108
109 void QRCodeDialog::on_chkReqPayment_toggled(bool)
110 {
111     genCode();
112 }