2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-09-01 15:15:13 +00:00

Use gtk not only to get image from clipboard, but also to set

This commit is contained in:
Ilya Fedin
2020-12-02 10:16:25 +04:00
committed by John Preston
parent 61b7b5d03b
commit f88c132c96
15 changed files with 112 additions and 21 deletions

View File

@@ -325,7 +325,7 @@ bool GenerateDesktopFile(
}
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
bool GetImageFromClipboardSupported() {
bool GetClipboardImageSupported() {
return (Libs::gtk_clipboard_wait_for_contents != nullptr)
&& (Libs::gtk_clipboard_wait_for_image != nullptr)
&& (Libs::gtk_selection_data_targets_include_image != nullptr)
@@ -337,6 +337,11 @@ bool GetImageFromClipboardSupported() {
&& (Libs::gdk_pixbuf_get_has_alpha != nullptr)
&& (Libs::gdk_atom_intern != nullptr);
}
bool SetClipboardImageSupported() {
return (Libs::gtk_clipboard_set_image != nullptr)
&& (Libs::gdk_pixbuf_new_from_data != nullptr);
}
#endif // !TDESKTOP_DISABLE_GTK_INTEGRATION
uint XCBMoveResizeFromEdges(Qt::Edges edges) {
@@ -729,11 +734,11 @@ QString GetIconName() {
return Result;
}
QImage GetImageFromClipboard() {
QImage GetClipboardImage() {
QImage data;
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
if (!GetImageFromClipboardSupported() || !Libs::GtkClipboard()) {
if (!GetClipboardImageSupported() || !Libs::GtkClipboard()) {
return data;
}
@@ -767,6 +772,57 @@ QImage GetImageFromClipboard() {
return data;
}
bool SetClipboardImage(const QImage &image) {
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
if (!SetClipboardImageSupported()
|| !Libs::GtkClipboard()
|| image.isNull()) {
return false;
}
const auto convertedImage = [&] {
if (image.hasAlphaChannel()) {
if (image.format() != QImage::Format_RGBA8888) {
return image.convertToFormat(QImage::Format_RGBA8888);
}
} else {
if (image.format() != QImage::Format_RGB888) {
return image.convertToFormat(QImage::Format_RGB888);
}
}
return image;
}();
auto imageBuf = reinterpret_cast<guchar*>(
malloc(convertedImage.sizeInBytes()));
memcpy(
imageBuf,
convertedImage.constBits(),
convertedImage.sizeInBytes());
auto imagePixbuf = Libs::gdk_pixbuf_new_from_data(
imageBuf,
GDK_COLORSPACE_RGB,
convertedImage.hasAlphaChannel(),
8,
convertedImage.width(),
convertedImage.height(),
convertedImage.bytesPerLine(),
[](guchar *pixels, gpointer data) {
free(pixels);
},
nullptr);
Libs::gtk_clipboard_set_image(Libs::GtkClipboard(), imagePixbuf);
g_object_unref(imagePixbuf);
return true;
#else // !TDESKTOP_DISABLE_GTK_INTEGRATION
return false;
#endif // TDESKTOP_DISABLE_GTK_INTEGRATION
}
std::optional<bool> IsDarkMode() {
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
if (Libs::GtkSettingSupported() && Libs::GtkLoaded()) {