You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

receiverequestdialog.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "receiverequestdialog.h"
  5. #include "ui_receiverequestdialog.h"
  6. #include "starwelsunits.h"
  7. #include "guiconstants.h"
  8. #include "guiutil.h"
  9. #include "optionsmodel.h"
  10. #include "walletmodel.h"
  11. #include <QClipboard>
  12. #include <QDrag>
  13. #include <QMenu>
  14. #include <QMimeData>
  15. #include <QMouseEvent>
  16. #include <QPixmap>
  17. #if QT_VERSION < 0x050000
  18. #include <QUrl>
  19. #endif
  20. #if defined(HAVE_CONFIG_H)
  21. #include "config/starwels-config.h" /* for USE_QRCODE */
  22. #endif
  23. #ifdef USE_QRCODE
  24. #include <qrencode.h>
  25. #endif
  26. QRImageWidget::QRImageWidget(QWidget *parent):
  27. QLabel(parent), contextMenu(0)
  28. {
  29. contextMenu = new QMenu(this);
  30. QAction *saveImageAction = new QAction(tr("&Save Image..."), this);
  31. connect(saveImageAction, SIGNAL(triggered()), this, SLOT(saveImage()));
  32. contextMenu->addAction(saveImageAction);
  33. QAction *copyImageAction = new QAction(tr("&Copy Image"), this);
  34. connect(copyImageAction, SIGNAL(triggered()), this, SLOT(copyImage()));
  35. contextMenu->addAction(copyImageAction);
  36. }
  37. QImage QRImageWidget::exportImage()
  38. {
  39. if(!pixmap())
  40. return QImage();
  41. return pixmap()->toImage();
  42. }
  43. void QRImageWidget::mousePressEvent(QMouseEvent *event)
  44. {
  45. if(event->button() == Qt::LeftButton && pixmap())
  46. {
  47. event->accept();
  48. QMimeData *mimeData = new QMimeData;
  49. mimeData->setImageData(exportImage());
  50. QDrag *drag = new QDrag(this);
  51. drag->setMimeData(mimeData);
  52. drag->exec();
  53. } else {
  54. QLabel::mousePressEvent(event);
  55. }
  56. }
  57. void QRImageWidget::saveImage()
  58. {
  59. if(!pixmap())
  60. return;
  61. QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Image (*.png)"), nullptr);
  62. if (!fn.isEmpty())
  63. {
  64. exportImage().save(fn);
  65. }
  66. }
  67. void QRImageWidget::copyImage()
  68. {
  69. if(!pixmap())
  70. return;
  71. QApplication::clipboard()->setImage(exportImage());
  72. }
  73. void QRImageWidget::contextMenuEvent(QContextMenuEvent *event)
  74. {
  75. if(!pixmap())
  76. return;
  77. contextMenu->exec(event->globalPos());
  78. }
  79. ReceiveRequestDialog::ReceiveRequestDialog(QWidget *parent) :
  80. QDialog(parent),
  81. ui(new Ui::ReceiveRequestDialog),
  82. model(0)
  83. {
  84. ui->setupUi(this);
  85. #ifndef USE_QRCODE
  86. ui->btnSaveAs->setVisible(false);
  87. ui->lblQRCode->setVisible(false);
  88. #endif
  89. connect(ui->btnSaveAs, SIGNAL(clicked()), ui->lblQRCode, SLOT(saveImage()));
  90. }
  91. ReceiveRequestDialog::~ReceiveRequestDialog()
  92. {
  93. delete ui;
  94. }
  95. void ReceiveRequestDialog::setModel(OptionsModel *_model)
  96. {
  97. this->model = _model;
  98. if (_model)
  99. connect(_model, SIGNAL(displayUnitChanged(int)), this, SLOT(update()));
  100. // update the display unit if necessary
  101. update();
  102. }
  103. void ReceiveRequestDialog::setInfo(const SendCoinsRecipient &_info)
  104. {
  105. this->info = _info;
  106. update();
  107. }
  108. void ReceiveRequestDialog::update()
  109. {
  110. if(!model)
  111. return;
  112. QString target = info.label;
  113. if(target.isEmpty())
  114. target = info.address;
  115. setWindowTitle(tr("Request payment to %1").arg(target));
  116. QString uri = GUIUtil::formatStarwelsURI(info);
  117. ui->btnSaveAs->setEnabled(false);
  118. QString html;
  119. html += "<html><font face='verdana, arial, helvetica, sans-serif'>";
  120. html += "<b>"+tr("Payment information")+"</b><br>";
  121. html += "<b>"+tr("URI")+"</b>: ";
  122. html += "<a href=\""+uri+"\">" + GUIUtil::HtmlEscape(uri) + "</a><br>";
  123. html += "<b>"+tr("Address")+"</b>: " + GUIUtil::HtmlEscape(info.address) + "<br>";
  124. if(info.amount)
  125. html += "<b>"+tr("Amount")+"</b>: " + StarwelsUnits::formatHtmlWithUnit(model->getDisplayUnit(), info.amount) + "<br>";
  126. if(!info.label.isEmpty())
  127. html += "<b>"+tr("Label")+"</b>: " + GUIUtil::HtmlEscape(info.label) + "<br>";
  128. if(!info.message.isEmpty())
  129. html += "<b>"+tr("Message")+"</b>: " + GUIUtil::HtmlEscape(info.message) + "<br>";
  130. ui->outUri->setText(html);
  131. #ifdef USE_QRCODE
  132. ui->lblQRCode->setText("");
  133. if(!uri.isEmpty())
  134. {
  135. // limit URI length
  136. if (uri.length() > MAX_URI_LENGTH)
  137. {
  138. ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
  139. } else {
  140. QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
  141. if (!code)
  142. {
  143. ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
  144. return;
  145. }
  146. QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
  147. qrImage.fill(0xffffff);
  148. unsigned char *p = code->data;
  149. for (int y = 0; y < code->width; y++)
  150. {
  151. for (int x = 0; x < code->width; x++)
  152. {
  153. qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
  154. p++;
  155. }
  156. }
  157. QRcode_free(code);
  158. QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE+20, QImage::Format_RGB32);
  159. qrAddrImage.fill(0xffffff);
  160. QPainter painter(&qrAddrImage);
  161. painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
  162. QFont font = GUIUtil::fixedPitchFont();
  163. font.setPixelSize(12);
  164. painter.setFont(font);
  165. QRect paddedRect = qrAddrImage.rect();
  166. paddedRect.setHeight(QR_IMAGE_SIZE+12);
  167. painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, info.address);
  168. painter.end();
  169. ui->lblQRCode->setPixmap(QPixmap::fromImage(qrAddrImage));
  170. ui->btnSaveAs->setEnabled(true);
  171. }
  172. }
  173. #endif
  174. }
  175. void ReceiveRequestDialog::on_btnCopyURI_clicked()
  176. {
  177. GUIUtil::setClipboard(GUIUtil::formatStarwelsURI(info));
  178. }
  179. void ReceiveRequestDialog::on_btnCopyAddress_clicked()
  180. {
  181. GUIUtil::setClipboard(info.address);
  182. }