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