Raven Core  3.0.0
P2P Digital Currency
receiverequestdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "receiverequestdialog.h"
7 #include "ui_receiverequestdialog.h"
8 
9 #include "ravenunits.h"
10 #include "guiconstants.h"
11 #include "guiutil.h"
12 #include "optionsmodel.h"
13 #include "walletmodel.h"
14 
15 #include <QClipboard>
16 #include <QDrag>
17 #include <QMenu>
18 #include <QMimeData>
19 #include <QMouseEvent>
20 #include <QPixmap>
21 #if QT_VERSION < 0x050000
22 #include <QUrl>
23 #endif
24 
25 #if defined(HAVE_CONFIG_H)
26 #include "config/raven-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 QRImageWidget::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 QRImageWidget::contextMenuEvent(QContextMenuEvent *event)
87 {
88  if(!pixmap())
89  return;
90  contextMenu->exec(event->globalPos());
91 }
92 
94  QDialog(parent),
95  ui(new Ui::ReceiveRequestDialog),
96  model(0)
97 {
98  ui->setupUi(this);
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  ui->outUri->setFont(GUIUtil::getSubLabelFont());
108  ui->btnCopyURI->setFont(GUIUtil::getSubLabelFont());
109  ui->btnCopyAddress->setFont(GUIUtil::getSubLabelFont());
110  ui->btnSaveAs->setFont(GUIUtil::getSubLabelFont());
111  ui->buttonBox->setFont(GUIUtil::getSubLabelFont());
112 }
113 
115 {
116  delete ui;
117 }
118 
120 {
121  this->model = _model;
122 
123  if (_model)
124  connect(_model, SIGNAL(displayUnitChanged(int)), this, SLOT(update()));
125 
126  // update the display unit if necessary
127  update();
128 }
129 
131 {
132  this->info = _info;
133  update();
134 }
135 
137 {
138  if(!model)
139  return;
140  QString target = info.label;
141  if(target.isEmpty())
142  target = info.address;
143  setWindowTitle(tr("Request payment to %1").arg(target));
144 
145  QString uri = GUIUtil::formatRavenURI(info);
146  ui->btnSaveAs->setEnabled(false);
147  QString html;
148  html += "<html><font face='verdana, arial, helvetica, sans-serif'>";
149  html += "<b>"+tr("Payment information")+"</b><br>";
150  html += "<b>"+tr("URI")+"</b>: ";
151  html += "<a href=\""+uri+"\">" + GUIUtil::HtmlEscape(uri) + "</a><br>";
152  html += "<b>"+tr("Address")+"</b>: " + GUIUtil::HtmlEscape(info.address) + "<br>";
153  if(info.amount)
154  html += "<b>"+tr("Amount")+"</b>: " + RavenUnits::formatHtmlWithUnit(model->getDisplayUnit(), info.amount) + "<br>";
155  if(!info.label.isEmpty())
156  html += "<b>"+tr("Label")+"</b>: " + GUIUtil::HtmlEscape(info.label) + "<br>";
157  if(!info.message.isEmpty())
158  html += "<b>"+tr("Message")+"</b>: " + GUIUtil::HtmlEscape(info.message) + "<br>";
159  ui->outUri->setText(html);
160 
161 #ifdef USE_QRCODE
162  ui->lblQRCode->setText("");
163  if(!uri.isEmpty())
164  {
165  // limit URI length
166  if (uri.length() > MAX_URI_LENGTH)
167  {
168  ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
169  } else {
170  QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
171  if (!code)
172  {
173  ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
174  return;
175  }
176  QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
177  qrImage.fill(0xffffff);
178  unsigned char *p = code->data;
179  for (int y = 0; y < code->width; y++)
180  {
181  for (int x = 0; x < code->width; x++)
182  {
183  qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
184  p++;
185  }
186  }
187  QRcode_free(code);
188 
189  QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE+20, QImage::Format_RGB32);
190  qrAddrImage.fill(0xffffff);
191  QPainter painter(&qrAddrImage);
192  painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
193  QFont font = GUIUtil::fixedPitchFont();
194  font.setPixelSize(12);
195  painter.setFont(font);
196  QRect paddedRect = qrAddrImage.rect();
197  paddedRect.setHeight(QR_IMAGE_SIZE+12);
198  painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, info.address);
199  painter.end();
200 
201  ui->lblQRCode->setPixmap(QPixmap::fromImage(qrAddrImage));
202  ui->btnSaveAs->setEnabled(true);
203  }
204  }
205 #endif
206 }
207 
209 {
211 }
212 
214 {
216 }
QFont fixedPitchFont()
Definition: guiutil.cpp:168
QRImageWidget(QWidget *parent=0)
#define QR_IMAGE_SIZE
Definition: guiconstants.h:108
Ui::ReceiveRequestDialog * ui
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:336
SendCoinsRecipient info
int getDisplayUnit() const
Definition: optionsmodel.h:68
QFont getSubLabelFont()
Definition: guiutil.cpp:86
ReceiveRequestDialog(QWidget *parent=0)
void setClipboard(const QString &str)
Definition: guiutil.cpp:945
void setInfo(const SendCoinsRecipient &info)
QString formatRavenURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:300
virtual void contextMenuEvent(QContextMenuEvent *event)
Interface from Qt to configuration data structure for Raven client.
Definition: optionsmodel.h:23
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:375
static QString formatHtmlWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as HTML string (with unit)
Definition: ravenunits.cpp:156
virtual void mousePressEvent(QMouseEvent *event)
void setModel(OptionsModel *model)