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