Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickabstractcolorpicker.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
7
8#include <QtQuickTemplates2/private/qquickcontrol_p_p.h>
9#include <QtQuickTemplates2/private/qquickdeferredexecute_p_p.h>
10
11#include <qpa/qplatformintegration.h>
12#include <private/qguiapplication_p.h>
13
15
17{
19 QQuickControlPrivate::handlePress(point, timestamp);
20 m_pressPoint = point;
21 q->setPressed(true);
22 q->updateColor(point);
23 return true;
24}
25
27{
29 QQuickControlPrivate::handleMove(point, timestamp);
30 if (point != m_pressPoint)
31 q->updateColor(point);
32 return true;
33}
34
36{
40 q->setKeepMouseGrab(false);
41 q->setKeepTouchGrab(false);
42 q->setPressed(false);
43 q->updateColor(point);
44 return true;
45}
46
54
60
62{
65 return;
66
67 if (!m_handle || complete)
69 if (complete)
71}
72
80
88
96
98{
100 return d->m_hsl ? QColor::fromHslF(d->m_hsva.h, d->m_hsva.s, d->m_hsva.l, d->m_hsva.a)
101 : QColor::fromHsvF(d->m_hsva.h, d->m_hsva.s, d->m_hsva.v, d->m_hsva.a);
102}
103
105{
107 // QColor represents a theoretical color, rather than simply an rgba value.
108 // Therefore, two QColor objects can be different,
109 // and yet translate to the same rgba value.
110 // Since the color picker can reuse the same rgba value for multiple pixels,
111 // we should not return early if the rgba() values are equal,
112 // but only if the QColor objects are exactly the same.
113
114 if (color() == c)
115 return;
116
117 // When called from QQuickColorDialogImpl, it might not have the same spec as the current color
118 // picker.
119 if (d->m_hsl && c.spec() == QColor::Spec::Hsv) {
120 const auto sl = getSaturationAndLightness(c.hsvSaturationF(), c.valueF());
121 d->m_hsva.h = qBound(.0, c.hsvHueF(), 1.0);
122 d->m_hsva.s = qBound(.0, sl.first, 1.0);
123 d->m_hsva.l = qBound(.0, sl.second, 1.0);
124 } else if (!d->m_hsl && c.spec() == QColor::Spec::Hsl) {
125 const auto sv = getSaturationAndValue(c.hslSaturationF(), c.lightnessF());
126 d->m_hsva.h = qBound(.0, c.hslHueF(), 1.0);
127 d->m_hsva.s = qBound(.0, sv.first, 1.0);
128 d->m_hsva.v = qBound(.0, sv.second, 1.0);
129 } else {
130 d->m_hsva.h = qBound(.0, d->m_hsl ? c.hslHueF() : c.hsvHueF(), 1.0);
131 d->m_hsva.s = qBound(.0, d->m_hsl ? c.hslSaturationF() : c.hsvSaturationF(), 1.0);
132 d->m_hsva.v = qBound(.0, d->m_hsl ? c.lightnessF() : c.valueF(), 1.0);
133 }
134
135 d->m_hsva.a = qBound(.0, c.alphaF(), 1.0);
136
138}
139
141{
142 Q_D(const QQuickAbstractColorPicker);
143 return d->m_hsva.a;
144}
145
147{
149
150 if (!qt_is_finite(alpha))
151 return;
152
153 alpha = qBound(.0, alpha, 1.0);
154
155 if (qFuzzyCompare(d->m_hsva.a, alpha))
156 return;
157
158 d->m_hsva.a = alpha;
159
161}
162
164{
165 Q_D(const QQuickAbstractColorPicker);
166 return d->m_hsva.h;
167}
169{
171
172 if (!qt_is_finite(hue))
173 return;
174
175 d->m_hsva.h = hue;
176
178}
179
181{
182 Q_D(const QQuickAbstractColorPicker);
183 return d->m_hsva.s;
184}
185
187{
190 return;
191
192 d->m_hsva.s = saturation;
193
195}
197{
198 Q_D(const QQuickAbstractColorPicker);
199 return d->m_hsl ? getSaturationAndValue(d->m_hsva.s, d->m_hsva.l).second : d->m_hsva.v;
200}
202{
204 if (!qt_is_finite(value))
205 return;
206
207 const auto sv = d->m_hsl ? getSaturationAndValue(d->m_hsva.s, d->m_hsva.l)
208 : std::pair<qreal, qreal>(d->m_hsva.s, value);
209 d->m_hsva.s = sv.first;
210 d->m_hsva.v = sv.second;
211
213}
214
216{
217 Q_D(const QQuickAbstractColorPicker);
218 return d->m_hsl ? d->m_hsva.l : getSaturationAndLightness(d->m_hsva.s, d->m_hsva.v).second;
219}
221{
224 return;
225
226 const auto sl = !d->m_hsl ? getSaturationAndLightness(d->m_hsva.s, d->m_hsva.v)
227 : std::pair<qreal, qreal>(d->m_hsva.s, lightness);
228 d->m_hsva.s = sl.first;
229 d->m_hsva.l = sl.second;
230
232}
233
240{
241 Q_D(const QQuickAbstractColorPicker);
242 return d->m_pressed;
243}
244
246{
248 if (pressed == d->m_pressed)
249 return;
250
251 d->m_pressed = pressed;
253}
254
261{
263 if (!d->m_handle)
264 d->executeHandle();
265 return d->m_handle;
266}
267
269{
271 if (handle == d->m_handle)
272 return;
273
274 if (!d->m_handle.isExecuting())
275 d->cancelHandle();
276
277 const qreal oldImplicitHandleWidth = implicitHandleWidth();
278 const qreal oldImplicitHandleHeight = implicitHandleHeight();
279
280 d->removeImplicitSizeListener(d->m_handle);
282 d->m_handle = handle;
283
284 if (handle) {
285 if (!handle->parentItem())
286 handle->setParentItem(this);
287 d->addImplicitSizeListener(handle);
288 }
289
290 if (!qFuzzyCompare(oldImplicitHandleWidth, implicitHandleWidth()))
292 if (!qFuzzyCompare(oldImplicitHandleHeight, implicitHandleHeight()))
294 if (!d->m_handle.isExecuting())
296}
297
312{
313 Q_D(const QQuickAbstractColorPicker);
314 if (!d->m_handle)
315 return 0;
316 return d->m_handle->implicitWidth();
317}
318
333{
334 Q_D(const QQuickAbstractColorPicker);
335 if (!d->m_handle)
336 return 0;
337 return d->m_handle->implicitHeight();
338}
339
346
347void QQuickAbstractColorPicker::updateColor(const QPointF &pos)
348{
349 QColor c = colorAt(pos);
350 c.setAlphaF(alpha());
351 setColor(c);
352
354}
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
@ Hsv
Definition qcolor.h:35
@ Hsl
Definition qcolor.h:35
static QColor fromHslF(float h, float s, float l, float a=1.0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcolor.cpp:2594
static QColor fromHsvF(float h, float s, float v, float a=1.0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcolor.cpp:2530
\inmodule QtCore\reentrant
Definition qpoint.h:217
bool handleRelease(const QPointF &point, ulong timestamp) override
void itemImplicitHeightChanged(QQuickItem *item) override
QQuickDeferredPointer< QQuickItem > m_handle
void itemImplicitWidthChanged(QQuickItem *item) override
bool handlePress(const QPointF &point, ulong timestamp) override
bool handleMove(const QPointF &point, ulong timestamp) override
void colorPicked(const QColor &color)
QQuickAbstractColorPicker(QQuickAbstractColorPickerPrivate &dd, QQuickItem *parent)
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
virtual QColor colorAt(const QPointF &pos)=0
void itemImplicitWidthChanged(QQuickItem *item) override
virtual bool handlePress(const QPointF &point, ulong timestamp)
static void hideOldItem(QQuickItem *item)
virtual void handleUngrab()
virtual bool handleRelease(const QPointF &point, ulong timestamp)
virtual bool handleMove(const QPointF &point, ulong timestamp)
void itemImplicitHeightChanged(QQuickItem *item) override
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void setParentItem(QQuickItem *parent)
void setAcceptedMouseButtons(Qt::MouseButtons buttons)
Sets the mouse buttons accepted by this item to buttons.
void setActiveFocusOnTab(bool)
void colorChanged()
@ LeftButton
Definition qnamespace.h:58
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
static Q_DECL_CONST_FUNCTION bool qt_is_finite(double d)
Definition qnumeric_p.h:117
GLuint64 GLenum void * handle
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
std::pair< qreal, qreal > getSaturationAndLightness(qreal saturation, qreal value)
std::pair< qreal, qreal > getSaturationAndValue(qreal saturation, qreal lightness)
void quickCancelDeferred(QObject *object, const QString &property)
void quickCompleteDeferred(QObject *object, const QString &property, QQuickDeferredPointer< T > &delegate)
void quickBeginDeferred(QObject *object, const QString &property, QQuickDeferredPointer< T > &delegate)
#define emit
static QString handleName()
unsigned long ulong
Definition qtypes.h:35
double qreal
Definition qtypes.h:187
QGraphicsItem * item