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"
11 #include "optionsmodel.h"
12 #include "walletmodel.h"
18 #include <QMouseEvent>
20 #if QT_VERSION < 0x050000
24 #if defined(HAVE_CONFIG_H)
25 #include "config/bitcoin-config.h" /* for USE_QRCODE */
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()
48 return pixmap()->toImage();
51 void QRImageWidget::mousePressEvent(QMouseEvent
*event
)
53 if(event
->button() == Qt::LeftButton
&& pixmap())
56 QMimeData
*mimeData
= new QMimeData
;
57 mimeData
->setImageData(exportImage());
59 QDrag
*drag
= new QDrag(this);
60 drag
->setMimeData(mimeData
);
63 QLabel::mousePressEvent(event
);
67 void QRImageWidget::saveImage()
71 QString fn
= GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Image (*.png)"), NULL
);
74 exportImage().save(fn
);
78 void QRImageWidget::copyImage()
82 QApplication::clipboard()->setImage(exportImage());
85 void QRImageWidget::contextMenuEvent(QContextMenuEvent
*event
)
89 contextMenu
->exec(event
->globalPos());
92 ReceiveRequestDialog::ReceiveRequestDialog(QWidget
*parent
) :
94 ui(new Ui::ReceiveRequestDialog
),
100 ui
->btnSaveAs
->setVisible(false);
101 ui
->lblQRCode
->setVisible(false);
104 connect(ui
->btnSaveAs
, SIGNAL(clicked()), ui
->lblQRCode
, SLOT(saveImage()));
107 ReceiveRequestDialog::~ReceiveRequestDialog()
112 void ReceiveRequestDialog::setModel(OptionsModel
*_model
)
114 this->model
= _model
;
117 connect(_model
, SIGNAL(displayUnitChanged(int)), this, SLOT(update()));
119 // update the display unit if necessary
123 void ReceiveRequestDialog::setInfo(const SendCoinsRecipient
&_info
)
129 void ReceiveRequestDialog::update()
133 QString target
= info
.label
;
135 target
= info
.address
;
136 setWindowTitle(tr("Request payment to %1").arg(target
));
138 QString uri
= GUIUtil::formatBitcoinURI(info
);
139 ui
->btnSaveAs
->setEnabled(false);
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>";
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
);
155 ui
->lblQRCode
->setText("");
159 if (uri
.length() > MAX_URI_LENGTH
)
161 ui
->lblQRCode
->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
163 QRcode
*code
= QRcode_encodeString(uri
.toUtf8().constData(), 0, QR_ECLEVEL_L
, QR_MODE_8
, 1);
166 ui
->lblQRCode
->setText(tr("Error encoding URI into QR Code."));
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));
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
);
194 ui
->lblQRCode
->setPixmap(QPixmap::fromImage(qrAddrImage
));
195 ui
->btnSaveAs
->setEnabled(true);
201 void ReceiveRequestDialog::on_btnCopyURI_clicked()
203 GUIUtil::setClipboard(GUIUtil::formatBitcoinURI(info
));
206 void ReceiveRequestDialog::on_btnCopyAddress_clicked()
208 GUIUtil::setClipboard(info
.address
);