DtkDeclarative
DTK Declarative module
载入中...
搜索中...
未找到
dquickcontrolpalette_p.h
1// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5#ifndef DQUICKCONTROLPALETTE_P_H
6#define DQUICKCONTROLPALETTE_P_H
7
8#include <dtkdeclarative_global.h>
9#include "dqmlglobalobject_p.h"
10
11#include <QColor>
12#include <QObject>
13#include <QQmlListProperty>
14#include <QQmlEngine>
15#include <QVector>
16#include <QPointer>
17
18#include <private/qqmldata_p.h>
19
20QT_BEGIN_NAMESPACE
21class QQuickItem;
22class QQuickWindow;
23QT_END_NAMESPACE
24
25DQUICK_BEGIN_NAMESPACE
27{
28 friend class DQuickControlPalette;
29 Q_GADGET
30 Q_PROPERTY(DTK_QUICK_NAMESPACE::DColor common READ common WRITE setCommon FINAL)
31 Q_PROPERTY(DTK_QUICK_NAMESPACE::DColor crystal READ crystal WRITE setCrystal FINAL)
32#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
33 QML_ANONYMOUS
34#endif
35
36public:
38 DQuickControlColor(const DColor &color);
40 {data = std::move(other.data); other.data = nullptr;
41 changed = std::move(other.changed);
42 isSingleColor = std::move(other.isSingleColor);}
44 isSingleColor = other.isSingleColor;
45 if (other.isSingleColor) {
46 data = new DColor(*other.data);
47 } else {
48 data = other.data;
49 changed = other.changed;
50 }
51 }
52 inline DQuickControlColor &operator=(const DQuickControlColor &other) {
53 if (isSingleColor) {
54 if (other.isSingleColor) {
55 *data = *other.data;
56 } else {
57 delete data;
58 data = other.data;
59 isSingleColor = false;
60 }
61 } else {
62 if (other.isSingleColor) {
63 if (data) {
64 setCommon(*other.data);
65 } else {
66 data = new DColor(*other.data);
67 }
68 } else {
69 data = other.data;
70 }
71 }
72 changed = other.changed;
73 return *this;
74 }
75 inline DQuickControlColor &operator=(DQuickControlColor &&other) {
76 if (data && isSingleColor) {
77 delete data;
78 }
79
80 data = std::move(other.data);
81 other.data = nullptr;
82 changed = other.changed;
83 isSingleColor = other.isSingleColor;
84 return *this;
85 }
87
88 const DColor &common() const;
89 void setCommon(const DColor &newCommon);
90 const DColor &crystal() const;
91 void setCrystal(const DColor &newCrystal);
92
93private:
95
96 DColor *data = nullptr;
97 bool changed = false;
98 bool isSingleColor = false;
99};
100DQUICK_END_NAMESPACE
101Q_DECLARE_METATYPE(DTK_QUICK_NAMESPACE::DQuickControlColor)
102
103DQUICK_BEGIN_NAMESPACE
104
105class DQuickControlPalette : public QObject
106{
107 friend class DQuickControlColorSelector;
108 Q_OBJECT
109 Q_DISABLE_COPY(DQuickControlPalette)
110 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
111 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlColor normal READ normal WRITE setNormal NOTIFY normalChanged)
112 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlColor normalDark READ normalDark WRITE setNormalDark NOTIFY normalDarkChanged)
113 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlColor hovered READ hovered WRITE setHovered NOTIFY hoveredChanged)
114 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlColor hoveredDark READ hoveredDark WRITE setHoveredDark NOTIFY hoveredDarkChanged)
115 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlColor pressed READ pressed WRITE setPressed NOTIFY pressedChanged)
116 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlColor pressedDark READ pressedDark WRITE setPressedDark NOTIFY pressedDarkChanged)
117 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlColor disabled READ disabled WRITE setDisabled NOTIFY disabledChanged)
118 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlColor disabledDark READ disabledDark WRITE setDisabledDark NOTIFY disabledDarkChanged)
119
120#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
121 QML_NAMED_ELEMENT(Palette)
122#endif
123public:
124 enum ColorFamily {
125 CommonColor = 0,
126 CrystalColor = 1,
127 ColorFamilyCount
128 };
129 Q_ENUM(ColorFamily)
130
131 enum ColorGroup {
132 Light = 0,
133 Dark = 1 * ColorFamilyCount,
134 ThemeTypeCount = 2,
135
136 Normal = 0,
137 Hovered = 1 * (ThemeTypeCount + ColorFamilyCount),
138 Pressed = 2 * (ThemeTypeCount + ColorFamilyCount),
139 Disabled = 3 * (ThemeTypeCount + ColorFamilyCount),
140 ColorTypeCount = 4
141 };
142
143 explicit DQuickControlPalette(QObject *parent = nullptr);
144 ~DQuickControlPalette();
145
146 bool enabled() const;
147 void setEnabled(bool newEnabled);
148
149 inline DColor *colorPointer(int colorPropertyIndex) {
150 return colors.data() + colorPropertyIndex * ColorFamilyCount;
151 }
152 inline bool setTo(const DQuickControlColor &color, int colorPropertyIndex) {
153 auto colors = colorPointer(colorPropertyIndex);
154 Q_ASSERT(colors);
155 if (color.data == colors)
156 return color.changed;
157 bool changed = false;
158 const int colorCount = color.isSingleColor ? 1 : ColorFamilyCount;
159 for (int i = 0; i < colorCount; ++i) {
160 if (color.data[i] == colors[i])
161 continue;
162 colors[i] = color.data[i];
163 changed = true;
164 }
165 return changed;
166 }
167 void setNormal(const DQuickControlColor &color) {
168 if (!setTo(color, 0))
169 return;
170 Q_EMIT normalChanged();
171 Q_EMIT changed();
172 }
173 DQuickControlColor normal() {
174 return DQuickControlColor(colorPointer(0));
175 }
176 void setNormalDark(const DQuickControlColor &color) {
177 if (!setTo(color, 1))
178 return;
179 Q_EMIT normalDarkChanged();
180 Q_EMIT changed();
181 }
182 DQuickControlColor normalDark() {
183 return DQuickControlColor(colorPointer(1));
184 }
185 void setHovered(const DQuickControlColor &color) {
186 if (!setTo(color, 2))
187 return;
188 Q_EMIT hoveredChanged();
189 Q_EMIT changed();
190 }
191 DQuickControlColor hovered() {
192 return DQuickControlColor(colorPointer(2));
193 }
194 void setHoveredDark(const DQuickControlColor &color) {
195 if (!setTo(color, 3))
196 return;
197 Q_EMIT hoveredDarkChanged();
198 Q_EMIT changed();
199 }
200 DQuickControlColor hoveredDark() {
201 return DQuickControlColor(colorPointer(3));
202 }
203 void setPressed(const DQuickControlColor &color) {
204 if (!setTo(color, 4))
205 return;
206 Q_EMIT pressedChanged();
207 Q_EMIT changed();
208 }
209 DQuickControlColor pressed() {
210 return DQuickControlColor(colorPointer(4));
211 }
212 void setPressedDark(const DQuickControlColor &color) {
213 if (!setTo(color, 5))
214 return;
215 Q_EMIT pressedDarkChanged();
216 Q_EMIT changed();
217 }
218 DQuickControlColor pressedDark() {
219 return DQuickControlColor(colorPointer(5));
220 }
221 void setDisabled(const DQuickControlColor &color) {
222 if (!setTo(color, 6))
223 return;
224 Q_EMIT disabledChanged();
225 Q_EMIT changed();
226 }
227 DQuickControlColor disabled() {
228 return DQuickControlColor(colorPointer(6));
229 }
230 void setDisabledDark(const DQuickControlColor &color) {
231 if (!setTo(color, 7))
232 return;
233 Q_EMIT disabledDarkChanged();
234 Q_EMIT changed();
235 }
236 DQuickControlColor disabledDark() {
237 return DQuickControlColor(colorPointer(7));
238 }
239
240Q_SIGNALS:
241 void enabledChanged();
242 void normalChanged();
243 void normalDarkChanged();
244 void hoveredChanged();
245 void hoveredDarkChanged();
246 void pressedChanged();
247 void pressedDarkChanged();
248 void disabledChanged();
249 void disabledDarkChanged();
250 void changed();
251
252public:
253 QVector<DColor> colors;
254
255private:
256 bool m_enabled = true;
257};
258DQUICK_END_NAMESPACE
259QML_DECLARE_TYPE(DTK_QUICK_NAMESPACE::DQuickControlPalette)
260
261DQUICK_BEGIN_NAMESPACE
262class CustomMetaObject;
263class DQuickControlColorSelector : public QObject
264{
265 friend class CustomMetaObject;
266 Q_OBJECT
267#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
268 QML_UNCREATABLE("ColorSelector is only available as an attached property.")
269 QML_NAMED_ELEMENT(ColorSelector)
270 QML_ATTACHED(DQuickControlColorSelector)
271#endif
272 Q_DISABLE_COPY(DQuickControlColorSelector)
273public:
274 Q_PROPERTY(QQuickItem *control READ control NOTIFY controlChanged)
275 Q_PROPERTY(DTK_GUI_NAMESPACE::DGuiApplicationHelper::ColorType controlTheme READ controlTheme NOTIFY controlThemeChanged)
276 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQMLGlobalObject::ControlState controlState READ controlState NOTIFY controlStateChanged)
277 Q_PROPERTY(DTK_QUICK_NAMESPACE::DQuickControlPalette::ColorFamily family READ family WRITE setFamily RESET resetFamily NOTIFY familyChanged)
278 Q_PROPERTY(bool hovered READ hovered WRITE setHovered RESET resetHovered NOTIFY hoveredChanged)
279 Q_PROPERTY(bool pressed READ pressed WRITE setPressed RESET resetPressed NOTIFY pressedChanged)
280 Q_PROPERTY(bool disabled READ disabled WRITE setDisabled RESET resetDisabled NOTIFY disabledChanged)
281 Q_PROPERTY(bool inactived READ inactived WRITE setInactived RESET resetInactived NOTIFY inactivedChanged)
282 Q_CLASSINFO("DefaultProperty", "palettes")
283
284 explicit DQuickControlColorSelector(QQuickItem *parent);
286
287 static DQuickControlColorSelector *qmlAttachedProperties(QObject *object);
288
289 QQuickItem *control() const;
290 void setControl(QQuickItem *newControl);
291
292 DQMLGlobalObject::ControlState controlState() const;
293 DGuiApplicationHelper::ColorType controlTheme() const;
294 Dtk::Quick::DQuickControlPalette::ColorFamily family() const;
295 void setFamily(const DQuickControlPalette::ColorFamily &newFamily);
296 void resetFamily();
297
298 bool hovered() const;
299 void setHovered(bool newHovered);
300 void resetHovered();
301
302 bool pressed() const;
303 void setPressed(bool newPressed);
304 void resetPressed();
305
306 bool disabled() const;
307 void setDisabled(bool newDisabled);
308 void resetDisabled();
309
310 bool inactived() const;
311 void setInactived(bool newInactived);
312 void resetInactived();
313
314 static QStringList specialObjectNameItems();
315
316Q_SIGNALS:
317 void controlThemeChanged();
318 void controlStateChanged();
319 void controlChanged();
320 void hoveredChanged();
321 void pressedChanged();
322 void disabledChanged();
323 void inactivedChanged();
324 void colorPropertyChanged(const QByteArray &name);
325 void colorPropertiesChanged();
326 void familyChanged();
327
328private:
329 bool doGetHoveredRecu(bool *value) const;
330 bool doGetPressedRecu(bool *value) const;
331 bool doGetDisabledRecu(bool *value) const;
332 bool doGetInactivedRecu(bool *value) const;
333 void doSetHovered(bool newHovered, bool isUserSet);
334 void doSetPressed(bool newPressed, bool isUserSet);
335 void doSetDisabled(bool newDisabled, bool isUserSet);
336 void doSetInactived(bool newInactived, bool isUserSet);
337
338 void ensureMetaObject();
339 void setupMetaPropertyPalettes(QQuickItem *object);
340 struct PaletteState;
341 QColor getColorOf(const DQuickControlPalette *palette, const PaletteState *state) const;
342 QColor getColorOf(const QByteArray &propertyName, const PaletteState *state) const;
343
344 int indexOfPalette(const QByteArray &name) const;
345 void setPalette(const QByteArray &name, DQuickControlPalette *palette);
346
347 void updatePaletteFromMetaProperty(const QMetaProperty &mp, const QObject *obj);
348 void updatePropertyFromName(const QByteArray &name, const DQuickControlPalette *palette = nullptr);
349 void updateFamilyForChildrenRecu(QQuickItem *parent);
350
351 void findAndSetControlParent();
352 QByteArray findPalettePropertyName(const DQuickControlPalette *palette) const;
353 void clearAndInheritParentProperties();
354
355 Q_SLOT void updateControlTheme();
356 Q_SLOT bool updateControlState();
357 Q_SLOT void updateAllColorProperties();
358 Q_SLOT void recvPaletteColorChanged();
359 Q_SLOT void onPaletteDestroyed();
360 Q_SLOT void updateControlWindow();
361 Q_SLOT void resolveMetaPropertyChanged();
362 Q_SLOT void notifyColorPropertyChanged();
363
364 bool setControlTheme(DGuiApplicationHelper::ColorType theme);
365 bool setControlState(DQMLGlobalObject::ControlState state);
366 bool doSetFamily(DQuickControlPalette::ColorFamily newFamily);
367 void doResetFamily();
368
369 void tryDestroyPalette(DQuickControlPalette *palette);
370 void setSuperColorSelector(DQuickControlColorSelector *parent);
371 void setFamilyPropertyParent(DQuickControlColorSelector *parent);
372
373 QQuickItem *m_control = nullptr;
374 QQuickWindow *m_controlWindow = nullptr;
375 QPointer<DQuickControlColorSelector> m_superColorSelector;
376 QPointer<DQuickControlColorSelector> m_parentOfFamilyProperty;
377 typedef QPair<QByteArray, DQuickControlPalette*> ControlPaletteData;
378 QList<ControlPaletteData> m_palettes;
379 CustomMetaObject *m_metaObject = nullptr;
380#if QT_VERSION <= QT_VERSION_CHECK(6, 2, 4)
381 QQmlPropertyCache *m_propertyCache = nullptr;
382#else
383 QQmlPropertyCache::ConstPtr m_propertyCache;
384#endif
385 struct PaletteState {
386 PaletteState(DQuickControlColorSelector *owner)
387 : owner(owner)
388 , controlState(DQMLGlobalObject::NormalState)
389 , family(DQuickControlPalette::CommonColor)
390 , familyIsUserSet(false)
391 , hovered(false)
392 , hoveredValueValid(false)
393 , pressed(false)
394 , pressedValueValid(false)
395 , disabled(false)
396 , disabledValueValid(false)
397 , inactived(false)
398 , inactivedValueValid(false)
399 {
400 if (auto appHelper = DGuiApplicationHelper::instance()) {
401 controlTheme = appHelper->themeType();
402 } else {
403 controlTheme = DGuiApplicationHelper::LightType;
404 }
405 }
406
407 DQuickControlColorSelector *owner = nullptr;
408 DGuiApplicationHelper::ColorType controlTheme;
409 DQMLGlobalObject::ControlState controlState;
410 DQuickControlPalette::ColorFamily family;
411 uint familyIsUserSet:1;
412 uint hovered:1;
413 uint hoveredValueValid:1;
414 uint pressed:1;
415 uint pressedValueValid:1;
416 uint disabled:1;
417 uint disabledValueValid:1;
418 uint inactived:1;
419 uint inactivedValueValid:1;
420 };
421 QScopedPointer<PaletteState> m_state;
422 QList<QMetaObject::Connection> m_itemParentChangeConnections;
423};
424
425DQUICK_END_NAMESPACE
426QML_DECLARE_TYPE(DTK_QUICK_NAMESPACE::DQuickControlColorSelector)
427QML_DECLARE_TYPEINFO(DTK_QUICK_NAMESPACE::DQuickControlColorSelector, QML_HAS_ATTACHED_PROPERTIES)
428
429#endif // DQUICKCONTROLPALETTE_P_H
DTK 用于指定控件颜色的统一取色辅助器.
Definition dquickcontrolpalette.cpp:155
Definition dqmlglobalobject_p.h:34
Definition dquickcontrolpalette_p.h:264
Definition dquickcontrolpalette_p.h:27
DTK 中用于取色器(ColorSelector)使用的统一调色板对象.