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