diff --git a/src/utils/screenshotsaver.cpp b/src/utils/screenshotsaver.cpp index 18f3715f..d81de441 100644 --- a/src/utils/screenshotsaver.cpp +++ b/src/utils/screenshotsaver.cpp @@ -6,6 +6,7 @@ #include "src/utils/confighandler.h" #include "src/utils/filenamehandler.h" #include "src/utils/systemnotification.h" +#include "utils/desktopinfo.h" #include #include #include @@ -25,6 +26,31 @@ ScreenshotSaver::ScreenshotSaver(const unsigned id) : m_id(id) {} +void ScreenshotSaver::saveToClipboardPng(const QPixmap& capture) +{ + QByteArray array; + QBuffer buffer{ &array }; + QImageWriter imageWriter{ &buffer, "PNG" }; + imageWriter.write(capture.toImage()); + + QPixmap pngPixmap; + bool isLoaded = pngPixmap.loadFromData( + reinterpret_cast(array.data()), array.size(), "PNG"); + if (isLoaded) { + // Need to send message before copying to clipboard + SystemNotification().sendMessage( + QObject::tr("Capture saved to clipboard")); + + QMimeData* mimeData = new QMimeData; + mimeData->setData("image/png", array); + QApplication::clipboard()->setMimeData(mimeData); + } else { + SystemNotification().sendMessage( + QObject::tr("Error while saving to clipboard")); + return; + } +} + // TODO: If data is saved to the clipboard before the notification is sent via // dbus, the application freezes. void ScreenshotSaver::saveToClipboard(const QPixmap& capture) @@ -36,7 +62,16 @@ void ScreenshotSaver::saveToClipboard(const QPixmap& capture) saveToFilesystem(capture, ConfigHandler().savePath(), QObject::tr("Capture saved to clipboard.")); +#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) + if (DesktopInfo().waylandDectected()) { + saveToClipboardPng(capture); + } else { + QApplication::clipboard()->setPixmap(capture); + } +#else QApplication::clipboard()->setPixmap(capture); +#endif + } // Otherwise only save to clipboard else { @@ -67,7 +102,15 @@ void ScreenshotSaver::saveToClipboard(const QPixmap& capture) // Need to send message before copying to clipboard SystemNotification().sendMessage( QObject::tr("Capture saved to clipboard")); +#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) + if (DesktopInfo().waylandDectected()) { + saveToClipboardPng(capture); + } else { + QApplication::clipboard()->setPixmap(capture); + } +#else QApplication::clipboard()->setPixmap(capture); +#endif } } } diff --git a/src/utils/screenshotsaver.h b/src/utils/screenshotsaver.h index aa6fbf74..1acf569c 100644 --- a/src/utils/screenshotsaver.h +++ b/src/utils/screenshotsaver.h @@ -15,6 +15,7 @@ public: ScreenshotSaver(const unsigned id); void saveToClipboard(const QPixmap& capture); + void saveToClipboardPng(const QPixmap& capture); bool saveToFilesystem(const QPixmap& capture, const QString& path, const QString& messagePrefix);