4#include "screencolorpicker.h"
6#include <qcolordialog.h>
7#include <qdbusargument.h>
8#include <qdbusconnection.h>
9#include <qdbusextratypes.h>
10#include <qdbusmessage.h>
11#include <qdbuspendingcall.h>
12#include <qdbuspendingreply.h>
14#include <qguiapplication.h>
16#include <qobjectdefs.h>
17#include <qpushbutton.h>
19#include <qstringbuilder.h>
20#include <qstringliteral.h>
26#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
38ScreenColorPicker::ScreenColorPicker(
QWidget *parent)
45ScreenColorPicker::~ScreenColorPicker()
52bool ScreenColorPicker::isAvailable()
54 if (hasPortalSupport()) {
57 initializeQColorDialogSupport();
58 return m_hasQColorDialogSupport.value();
68bool ScreenColorPicker::hasPortalSupport()
70 static const bool m_hasPortalSupport = queryPortalSupport();
71 return m_hasPortalSupport;
84bool ScreenColorPicker::queryPortalSupport()
87 QStringLiteral(
"org.freedesktop.portal.Desktop"),
88 QStringLiteral(
"/org/freedesktop/portal/desktop"),
89 QStringLiteral(
"org.freedesktop.DBus.Properties"),
90 QStringLiteral(
"Get"));
91 message << QStringLiteral(
"org.freedesktop.portal.Screenshot")
92 << QStringLiteral(
"version");
94 if (reply.
type() != QDBusMessage::MessageType::ReplyMessage) {
97 constexpr quint8 minimumSupportedPortalVersion = 2;
98 const qulonglong actualPortalVersion = reply
104 if (actualPortalVersion < minimumSupportedPortalVersion) {
115QString ScreenColorPicker::translateViaQColorDialog(
const char *sourceText)
136void ScreenColorPicker::initializeQColorDialogSupport()
138 if (m_hasQColorDialogSupport.has_value()) {
139 if (m_hasQColorDialogSupport.value() ==
false) {
146 if (m_qColorDialogScreenButton) {
152 m_qColorDialog->setOptions(
154 const auto buttonList = m_qColorDialog->findChildren<
QPushButton *>();
155 for (
const auto &button : std::as_const(buttonList)) {
156 button->setDefault(
false);
160 if (button->text() == translateViaQColorDialog(
"&Pick Screen Color")) {
161 m_qColorDialogScreenButton = button;
164 m_hasQColorDialogSupport = m_qColorDialogScreenButton;
165 if (m_hasQColorDialogSupport) {
166 m_qColorDialog->setParent(
this);
167 m_qColorDialog->hide();
171 [
this](
const QColor &color) {
172 const auto red =
static_cast<double>(color.
redF());
173 const auto green =
static_cast<double>(color.
greenF());
174 const auto blue =
static_cast<double>(color.
blueF());
175 Q_EMIT newColor(red, green, blue,
false);
178 delete m_qColorDialog;
179 m_qColorDialog =
nullptr;
210void ScreenColorPicker::startPicking(quint8 previousColorRed, quint8 previousColorGreen, quint8 previousColorBlue)
232 if (hasPortalSupport()) {
237 initializeQColorDialogSupport();
238 if (m_qColorDialogScreenButton) {
239 const auto previousColor =
QColor(previousColorRed,
242 m_qColorDialog->setCurrentColor(previousColor);
243 m_qColorDialogScreenButton->click();
248void ScreenColorPicker::pickWithPortal()
264 QString parentWindowIdentifier;
266 const QWidget *
const parentWidget = qobject_cast<QWidget *>(parent());
267 if (parentWidget !=
nullptr) {
268 parentWindowIdentifier = QStringLiteral(
"x11:")
276 QStringLiteral(
"org.freedesktop.portal.Desktop"),
277 QStringLiteral(
"/org/freedesktop/portal/desktop"),
278 QStringLiteral(
"org.freedesktop.portal.Screenshot"),
279 QStringLiteral(
"PickColor"));
280 message << parentWindowIdentifier
294 QStringLiteral(
"org.freedesktop.portal.Desktop"),
296 reply.
value().path(),
298 QStringLiteral(
"org.freedesktop.portal.Request"),
300 QStringLiteral(
"Response"),
304 SLOT(getPortalResponse(uint, QVariantMap)));
315void ScreenColorPicker::getPortalResponse(uint exitCode,
const QVariantMap &responseArguments)
321 .value(QStringLiteral(
"color"))
325 while (!responseColor.
atEnd()) {
327 responseColor >> temp;
331 if (rgb.count() == 3) {
335 Q_EMIT newColor(rgb.at(0), rgb.at(1), rgb.at(2),
true);
The namespace of this library.
float blueF() const const
float greenF() const const
void currentColorChanged(const QColor &color)
QDBusPendingCall asyncCall(const QDBusMessage &message, int timeout) const const
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const const
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
QDBusConnection sessionBus()
QList< QVariant > arguments() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
MessageType type() const const
void finished(QDBusPendingCallWatcher *self)
bool isError() const const
typename Select< 0 >::Type value() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString number(double n, char format, int precision)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)