Added QRCode generation functions via libqrencode. Switch on with USE_QRENCODE=1.
[novacoin.git] / src / qt / qrcodedialog.cpp
1 #include "qrcodedialog.h"
2 #include "ui_qrcodedialog.h"
3 #include <QPixmap>
4 #include <QUrl>
5 #include <QFileDialog>
6 #include <QDesktopServices>
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     QString ret = QString("bitcoin:%1").arg(address);
57
58     int paramCount = 0;
59     if(ui->chkReq->isChecked() && ui->lnReqAmount->text().isEmpty() == false) {
60         bool ok= false;
61         double amount = ui->lnReqAmount->text().toDouble(&ok);
62         if(ok) {
63             ret += QString("?amount=%1X8").arg(ui->lnReqAmount->text());
64             paramCount++;
65         }
66     }
67
68     if(ui->lnLabel->text().isEmpty() == false) {
69         QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
70         ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
71         paramCount++;
72     }
73
74     if(ui->lnMessage->text().isEmpty() == false) {
75         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
76         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
77         paramCount++;
78     }
79
80     return ret;
81 }
82
83 void QRCodeDialog::on_lnReqAmount_textChanged(const QString &) {
84     genCode();
85 }
86
87 void QRCodeDialog::on_lnLabel_textChanged(const QString &) {
88     genCode();
89 }
90
91 void QRCodeDialog::on_lnMessage_textChanged(const QString &) {
92     genCode();
93 }
94
95 void QRCodeDialog::on_btnSaveAs_clicked()
96 {
97     QString fn = QFileDialog::getSaveFileName(this, "Save Image...", QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation), "Images (*.png)");
98     if(!fn.isEmpty()) {
99         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
100     }
101 }
102
103 void QRCodeDialog::on_chkReq_toggled(bool)
104 {
105     genCode();
106 }