From 82095923bb71a670aa67ad15987bb6d4d7726abe Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Fri, 18 Oct 2013 13:08:30 +0200 Subject: [PATCH] qt: allow dragging, copying and saving QR code Add context menu and drag handling to QR code widget. --- src/qt/forms/receiverequestdialog.ui | 7 +++- src/qt/receiverequestdialog.cpp | 59 +++++++++++++++++++++++----- src/qt/receiverequestdialog.h | 22 ++++++++++- 3 files changed, 75 insertions(+), 13 deletions(-) diff --git a/src/qt/forms/receiverequestdialog.ui b/src/qt/forms/receiverequestdialog.ui index 7430b34d0..4f53dc19b 100644 --- a/src/qt/forms/receiverequestdialog.ui +++ b/src/qt/forms/receiverequestdialog.ui @@ -15,7 +15,7 @@ - + 0 @@ -233,6 +233,11 @@ QWidget
bitcoinamountfield.h
+ + QRImageWidget + QLabel +
receiverequestdialog.h
+
diff --git a/src/qt/receiverequestdialog.cpp b/src/qt/receiverequestdialog.cpp index b40cda9b3..dd4f5f16f 100644 --- a/src/qt/receiverequestdialog.cpp +++ b/src/qt/receiverequestdialog.cpp @@ -7,6 +7,7 @@ #include "optionsmodel.h" #include +#include #if QT_VERSION < 0x050000 #include #endif @@ -17,6 +18,51 @@ #include #endif +QRImageWidget::QRImageWidget(QWidget *parent): + QLabel(parent) +{ + setContextMenuPolicy(Qt::ActionsContextMenu); + + QAction *saveImageAction = new QAction(tr("&Save Image..."), this); + connect(saveImageAction, SIGNAL(triggered()), this, SLOT(saveImage())); + addAction(saveImageAction); + QAction *copyImageAction = new QAction(tr("&Copy Image"), this); + connect(copyImageAction, SIGNAL(triggered()), this, SLOT(copyImage())); + addAction(copyImageAction); +} + +QImage QRImageWidget::exportImage() +{ + return pixmap()->toImage().scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE); +} + +void QRImageWidget::mousePressEvent(QMouseEvent *event) +{ + if(event->button() == Qt::LeftButton) + { + QMimeData *mimeData = new QMimeData; + mimeData->setImageData(exportImage()); + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->exec(); + } +} + +void QRImageWidget::saveImage() +{ + QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Images (*.png)")); + if (!fn.isEmpty()) + { + exportImage().save(fn); + } +} + +void QRImageWidget::copyImage() +{ + QApplication::clipboard()->setImage(exportImage()); +} + ReceiveRequestDialog::ReceiveRequestDialog(const QString &addr, const QString &label, quint64 amount, const QString &message, QWidget *parent) : QDialog(parent), ui(new Ui::ReceiveRequestDialog), @@ -42,6 +88,8 @@ ReceiveRequestDialog::ReceiveRequestDialog(const QString &addr, const QString &l ui->lblQRCode->setVisible(false); #endif + connect(ui->btnSaveAs, SIGNAL(clicked()), ui->lblQRCode, SLOT(saveImage())); + genCode(); } @@ -77,7 +125,7 @@ void ReceiveRequestDialog::genCode() ui->lblQRCode->setText(tr("Error encoding URI into QR Code.")); return; } - myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32); + QImage myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32); myImage.fill(0xffffff); unsigned char *p = code->data; for (int y = 0; y < code->width; y++) @@ -147,15 +195,6 @@ void ReceiveRequestDialog::on_lnMessage_textChanged() genCode(); } -void ReceiveRequestDialog::on_btnSaveAs_clicked() -{ -#ifdef USE_QRCODE - QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Images (*.png)")); - if (!fn.isEmpty()) - myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn); -#endif -} - void ReceiveRequestDialog::updateDisplayUnit() { if (model) diff --git a/src/qt/receiverequestdialog.h b/src/qt/receiverequestdialog.h index 37a3fa22d..e560a24ff 100644 --- a/src/qt/receiverequestdialog.h +++ b/src/qt/receiverequestdialog.h @@ -3,12 +3,32 @@ #include #include +#include namespace Ui { class ReceiveRequestDialog; } class OptionsModel; +/* Label widget for QR code. This image can be dragged, dropped, copied and saved + * to disk. + */ +class QRImageWidget : public QLabel +{ + Q_OBJECT + +public: + explicit QRImageWidget(QWidget *parent = 0); + QImage exportImage(); + +public slots: + void saveImage(); + void copyImage(); + +protected: + virtual void mousePressEvent(QMouseEvent *event); +}; + class ReceiveRequestDialog : public QDialog { Q_OBJECT @@ -23,7 +43,6 @@ private slots: void on_lnReqAmount_textChanged(); void on_lnLabel_textChanged(); void on_lnMessage_textChanged(); - void on_btnSaveAs_clicked(); void updateDisplayUnit(); @@ -31,7 +50,6 @@ private: Ui::ReceiveRequestDialog *ui; OptionsModel *model; QString address; - QImage myImage; void genCode(); QString getURI();