From 9917f980d13960f26faf0082771fde87aa99aab9 Mon Sep 17 00:00:00 2001 From: Oleksandr Honcharov Date: Sun, 15 Sep 2024 16:48:19 +0200 Subject: [PATCH 1/2] fix issue with saving JPEG on macOS to clipboard --- src/utils/screenshotsaver.cpp | 48 ++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/utils/screenshotsaver.cpp b/src/utils/screenshotsaver.cpp index f7f5a7102b..f8a5ec3f5e 100644 --- a/src/utils/screenshotsaver.cpp +++ b/src/utils/screenshotsaver.cpp @@ -10,6 +10,13 @@ #include "src/utils/globalvalues.h" #include "utils/desktopinfo.h" +#include +#include +#include +#include +#include +#include + #if USE_WAYLAND_CLIPBOARD #include #endif @@ -101,6 +108,42 @@ QString ShowSaveFileDialog(const QString& title, const QString& directory) } } +void saveJpegToClipboardMacOS(const QPixmap& capture) { + // Convert QPixmap to JPEG data + QByteArray jpegData; + QBuffer buffer(&jpegData); + buffer.open(QIODevice::WriteOnly); + + QImageWriter imageWriter(&buffer, "jpeg"); + imageWriter.setQuality(ConfigHandler().jpegQuality()); // Set JPEG quality to whatever is in settings + if (!imageWriter.write(capture.toImage())) { + qWarning() << "Failed to write image to JPEG format."; + return; + } + + // Save JPEG data to a temporary file + QTemporaryFile tempFile; + if (!tempFile.open()) { + qWarning() << "Failed to open temporary file for writing."; + return; + } + tempFile.write(jpegData); + tempFile.close(); + + // Use osascript to copy the contents of the file to clipboard + QProcess process; + QString script = QString( + "set the clipboard to (read (POSIX file \"%1\") as «class PNGf»)" + ).arg(tempFile.fileName()); + process.start("osascript", QStringList() << "-e" << script); + if (!process.waitForFinished()) { + qWarning() << "Failed to execute AppleScript."; + } + + // Clean up + tempFile.remove(); +} + void saveToClipboardMime(const QPixmap& capture, const QString& imageType) { QByteArray array; @@ -152,8 +195,11 @@ void saveToClipboard(const QPixmap& capture) AbstractLogger() << QObject::tr("Capture saved to clipboard."); } if (ConfigHandler().useJpgForClipboard()) { - // FIXME - it doesn't work on MacOS +#ifdef Q_OS_MAC + saveJpegToClipboardMacOS(capture); +#else saveToClipboardMime(capture, "jpeg"); +#endif } else { // Need to send message before copying to clipboard #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) From 8b4e6c74e4cdc1d5a4963c816e55ca991816375f Mon Sep 17 00:00:00 2001 From: Oleksandr Honcharov Date: Sun, 15 Sep 2024 16:49:49 +0200 Subject: [PATCH 2/2] don't hide useJpgForClipboard from macOS GUI configuration --- src/config/generalconf.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/config/generalconf.cpp b/src/config/generalconf.cpp index 8c8024def7..952b0f9282 100644 --- a/src/config/generalconf.cpp +++ b/src/config/generalconf.cpp @@ -614,10 +614,6 @@ void GeneralConf::initUseJpgForClipboard() tr("Use lossy JPG format for clipboard (lossless PNG default)")); m_scrollAreaLayout->addWidget(m_useJpgForClipboard); -#if defined(Q_OS_MACOS) - // FIXME - temporary fix to disable option for MacOS - m_useJpgForClipboard->hide(); -#endif connect(m_useJpgForClipboard, &QCheckBox::clicked, this,