Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

qrdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 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/qrdialog.h>
6 #include <qt/forms/ui_qrdialog.h>
7 
8 #include <qt/bitcoinunits.h>
9 #include <qt/guiconstants.h>
10 #include <qt/guiutil.h>
11 #include <qt/optionsmodel.h>
12 #include <qt/walletmodel.h>
13 
14 #include <QClipboard>
15 #include <QDrag>
16 #include <QMenu>
17 #include <QMimeData>
18 #include <QMouseEvent>
19 #include <QPainter>
20 #include <QPixmap>
21 #if QT_VERSION < 0x050000
22 #include <QUrl>
23 #endif
24 
25 #if defined(HAVE_CONFIG_H)
26 #include <config/dash-config.h> /* for USE_QRCODE */
27 #endif
28 
29 #ifdef USE_QRCODE
30 #include <qrencode.h>
31 #endif
32 
34  QLabel(parent), contextMenu(0)
35 {
36  contextMenu = new QMenu(this);
37  QAction *saveImageAction = new QAction(tr("&Save Image..."), this);
38  connect(saveImageAction, SIGNAL(triggered()), this, SLOT(saveImage()));
39  contextMenu->addAction(saveImageAction);
40  QAction *copyImageAction = new QAction(tr("&Copy Image"), this);
41  connect(copyImageAction, SIGNAL(triggered()), this, SLOT(copyImage()));
42  contextMenu->addAction(copyImageAction);
43 }
44 
46 {
47  if(!pixmap())
48  return QImage();
49  return pixmap()->toImage();
50 }
51 
52 void QRGeneralImageWidget::mousePressEvent(QMouseEvent *event)
53 {
54  if(event->button() == Qt::LeftButton && pixmap())
55  {
56  event->accept();
57  QMimeData *mimeData = new QMimeData;
58  mimeData->setImageData(exportImage());
59 
60  QDrag *drag = new QDrag(this);
61  drag->setMimeData(mimeData);
62  drag->exec();
63  } else {
64  QLabel::mousePressEvent(event);
65  }
66 }
67 
69 {
70  if(!pixmap())
71  return;
72  QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Image (*.png)"), nullptr);
73  if (!fn.isEmpty())
74  {
75  exportImage().save(fn);
76  }
77 }
78 
80 {
81  if(!pixmap())
82  return;
83  QApplication::clipboard()->setImage(exportImage());
84 }
85 
86 void QRGeneralImageWidget::contextMenuEvent(QContextMenuEvent *event)
87 {
88  if(!pixmap())
89  return;
90  contextMenu->exec(event->globalPos());
91 }
92 
93 QRDialog::QRDialog(QWidget *parent) :
94  QDialog(parent),
95  ui(new Ui::QRDialog),
96  model(0)
97 {
98  ui->setupUi(this);
99 
100  GUIUtil::setFont({ui->labelQRCodeTitle}, GUIUtil::FontWeight::Bold, 16);
101 
103 
104 #ifndef USE_QRCODE
105  ui->button_saveImage->setVisible(false);
106  ui->lblQRCode->setVisible(false);
107 #endif
108 
109  connect(ui->button_saveImage, SIGNAL(clicked()), ui->lblQRCode, SLOT(saveImage()));
110 }
111 
113 {
114  delete ui;
115 }
116 
118 {
119  this->model = model;
120 
121  if (model)
122  connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(update()));
123 
124  // update the display unit if necessary
125  update();
126 }
127 
128 void QRDialog::setInfo(QString strWindowtitle, QString strQRCode, QString strTextInfo, QString strQRCodeTitle)
129 {
130  this->strWindowtitle = strWindowtitle;
131  this->strQRCode = strQRCode;
132  this->strTextInfo = strTextInfo;
133  this->strQRCodeTitle = strQRCodeTitle;
134  update();
135 }
136 
138 {
139  if(!model)
140  return;
141 
142  setWindowTitle(strWindowtitle);
143  ui->button_saveImage->setEnabled(false);
144  if (strTextInfo.isEmpty()) {
145  ui->outUri->setVisible(false);
146  adjustSize();
147  }
148  ui->outUri->setText(strTextInfo);
149 
150 #ifdef USE_QRCODE
151  // Create QR-code
152  ui->lblQRCode->setText("");
153  if(!strQRCode.isEmpty())
154  {
155  QRcode *code = QRcode_encodeString(strQRCode.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
156  if (!code)
157  {
158  ui->lblQRCode->setText(tr("Error creating QR Code."));
159  return;
160  }
161  ui->lblQRCode->setToolTip(strQRCode);
162  QImage myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
164  unsigned char *p = code->data;
165  for (int y = 0; y < code->width; y++)
166  {
167  for (int x = 0; x < code->width; x++)
168  {
170  p++;
171  }
172  }
173  QRcode_free(code);
174 
175  QImage qrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE, QImage::Format_RGB32);
177  QPainter painter(&qrImage);
178  QRect paddedRect = qrImage.rect().adjusted(1, 1, -1, -1);
180  painter.drawImage(1, 1, myImage.scaled(QR_IMAGE_SIZE - 2, QR_IMAGE_SIZE - 2));
181 
182  ui->labelQRCodeTitle->setText(strQRCodeTitle);
183  ui->lblQRCode->setPixmap(QPixmap::fromImage(qrImage));
184  ui->button_saveImage->setEnabled(true);
185  }
186 #endif
187 }
void setInfo(QString strWindowtitle, QString strQRCode, QString strTextInfo, QString strQRCodeTitle)
Definition: qrdialog.cpp:128
#define QR_IMAGE_SIZE
Definition: guiconstants.h:39
void setFont(const std::vector< QWidget *> &vecWidgets, FontWeight weight, int nPointSize, bool fItalic)
Workaround to set correct font styles in all themes since there is a bug in macOS which leads to issu...
Definition: guiutil.cpp:1552
QColor getThemedQColor(ThemedColor color)
Helper to get colors for various themes which can&#39;t be applied via css for some reason.
Definition: guiutil.cpp:204
void setModel(OptionsModel *model)
Definition: qrdialog.cpp:117
QImage exportImage()
Definition: qrdialog.cpp:45
QString strTextInfo
Definition: qrdialog.h:66
QString strQRCode
Definition: qrdialog.h:65
virtual void mousePressEvent(QMouseEvent *event)
Definition: qrdialog.cpp:52
void updateFonts()
Update the font of all widgets where a custom font has been set with GUIUtil::setFont.
Definition: guiutil.cpp:1563
QRGeneralImageWidget(QWidget *parent=0)
Definition: qrdialog.cpp:33
QString strWindowtitle
Definition: qrdialog.h:64
OptionsModel * model
Definition: qrdialog.h:63
void update()
Definition: qrdialog.cpp:137
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:25
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:521
virtual void contextMenuEvent(QContextMenuEvent *event)
Definition: qrdialog.cpp:86
QString strQRCodeTitle
Definition: qrdialog.h:67
QRDialog(QWidget *parent=0)
Definition: qrdialog.cpp:93
Ui::QRDialog * ui
Definition: qrdialog.h:62
QMenu * contextMenu
Definition: qrdialog.h:44
Released under the MIT license