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