File indexing completed on 2024-04-28 13:43:28
0001 /** 0002 * SPDX-FileCopyrightText: (C) 2003 Sébastien Laoût <slaout@linux62.org> 0003 * SPDX-FileCopyrightText: (C) 2020 Carl Schwan <carl@carlschwan.eu> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "colorpicker.h" 0009 0010 #ifndef _WIN32 0011 0012 #include <QDBusMessage> 0013 #include <QDBusMetaType> 0014 #include <QDBusPendingCall> 0015 #include <QDBusPendingCallWatcher> 0016 #include <QDBusPendingReply> 0017 #include <QDBusObjectPath> 0018 #include <QDBusConnection> 0019 #include <QDebug> 0020 0021 QDBusArgument &operator <<(QDBusArgument &arg, const QColor &color) 0022 { 0023 arg.beginStructure(); 0024 arg << color.redF() << color.greenF() << color.blueF(); 0025 arg.endStructure(); 0026 return arg; 0027 } 0028 0029 const QDBusArgument &operator >>(const QDBusArgument &arg, QColor &color) 0030 { 0031 double red, green, blue; 0032 arg.beginStructure(); 0033 arg >> red >> green >> blue; 0034 color.setRedF(red); 0035 color.setGreenF(green); 0036 color.setBlueF(blue); 0037 arg.endStructure(); 0038 0039 return arg; 0040 } 0041 0042 ColorPicker::ColorPicker(QObject *parent) 0043 : QObject(parent) 0044 { 0045 setObjectName(QStringLiteral("ColorPicker")); 0046 qDBusRegisterMetaType<QColor>(); 0047 } 0048 0049 void ColorPicker::grabColor() 0050 { 0051 QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"), 0052 QLatin1String("/org/freedesktop/portal/desktop"), 0053 QLatin1String("org.freedesktop.portal.Screenshot"), 0054 QLatin1String("PickColor")); 0055 message << QLatin1String("x11:") << QVariantMap{}; 0056 QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(message); 0057 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall); 0058 connect(watcher, &QDBusPendingCallWatcher::finished, [this] (QDBusPendingCallWatcher *watcher) { 0059 QDBusPendingReply<QDBusObjectPath> reply = *watcher; 0060 if (reply.isError()) { 0061 qWarning() << "Couldn't get reply"; 0062 qWarning() << "Error: " << reply.error().message(); 0063 } else { 0064 QDBusConnection::sessionBus().connect(QString(), 0065 reply.value().path(), 0066 QLatin1String("org.freedesktop.portal.Request"), 0067 QLatin1String("Response"), 0068 this, 0069 SLOT(gotColorResponse(uint, QVariantMap))); 0070 } 0071 }); 0072 } 0073 0074 void ColorPicker::gotColorResponse(uint response, const QVariantMap& results) 0075 { 0076 if (!response) { 0077 if (results.contains(QLatin1String("color"))) { 0078 const QColor color = qdbus_cast<QColor>(results.value(QLatin1String("color"))); 0079 Q_EMIT colorGrabbed(color); 0080 } 0081 } else { 0082 qWarning() << "Failed to take screenshot"; 0083 } 0084 } 0085 0086 #include "moc_colorpicker.cpp" 0087 0088 #endif