Use an .ini config file for environment vars in bitcoin-util-test.py
[bitcoinplatinum.git] / src / qt / receiverequestdialog.cpp
blob3752fa4b66b2b41e3dcf2294ddc68605d838e6c4
1 // Copyright (c) 2011-2016 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.
5 #include "receiverequestdialog.h"
6 #include "ui_receiverequestdialog.h"
8 #include "bitcoinunits.h"
9 #include "guiconstants.h"
10 #include "guiutil.h"
11 #include "optionsmodel.h"
12 #include "walletmodel.h"
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
24 #if defined(HAVE_CONFIG_H)
25 #include "config/bitcoin-config.h" /* for USE_QRCODE */
26 #endif
28 #ifdef USE_QRCODE
29 #include <qrencode.h>
30 #endif
32 QRImageWidget::QRImageWidget(QWidget *parent):
33 QLabel(parent), contextMenu(0)
35 contextMenu = new QMenu(this);
36 QAction *saveImageAction = new QAction(tr("&Save Image..."), this);
37 connect(saveImageAction, SIGNAL(triggered()), this, SLOT(saveImage()));
38 contextMenu->addAction(saveImageAction);
39 QAction *copyImageAction = new QAction(tr("&Copy Image"), this);
40 connect(copyImageAction, SIGNAL(triggered()), this, SLOT(copyImage()));
41 contextMenu->addAction(copyImageAction);
44 QImage QRImageWidget::exportImage()
46 if(!pixmap())
47 return QImage();
48 return pixmap()->toImage();
51 void QRImageWidget::mousePressEvent(QMouseEvent *event)
53 if(event->button() == Qt::LeftButton && pixmap())
55 event->accept();
56 QMimeData *mimeData = new QMimeData;
57 mimeData->setImageData(exportImage());
59 QDrag *drag = new QDrag(this);
60 drag->setMimeData(mimeData);
61 drag->exec();
62 } else {
63 QLabel::mousePressEvent(event);
67 void QRImageWidget::saveImage()
69 if(!pixmap())
70 return;
71 QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Image (*.png)"), NULL);
72 if (!fn.isEmpty())
74 exportImage().save(fn);
78 void QRImageWidget::copyImage()
80 if(!pixmap())
81 return;
82 QApplication::clipboard()->setImage(exportImage());
85 void QRImageWidget::contextMenuEvent(QContextMenuEvent *event)
87 if(!pixmap())
88 return;
89 contextMenu->exec(event->globalPos());
92 ReceiveRequestDialog::ReceiveRequestDialog(QWidget *parent) :
93 QDialog(parent),
94 ui(new Ui::ReceiveRequestDialog),
95 model(0)
97 ui->setupUi(this);
99 #ifndef USE_QRCODE
100 ui->btnSaveAs->setVisible(false);
101 ui->lblQRCode->setVisible(false);
102 #endif
104 connect(ui->btnSaveAs, SIGNAL(clicked()), ui->lblQRCode, SLOT(saveImage()));
107 ReceiveRequestDialog::~ReceiveRequestDialog()
109 delete ui;
112 void ReceiveRequestDialog::setModel(OptionsModel *_model)
114 this->model = _model;
116 if (_model)
117 connect(_model, SIGNAL(displayUnitChanged(int)), this, SLOT(update()));
119 // update the display unit if necessary
120 update();
123 void ReceiveRequestDialog::setInfo(const SendCoinsRecipient &_info)
125 this->info = _info;
126 update();
129 void ReceiveRequestDialog::update()
131 if(!model)
132 return;
133 QString target = info.label;
134 if(target.isEmpty())
135 target = info.address;
136 setWindowTitle(tr("Request payment to %1").arg(target));
138 QString uri = GUIUtil::formatBitcoinURI(info);
139 ui->btnSaveAs->setEnabled(false);
140 QString html;
141 html += "<html><font face='verdana, arial, helvetica, sans-serif'>";
142 html += "<b>"+tr("Payment information")+"</b><br>";
143 html += "<b>"+tr("URI")+"</b>: ";
144 html += "<a href=\""+uri+"\">" + GUIUtil::HtmlEscape(uri) + "</a><br>";
145 html += "<b>"+tr("Address")+"</b>: " + GUIUtil::HtmlEscape(info.address) + "<br>";
146 if(info.amount)
147 html += "<b>"+tr("Amount")+"</b>: " + BitcoinUnits::formatHtmlWithUnit(model->getDisplayUnit(), info.amount) + "<br>";
148 if(!info.label.isEmpty())
149 html += "<b>"+tr("Label")+"</b>: " + GUIUtil::HtmlEscape(info.label) + "<br>";
150 if(!info.message.isEmpty())
151 html += "<b>"+tr("Message")+"</b>: " + GUIUtil::HtmlEscape(info.message) + "<br>";
152 ui->outUri->setText(html);
154 #ifdef USE_QRCODE
155 ui->lblQRCode->setText("");
156 if(!uri.isEmpty())
158 // limit URI length
159 if (uri.length() > MAX_URI_LENGTH)
161 ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
162 } else {
163 QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
164 if (!code)
166 ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
167 return;
169 QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
170 qrImage.fill(0xffffff);
171 unsigned char *p = code->data;
172 for (int y = 0; y < code->width; y++)
174 for (int x = 0; x < code->width; x++)
176 qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
177 p++;
180 QRcode_free(code);
182 QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE+20, QImage::Format_RGB32);
183 qrAddrImage.fill(0xffffff);
184 QPainter painter(&qrAddrImage);
185 painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
186 QFont font = GUIUtil::fixedPitchFont();
187 font.setPixelSize(12);
188 painter.setFont(font);
189 QRect paddedRect = qrAddrImage.rect();
190 paddedRect.setHeight(QR_IMAGE_SIZE+12);
191 painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, info.address);
192 painter.end();
194 ui->lblQRCode->setPixmap(QPixmap::fromImage(qrAddrImage));
195 ui->btnSaveAs->setEnabled(true);
198 #endif
201 void ReceiveRequestDialog::on_btnCopyURI_clicked()
203 GUIUtil::setClipboard(GUIUtil::formatBitcoinURI(info));
206 void ReceiveRequestDialog::on_btnCopyAddress_clicked()
208 GUIUtil::setClipboard(info.address);