|
10 | 10 | #include "src/utils/globalvalues.h" |
11 | 11 | #include "utils/desktopinfo.h" |
12 | 12 |
|
| 13 | +#include <QByteArray> |
| 14 | +#include <QDebug> |
| 15 | +#include <QImageWriter> |
| 16 | +#include <QPixmap> |
| 17 | +#include <QProcess> |
| 18 | +#include <QTemporaryFile> |
| 19 | + |
13 | 20 | #if USE_WAYLAND_CLIPBOARD |
14 | 21 | #include <KSystemClipboard> |
15 | 22 | #endif |
@@ -101,6 +108,45 @@ QString ShowSaveFileDialog(const QString& title, const QString& directory) |
101 | 108 | } |
102 | 109 | } |
103 | 110 |
|
| 111 | +void saveJpegToClipboardMacOS(const QPixmap& capture) |
| 112 | +{ |
| 113 | + // Convert QPixmap to JPEG data |
| 114 | + QByteArray jpegData; |
| 115 | + QBuffer buffer(&jpegData); |
| 116 | + buffer.open(QIODevice::WriteOnly); |
| 117 | + |
| 118 | + QImageWriter imageWriter(&buffer, "jpeg"); |
| 119 | + |
| 120 | + // Set JPEG quality to whatever is in settings |
| 121 | + imageWriter.setQuality(ConfigHandler().jpegQuality()); |
| 122 | + if (!imageWriter.write(capture.toImage())) { |
| 123 | + qWarning() << "Failed to write image to JPEG format."; |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + // Save JPEG data to a temporary file |
| 128 | + QTemporaryFile tempFile; |
| 129 | + if (!tempFile.open()) { |
| 130 | + qWarning() << "Failed to open temporary file for writing."; |
| 131 | + return; |
| 132 | + } |
| 133 | + tempFile.write(jpegData); |
| 134 | + tempFile.close(); |
| 135 | + |
| 136 | + // Use osascript to copy the contents of the file to clipboard |
| 137 | + QProcess process; |
| 138 | + QString script = |
| 139 | + QString("set the clipboard to (read (POSIX file \"%1\") as «class PNGf»)") |
| 140 | + .arg(tempFile.fileName()); |
| 141 | + process.start("osascript", QStringList() << "-e" << script); |
| 142 | + if (!process.waitForFinished()) { |
| 143 | + qWarning() << "Failed to execute AppleScript."; |
| 144 | + } |
| 145 | + |
| 146 | + // Clean up |
| 147 | + tempFile.remove(); |
| 148 | +} |
| 149 | + |
104 | 150 | void saveToClipboardMime(const QPixmap& capture, const QString& imageType) |
105 | 151 | { |
106 | 152 | QByteArray array; |
@@ -152,8 +198,11 @@ void saveToClipboard(const QPixmap& capture) |
152 | 198 | AbstractLogger() << QObject::tr("Capture saved to clipboard."); |
153 | 199 | } |
154 | 200 | if (ConfigHandler().useJpgForClipboard()) { |
155 | | - // FIXME - it doesn't work on MacOS |
| 201 | +#ifdef Q_OS_MAC |
| 202 | + saveJpegToClipboardMacOS(capture); |
| 203 | +#else |
156 | 204 | saveToClipboardMime(capture, "jpeg"); |
| 205 | +#endif |
157 | 206 | } else { |
158 | 207 | // Need to send message before copying to clipboard |
159 | 208 | #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) |
|
0 commit comments