Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Add context menu option to save images
Browse files Browse the repository at this point in the history
fixes #265
  • Loading branch information
mujx committed Mar 14, 2018
1 parent 1b5e18c commit 511c58d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/timeline/TimelineItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class TimelineItem : public QWidget

private:
void init();
//! Add a context menu option to save the image of the timeline item.
void addSaveImageAction(ImageItem *image);

template<class Widget>
void setupLocalWidgetLayout(Widget *widget,
Expand Down
4 changes: 4 additions & 0 deletions include/timeline/widgets/ImageItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class ImageItem : public QWidget

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;
Expand Down
15 changes: 15 additions & 0 deletions src/timeline/TimelineItem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ TimelineItem::TimelineItem(ImageItem *image,
init();

setupLocalWidgetLayout<ImageItem>(image, userid, "sent an image", withSender);

addSaveImageAction(image);
}

TimelineItem::TimelineItem(FileItem *file, const QString &userid, bool withSender, QWidget *parent)
Expand Down Expand Up @@ -177,6 +179,8 @@ TimelineItem::TimelineItem(ImageItem *image,
{
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Image>, ImageItem>(
image, event, " sent an image", with_sender);

addSaveImageAction(image);
}

TimelineItem::TimelineItem(FileItem *file,
Expand Down Expand Up @@ -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);
}
}
30 changes: 30 additions & 0 deletions src/timeline/widgets/ImageItem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QBrush>
#include <QDebug>
#include <QDesktopServices>
#include <QFileDialog>
#include <QFileInfo>
#include <QPainter>
#include <QPixmap>
Expand Down Expand Up @@ -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();
}
});
}

0 comments on commit 511c58d

Please sign in to comment.