1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickpaletteprovider_p.h"
38#include "qquickpalette_p.h"
39
40#include <QtQml/private/qqmlvaluetype_p.h>
41
42QT_BEGIN_NAMESPACE
43
44static QQmlValueTypeProvider *instance()
45{
46 static QQuickPaletteProvider provider;
47 return &provider;
48}
49
50void QQuickPaletteProvider::init()
51{
52 QQml_addValueTypeProvider(instance());
53}
54
55void QQuickPaletteProvider::cleanup()
56{
57 QQml_removeValueTypeProvider(instance());
58}
59
60#if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
61 #define ASSERT_VALID_SIZE(size, min) Q_UNUSED(size)
62#else
63 #define ASSERT_VALID_SIZE(size, min) Q_ASSERT(size >= min)
64#endif
65
66const QMetaObject *QQuickPaletteProvider::getMetaObjectForMetaType(int type)
67{
68 switch (type) {
69 case QMetaType::QPalette:
70 return &QQuickPalette::staticMetaObject;
71 default:
72 break;
73 }
74
75 return nullptr;
76}
77
78bool QQuickPaletteProvider::init(int type, QVariant& dst)
79{
80 switch (type) {
81 case QMetaType::QPalette:
82 dst.setValue<QPalette>(QPalette());
83 return true;
84 default: break;
85 }
86
87 return false;
88}
89
90template<typename T>
91bool typedEqual(const void *lhs, const QVariant& rhs)
92{
93 return (*(reinterpret_cast<const T *>(lhs)) == rhs.value<T>());
94}
95
96bool QQuickPaletteProvider::equal(int type, const void *lhs, const QVariant &rhs)
97{
98 switch (type) {
99 case QMetaType::QPalette:
100 return typedEqual<QPalette>(lhs, rhs);
101 default: break;
102 }
103
104 return false;
105}
106
107template<typename T>
108bool typedStore(const void *src, void *dst, size_t dstSize)
109{
110 ASSERT_VALID_SIZE(dstSize, sizeof(T));
111 const T *srcT = reinterpret_cast<const T *>(src);
112 T *dstT = reinterpret_cast<T *>(dst);
113 new (dstT) T(*srcT);
114 return true;
115}
116
117bool QQuickPaletteProvider::store(int type, const void *src, void *dst, size_t dstSize)
118{
119 switch (type) {
120 case QMetaType::QPalette:
121 return typedStore<QPalette>(src, dst, dstSize);
122 default: break;
123 }
124
125 return false;
126}
127
128template<typename T>
129bool typedRead(const QVariant& src, int dstType, void *dst)
130{
131 T *dstT = reinterpret_cast<T *>(dst);
132 if (src.type() == static_cast<uint>(dstType)) {
133 *dstT = src.value<T>();
134 } else {
135 *dstT = T();
136 }
137 return true;
138}
139
140bool QQuickPaletteProvider::read(const QVariant &src, void *dst, int dstType)
141{
142 switch (dstType) {
143 case QMetaType::QPalette:
144 return typedRead<QPalette>(src, dstType, dst);
145 default: break;
146 }
147
148 return false;
149}
150
151template<typename T>
152bool typedWrite(const void *src, QVariant& dst)
153{
154 const T *srcT = reinterpret_cast<const T *>(src);
155 if (dst.value<T>() != *srcT) {
156 dst = *srcT;
157 return true;
158 }
159 return false;
160}
161
162bool QQuickPaletteProvider::write(int type, const void *src, QVariant& dst)
163{
164 switch (type) {
165 case QMetaType::QPalette:
166 return typedWrite<QPalette>(src, dst);
167 default: break;
168 }
169
170 return false;
171}
172
173#undef ASSERT_VALID_SIZE
174
175QT_END_NAMESPACE
176

source code of qtquickcontrols2/src/quicktemplates2/qquickpaletteprovider.cpp