Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

receiverequestdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 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 
6 #include <qt/forms/ui_receiverequestdialog.h>
7 
8 #include <qt/bitcoinunits.h>
9 #include <qt/guiconstants.h>
10 #include <qt/guiutil.h>
11 #include <qt/optionsmodel.h>
12 
13 #include <QClipboard>
14 #include <QDrag>
15 #include <QMenu>
16 #include <QMimeData>
17 #include <QMouseEvent>
18 #include <QPixmap>
19 #if QT_VERSION < 0x050000
20 #include <QUrl>
21 #endif
22 
23 #if defined(HAVE_CONFIG_H)
24 #include <config/dash-config.h> /* for USE_QRCODE */
25 #endif
26 
27 #ifdef USE_QRCODE
28 #include <qrencode.h>
29 #endif
30 
32  QLabel(parent), contextMenu(0)
33 {
34  contextMenu = new QMenu(this);
35  QAction *saveImageAction = new QAction(tr("&Save Image..."), this);
36  connect(saveImageAction, SIGNAL(triggered()), this, SLOT(saveImage()));
37  contextMenu->addAction(saveImageAction);
38  QAction *copyImageAction = new QAction(tr("&Copy Image"), this);
39  connect(copyImageAction, SIGNAL(triggered()), this, SLOT(copyImage()));
40  contextMenu->addAction(copyImageAction);
41 }
42 
44 {
45  if(!pixmap())
46  return QImage();
47  return pixmap()->toImage();
48 }
49 
50 void QRImageWidget::mousePressEvent(QMouseEvent *event)
51 {
52  if(event->button() == Qt::LeftButton && pixmap())
53  {
54  event->accept();
55  QMimeData *mimeData = new QMimeData;
56  mimeData->setImageData(exportImage());
57 
58  QDrag *drag = new QDrag(this);
59  drag->setMimeData(mimeData);
60  drag->exec();
61  } else {
62  QLabel::mousePressEvent(event);
63  }
64 }
65 
67 {
68  if(!pixmap())
69  return;
70  QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Image (*.png)"), nullptr);
71  if (!fn.isEmpty())
72  {
73  exportImage().save(fn);
74  }
75 }
76 
78 {
79  if(!pixmap())
80  return;
81  QApplication::clipboard()->setImage(exportImage());
82 }
83 
84 void QRImageWidget::contextMenuEvent(QContextMenuEvent *event)
85 {
86  if(!pixmap())
87  return;
88  contextMenu->exec(event->globalPos());
89 }
90 
92  QDialog(parent),
93  ui(new Ui::ReceiveRequestDialog),
94  model(0)
95 {
96  ui->setupUi(this);
97 
99 
100 #ifndef USE_QRCODE
101  ui->btnSaveAs->setVisible(false);
102  ui->lblQRCode->setVisible(false);
103 #endif
104 
105  connect(ui->btnSaveAs, SIGNAL(clicked()), ui->lblQRCode, SLOT(saveImage()));
106 }
107 
109 {
110  delete ui;
111 }
112 
114 {
115  this->model = _model;
116 
117  if (_model)
118  connect(_model, SIGNAL(displayUnitChanged(int)), this, SLOT(update()));
119 
120  // update the display unit if necessary
121  update();
122 }
123 
125 {
126  this->info = _info;
127  update();
128 }
129 
131 {
132  if(!model)
133  return;
134  QString target = info.label;
135  if(target.isEmpty())
136  target = info.address;
137  setWindowTitle(tr("Request payment to %1").arg(target));
138 
139  QString uri = GUIUtil::formatBitcoinURI(info);
140  ui->btnSaveAs->setEnabled(false);
141  QString html;
142  html += "<html>";
143  html += "<b>" + tr("Payment information") + "</b><br><br>";
144  html += "<b>"+tr("URI")+"</b>: ";
145  html += QString("<a style=\"%1\"href=\"").arg(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_COMMAND)) +
146  uri + "\">" + GUIUtil::HtmlEscape(uri) + "</a><br>";
147  html += "<b>"+tr("Address")+"</b>: " + GUIUtil::HtmlEscape(info.address) + "<br>";
148  if(info.amount)
149  html += "<b>"+tr("Amount")+"</b>: " + BitcoinUnits::formatHtmlWithUnit(model->getDisplayUnit(), info.amount) + "<br>";
150  if(!info.label.isEmpty())
151  html += "<b>"+tr("Label")+"</b>: " + GUIUtil::HtmlEscape(info.label) + "<br>";
152  if(!info.message.isEmpty())
153  html += "<b>"+tr("Message")+"</b>: " + GUIUtil::HtmlEscape(info.message) + "<br>";
154  ui->outUri->setText(html);
155 
156 #ifdef USE_QRCODE
157  ui->lblQRCode->setText("");
158  if(!uri.isEmpty())
159  {
160  // limit URI length
161  if (uri.length() > MAX_URI_LENGTH)
162  {
163  ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
164  } else {
165  QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
166  if (!code)
167  {
168  ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
169  return;
170  }
171  QImage qrImage = QImage(code->width + 6, code->width + 6, QImage::Format_RGB32);
173  unsigned char *p = code->data;
174  for (int y = 0; y < code->width; y++)
175  {
176  for (int x = 0; x < code->width; x++)
177  {
179  p++;
180  }
181  }
182  QRcode_free(code);
183  // Create the image with respect to the device pixel ratio
184  int qrAddrImageWidth = QR_IMAGE_SIZE;
185  int qrAddrImageHeight = QR_IMAGE_SIZE + 20;
186  qreal scale = qApp->devicePixelRatio();
187  QImage qrAddrImage = QImage(qrAddrImageWidth * scale, qrAddrImageHeight * scale, QImage::Format_RGB32);
188  qrAddrImage.setDevicePixelRatio(scale);
189  QPainter painter(&qrAddrImage);
190  // Fill the whole image with border color
192  // Create a 2px/2px smaller rect and fill it with background color to keep the 1px border with the border color
193  QRect paddedRect = QRect(1, 1, qrAddrImageWidth - 2, qrAddrImageHeight - 2);
195  painter.drawImage(2, 2, qrImage.scaled(QR_IMAGE_SIZE - 4, QR_IMAGE_SIZE - 4));
196  // calculate ideal font size
197  QFont font = GUIUtil::getFontNormal();
198  qreal font_size = GUIUtil::calculateIdealFontSize((paddedRect.width() - 20), info.address, font);
199  font.setPointSizeF(font_size);
200  // paint the address
201  painter.setFont(font);
203  paddedRect.setHeight(QR_IMAGE_SIZE + 3);
204  painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, info.address);
205  painter.end();
206 
207  ui->lblQRCode->setPixmap(QPixmap::fromImage(qrAddrImage));
208  ui->btnSaveAs->setEnabled(true);
209  }
210  }
211 #endif
212 }
213 
215 {
217 }
218 
220 {
222 }
QRImageWidget(QWidget *parent=0)
#define QR_IMAGE_SIZE
Definition: guiconstants.h:39
static QString formatHtmlWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as HTML string (with unit)
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
QFont getFontNormal()
Get the default normal QFont.
Definition: guiutil.cpp:1735
Ui::ReceiveRequestDialog * ui
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:481
SendCoinsRecipient info
QString formatBitcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:445
int getDisplayUnit() const
Definition: optionsmodel.h:82
virtual void contextMenuEvent(QContextMenuEvent *event) override
void updateFonts()
Update the font of all widgets where a custom font has been set with GUIUtil::setFont.
Definition: guiutil.cpp:1563
ReceiveRequestDialog(QWidget *parent=0)
qreal calculateIdealFontSize(int width, const QString &text, QFont font, qreal minPointSize, qreal font_size)
Definition: guiutil.cpp:1947
void setClipboard(const QString &str)
Definition: guiutil.cpp:1817
void setInfo(const SendCoinsRecipient &info)
virtual void mousePressEvent(QMouseEvent *event) override
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
QString getThemedStyleQString(ThemedStyle style)
Helper to get css style strings which are injected into rich text through qt.
Definition: guiutil.cpp:210
void setModel(OptionsModel *model)
static const int MAX_URI_LENGTH
Definition: guiconstants.h:36
Released under the MIT license