Perceptual Color

screencolorpicker.h
1// SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR MIT
3
4#ifndef SCREENCOLORPICKER
5#define SCREENCOLORPICKER
6
7#include <optional>
8#include <qglobal.h>
9#include <qpointer.h>
10#include <qstring.h>
11#include <qwidget.h>
12class QColorDialog;
13class QPushButton;
14
15#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
16#include <qcontainerfwd.h>
17#include <qtmetamacros.h>
18#else
19#include <qmetatype.h>
20#include <qobjectdefs.h>
21class QObject;
22#endif
23
24namespace PerceptualColor
25{
26
27/** @internal
28 *
29 * @brief Pick a color from the screen.
30 *
31 * Provides an interface to let the user pick a color from the screen.
32 *
33 * This feature will not work on all platforms. Use @ref isAvailable()
34 * to check if it is actually available at runtime. */
35class ScreenColorPicker : public QWidget
36{
38
39public:
40 explicit ScreenColorPicker(QWidget *parent);
41 virtual ~ScreenColorPicker() override;
42 [[nodiscard]] bool isAvailable();
43
44public Q_SLOTS:
45 void startPicking(quint8 previousColorRed, quint8 previousColorGreen, quint8 previousColorBlue);
46
48 /** @brief A new color.
49 *
50 * Emitted when the user has clicked on the screen to select a new color.
51 *
52 * @note On some platforms, furthermore this signal is also emitted while
53 * the user hovers over the screen with the mouse. Than, if the user
54 * cancels with the ESC key, a new signal is emitted with the old color
55 * passed originally to @ref startPicking.
56 *
57 * @param red The <em>red</em> component of the new color.
58 * Range: <tt>[0, 255]</tt>
59 * @param green Same for green.
60 * @param blue Same for blue.
61 * @param isSRgbGuaranteed If <tt>true</tt>, the RGB values are guaranteed
62 * to be in the sRGB color space. If <tt>false</tt>, it is not guaranteed
63 * (but still likely) that the RGB values are in the sRGB color space.
64 *
65 * @internal
66 *
67 * @note This signal uses integers with the range <tt>[0, 255]</tt> as
68 * return values because this is the maximum precision of the underlying
69 * implementation: The QColorDialog implementation rounds on this
70 * precision when the user pushes the ESC key, even if the previous
71 * value was more exact. */
72 // Choosing three “double” values as return type, as it makes clear
73 // what data type returns and as “Portal” actually provides
74 // double-precision in its return values.
75 void newColor(double red, double green, double blue, bool isSRgbGuaranteed);
76
77private:
78 /** @internal @brief Only for unit tests. */
79 friend class TestScreenColorPicker;
80
81 void pickWithPortal();
82 [[nodiscard]] static bool hasPortalSupport();
83 void initializeQColorDialogSupport();
84 [[nodiscard]] static bool queryPortalSupport();
85 [[nodiscard]] static QString translateViaQColorDialog(const char *sourceText);
86 /** @brief If on the current platform there is support for
87 * QColorDialog-based screen color picking.
88 *
89 * Might hold an empty value if @ref initializeQColorDialogSupport has
90 * never been called.
91 *
92 * @warning The declaration as <tt>static in‍line</tt> can be problematic:
93 * At least when linking on MSVC against a shared/static library,
94 * apparently there are two instances of this variable: One that is used
95 * within the shared/dynamic library and another one that is used within
96 * the executable that links against this library. While on GCC and Clang
97 * this does not happen, maybe this behaviour is implementation-defined.
98 * And we do not want to rely on implementation-defined behaviour. However,
99 * because the variable is <tt>private</tt>, this won't make any problems
100 * under normal circumstances, because it's inaccessible anyway. Only when
101 * doing a whitebox test and bypass the private access modifier via the
102 * @ref ScreenColorPicker::TestScreenColorPicker "friend declaration" for
103 * unit tests, you might see the wrong variable and consequently possibly
104 * the wrong value. Therefore, unit tests should only access this variable
105 * when building against the static library. */
106 static inline std::optional<bool> m_hasQColorDialogSupport = std::nullopt;
107 /** @brief The hidden QColorDialog widget (if any).
108 *
109 * @sa @ref initializeQColorDialogSupport */
110 QPointer<QColorDialog> m_qColorDialog;
111 /** @brief The screen-color-picker button of the hidden QColorDialog widget
112 * (if any).
113 *
114 * @sa @ref initializeQColorDialogSupport */
115 QPointer<QPushButton> m_qColorDialogScreenButton;
116
117private Q_SLOTS:
118 void getPortalResponse(uint exitCode, const QVariantMap &responseArguments);
119};
120
121} // namespace PerceptualColor
122
123#endif // SCREENCOLORPICKER
The namespace of this library.
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.