Fabcoin Core  0.16.2
P2P Digital Currency
receiverequestdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 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 <receiverequestdialog.h>
7 
8 #include <fabcoinunits.h>
9 #include <guiconstants.h>
10 #include <guiutil.h>
11 #include <optionsmodel.h>
12 #include <walletmodel.h>
13 #include <styleSheet.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/fabcoin-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  SetObjectStyleSheet(ui->btnCopyURI, StyleSheetNames::ButtonWhite);
101  SetObjectStyleSheet(ui->btnSaveAs, StyleSheetNames::ButtonWhite);
102  SetObjectStyleSheet(ui->btnCopyAddress, StyleSheetNames::ButtonWhite);
103 
104 #ifndef USE_QRCODE
105  ui->btnSaveAs->setVisible(false);
106  ui->lblQRCode->setVisible(false);
107 #endif
108 
109  connect(ui->btnSaveAs, 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 
129 {
130  this->info = _info;
131  update();
132 }
133 
134 bool ReceiveRequestDialog::createQRCode(QLabel *label, SendCoinsRecipient _info, bool showAddress)
135 {
136 #ifdef USE_QRCODE
137  QString uri = GUIUtil::formatFabcoinURI(_info);
138  label->setText("");
139  if(!uri.isEmpty())
140  {
141  // limit URI length
142  if (uri.length() > MAX_URI_LENGTH)
143  {
144  label->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
145  } else {
146  QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
147  if (!code)
148  {
149  label->setText(tr("Error encoding URI into QR Code."));
150  return false;
151  }
152  QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_ARGB32);
153  qrImage.fill(qRgba(0, 0, 0, 0));
154  unsigned char *p = code->data;
155  for (int y = 0; y < code->width; y++)
156  {
157  for (int x = 0; x < code->width; x++)
158  {
159  qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? qRgba(0, 0, 0, 255) : qRgba(255, 255, 255, 255)));
160  p++;
161  }
162  }
163  QRcode_free(code);
164 
165  QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE+20, QImage::Format_ARGB32);
166  qrAddrImage.fill(qRgba(0, 0, 0, 0));
167  QPainter painter(&qrAddrImage);
168  painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
169 
170  if(showAddress)
171  {
172  QFont font = GUIUtil::fixedPitchFont();
173  font.setPixelSize(12);
174  painter.setFont(font);
175  QRect paddedRect = qrAddrImage.rect();
176  paddedRect.setHeight(QR_IMAGE_SIZE+12);
177  painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, _info.address);
178  painter.end();
179  }
180 
181  label->setPixmap(QPixmap::fromImage(qrAddrImage));
182  return true;
183  }
184  }
185 #else
186  Q_UNUSED(label);
187  Q_UNUSED(_info);
188 #endif
189  return false;
190 }
191 
193 {
194  if(!model)
195  return;
196  QString target = info.label;
197  if(target.isEmpty())
198  target = info.address;
199  setWindowTitle(tr("Request payment to %1").arg(target));
200 
201  QString uri = GUIUtil::formatFabcoinURI(info);
202  ui->btnSaveAs->setEnabled(false);
203  QString html;
204  html += "<html><font face='verdana, arial, helvetica, sans-serif'>";
205  html += "<font color='#ffffff'>" + tr("PAYMENT INFORMATION")+"</font><br><br>";
206  html += tr("URI")+": ";
207  html += "<a href=\""+uri+"\">" + GUIUtil::HtmlEscape(uri) + "</a><br>";
208  html += tr("Address")+": <font color='#ffffff'>" + GUIUtil::HtmlEscape(info.address) + "</font><br>";
209  if(info.amount)
210  html += tr("Amount")+": <font color='#ffffff'>" + FabcoinUnits::formatHtmlWithUnit(model->getDisplayUnit(), info.amount) + "</font><br>";
211  if(!info.label.isEmpty())
212  html += tr("Label")+": <font color='#ffffff'>" + GUIUtil::HtmlEscape(info.label) + "</font><br>";
213  if(!info.message.isEmpty())
214  html += tr("Message")+": <font color='#ffffff'>" + GUIUtil::HtmlEscape(info.message) + "</font><br>";
215  ui->outUri->setText(html);
216 
217 #ifdef USE_QRCODE
219  {
220  ui->lblQRCode->setScaledContents(true);
221  ui->btnSaveAs->setEnabled(true);
222  }
223 #endif
224 }
225 
227 {
229 }
230 
232 {
234 }
#define SetObjectStyleSheet(object, name)
Definition: styleSheet.h:10
QFont fixedPitchFont()
Definition: guiutil.cpp:90
QRImageWidget(QWidget *parent=0)
#define QR_IMAGE_SIZE
Definition: guiconstants.h:46
Ui::ReceiveRequestDialog * ui
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:258
SendCoinsRecipient info
bytes code
Definition: SmartVM.cpp:45
void setupUi(QDialog *ReceiveRequestDialog)
#define x(i)
ReceiveRequestDialog(QWidget *parent=0)
void setClipboard(const QString &str)
Definition: guiutil.cpp:858
void setInfo(const SendCoinsRecipient &info)
int getDisplayUnit()
Definition: optionsmodel.h:70
static bool createQRCode(QLabel *label, SendCoinsRecipient info, bool showAddress=false)
QString formatFabcoinURI(const SendCoinsRecipient &info)
Definition: guiutil.cpp:222
virtual void contextMenuEvent(QContextMenuEvent *event)
Interface from Qt to configuration data structure for Fabcoin client.
Definition: optionsmodel.h:22
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:309
virtual void mousePressEvent(QMouseEvent *event)
static QString formatHtmlWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as HTML string (with unit)
void setModel(OptionsModel *model)