aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickdialogs/quickdialogsquickimpl/qquickabstractcolorpicker.cpp
blob: 5b5b2abdae46e1c6496a8be22ff8079903ed8aed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qquickabstractcolorpicker_p_p.h"

#include "qquickcolordialogutils_p.h"

#include <QtQuickTemplates2/private/qquickcontrol_p_p.h>
#include <QtQuickTemplates2/private/qquickdeferredexecute_p_p.h>

#include <qpa/qplatformintegration.h>
#include <private/qguiapplication_p.h>

QQuickAbstractColorPickerPrivate::QQuickAbstractColorPickerPrivate() = default;

bool QQuickAbstractColorPickerPrivate::handlePress(const QPointF &point, ulong timestamp)
{
    Q_Q(QQuickAbstractColorPicker);
    QQuickControlPrivate::handlePress(point, timestamp);
    m_pressPoint = point;
    q->setPressed(true);
    q->updateColor(point);
    return true;
}

bool QQuickAbstractColorPickerPrivate::handleMove(const QPointF &point, ulong timestamp)
{
    Q_Q(QQuickAbstractColorPicker);
    QQuickControlPrivate::handleMove(point, timestamp);
    if (point != m_pressPoint)
        q->updateColor(point);
    return true;
}

bool QQuickAbstractColorPickerPrivate::handleRelease(const QPointF &point, ulong timestamp)
{
    Q_Q(QQuickAbstractColorPicker);
    QQuickControlPrivate::handleRelease(point, timestamp);
    m_pressPoint = QPointF();
    q->setKeepMouseGrab(false);
    q->setKeepTouchGrab(false);
    q->setPressed(false);
    q->updateColor(point);
    return true;
}

void QQuickAbstractColorPickerPrivate::handleUngrab()
{
    Q_Q(QQuickAbstractColorPicker);
    QQuickControlPrivate::handleUngrab();
    m_pressPoint = QPointF();
    q->setPressed(false);
}

void QQuickAbstractColorPickerPrivate::cancelHandle()
{
    Q_Q(QQuickAbstractColorPicker);
    quickCancelDeferred(q, handleName());
}

void QQuickAbstractColorPickerPrivate::executeHandle(bool complete)
{
    Q_Q(QQuickAbstractColorPicker);
    if (m_handle.wasExecuted())
        return;

    if (!m_handle || complete)
        quickBeginDeferred(q, handleName(), m_handle);
    if (complete)
        quickCompleteDeferred(q, handleName(), m_handle);
}

void QQuickAbstractColorPickerPrivate::itemImplicitWidthChanged(QQuickItem *item)
{
    Q_Q(QQuickAbstractColorPicker);
    QQuickControlPrivate::itemImplicitWidthChanged(item);
    if (item == m_handle)
        emit q->implicitHandleWidthChanged();
}

void QQuickAbstractColorPickerPrivate::itemImplicitHeightChanged(QQuickItem *item)
{
    Q_Q(QQuickAbstractColorPicker);
    QQuickControlPrivate::itemImplicitHeightChanged(item);
    if (item == m_handle)
        emit q->implicitHandleHeightChanged();
}

QQuickAbstractColorPicker::QQuickAbstractColorPicker(QQuickAbstractColorPickerPrivate &dd,
                                                     QQuickItem *parent)
    : QQuickControl(dd, parent)
{
    setActiveFocusOnTab(true);
    setAcceptedMouseButtons(Qt::LeftButton);
}

QColor QQuickAbstractColorPicker::color() const
{
    Q_D(const QQuickAbstractColorPicker);
    return d->m_hsl ? QColor::fromHslF(d->m_hsva.h, d->m_hsva.s, d->m_hsva.l, d->m_hsva.a)
                    : QColor::fromHsvF(d->m_hsva.h, d->m_hsva.s, d->m_hsva.v, d->m_hsva.a);
}

