Merge pull request #2710 from defnax/copyimage-clipboard

Added copy image to clipboard feature
This commit is contained in:
csoler 2023-04-17 22:16:39 +02:00 committed by GitHub
commit 44bb2c0f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 0 deletions

View File

@ -356,6 +356,11 @@ QMenu *RSTextBrowser::createStandardContextMenuFromPoint(const QPoint &widgetPos
a->setData(widgetPos); a->setData(widgetPos);
} }
if (checkImage(widgetPos)) {
a = menu->addAction( tr("Copy image"), this, SLOT(copyImage()));
a->setData(widgetPos);
}
return menu; return menu;
} }
@ -389,3 +394,15 @@ void RSTextBrowser::saveImage()
QTextCursor cursor = cursorForPosition(point); QTextCursor cursor = cursorForPosition(point);
ImageUtil::extractImage(window(), cursor); ImageUtil::extractImage(window(), cursor);
} }
void RSTextBrowser::copyImage()
{
QAction *action = dynamic_cast<QAction*>(sender()) ;
if (!action) {
return;
}
QPoint point = action->data().toPoint();
QTextCursor cursor = cursorForPosition(point);
ImageUtil::copyImage(window(), cursor);
}

View File

@ -75,6 +75,7 @@ private slots:
void destroyImageBlockWidget(); void destroyImageBlockWidget();
void viewSource(); void viewSource();
void saveImage(); void saveImage();
void copyImage();
protected: protected:
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);

View File

@ -25,6 +25,8 @@
#include <QApplication> #include <QApplication>
#include <QByteArray> #include <QByteArray>
#include <QImage> #include <QImage>
#include <QClipboard>
#include <QMimeData>
#include <QMessageBox> #include <QMessageBox>
#include <QString> #include <QString>
#include <QTextCursor> #include <QTextCursor>
@ -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) 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 //nothing to do if it fits into the limits

View File

@ -32,6 +32,7 @@ public:
ImageUtil(); ImageUtil();
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 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);