fix default suffixes in save dialog in GNOME, make it more clear that PNG is used...
[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     myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
43     myImage.fill(0xffffff);
44     unsigned char *p = code->data;
45     for(int y = 0; y < code->width; y++) {
46         for(int x = 0; x < code->width; x++) {
47             myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
48             p++;
49         }
50     }
51     QRcode_free(code);
52     ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
53 }
54
55 QString QRCodeDialog::getURI()
56 {
57     QString ret = QString("bitcoin:%1").arg(address);
58
59     int paramCount = 0;
60     if(ui->chkReq->isChecked() && ui->lnReqAmount->text().isEmpty() == false) {
61         bool ok= false;
62         double amount = ui->lnReqAmount->text().toDouble(&ok);
63         if(ok) {
64             ret += QString("?amount=%1X8").arg(ui->lnReqAmount->text());
65             paramCount++;
66         }
67     }
68
69     if(ui->lnLabel->text().isEmpty() == false) {
70         QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
71         ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
72         paramCount++;
73     }
74
75     if(ui->lnMessage->text().isEmpty() == false) {
76         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
77         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
78         paramCount++;
79     }
80
81     return ret;
82 }
83
84 void QRCodeDialog::on_lnReqAmount_textChanged(const QString &)
85 {
86     genCode();
87 }
88
89 void QRCodeDialog::on_lnLabel_textChanged(const QString &)
90 {
91     genCode();
92 }
93
94 void QRCodeDialog::on_lnMessage_textChanged(const QString &)
95 {
96     genCode();
97 }
98
99 void QRCodeDialog::on_btnSaveAs_clicked()
100 {
101     QString fn = GUIUtil::getSaveFileName(this, tr("Save Image..."), QString(), tr("PNG Images (*.png)"));
102     if(!fn.isEmpty()) {
103         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
104     }
105 }
106
107 void QRCodeDialog::on_chkReq_toggled(bool)
108 {
109     genCode();
110 }