qt: allow dragging, copying and saving QR code

Add context menu and drag handling to QR code widget.
This commit is contained in:
Wladimir J. van der Laan 2013-10-18 13:08:30 +02:00
parent 864a6f790e
commit 82095923bb
3 changed files with 75 additions and 13 deletions

View file

@ -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/>

View file

@ -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)

View file

@ -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();