mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-13 16:39:43 -05:00
Added copy and save of image to AspectRatioPixmapLabel
This commit is contained in:
parent
e829292eaf
commit
8422d3fc8c
@ -18,8 +18,14 @@
|
|||||||
* *
|
* *
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QClipboard>
|
||||||
|
#include <QMenu>
|
||||||
|
|
||||||
#include "AspectRatioPixmapLabel.h"
|
#include "AspectRatioPixmapLabel.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "util/imageutil.h"
|
||||||
|
#include "gui/common/FilesDefs.h"
|
||||||
|
|
||||||
AspectRatioPixmapLabel::AspectRatioPixmapLabel(QWidget *parent) :
|
AspectRatioPixmapLabel::AspectRatioPixmapLabel(QWidget *parent) :
|
||||||
QLabel(parent)
|
QLabel(parent)
|
||||||
@ -31,7 +37,7 @@ AspectRatioPixmapLabel::AspectRatioPixmapLabel(QWidget *parent) :
|
|||||||
void AspectRatioPixmapLabel::setPixmap ( const QPixmap & p)
|
void AspectRatioPixmapLabel::setPixmap ( const QPixmap & p)
|
||||||
{
|
{
|
||||||
pix = p;
|
pix = p;
|
||||||
QLabel::setPixmap(pix);
|
QLabel::setPixmap(scaledPixmap());
|
||||||
//std::cout << "Information size: " << pix.width() << 'x' << pix.height() << std::endl;
|
//std::cout << "Information size: " << pix.width() << 'x' << pix.height() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,3 +63,53 @@ void AspectRatioPixmapLabel::resizeEvent(QResizeEvent * e)
|
|||||||
QLabel::resizeEvent(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;
|
//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());
|
||||||
|
}
|
||||||
|
@ -28,17 +28,33 @@
|
|||||||
class AspectRatioPixmapLabel : public QLabel
|
class AspectRatioPixmapLabel : public QLabel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AspectRatioPixmapLabel(QWidget *parent = nullptr);
|
explicit AspectRatioPixmapLabel(QWidget *parent = nullptr);
|
||||||
virtual int heightForWidth( int width ) const override;
|
virtual int heightForWidth( int width ) const override;
|
||||||
virtual QSize sizeHint() const override;
|
virtual QSize sizeHint() const override;
|
||||||
QPixmap scaledPixmap() const;
|
QPixmap scaledPixmap() const;
|
||||||
|
|
||||||
|
// Add QAction to context menu (action won't be deleted)
|
||||||
|
void addContextMenuAction(QAction *action);
|
||||||
|
|
||||||
|
Q_SIGNALS:
|
||||||
|
void calculateContextMenuActions();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setPixmap ( const QPixmap & );
|
void setPixmap ( const QPixmap & );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void resizeEvent(QResizeEvent *event) override;
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
|
virtual void contextMenuEvent(QContextMenuEvent *event);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void copyImage();
|
||||||
|
void saveImage();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QPixmap pix;
|
QPixmap pix;
|
||||||
|
QList<QAction*> mContextMenuActions;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ASPECTRATIOPIXMAPLABEL_H
|
#endif // ASPECTRATIOPIXMAPLABEL_H
|
||||||
|
@ -115,13 +115,7 @@ void ImageUtil::extractImage(QWidget *window, QTextCursor cursor, QString file)
|
|||||||
if(!image.isNull())
|
if(!image.isNull())
|
||||||
{
|
{
|
||||||
success = true;
|
success = true;
|
||||||
if(!file.isEmpty() || misc::getSaveFileName(window, RshareSettings::LASTDIR_IMAGES, "Save Picture File", "Pictures (*.png *.xpm *.jpg)", file))
|
saveImage(window, image, 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!success)
|
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)
|
void ImageUtil::copyImage(QWidget *window, QTextCursor cursor)
|
||||||
{
|
{
|
||||||
cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1);
|
cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1);
|
||||||
|
@ -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 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 extractImage(QWidget *window, QTextCursor cursor, QString file = "");
|
||||||
static void copyImage(QWidget *window, QTextCursor cursor);
|
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 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 optimizeSizeBytes(QByteArray &bytearray, const QImage &original, QImage &optimized, const char *format, int maxPixels, int maxBytes);
|
||||||
static bool hasAlphaContent(const QImage& image);
|
static bool hasAlphaContent(const QImage& image);
|
||||||
|
Loading…
Reference in New Issue
Block a user