Add context menu option to save images

fixes #265
This commit is contained in:
Konstantinos Sideris 2018-03-14 21:31:09 +02:00
parent 1b5e18cb1a
commit 511c58d608
4 changed files with 51 additions and 0 deletions

View file

@ -99,6 +99,8 @@ protected:
private: private:
void init(); void init();
//! Add a context menu option to save the image of the timeline item.
void addSaveImageAction(ImageItem *image);
template<class Widget> template<class Widget>
void setupLocalWidgetLayout(Widget *widget, void setupLocalWidgetLayout(Widget *widget,

View file

@ -48,6 +48,10 @@ public:
QSize sizeHint() const override; QSize sizeHint() const override;
public slots:
//! Show a save as dialog for the image.
void saveAs();
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;

View file

@ -137,6 +137,8 @@ TimelineItem::TimelineItem(ImageItem *image,
init(); init();
setupLocalWidgetLayout<ImageItem>(image, userid, "sent an image", withSender); setupLocalWidgetLayout<ImageItem>(image, userid, "sent an image", withSender);
addSaveImageAction(image);
} }
TimelineItem::TimelineItem(FileItem *file, const QString &userid, bool withSender, QWidget *parent) TimelineItem::TimelineItem(FileItem *file, const QString &userid, bool withSender, QWidget *parent)
@ -177,6 +179,8 @@ TimelineItem::TimelineItem(ImageItem *image,
{ {
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Image>, ImageItem>( setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Image>, ImageItem>(
image, event, " sent an image", with_sender); image, event, " sent an image", with_sender);
addSaveImageAction(image);
} }
TimelineItem::TimelineItem(FileItem *file, TimelineItem::TimelineItem(FileItem *file,
@ -518,3 +522,14 @@ TimelineItem::paintEvent(QPaintEvent *)
QPainter p(this); QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &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);
}
}

View file

@ -18,6 +18,7 @@
#include <QBrush> #include <QBrush>
#include <QDebug> #include <QDebug>
#include <QDesktopServices> #include <QDesktopServices>
#include <QFileDialog>
#include <QFileInfo> #include <QFileInfo>
#include <QPainter> #include <QPainter>
#include <QPixmap> #include <QPixmap>
@ -219,3 +220,32 @@ ImageItem::paintEvent(QPaintEvent *event)
painter.drawText(textRegion_, Qt::AlignVCenter, elidedText); 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();
}
});
}