1 // Copyright (c) 2011-2017 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 <qt/receiverequestdialog.h>
6 #include <qt/forms/ui_receiverequestdialog.h>
8 #include <qt/bitcoinunits.h>
9 #include <qt/guiconstants.h>
10 #include <qt/guiutil.h>
11 #include <qt/optionsmodel.h>
17 #include <QMouseEvent>
19 #if QT_VERSION < 0x050000
23 #if defined(HAVE_CONFIG_H)
24 #include <config/bitcoin-config.h> /* for USE_QRCODE */
31 QRImageWidget::QRImageWidget(QWidget
*parent
):
32 QLabel(parent
), contextMenu(0)
34 contextMenu
= new QMenu(this);
35 QAction
*saveImageAction
= new QAction(tr("&Save Image..."), this);
36 connect(saveImageAction
, SIGNAL(triggered()), this, SLOT(saveImage()));
37 contextMenu
->addAction(saveImageAction
);
38 QAction
*copyImageAction
= new QAction(tr("&Copy Image"), this);
39 connect(copyImageAction
, SIGNAL(triggered()), this, SLOT(copyImage()));
40 contextMenu
->addAction(copyImageAction
);
43 QImage
QRImageWidget::exportImage()
47 return pixmap()->toImage();
50 void QRImageWidget::mousePressEvent(QMouseEvent
*event
)
52 if(event
->button() == Qt::LeftButton
&& pixmap())
55 QMimeData
*mimeData
= new QMimeData
;
56 mimeData
->setImageData(exportImage());
58 QDrag
*drag
= new QDrag(this);
59 drag
->setMimeData(mimeData
);
62 QLabel::mousePressEvent(event
);
66 void QRImageWidget::saveImage()
70 QString fn
= GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Image (*.png)"), nullptr);
73 exportImage().save(fn
);
77 void QRImageWidget::copyImage()
81 QApplication::clipboard()->setImage(exportImage());
84 void QRImageWidget::contextMenuEvent(QContextMenuEvent
*event
)
88 contextMenu
->exec(event
->globalPos());
91 ReceiveRequestDialog::ReceiveRequestDialog(QWidget
*parent
) :
93 ui(new Ui::ReceiveRequestDialog
),
99 ui
->btnSaveAs
->setVisible(false);
100 ui
->lblQRCode
->setVisible(false);
103 connect(ui
->btnSaveAs
, SIGNAL(clicked()), ui
->lblQRCode
, SLOT(saveImage()));
106 ReceiveRequestDialog::~ReceiveRequestDialog()
111 void ReceiveRequestDialog::setModel(OptionsModel
*_model
)
113 this->model
= _model
;
116 connect(_model
, SIGNAL(displayUnitChanged(int)), this, SLOT(update()));
118 // update the display unit if necessary
122 void ReceiveRequestDialog::setInfo(const SendCoinsRecipient
&_info
)
128 void ReceiveRequestDialog::update()
132 QString target
= info
.label
;
134 target
= info
.address
;
135 setWindowTitle(tr("Request payment to %1").arg(target
));
137 QString uri
= GUIUtil::formatBitcoinURI(info
);
138 ui
->btnSaveAs
->setEnabled(false);
140 html
+= "<html><font face='verdana, arial, helvetica, sans-serif'>";
141 html
+= "<b>"+tr("Payment information")+"</b><br>";
142 html
+= "<b>"+tr("URI")+"</b>: ";
143 html
+= "<a href=\""+uri
+"\">" + GUIUtil::HtmlEscape(uri
) + "</a><br>";
144 html
+= "<b>"+tr("Address")+"</b>: " + GUIUtil::HtmlEscape(info
.address
) + "<br>";
146 html
+= "<b>"+tr("Amount")+"</b>: " + BitcoinUnits::formatHtmlWithUnit(model
->getDisplayUnit(), info
.amount
) + "<br>";
147 if(!info
.label
.isEmpty())
148 html
+= "<b>"+tr("Label")+"</b>: " + GUIUtil::HtmlEscape(info
.label
) + "<br>";
149 if(!info
.message
.isEmpty())
150 html
+= "<b>"+tr("Message")+"</b>: " + GUIUtil::HtmlEscape(info
.message
) + "<br>";
151 ui
->outUri
->setText(html
);
154 ui
->lblQRCode
->setText("");
158 if (uri
.length() > MAX_URI_LENGTH
)
160 ui
->lblQRCode
->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
162 QRcode
*code
= QRcode_encodeString(uri
.toUtf8().constData(), 0, QR_ECLEVEL_L
, QR_MODE_8
, 1);
165 ui
->lblQRCode
->setText(tr("Error encoding URI into QR Code."));
168 QImage qrImage
= QImage(code
->width
+ 8, code
->width
+ 8, QImage::Format_RGB32
);
169 qrImage
.fill(0xffffff);
170 unsigned char *p
= code
->data
;
171 for (int y
= 0; y
< code
->width
; y
++)
173 for (int x
= 0; x
< code
->width
; x
++)
175 qrImage
.setPixel(x
+ 4, y
+ 4, ((*p
& 1) ? 0x0 : 0xffffff));
181 QImage qrAddrImage
= QImage(QR_IMAGE_SIZE
, QR_IMAGE_SIZE
+20, QImage::Format_RGB32
);
182 qrAddrImage
.fill(0xffffff);
183 QPainter
painter(&qrAddrImage
);
184 painter
.drawImage(0, 0, qrImage
.scaled(QR_IMAGE_SIZE
, QR_IMAGE_SIZE
));
185 QFont font
= GUIUtil::fixedPitchFont();
186 font
.setPixelSize(12);
187 painter
.setFont(font
);
188 QRect paddedRect
= qrAddrImage
.rect();
189 paddedRect
.setHeight(QR_IMAGE_SIZE
+12);
190 painter
.drawText(paddedRect
, Qt::AlignBottom
|Qt::AlignCenter
, info
.address
);
193 ui
->lblQRCode
->setPixmap(QPixmap::fromImage(qrAddrImage
));
194 ui
->btnSaveAs
->setEnabled(true);
200 void ReceiveRequestDialog::on_btnCopyURI_clicked()
202 GUIUtil::setClipboard(GUIUtil::formatBitcoinURI(info
));
205 void ReceiveRequestDialog::on_btnCopyAddress_clicked()
207 GUIUtil::setClipboard(info
.address
);