Added copy and save of image to AspectRatioPixmapLabel

This commit is contained in:
thunder2 2023-05-14 22:19:16 +02:00
parent e829292eaf
commit 8422d3fc8c
4 changed files with 95 additions and 8 deletions

View File

@ -18,8 +18,14 @@
* *
*******************************************************************************/
#include <QApplication>
#include <QClipboard>
#include <QMenu>
#include "AspectRatioPixmapLabel.h"
#include <iostream>
#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<QAction*>::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());
}

View File

@ -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<QAction*> mContextMenuActions;
};
#endif // ASPECTRATIOPIXMAPLABEL_H

View File

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

View File

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