From 511c58d608eeec4899cff4d016d4d5d99fbff36f Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Wed, 14 Mar 2018 21:31:09 +0200 Subject: [PATCH] Add context menu option to save images fixes #265 --- include/timeline/TimelineItem.h | 2 ++ include/timeline/widgets/ImageItem.h | 4 ++++ src/timeline/TimelineItem.cc | 15 ++++++++++++++ src/timeline/widgets/ImageItem.cc | 30 ++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/include/timeline/TimelineItem.h b/include/timeline/TimelineItem.h index 69bb89e2..7c04e167 100644 --- a/include/timeline/TimelineItem.h +++ b/include/timeline/TimelineItem.h @@ -99,6 +99,8 @@ protected: private: void init(); + //! Add a context menu option to save the image of the timeline item. + void addSaveImageAction(ImageItem *image); template void setupLocalWidgetLayout(Widget *widget, diff --git a/include/timeline/widgets/ImageItem.h b/include/timeline/widgets/ImageItem.h index 210394bc..d8a12c22 100644 --- a/include/timeline/widgets/ImageItem.h +++ b/include/timeline/widgets/ImageItem.h @@ -48,6 +48,10 @@ public: QSize sizeHint() const override; +public slots: + //! Show a save as dialog for the image. + void saveAs(); + protected: void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *event) override; diff --git a/src/timeline/TimelineItem.cc b/src/timeline/TimelineItem.cc index 1707dd19..2f04a1bd 100644 --- a/src/timeline/TimelineItem.cc +++ b/src/timeline/TimelineItem.cc @@ -137,6 +137,8 @@ TimelineItem::TimelineItem(ImageItem *image, init(); setupLocalWidgetLayout(image, userid, "sent an image", withSender); + + addSaveImageAction(image); } TimelineItem::TimelineItem(FileItem *file, const QString &userid, bool withSender, QWidget *parent) @@ -177,6 +179,8 @@ TimelineItem::TimelineItem(ImageItem *image, { setupWidgetLayout, ImageItem>( image, event, " sent an image", with_sender); + + addSaveImageAction(image); } TimelineItem::TimelineItem(FileItem *file, @@ -518,3 +522,14 @@ TimelineItem::paintEvent(QPaintEvent *) QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); } + +void +TimelineItem::addSaveImageAction(ImageItem *image) +{ + if (contextMenu_) { + auto saveImage = new QAction("Save image", this); + contextMenu_->addAction(saveImage); + + connect(saveImage, &QAction::triggered, image, &ImageItem::saveAs); + } +} diff --git a/src/timeline/widgets/ImageItem.cc b/src/timeline/widgets/ImageItem.cc index 2784f386..d28a4017 100644 --- a/src/timeline/widgets/ImageItem.cc +++ b/src/timeline/widgets/ImageItem.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -219,3 +220,32 @@ ImageItem::paintEvent(QPaintEvent *event) painter.drawText(textRegion_, Qt::AlignVCenter, elidedText); } } + +void +ImageItem::saveAs() +{ + auto filename = QFileDialog::getSaveFileName(this, tr("Save image"), text_); + + if (filename.isEmpty()) + return; + + auto proxy = client_->downloadFile(url_); + connect(proxy, + &DownloadMediaProxy::fileDownloaded, + this, + [proxy, this, filename](const QByteArray &data) { + proxy->deleteLater(); + + try { + QFile file(filename); + + if (!file.open(QIODevice::WriteOnly)) + return; + + file.write(data); + file.close(); + } catch (const std::exception &ex) { + qDebug() << "Error while saving file to:" << ex.what(); + } + }); +}