Bitcoin Core 28.99.0
P2P Digital Currency
qrimagewidget.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2021 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 <qt/qrimagewidget.h>
6
7#include <qt/guiutil.h>
8
9#include <QApplication>
10#include <QClipboard>
11#include <QDrag>
12#include <QFontDatabase>
13#include <QMenu>
14#include <QMimeData>
15#include <QMouseEvent>
16#include <QPainter>
17
18#include <bitcoin-build-config.h> // IWYU pragma: keep
19
20#ifdef USE_QRCODE
21#include <qrencode.h>
22#endif
23
25 : QLabel(parent)
26{
27 contextMenu = new QMenu(this);
28 contextMenu->addAction(tr("&Save Imageā€¦"), this, &QRImageWidget::saveImage);
29 contextMenu->addAction(tr("&Copy Image"), this, &QRImageWidget::copyImage);
30}
31
32bool QRImageWidget::setQR(const QString& data, const QString& text)
33{
34#ifdef USE_QRCODE
35 setText("");
36 if (data.isEmpty()) return false;
37
38 // limit length
39 if (data.length() > MAX_URI_LENGTH) {
40 setText(tr("Resulting URI too long, try to reduce the text for label / message."));
41 return false;
42 }
43
44 QRcode *code = QRcode_encodeString(data.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
45
46 if (!code) {
47 setText(tr("Error encoding URI into QR Code."));
48 return false;
49 }
50
51 QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
52 qrImage.fill(0xffffff);
53 unsigned char *p = code->data;
54 for (int y = 0; y < code->width; ++y) {
55 for (int x = 0; x < code->width; ++x) {
56 qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
57 ++p;
58 }
59 }
60 QRcode_free(code);
61
62 const int qr_image_size = QR_IMAGE_SIZE + (text.isEmpty() ? 0 : 2 * QR_IMAGE_MARGIN);
63 QImage qrAddrImage(qr_image_size, qr_image_size, QImage::Format_RGB32);
64 qrAddrImage.fill(0xffffff);
65 {
66 QPainter painter(&qrAddrImage);
67 painter.drawImage(QR_IMAGE_MARGIN, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
68
69 if (!text.isEmpty()) {
70 QRect paddedRect = qrAddrImage.rect();
71 paddedRect.setHeight(QR_IMAGE_SIZE + QR_IMAGE_TEXT_MARGIN);
72
73 QFont font = GUIUtil::fixedPitchFont();
74 font.setStretch(QFont::SemiCondensed);
75 font.setLetterSpacing(QFont::AbsoluteSpacing, 1);
76 const qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 2 * QR_IMAGE_TEXT_MARGIN, text, font);
77 font.setPointSizeF(font_size);
78
79 painter.setFont(font);
80 painter.drawText(paddedRect, Qt::AlignBottom | Qt::AlignCenter, text);
81 }
82 }
83
84 setPixmap(QPixmap::fromImage(qrAddrImage));
85
86 return true;
87#else
88 setText(tr("QR code support not available."));
89 return false;
90#endif
91}
92
94{
95 return GUIUtil::GetImage(this);
96}
97
98void QRImageWidget::mousePressEvent(QMouseEvent *event)
99{
100 if (event->button() == Qt::LeftButton && GUIUtil::HasPixmap(this)) {
101 event->accept();
102 QMimeData *mimeData = new QMimeData;
103 mimeData->setImageData(exportImage());
104
105 QDrag *drag = new QDrag(this);
106 drag->setMimeData(mimeData);
107 drag->exec();
108 } else {
109 QLabel::mousePressEvent(event);
110 }
111}
112
114{
115 if (!GUIUtil::HasPixmap(this))
116 return;
117 QString fn = GUIUtil::getSaveFileName(
118 this, tr("Save QR Code"), QString(),
119 /*: Expanded name of the PNG file format.
120 See: https://en.wikipedia.org/wiki/Portable_Network_Graphics. */
121 tr("PNG Image") + QLatin1String(" (*.png)"), nullptr);
122 if (!fn.isEmpty())
123 {
124 exportImage().save(fn);
125 }
126}
127
129{
130 if (!GUIUtil::HasPixmap(this))
131 return;
132 QApplication::clipboard()->setImage(exportImage());
133}
134
135void QRImageWidget::contextMenuEvent(QContextMenuEvent *event)
136{
137 if (!GUIUtil::HasPixmap(this))
138 return;
139 contextMenu->exec(event->globalPos());
140}
QMenu * contextMenu
Definition: qrimagewidget.h:44
QImage exportImage()
virtual void contextMenuEvent(QContextMenuEvent *event) override
bool setQR(const QString &data, const QString &text="")
virtual void mousePressEvent(QMouseEvent *event) override
QRImageWidget(QWidget *parent=nullptr)
QImage GetImage(const QLabel *label)
Definition: guiutil.cpp:973
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:100
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:313
bool HasPixmap(const QLabel *label)
Returns true if pixmap has been set.
Definition: guiutil.cpp:964
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:836
static constexpr int QR_IMAGE_TEXT_MARGIN
Definition: qrimagewidget.h:16
static constexpr int QR_IMAGE_SIZE
Definition: qrimagewidget.h:15
static constexpr int QR_IMAGE_MARGIN
Definition: qrimagewidget.h:17
static const int MAX_URI_LENGTH
Definition: qrimagewidget.h:12