void QQuickAbstractColorPicker::setColor(const QColor &c)
{
    Q_D(QQuickAbstractColorPicker);
    // QColor represents a theoretical color, rather than simply an rgba value.
    // Therefore, two QColor objects can be different,
    // and yet translate to the same rgba value.
    // Since the color picker can reuse the same rgba value for multiple pixels,
    // we should not return early if the rgba() values are equal,
    // but only if the QColor objects are exactly the same.

    if (color() == c)
        return;

    // When called from QQuickColorDialogImpl, it might not have the same spec as the current color
    // picker.
    if (d->m_hsl && c.spec() == QColor::Spec::Hsv) {
        const auto sl = getSaturationAndLightness(c.hsvSaturationF(), c.valueF());
        d->m_hsva.h = qBound(.0, c.hsvHueF(), 1.0);
        d->m_hsva.s = qBound(.0, sl.first, 1.0);
        d->m_hsva.l = qBound(.0, sl.second, 1.0);
    } else if (!d->m_hsl && c.spec() == QColor::Spec::Hsl) {
        const auto sv = getSaturationAndValue(c.hslSaturationF(), c.lightnessF());
        d->m_hsva.h = qBound(.0, c.hslHueF(), 1.0);
        d->m_hsva.s = qBound(.0, sv.first, 1.0);
        d->m_hsva.v = qBound(.0, sv.second, 1.0);
    } else {
        d->m_hsva.h = qBound(.0, d->m_hsl ? c.hslHueF() : c.hsvHueF(), 1.0);
        d->m_hsva.s = qBound(.0, d->m_hsl ? c.hslSaturationF() : c.hsvSaturationF(), 1.0);
        d->m_hsva.v = qBound(.0, d->m_hsl ? c.lightnessF() : c.valueF(), 1.0);
    }

    d->m_hsva.a = qBound(.0, c.alphaF(), 1.0);

    emit colorChanged(color());
}

qreal QQuickAbstractColorPicker::alpha() const
{
    Q_D(const QQuickAbstractColorPicker);
    return d->m_hsva.a;
}

void QQuickAbstractColorPicker::setAlpha(qreal alpha)
{
    Q_D(QQuickAbstractColorPicker);

    if (!qt_is_finite(alpha))
        return;

    alpha = qBound(.0, alpha, 1.0);

    if (qFuzzyCompare(d->m_hsva.a, alpha))
        return;

    d->m_hsva.a = alpha;

    emit colorChanged(color());
}

qreal QQuickAbstractColorPicker::hue() const
{
    Q_D(const QQuickAbstractColorPicker);
    return d->m_hsva.h;
}
void QQuickAbstractColorPicker::setHue(qreal hue)
{
    Q_D(QQuickAbstractColorPicker);

    if (!qt_is_finite(hue))
        return;

    d->m_hsva.h = hue;

    emit colorChanged(color());
}

qreal QQuickAbstractColorPicker::saturation() const
{
    Q_D(const QQuickAbstractColorPicker);
    return d->m_hsva.s;
}

void QQuickAbstractColorPicker::setSaturation(qreal saturation)
{
    Q_D(QQuickAbstractColorPicker);
    if (!qt_is_finite(saturation))
        return;

    d->m_hsva.s = saturation;

    emit colorChanged(color());
}
qreal QQuickAbstractColorPicker::value() const
{
    Q_D(const QQuickAbstractColorPicker);
    return d->m_hsl ? getSaturationAndValue(d->m_hsva.s, d->m_hsva.l).second : d->m_hsva.v;
}
void QQuickAbstractColorPicker::setValue(qreal value)
{
    Q_D(QQuickAbstractColorPicker);
    if (!qt_is_finite(value))
        return;

    const auto sv = d->m_hsl ? getSaturationAndValue(d->m_hsva.s, d->m_hsva.l)
                       : std::pair<qreal, qreal>(d->m_hsva.s, value);
    d->m_hsva.s = sv.first;
    d->m_hsva.v = sv.second;

    emit colorChanged(color());
}

