Display an error, rather than crashing, if encoding a QR Code failed.
[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     QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
42     if (code)
43     {
44         ui->lblQRCode->setText("");
45
46         QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
47         myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
48         myImage.fill(0xffffff);
49         unsigned char *p = code->data;
50         for (int y = 0; y < code->width; y++)
51         {
52             for (int x = 0; x < code->width; x++)
53             {
54                 myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
55                 p++;
56             }
57         }
58         QRcode_free(code);
59         ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
60     }
61     else
62         ui->lblQRCode->setText(tr("Error encoding URI into QR Code; try to reduce the text for label / message."));
63 }
64
65 QString QRCodeDialog::getURI()
66 {
67     QString ret = QString("bitcoin:%1").arg(address);
68
69     int paramCount = 0;
70     if(ui->chkReq->isChecked() && ui->lnReqAmount->text().isEmpty() == false) {
71         bool ok= false;
72         double amount = ui->lnReqAmount->text().toDouble(&ok);
73         if(ok) {
74             ret += QString("?amount=%1X8").arg(ui->lnReqAmount->text());
75             paramCount++;
76         }
77     }
78
79     if(ui->lnLabel->text().isEmpty() == false) {
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() == false) {
86         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
87         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
88         paramCount++;
89     }
90
91     return ret;
92 }
93
94 void QRCodeDialog::on_lnReqAmount_textChanged(const QString &)
95 {
96     genCode();
97 }
98
99 void QRCodeDialog::on_lnLabel_textChanged(const QString &)
100 {
101     genCode();
102 }
103
104 void QRCodeDialog::on_lnMessage_textChanged(const QString &)
105 {
106     genCode();
107 }
108
109 void QRCodeDialog::on_btnSaveAs_clicked()
110 {
111     QString fn = GUIUtil::getSaveFileName(this, tr("Save Image..."), QString(), tr("PNG Images (*.png)"));
112     if(!fn.isEmpty()) {
113         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
114     }
115 }
116
117 void QRCodeDialog::on_chkReq_toggled(bool)
118 {
119     genCode();
120 }