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