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