enable wordWrap on lblQRCode / small code comment change
[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
39     if (uri != "")
40     {
41         ui->lblQRCode->setText("");
42
43         QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
44         myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
45         myImage.fill(0xffffff);
46         unsigned char *p = code->data;
47         for (int y = 0; y < code->width; y++)
48         {
49             for (int x = 0; x < code->width; x++)
50             {
51                 myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
52                 p++;
53             }
54         }
55         QRcode_free(code);
56         ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
57     }
58     else
59         ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
60 }
61
62 QString QRCodeDialog::getURI()
63 {
64     QString ret = QString("bitcoin:%1").arg(address);
65
66     int paramCount = 0;
67     if (ui->chkReqPayment->isChecked() && !ui->lnReqAmount->text().isEmpty())
68     {
69         bool ok = false;
70         ui->lnReqAmount->text().toDouble(&ok);
71         if (ok)
72         {
73             ret += QString("?amount=%1").arg(ui->lnReqAmount->text());
74             paramCount++;
75         }
76     }
77
78     if (!ui->lnLabel->text().isEmpty())
79     {
80         QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
81         ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
82         paramCount++;
83     }
84
85     if (!ui->lnMessage->text().isEmpty())
86     {
87         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
88         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
89         paramCount++;
90     }
91
92     // limit URI length to 255 chars, to prevent a DoS against the QR-Code dialog
93     if (ret.length() < 256)
94         return ret;
95     else
96         return QString("");
97 }
98
99 void QRCodeDialog::on_lnReqAmount_textChanged(const QString &arg1)
100 {
101     genCode();
102 }
103
104 void QRCodeDialog::on_lnLabel_textChanged(const QString &arg1)
105 {
106     genCode();
107 }
108
109 void QRCodeDialog::on_lnMessage_textChanged(const QString &arg1)
110 {
111     genCode();
112 }
113
114 void QRCodeDialog::on_btnSaveAs_clicked()
115 {
116     QString fn = GUIUtil::getSaveFileName(this, tr("Save Image..."), QString(), tr("PNG Images (*.png)"));
117     if (!fn.isEmpty())
118         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
119 }
120
121 void QRCodeDialog::on_chkReqPayment_toggled(bool)
122 {
123     genCode();
124 }