Bitcoin Core  0.18.99
P2P Digital Currency
qrimagewidget.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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  QRect paddedRect = qrAddrImage.rect();
75 
76  // calculate ideal font size
77  qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 20, text, font);
78  font.setPointSizeF(font_size);
79 
80  painter.setFont(font);
81  paddedRect.setHeight(QR_IMAGE_SIZE+12);
82  painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, text);
83  }
84 
85  painter.end();
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  if(!pixmap())
98  return QImage();
99  return pixmap()->toImage();
100 }
101 
102 void QRImageWidget::mousePressEvent(QMouseEvent *event)
103 {
104  if(event->button() == Qt::LeftButton && pixmap())
105  {
106  event->accept();
107  QMimeData *mimeData = new QMimeData;
108  mimeData->setImageData(exportImage());
109 
110  QDrag *drag = new QDrag(this);
111  drag->setMimeData(mimeData);
112  drag->exec();
113  } else {
114  QLabel::mousePressEvent(event);
115  }
116 }
117 
119 {
120  if(!pixmap())
121  return;
122  QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Image (*.png)"), nullptr);
123  if (!fn.isEmpty())
124  {
125  exportImage().save(fn);
126  }
127 }
128 
130 {
131  if(!pixmap())
132  return;
133  QApplication::clipboard()->setImage(exportImage());
134 }
135 
136 void QRImageWidget::contextMenuEvent(QContextMenuEvent *event)
137 {
138  if(!pixmap())
139  return;
140  contextMenu->exec(event->globalPos());
141 }
QFont fixedPitchFont()
Definition: guiutil.cpp:78
QRImageWidget(QWidget *parent=nullptr)
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:918
QMenu * contextMenu
Definition: qrimagewidget.h:42
virtual void contextMenuEvent(QContextMenuEvent *event)
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:255
virtual void mousePressEvent(QMouseEvent *event)
bool setQR(const QString &data, const QString &text="")
QImage exportImage()