improve screenshot saver copy (#1604)
* small refactor to screenshotsaver clipboard * clang-format
This commit is contained in:
@@ -26,28 +26,26 @@ ScreenshotSaver::ScreenshotSaver(const unsigned id)
|
||||
: m_id(id)
|
||||
{}
|
||||
|
||||
void ScreenshotSaver::saveToClipboardPng(const QPixmap& capture)
|
||||
void ScreenshotSaver::saveToClipboardMime(const QPixmap& capture,
|
||||
const QString& imageType)
|
||||
{
|
||||
QByteArray array;
|
||||
QBuffer buffer{ &array };
|
||||
QImageWriter imageWriter{ &buffer, "PNG" };
|
||||
QImageWriter imageWriter{ &buffer, imageType.toUpper().toUtf8() };
|
||||
imageWriter.write(capture.toImage());
|
||||
|
||||
QPixmap pngPixmap;
|
||||
bool isLoaded = pngPixmap.loadFromData(
|
||||
reinterpret_cast<uchar*>(array.data()), array.size(), "PNG");
|
||||
bool isLoaded =
|
||||
pngPixmap.loadFromData(reinterpret_cast<uchar*>(array.data()),
|
||||
array.size(),
|
||||
imageType.toUpper().toUtf8());
|
||||
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);
|
||||
mimeData->setData("image/" + imageType, array);
|
||||
QApplication::clipboard()->setMimeData(mimeData);
|
||||
} else {
|
||||
SystemNotification().sendMessage(
|
||||
QObject::tr("Error while saving to clipboard"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,56 +60,24 @@ void ScreenshotSaver::saveToClipboard(const QPixmap& capture)
|
||||
saveToFilesystem(capture,
|
||||
ConfigHandler().savePath(),
|
||||
QObject::tr("Capture saved to clipboard."));
|
||||
} else {
|
||||
SystemNotification().sendMessage(
|
||||
QObject::tr("Capture saved to clipboard."));
|
||||
}
|
||||
if (ConfigHandler().useJpgForClipboard()) {
|
||||
// FIXME - it doesn't work on MacOS
|
||||
saveToClipboardMime(capture, "jpeg");
|
||||
} else {
|
||||
// Need to send message before copying to clipboard
|
||||
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
|
||||
if (DesktopInfo().waylandDectected()) {
|
||||
saveToClipboardPng(capture);
|
||||
saveToClipboardMime(capture, "png");
|
||||
} else {
|
||||
QApplication::clipboard()->setPixmap(capture);
|
||||
}
|
||||
#else
|
||||
QApplication::clipboard()->setPixmap(capture);
|
||||
#endif
|
||||
|
||||
}
|
||||
// Otherwise only save to clipboard
|
||||
else {
|
||||
if (ConfigHandler().useJpgForClipboard()) {
|
||||
// FIXME - it doesn't work on MacOS
|
||||
QByteArray array;
|
||||
QBuffer buffer{ &array };
|
||||
QImageWriter imageWriter{ &buffer, "JPEG" };
|
||||
imageWriter.write(capture.toImage());
|
||||
|
||||
QPixmap jpgPixmap;
|
||||
bool isLoaded = jpgPixmap.loadFromData(
|
||||
reinterpret_cast<uchar*>(array.data()), array.size(), "JPEG");
|
||||
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/jpeg", array);
|
||||
QApplication::clipboard()->setMimeData(mimeData);
|
||||
} else {
|
||||
SystemNotification().sendMessage(
|
||||
QObject::tr("Error while saving to clipboard"));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user