Add selectable Qt GUI Styles
[novacoin.git] / src / qt / qrcodedialog.cpp
1 #include "qrcodedialog.h"
2 #include "ui_qrcodedialog.h"
3
4 #include "bitcoinunits.h"
5 #include "dialogwindowflags.h"
6 #include "guiconstants.h"
7 #include "guiutil.h"
8 #include "optionsmodel.h"
9
10 #include <QPixmap>
11 #include <QUrl>
12
13 #include <qrencode.h>
14
15 QRCodeDialog::QRCodeDialog(const QString &addr, const QString &label, bool enableReq, QWidget *parent) :
16     QDialog(parent, DIALOGWINDOWHINTS),
17     ui(new Ui::QRCodeDialog),
18     model(0),
19     address(addr)
20 {
21     ui->setupUi(this);
22
23     setWindowTitle(QString("%1").arg(address));
24
25     ui->chkReqPayment->setVisible(enableReq);
26     ui->lblAmount->setVisible(enableReq);
27     ui->lnReqAmount->setVisible(enableReq);
28
29     ui->lnLabel->setText(label);
30
31     ui->btnSaveAs->setEnabled(false);
32
33     genCode();
34 }
35
36 QRCodeDialog::~QRCodeDialog()
37 {
38     delete ui;
39 }
40
41 void QRCodeDialog::setModel(OptionsModel *model)
42 {
43     this->model = model;
44
45     if (model)
46         connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
47
48     // update the display unit, to not use the default ("BTC")
49     updateDisplayUnit();
50 }
51
52 void QRCodeDialog::genCode()
53 {
54     QString uri = getURI();
55
56     if (uri != "")
57     {
58         ui->lblQRCode->setText("");
59
60         QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
61         if (!code)
62         {
63             ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
64             return;
65         }
66         myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
67         myImage.fill(0xffffff);
68         unsigned char *p = code->data;
69         for (int y = 0; y < code->width; y++)
70         {
71             for (int x = 0; x < code->width; x++)
72             {
73                 myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
74                 p++;
75             }
76         }
77         QRcode_free(code);
78
79         ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
80
81         ui->outUri->setPlainText(uri);
82     }
83 }
84
85 QString QRCodeDialog::getURI()
86 {
87     QString ret = QString("novacoin:%1").arg(address);
88     int paramCount = 0;
89
90     ui->outUri->clear();
91
92     if (ui->chkReqPayment->isChecked())
93     {
94         if (ui->lnReqAmount->validate())
95         {
96             // even if we allow a non BTC unit input in lnReqAmount, we generate the URI with BTC as unit (as defined in BIP21)
97             ret += QString("?amount=%1").arg(BitcoinUnits::format(BitcoinUnits::BTC, ui->lnReqAmount->value()));
98             paramCount++;
99         }
100         else
101         {
102             ui->btnSaveAs->setEnabled(false);
103             ui->lblQRCode->setText(tr("The entered amount is invalid, please check."));
104             return QString("");
105         }
106     }
107
108     if (!ui->lnLabel->text().isEmpty())
109     {
110         QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
111         ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
112         paramCount++;
113     }
114
115     if (!ui->lnMessage->text().isEmpty())
116     {
117         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
118         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
119         paramCount++;
120     }
121
122     // limit URI length to prevent a DoS against the QR-Code dialog
123     if (ret.length() > MAX_URI_LENGTH)
124     {
125         ui->btnSaveAs->setEnabled(false);
126         ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
127         return QString("");
128     }
129
130     ui->btnSaveAs->setEnabled(true);
131     return ret;
132 }
133
134 void QRCodeDialog::on_lnReqAmount_textChanged()
135 {
136     genCode();
137 }
138
139 void QRCodeDialog::on_lnLabel_textChanged()
140 {
141     genCode();
142 }
143
144 void QRCodeDialog::on_lnMessage_textChanged()
145 {
146     genCode();
147 }
148
149 void QRCodeDialog::on_btnSaveAs_clicked()
150 {
151     QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Images (*.png)"));
152     if (!fn.isEmpty())
153         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
154 }
155
156 void QRCodeDialog::on_chkReqPayment_toggled(bool fChecked)
157 {
158     if (!fChecked)
159         // if chkReqPayment is not active, don't display lnReqAmount as invalid
160         ui->lnReqAmount->setValid(true);
161
162     genCode();
163 }
164
165 void QRCodeDialog::updateDisplayUnit()
166 {
167     if (model)
168     {
169         // Update lnReqAmount with the current unit
170         ui->lnReqAmount->setDisplayUnit(model->getDisplayUnit());
171     }
172 }