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