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
qquickcolorinputs.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
6#include <QtCore/QRegularExpression>
7
9
11
13{
14 return QColor::fromHsvF(m_hsva.h, m_hsva.s, m_hsva.v, m_hsva.a);
15}
16
18{
19 if (color().rgba() == c.rgba())
20 return;
21
22 // If we get a QColor from an Hsv or Hsl color system,
23 // we want to get the raw values without the risk of QColor converting them,
24 // and possible deleting relevant information for achromatic cases.
25 if (c.spec() == QColor::Spec::Hsl) {
26 const auto sv = getSaturationAndValue(c.hslSaturationF(), c.lightnessF());
27 m_hsva.h = qBound(.0, c.hslHueF(), 1.0);
28 m_hsva.s = qBound(.0, sv.first, 1.0);
29 m_hsva.v = qBound(.0, sv.second, 1.0);
30 } else {
31 m_hsva.h = qBound(.0, c.hsvHueF(), 1.0);
32 m_hsva.s = qBound(.0, c.hsvSaturationF(), 1.0);
33 m_hsva.v = qBound(.0, c.valueF(), 1.0);
34 }
35
36 m_hsva.a = c.alphaF();
37
39}
40
42{
43 return color().red();
44}
45
47{
48 return color().green();
49}
50
52{
53 return color().blue();
54}
55
57{
58 return m_hsva.a;
59}
60
62{
63 return m_hsva.h;
64}
65
67{
68 return getSaturationAndLightness(m_hsva.s, m_hsva.v).first;
69}
70
72{
73 return m_hsva.s;
74}
75
77{
78 return m_hsva.v;
79}
80
82{
83 return getSaturationAndLightness(m_hsva.s, m_hsva.v).second;
84}
85
87{
88 return m_showAlpha;
89}
90
92{
93 if (m_showAlpha == showAlpha)
94 return;
95
96 m_showAlpha = showAlpha;
97 emit showAlphaChanged(m_showAlpha);
98}
99
101{
102 return m_hexInput;
103}
104
106{
107 if (m_hexInput == hexInput)
108 return;
109
110 if (m_hexInput)
111 disconnect(m_hexInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHexChanged);
112
113 m_hexInput = hexInput;
114
115 if (m_hexInput)
116 connect(m_hexInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHexChanged);
117
119}
120
122{
123 return m_redInput;
124}
125
127{
128 if (m_redInput == redInput)
129 return;
130
131 if (m_redInput)
132 disconnect(m_redInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleRedChanged);
133
134 m_redInput = redInput;
135
136 if (m_redInput)
137 connect(m_redInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleRedChanged);
138
140}
141
143{
144 return m_greenInput;
145}
146
148{
149 if (m_greenInput == greenInput)
150 return;
151
152 if (m_greenInput)
153 disconnect(m_greenInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleGreenChanged);
154
155 m_greenInput = greenInput;
156
157 if (m_greenInput)
158 connect(m_greenInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleGreenChanged);
159
161}
162
164{
165 return m_blueInput;
166}
167
169{
170 if (m_blueInput == blueInput)
171 return;
172
173 if (m_blueInput)
174 disconnect(m_blueInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleBlueChanged);
175
176 m_blueInput = blueInput;
177
178 if (m_blueInput)
179 connect(m_blueInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleBlueChanged);
180
182}
183
185{
186 return m_hsvHueInput;
187}
188
190{
191 if (m_hsvHueInput == hsvHueInput)
192 return;
193
194 if (m_hsvHueInput)
195 disconnect(m_hsvHueInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHsvHueChanged);
196
197 m_hsvHueInput = hsvHueInput;
198
199 if (m_hsvHueInput)
200 connect(m_hsvHueInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHsvHueChanged);
201
203}
204
206{
207 return m_hslHueInput;
208}
209
211{
212 if (m_hslHueInput == hslHueInput)
213 return;
214
215 if (m_hslHueInput)
216 disconnect(m_hslHueInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHslHueChanged);
217
218 m_hslHueInput = hslHueInput;
219
220 if (m_hslHueInput)
221 connect(m_hslHueInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHslHueChanged);
222
224}
225
227{
228 return m_hsvSaturationInput;
229}
230
232{
233 if (m_hsvSaturationInput == hsvSaturationInput)
234 return;
235
236 if (m_hsvSaturationInput)
237 disconnect(m_hsvSaturationInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHsvSaturationChanged);
238
239 m_hsvSaturationInput = hsvSaturationInput;
240
241 if (m_hsvSaturationInput)
242 connect(m_hsvSaturationInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHsvSaturationChanged);
243
245}
246
248{
249 return m_hslSaturationInput;
250}
251
253{
254 if (m_hslSaturationInput == hslSaturationInput)
255 return;
256
257 if (m_hslSaturationInput)
258 disconnect(m_hslSaturationInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHslSaturationChanged);
259
260 m_hslSaturationInput = hslSaturationInput;
261
262 if (m_hslSaturationInput)
263 connect(m_hslSaturationInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHslSaturationChanged);
264
266}
267
269{
270 return m_valueInput;
271}
272
274{
275 if (m_valueInput == valueInput)
276 return;
277
278 if (m_valueInput)
279 disconnect(m_valueInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleValueChanged);
280
281 m_valueInput = valueInput;
282
283 if (m_valueInput)
284 connect(m_valueInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleValueChanged);
285
287}
288
290{
291 return m_lightnessInput;
292}
293
295{
296 if (m_lightnessInput == lightnessInput)
297 return;
298
299 if (m_lightnessInput)
300 disconnect(m_lightnessInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleLightnessChanged);
301
302 m_lightnessInput = lightnessInput;
303
304 if (m_lightnessInput)
305 connect(m_lightnessInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleLightnessChanged);
306
308}
309
311{
312 return m_rgbAlphaInput;
313}
314
316{
317 if (alphaInput == m_rgbAlphaInput)
318 return;
319
320 if (m_rgbAlphaInput) {
321 disconnect(m_rgbAlphaInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleRgbAlphaChanged);
323 }
324
325 m_rgbAlphaInput = alphaInput;
326
327 if (m_rgbAlphaInput) {
328 connect(m_rgbAlphaInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleRgbAlphaChanged);
330 m_rgbAlphaInput->setVisible(showAlpha());
331 }
332
334}
335
337{
338 return m_hsvAlphaInput;
339}
340
342{
343 if (alphaInput == m_hsvAlphaInput)
344 return;
345
346 if (m_hsvAlphaInput) {
347 disconnect(m_hsvAlphaInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHsvAlphaChanged);
349 }
350
351 m_hsvAlphaInput = alphaInput;
352
353 if (m_hsvAlphaInput) {
354 connect(m_hsvAlphaInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHsvAlphaChanged);
356 m_hsvAlphaInput->setVisible(showAlpha());
357 }
358
360}
361
363{
364 return m_hslAlphaInput;
365}
366
368{
369 if (alphaInput == m_hslAlphaInput)
370 return;
371
372 if (m_hslAlphaInput) {
373 disconnect(m_hslAlphaInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHslAlphaChanged);
375 }
376
377 m_hslAlphaInput = alphaInput;
378
379 if (m_hslAlphaInput) {
380 connect(m_hslAlphaInput, &QQuickTextInput::editingFinished, this, &QQuickColorInputs::handleHslAlphaChanged);
382 m_hslAlphaInput->setVisible(showAlpha());
383 }
384
386}
387
388void QQuickColorInputs::handleHexChanged()
389{
391}
392
393void QQuickColorInputs::handleRedChanged()
394{
395 QColor c = color();
396 c.setRed(qBound(0, m_redInput->text().toInt(), 255));
398}
399
400void QQuickColorInputs::handleGreenChanged()
401{
402 QColor c = color();
403 c.setGreen(qBound(0, m_greenInput->text().toInt(), 255));
405}
406
407void QQuickColorInputs::handleBlueChanged()
408{
409 QColor c = color();
410 c.setBlue(qBound(0, m_blueInput->text().toInt(), 255));
412}
413
416
417void QQuickColorInputs::handleHsvHueChanged()
418{
420 const auto match = pattern.match(m_hsvHueInput->text());
421 if (match.hasMatch()) {
422 const auto substr = match.captured(1);
423 const qreal input = static_cast<qreal>(qBound(0, substr.toInt(), 360)) / static_cast<qreal>(360);
425 }
426}
427
428void QQuickColorInputs::handleHslHueChanged()
429{
431 const auto match = pattern.match(m_hslHueInput->text());
432 if (match.hasMatch()) {
433 const auto substr = match.captured(1);
434 const qreal input = static_cast<qreal>(qBound(0, substr.toInt(), 360)) / static_cast<qreal>(360);
436 }
437}
438
439void QQuickColorInputs::handleHsvSaturationChanged()
440{
442 const auto match = pattern.match(m_hsvSaturationInput->text());
443 if (match.hasMatch()) {
444 const auto substr = match.captured(1);
445 const qreal input = static_cast<qreal>(qBound(0, substr.toInt(), 100)) / static_cast<qreal>(100);
447 }
448}
449
450void QQuickColorInputs::handleHslSaturationChanged()
451{
453 const auto match = pattern.match(m_hslSaturationInput->text());
454 if (match.hasMatch()) {
455 const auto substr = match.captured(1);
456 const qreal input = static_cast<qreal>(qBound(0, substr.toInt(), 100)) / static_cast<qreal>(100);
458 }
459}
460
461void QQuickColorInputs::handleValueChanged()
462{
464 const auto match = pattern.match(m_valueInput->text());
465 if (match.hasMatch()) {
466 const auto substr = match.captured(1);
467 const qreal input = static_cast<qreal>(qBound(0, substr.toInt(), 100)) / static_cast<qreal>(100);
469 }
470}
471
472void QQuickColorInputs::handleLightnessChanged()
473{
475 const auto match = pattern.match(m_lightnessInput->text());
476 if (match.hasMatch()) {
477 const auto substr = match.captured(1);
478 const qreal input = static_cast<qreal>(qBound(0, substr.toInt(), 100)) / static_cast<qreal>(100);
480 }
481}
482
483void QQuickColorInputs::handleRgbAlphaChanged()
484{
485 handleAlphaChanged(m_rgbAlphaInput->text());
486}
487
488void QQuickColorInputs::handleHsvAlphaChanged()
489{
490 handleAlphaChanged(m_hsvAlphaInput->text());
491}
492
493void QQuickColorInputs::handleHslAlphaChanged()
494{
495 handleAlphaChanged(m_hslAlphaInput->text());
496}
497
498void QQuickColorInputs::handleAlphaChanged(const QString &input)
499{
501 const auto match = pattern.match(input);
502 if (match.hasMatch()) {
503 QColor c = color();
504 const auto substr = match.captured(1);
505 const qreal input = static_cast<qreal>(qBound(0, substr.toInt(), 100)) / static_cast<qreal>(100);
506 c.setAlphaF(input);
508 }
509}
510
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
@ 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
QRgb rgba() const noexcept
Returns the RGB value of the color, including its alpha.
Definition qcolor.cpp:1376
int red() const noexcept
Returns the red color component of this color.
Definition qcolor.cpp:1528
static QColor fromString(QAnyStringView name) noexcept
Definition qcolor.cpp:980
int blue() const noexcept
Returns the blue color component of this color.
Definition qcolor.cpp:1583
int green() const noexcept
Returns the green color component of this color.
Definition qcolor.cpp:1555
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
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void rgbAlphaInputChanged()
void setHsvAlphaInput(QQuickTextInput *alphaInput)
void setShowAlpha(bool showAlpha)
void showAlphaChanged(bool)
void setRedInput(QQuickTextInput *redInput)
void setGreenInput(QQuickTextInput *greenInput)
void hslAlphaInputChanged()
QQuickTextInput * hslHueInput
void setRgbAlphaInput(QQuickTextInput *alphaInput)
void setLightnessInput(QQuickTextInput *lightnessInput)
void hsvAlphaInputChanged()
QQuickTextInput * hslSaturationInput
void setBlueInput(QQuickTextInput *blueInput)
void hsvSaturationInputChanged()
void setHsvHueInput(QQuickTextInput *hsvHueInput)
void setHsvSaturationInput(QQuickTextInput *hsvSaturationInput)
void colorModified(const QColor &c)
void setColor(const QColor &c)
QQuickTextInput * lightnessInput
QQuickTextInput * hexInput
QQuickTextInput * hsvSaturationInput
QQuickTextInput * hsvAlphaInput
void valueInputChanged()
void hsvHueInputChanged()
void hslSaturationInputChanged()
void setHslSaturationInput(QQuickTextInput *hslSaturationInput)
QQuickTextInput * hsvHueInput
void setHslHueInput(QQuickTextInput *hslHueInput)
void setValueInput(QQuickTextInput *valueInput)
void lightnessInputChanged()
void setHexInput(QQuickTextInput *hexInput)
void hslHueInputChanged()
QQuickTextInput * valueInput
QQuickTextInput * hslAlphaInput
void greenInputChanged()
void setHslAlphaInput(QQuickTextInput *alphaInput)
QQuickTextInput * greenInput
QQuickTextInput * redInput
QQuickTextInput * rgbAlphaInput
QQuickTextInput * blueInput
void setVisible(bool)
\inmodule QtCore \reentrant
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
int toInt(bool *ok=nullptr, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition qstring.h:731
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
void colorChanged()
Combined button and popup list for selecting options.
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
const GLubyte * c
GLubyte * pattern
GLenum GLenum GLenum input
std::pair< qreal, qreal > getSaturationAndLightness(qreal saturation, qreal value)
std::pair< qreal, qreal > getSaturationAndValue(qreal saturation, qreal lightness)
static QString s_degree_pattern
static QString s_percentage_pattern
#define emit
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)
double qreal
Definition qtypes.h:187
myObject disconnect()
[26]