diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2013-10-18 13:08:30 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2013-10-23 09:15:25 +0200 |
commit | 82095923bb71a670aa67ad15987bb6d4d7726abe (patch) | |
tree | 1bd9b4747659277bb365d8d56008fbed9ead0945 /src | |
parent | 864a6f790ee80a4acc56189cdc1897c9a3ed6998 (diff) |
qt: allow dragging, copying and saving QR code
Add context menu and drag handling to QR code widget.
Diffstat (limited to 'src')
-rw-r--r-- | src/qt/forms/receiverequestdialog.ui | 7 | ||||
-rw-r--r-- | src/qt/receiverequestdialog.cpp | 59 | ||||
-rw-r--r-- | 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 7430b34d0c..4f53dc19b8 100644 --- a/src/qt/forms/receiverequestdialog.ui +++ b/src/qt/forms/receiverequestdialog.ui @@ -15,7 +15,7 @@ </property> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> - <widget class="QLabel" name="lblQRCode"> + <widget class="QRImageWidget" name="lblQRCode"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <horstretch>0</horstretch> @@ -233,6 +233,11 @@ <extends>QWidget</extends> <header>bitcoinamountfield.h</header> </customwidget> + <customwidget> + <class>QRImageWidget</class> + <extends>QLabel</extends> + <header>receiverequestdialog.h</header> + </customwidget> </customwidgets> <resources/> <connections/> diff --git a/src/qt/receiverequestdialog.cpp b/src/qt/receiverequestdialog.cpp index b40cda9b32..dd4f5f16f4 100644 --- a/src/qt/receiverequestdialog.cpp +++ b/src/qt/receiverequestdialog.cpp @@ -7,6 +7,7 @@ #include "optionsmodel.h" #include <QPixmap> +#include <QClipboard> #if QT_VERSION < 0x050000 #include <QUrl> #endif @@ -17,6 +18,51 @@ #include <qrencode.h> #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 37a3fa22d6..e560a24ff1 100644 --- a/src/qt/receiverequestdialog.h +++ b/src/qt/receiverequestdialog.h @@ -3,12 +3,32 @@ #include <QDialog> #include <QImage> +#include <QLabel> 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(); |