Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the known issue of saving JPEG to clipboard on macOS #3724

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/config/generalconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 47 additions & 1 deletion src/utils/screenshotsaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
#include "src/utils/globalvalues.h"
#include "utils/desktopinfo.h"

#include <QTemporaryFile>
#include <QImageWriter>
#include <QPixmap>
#include <QByteArray>
#include <QProcess>
#include <QDebug>

#if USE_WAYLAND_CLIPBOARD
#include <KSystemClipboard>
#endif
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down