From 3004505832cbda8565ef871e50b8d282cdabf565 Mon Sep 17 00:00:00 2001 From: defnax Date: Mon, 17 Apr 2023 21:01:57 +0200 Subject: [PATCH] Added copy image to clipboard feature --- .../src/gui/common/RSTextBrowser.cpp | 17 +++++++++++ retroshare-gui/src/gui/common/RSTextBrowser.h | 1 + retroshare-gui/src/util/imageutil.cpp | 30 +++++++++++++++++++ retroshare-gui/src/util/imageutil.h | 1 + 4 files changed, 49 insertions(+) diff --git a/retroshare-gui/src/gui/common/RSTextBrowser.cpp b/retroshare-gui/src/gui/common/RSTextBrowser.cpp index 449f4e1c6..c3aaaf454 100644 --- a/retroshare-gui/src/gui/common/RSTextBrowser.cpp +++ b/retroshare-gui/src/gui/common/RSTextBrowser.cpp @@ -356,6 +356,11 @@ QMenu *RSTextBrowser::createStandardContextMenuFromPoint(const QPoint &widgetPos a->setData(widgetPos); } + if (checkImage(widgetPos)) { + a = menu->addAction( tr("Copy image"), this, SLOT(copyImage())); + a->setData(widgetPos); + } + return menu; } @@ -389,3 +394,15 @@ void RSTextBrowser::saveImage() QTextCursor cursor = cursorForPosition(point); ImageUtil::extractImage(window(), cursor); } + +void RSTextBrowser::copyImage() +{ + QAction *action = dynamic_cast(sender()) ; + if (!action) { + return; + } + + QPoint point = action->data().toPoint(); + QTextCursor cursor = cursorForPosition(point); + ImageUtil::copyImage(window(), cursor); +} diff --git a/retroshare-gui/src/gui/common/RSTextBrowser.h b/retroshare-gui/src/gui/common/RSTextBrowser.h index 04de979c5..120dd22e8 100644 --- a/retroshare-gui/src/gui/common/RSTextBrowser.h +++ b/retroshare-gui/src/gui/common/RSTextBrowser.h @@ -75,6 +75,7 @@ private slots: void destroyImageBlockWidget(); void viewSource(); void saveImage(); + void copyImage(); protected: void paintEvent(QPaintEvent *event); diff --git a/retroshare-gui/src/util/imageutil.cpp b/retroshare-gui/src/util/imageutil.cpp index d710a9ae3..9356ae3b2 100644 --- a/retroshare-gui/src/util/imageutil.cpp +++ b/retroshare-gui/src/util/imageutil.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include #include @@ -68,6 +70,34 @@ void ImageUtil::extractImage(QWidget *window, QTextCursor cursor, QString file) } } +void ImageUtil::copyImage(QWidget *window, QTextCursor cursor) +{ + cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1); + cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2); + QString imagestr = cursor.selection().toHtml(); + bool success = false; + int start = imagestr.indexOf("base64,") + 7; + int stop = imagestr.indexOf("\"", start); + int length = stop - start; + if((start >= 0) && (length > 0)) + { + QByteArray ba = QByteArray::fromBase64(imagestr.mid(start, length).toLatin1()); + QImage image = QImage::fromData(ba); + if(!image.isNull()) + { + success = true; + QClipboard *clipboard = QApplication::clipboard(); + QMimeData *data = new QMimeData; + data->setImageData(image); + clipboard->setMimeData(data, QClipboard::Clipboard); + } + } + if(!success) + { + QMessageBox::warning(window, QApplication::translate("ImageUtil", "Copy image"), QApplication::translate("ImageUtil", "Not an image")); + } +} + bool ImageUtil::optimizeSizeBytes(QByteArray &bytearray, const QImage &original, QImage &optimized, const char *format, int maxPixels, int maxBytes) { //nothing to do if it fits into the limits diff --git a/retroshare-gui/src/util/imageutil.h b/retroshare-gui/src/util/imageutil.h index 9e5bf37cb..137c0ea8e 100644 --- a/retroshare-gui/src/util/imageutil.h +++ b/retroshare-gui/src/util/imageutil.h @@ -32,6 +32,7 @@ public: ImageUtil(); static void extractImage(QWidget *window, QTextCursor cursor, QString file = ""); + static void copyImage(QWidget *window, QTextCursor cursor); 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);