Line data Source code
1 : // Copyright (c) 2011-2013 The Bitcoin Core developers
2 : // Distributed under the MIT software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include "receiverequestdialog.h"
6 : #include "ui_receiverequestdialog.h"
7 :
8 : #include "bitcoinunits.h"
9 : #include "guiconstants.h"
10 : #include "guiutil.h"
11 : #include "optionsmodel.h"
12 : #include "walletmodel.h"
13 :
14 : #include <QClipboard>
15 : #include <QDrag>
16 : #include <QMenu>
17 : #include <QMimeData>
18 : #include <QMouseEvent>
19 : #include <QPixmap>
20 : #if QT_VERSION < 0x050000
21 : #include <QUrl>
22 : #endif
23 :
24 : #if defined(HAVE_CONFIG_H)
25 : #include "config/bitcoin-config.h" /* for USE_QRCODE */
26 : #endif
27 :
28 : #ifdef USE_QRCODE
29 : #include <qrencode.h>
30 : #endif
31 :
32 0 : QRImageWidget::QRImageWidget(QWidget *parent):
33 0 : QLabel(parent), contextMenu(0)
34 : {
35 0 : contextMenu = new QMenu();
36 0 : QAction *saveImageAction = new QAction(tr("&Save Image..."), this);
37 0 : connect(saveImageAction, SIGNAL(triggered()), this, SLOT(saveImage()));
38 0 : contextMenu->addAction(saveImageAction);
39 0 : QAction *copyImageAction = new QAction(tr("&Copy Image"), this);
40 0 : connect(copyImageAction, SIGNAL(triggered()), this, SLOT(copyImage()));
41 0 : contextMenu->addAction(copyImageAction);
42 0 : }
43 :
44 0 : QImage QRImageWidget::exportImage()
45 : {
46 0 : if(!pixmap())
47 0 : return QImage();
48 0 : return pixmap()->toImage().scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE);
49 : }
50 :
51 0 : void QRImageWidget::mousePressEvent(QMouseEvent *event)
52 : {
53 0 : if(event->button() == Qt::LeftButton && pixmap())
54 : {
55 0 : event->accept();
56 0 : QMimeData *mimeData = new QMimeData;
57 0 : mimeData->setImageData(exportImage());
58 :
59 0 : QDrag *drag = new QDrag(this);
60 0 : drag->setMimeData(mimeData);
61 0 : drag->exec();
62 : } else {
63 0 : QLabel::mousePressEvent(event);
64 : }
65 0 : }
66 :
67 0 : void QRImageWidget::saveImage()
68 : {
69 0 : if(!pixmap())
70 0 : return;
71 0 : QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Image (*.png)"), NULL);
72 0 : if (!fn.isEmpty())
73 : {
74 0 : exportImage().save(fn);
75 0 : }
76 : }
77 :
78 0 : void QRImageWidget::copyImage()
79 : {
80 0 : if(!pixmap())
81 0 : return;
82 0 : QApplication::clipboard()->setImage(exportImage());
83 : }
84 :
85 0 : void QRImageWidget::contextMenuEvent(QContextMenuEvent *event)
86 : {
87 0 : if(!pixmap())
88 0 : return;
89 0 : contextMenu->exec(event->globalPos());
90 : }
91 :
92 0 : ReceiveRequestDialog::ReceiveRequestDialog(QWidget *parent) :
93 : QDialog(parent),
94 0 : ui(new Ui::ReceiveRequestDialog),
95 0 : model(0)
96 : {
97 0 : ui->setupUi(this);
98 :
99 : #ifndef USE_QRCODE
100 : ui->btnSaveAs->setVisible(false);
101 : ui->lblQRCode->setVisible(false);
102 : #endif
103 :
104 0 : connect(ui->btnSaveAs, SIGNAL(clicked()), ui->lblQRCode, SLOT(saveImage()));
105 0 : }
106 :
107 0 : ReceiveRequestDialog::~ReceiveRequestDialog()
108 : {
109 0 : delete ui;
110 0 : }
111 :
112 0 : void ReceiveRequestDialog::setModel(OptionsModel *model)
113 : {
114 0 : this->model = model;
115 :
116 0 : if (model)
117 0 : connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(update()));
118 :
119 : // update the display unit if necessary
120 0 : update();
121 0 : }
122 :
123 0 : void ReceiveRequestDialog::setInfo(const SendCoinsRecipient &info)
124 : {
125 0 : this->info = info;
126 0 : update();
127 0 : }
128 :
129 0 : void ReceiveRequestDialog::update()
130 : {
131 0 : if(!model)
132 0 : return;
133 0 : QString target = info.label;
134 0 : if(target.isEmpty())
135 0 : target = info.address;
136 0 : setWindowTitle(tr("Request payment to %1").arg(target));
137 :
138 0 : QString uri = GUIUtil::formatBitcoinURI(info);
139 0 : ui->btnSaveAs->setEnabled(false);
140 0 : QString html;
141 0 : html += "<html><font face='verdana, arial, helvetica, sans-serif'>";
142 0 : html += "<b>"+tr("Payment information")+"</b><br>";
143 0 : html += "<b>"+tr("URI")+"</b>: ";
144 0 : html += "<a href=\""+uri+"\">" + GUIUtil::HtmlEscape(uri) + "</a><br>";
145 0 : html += "<b>"+tr("Address")+"</b>: " + GUIUtil::HtmlEscape(info.address) + "<br>";
146 0 : if(info.amount)
147 0 : html += "<b>"+tr("Amount")+"</b>: " + BitcoinUnits::formatWithUnit(model->getDisplayUnit(), info.amount) + "<br>";
148 0 : if(!info.label.isEmpty())
149 0 : html += "<b>"+tr("Label")+"</b>: " + GUIUtil::HtmlEscape(info.label) + "<br>";
150 0 : if(!info.message.isEmpty())
151 0 : html += "<b>"+tr("Message")+"</b>: " + GUIUtil::HtmlEscape(info.message) + "<br>";
152 0 : ui->outUri->setText(html);
153 :
154 : #ifdef USE_QRCODE
155 0 : ui->lblQRCode->setText("");
156 0 : if(!uri.isEmpty())
157 : {
158 : // limit URI length
159 0 : if (uri.length() > MAX_URI_LENGTH)
160 : {
161 0 : ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
162 : } else {
163 0 : QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
164 0 : if (!code)
165 : {
166 0 : ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
167 0 : return;
168 : }
169 0 : QImage myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
170 0 : myImage.fill(0xffffff);
171 0 : unsigned char *p = code->data;
172 0 : for (int y = 0; y < code->width; y++)
173 : {
174 0 : for (int x = 0; x < code->width; x++)
175 : {
176 0 : myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
177 0 : p++;
178 : }
179 : }
180 0 : QRcode_free(code);
181 :
182 0 : ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
183 0 : ui->btnSaveAs->setEnabled(true);
184 : }
185 0 : }
186 : #endif
187 : }
188 :
189 0 : void ReceiveRequestDialog::on_btnCopyURI_clicked()
190 : {
191 0 : GUIUtil::setClipboard(GUIUtil::formatBitcoinURI(info));
192 0 : }
193 :
194 0 : void ReceiveRequestDialog::on_btnCopyAddress_clicked()
195 : {
196 0 : GUIUtil::setClipboard(info.address);
197 0 : }
|