From 8422d3fc8c6fecaa4cacfd98ba448342c7a6e844 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Sun, 14 May 2023 22:19:16 +0200 Subject: [PATCH] Added copy and save of image to AspectRatioPixmapLabel --- .../src/util/AspectRatioPixmapLabel.cpp | 58 ++++++++++++++++++- .../src/util/AspectRatioPixmapLabel.h | 16 +++++ retroshare-gui/src/util/imageutil.cpp | 28 ++++++--- retroshare-gui/src/util/imageutil.h | 1 + 4 files changed, 95 insertions(+), 8 deletions(-) diff --git a/retroshare-gui/src/util/AspectRatioPixmapLabel.cpp b/retroshare-gui/src/util/AspectRatioPixmapLabel.cpp index 08ecf18c6..0ff9d851b 100644 --- a/retroshare-gui/src/util/AspectRatioPixmapLabel.cpp +++ b/retroshare-gui/src/util/AspectRatioPixmapLabel.cpp @@ -18,8 +18,14 @@ * * *******************************************************************************/ +#include +#include +#include + #include "AspectRatioPixmapLabel.h" #include +#include "util/imageutil.h" +#include "gui/common/FilesDefs.h" AspectRatioPixmapLabel::AspectRatioPixmapLabel(QWidget *parent) : QLabel(parent) @@ -31,7 +37,7 @@ AspectRatioPixmapLabel::AspectRatioPixmapLabel(QWidget *parent) : void AspectRatioPixmapLabel::setPixmap ( const QPixmap & p) { pix = p; - QLabel::setPixmap(pix); + QLabel::setPixmap(scaledPixmap()); //std::cout << "Information size: " << pix.width() << 'x' << pix.height() << std::endl; } @@ -57,3 +63,53 @@ void AspectRatioPixmapLabel::resizeEvent(QResizeEvent * e) QLabel::resizeEvent(e); //std::cout << "Information resized: " << e->oldSize().width() << 'x' << e->oldSize().height() << " to " << e->size().width() << 'x' << e->size().height() << std::endl; } + +void AspectRatioPixmapLabel::addContextMenuAction(QAction *action) +{ + mContextMenuActions.push_back(action); +} + +void AspectRatioPixmapLabel::contextMenuEvent(QContextMenuEvent *event) +{ + emit calculateContextMenuActions(); + + QMenu *contextMenu = new QMenu(); + + QAction *actionSaveImage = contextMenu->addAction(FilesDefs::getIconFromQtResourcePath(":/images/document_save.png"), tr("Save image"), this, SLOT(saveImage())); + QAction *actionCopyImage = contextMenu->addAction(tr("Copy image"), this, SLOT(copyImage())); + + if (pix.isNull()) { + actionSaveImage->setEnabled(false); + actionCopyImage->setEnabled(false); + } else { + actionSaveImage->setEnabled(true); + actionCopyImage->setEnabled(true); + } + + QList::iterator it; + for (it = mContextMenuActions.begin(); it != mContextMenuActions.end(); ++it) { + contextMenu->addAction(*it); + } + + contextMenu->exec(event->globalPos()); + + delete(contextMenu); +} + +void AspectRatioPixmapLabel::copyImage() +{ + if (pix.isNull()) { + return; + } + + QApplication::clipboard()->setPixmap(pix, QClipboard::Clipboard); +} + +void AspectRatioPixmapLabel::saveImage() +{ + if (pix.isNull()) { + return; + } + + ImageUtil::saveImage(window(), pix.toImage()); +} diff --git a/retroshare-gui/src/util/AspectRatioPixmapLabel.h b/retroshare-gui/src/util/AspectRatioPixmapLabel.h index ad7c40e4c..be6e79077 100644 --- a/retroshare-gui/src/util/AspectRatioPixmapLabel.h +++ b/retroshare-gui/src/util/AspectRatioPixmapLabel.h @@ -28,17 +28,33 @@ class AspectRatioPixmapLabel : public QLabel { Q_OBJECT + public: explicit AspectRatioPixmapLabel(QWidget *parent = nullptr); virtual int heightForWidth( int width ) const override; virtual QSize sizeHint() const override; QPixmap scaledPixmap() const; + + // Add QAction to context menu (action won't be deleted) + void addContextMenuAction(QAction *action); + +Q_SIGNALS: + void calculateContextMenuActions(); + public slots: void setPixmap ( const QPixmap & ); + protected: void resizeEvent(QResizeEvent *event) override; + virtual void contextMenuEvent(QContextMenuEvent *event); + +private slots: + void copyImage(); + void saveImage(); + private: QPixmap pix; + QList mContextMenuActions; }; #endif // ASPECTRATIOPIXMAPLABEL_H diff --git a/retroshare-gui/src/util/imageutil.cpp b/retroshare-gui/src/util/imageutil.cpp index 3368db309..612387724 100644 --- a/retroshare-gui/src/util/imageutil.cpp +++ b/retroshare-gui/src/util/imageutil.cpp @@ -115,13 +115,7 @@ void ImageUtil::extractImage(QWidget *window, QTextCursor cursor, QString file) if(!image.isNull()) { success = true; - if(!file.isEmpty() || misc::getSaveFileName(window, RshareSettings::LASTDIR_IMAGES, "Save Picture File", "Pictures (*.png *.xpm *.jpg)", file)) - { - if(!image.save(file, nullptr, 100)) - if(!image.save(file + ".png", nullptr, 100)) - QMessageBox::warning(window, QApplication::translate("ImageUtil", "Save image"), QApplication::translate("ImageUtil", "Cannot save the image, invalid filename") - + "\n" + file); - } + saveImage(window, image, file); } } if(!success) @@ -130,6 +124,26 @@ void ImageUtil::extractImage(QWidget *window, QTextCursor cursor, QString file) } } +bool ImageUtil::saveImage(QWidget *window, const QImage &image, QString file) +{ + bool result = false; + + if (!file.isEmpty() || misc::getSaveFileName(window, RshareSettings::LASTDIR_IMAGES, QApplication::translate("ImageUtil", "Save Picture File"), QApplication::translate("ImageUtil", "Pictures (*.png *.xpm *.jpg)"), file)) { + if (image.save(file, nullptr, 100)) { + result = true; + } else { + if (image.save(file + ".png", nullptr, 100)) { + result = true; + } else { + QMessageBox::warning(window, QApplication::translate("ImageUtil", "Save image"), QApplication::translate("ImageUtil", "Cannot save the image, invalid filename") + + "\n" + file); + } + } + } + + return result; +} + void ImageUtil::copyImage(QWidget *window, QTextCursor cursor) { cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1); diff --git a/retroshare-gui/src/util/imageutil.h b/retroshare-gui/src/util/imageutil.h index 8e7d26987..46e83e728 100644 --- a/retroshare-gui/src/util/imageutil.h +++ b/retroshare-gui/src/util/imageutil.h @@ -37,6 +37,7 @@ public: static bool checkImage(const QTextEdit *edit, const QPoint &pos, QString &imageStr, QRect *cursorRectStartOut = NULL, QRect *cursorRectLeftOut = NULL, QRect *cursorRectRightOut = NULL, QRect *cursorRectEndOut = NULL); static void extractImage(QWidget *window, QTextCursor cursor, QString file = ""); static void copyImage(QWidget *window, QTextCursor cursor); + static bool saveImage(QWidget *window, const QImage &image, QString file = ""); static bool optimizeSizeHtml(QString &html, const QImage& original, QImage &optimized, int maxPixels = -1, int maxBytes = -1); static bool optimizeSizeBytes(QByteArray &bytearray, const QImage &original, QImage &optimized, const char *format, int maxPixels, int maxBytes); static bool hasAlphaContent(const QImage& image);