qreal QQuickAbstractColorPicker::lightness() const
{
    Q_D(const QQuickAbstractColorPicker);
    return d->m_hsl ? d->m_hsva.l : getSaturationAndLightness(d->m_hsva.s, d->m_hsva.v).second;
}
void QQuickAbstractColorPicker::setLightness(qreal lightness)
{
    Q_D(QQuickAbstractColorPicker);
    if (!qt_is_finite(lightness))
        return;

    const auto sl = !d->m_hsl ? getSaturationAndLightness(d->m_hsva.s, d->m_hsva.v)
                        : std::pair<qreal, qreal>(d->m_hsva.s, lightness);
    d->m_hsva.s = sl.first;
    d->m_hsva.l = sl.second;

    emit colorChanged(color());
}

/*!
    \internal

    This property holds whether the slider is pressed.
*/
bool QQuickAbstractColorPicker::isPressed() const
{
    Q_D(const QQuickAbstractColorPicker);
    return d->m_pressed;
}

void QQuickAbstractColorPicker::setPressed(bool pressed)
{
    Q_D(QQuickAbstractColorPicker);
    if (pressed == d->m_pressed)
        return;

    d->m_pressed = pressed;
    emit pressedChanged();
}

/*!
    \internal

    This property holds the handle item.
*/
QQuickItem *QQuickAbstractColorPicker::handle() const
{
    QQuickAbstractColorPickerPrivate *d = const_cast<QQuickAbstractColorPickerPrivate *>(d_func());
    if (!d->m_handle)
        d->executeHandle();
    return d->m_handle;
}

void QQuickAbstractColorPicker::setHandle(QQuickItem *handle)
{
    Q_D(QQuickAbstractColorPicker);
    if (handle == d->m_handle)
        return;

    if (!d->m_handle.isExecuting())
        d->cancelHandle();

    const qreal oldImplicitHandleWidth = implicitHandleWidth();
    const qreal oldImplicitHandleHeight = implicitHandleHeight();

    d->removeImplicitSizeListener(d->m_handle);
    QQuickControlPrivate::hideOldItem(d->m_handle);
    d->m_handle = handle;

    if (handle) {
        if (!handle->parentItem())
            handle->setParentItem(this);
        d->addImplicitSizeListener(handle);
    }

    if (!qFuzzyCompare(oldImplicitHandleWidth, implicitHandleWidth()))
        emit implicitHandleWidthChanged();
    if (!qFuzzyCompare(oldImplicitHandleHeight, implicitHandleHeight()))
        emit implicitHandleHeightChanged();
    if (!d->m_handle.isExecuting())
        emit handleChanged();
}

/*!
    \internal
    \readonly

    This property holds the implicit handle width.

    The value is equal to \c {handle ? handle.implicitWidth : 0}.

    This is typically used, together with \l {Control::}{implicitContentWidth} and
    \l {Control::}{implicitBackgroundWidth}, to calculate the \l {Item::}{implicitWidth}.

    \sa implicitHandleHeight
*/
qreal QQuickAbstractColorPicker::implicitHandleWidth() const
{
    Q_D(const QQuickAbstractColorPicker);
    if (!d->m_handle)
        return 0;
    return d->m_handle->implicitWidth();
}

/*!
    \internal
    \readonly

    This property holds the implicit handle height.

    The value is equal to \c {handle ? handle.implicitHeight : 0}.

    This is typically used, together with \l {Control::}{implicitContentHeight} and
    \l {Control::}{implicitBackgroundHeight}, to calculate the \l {Item::}{implicitHeight}.

    \sa implicitHandleWidth
*/
qreal QQuickAbstractColorPicker::implicitHandleHeight() const
{
    Q_D(const QQuickAbstractColorPicker);
    if (!d->m_handle)
        return 0;
    return d->m_handle->implicitHeight();
}

void QQuickAbstractColorPicker::componentComplete()
{
    Q_D(QQuickAbstractColorPicker);
    d->executeHandle(true);
    QQuickControl::componentComplete();
}

void QQuickAbstractColorPicker::updateColor(const QPointF &pos)
{
    QColor c = colorAt(pos);
    c.setAlphaF(alpha());
    setColor(c);

    emit colorPicked(c);
}