Cleanup usage of QVariant::Type.
[profile/ivi/qtbase.git] / src / widgets / kernel / qwidgetsvariant.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qvariant.h"
43
44 #include "qicon.h"
45 #include "qsizepolicy.h"
46
47 #include "private/qvariant_p.h"
48 #include <private/qmetatype_p.h>
49
50 QT_BEGIN_NAMESPACE
51
52 namespace {
53 static void construct(QVariant::Private *x, const void *copy)
54 {
55     switch (x->type) {
56 #ifndef QT_NO_ICON
57     case QVariant::Icon:
58         v_construct<QIcon>(x, copy);
59         break;
60 #endif
61     case QVariant::SizePolicy:
62         v_construct<QSizePolicy>(x, copy);
63         break;
64     default:
65         Q_ASSERT(false);
66         return;
67     }
68     x->is_null = !copy;
69 }
70
71 static void clear(QVariant::Private *d)
72 {
73     switch (d->type) {
74 #ifndef QT_NO_ICON
75     case QVariant::Icon:
76         v_clear<QIcon>(d);
77         break;
78 #endif
79     case QVariant::SizePolicy:
80         v_clear<QSizePolicy>(d);
81         break;
82     default:
83         Q_ASSERT(false);
84         return;
85     }
86
87     d->type = QVariant::Invalid;
88     d->is_null = true;
89     d->is_shared = false;
90 }
91
92
93 static bool isNull(const QVariant::Private *d)
94 {
95     switch(d->type) {
96 #ifndef QT_NO_ICON
97     case QVariant::Icon:
98         return v_cast<QIcon>(d)->isNull();
99 #endif
100     }
101     return false;
102 }
103
104 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
105 {
106     Q_ASSERT(a->type == b->type);
107     switch(a->type) {
108 #ifndef QT_NO_ICON
109     case QVariant::Icon:
110         return false;
111 #endif
112     case QVariant::SizePolicy:
113         return *v_cast<QSizePolicy>(a) == *v_cast<QSizePolicy>(b);
114     default:
115         Q_ASSERT(false);
116     }
117     return false;
118 }
119
120 static bool convert(const QVariant::Private *d, int type, void *result, bool *ok)
121 {
122     Q_UNUSED(d);
123     Q_UNUSED(type);
124     Q_UNUSED(result);
125     if (ok)
126         *ok = false;
127     return false;
128 }
129
130 #if !defined(QT_NO_DEBUG_STREAM)
131 static void streamDebug(QDebug dbg, const QVariant &v)
132 {
133     QVariant::Private *d = const_cast<QVariant::Private *>(&v.data_ptr());
134     switch (d->type) {
135 #ifndef QT_NO_ICON
136     case QVariant::Icon:
137         dbg.nospace() << *v_cast<QIcon>(d);
138         break;
139 #endif
140     case QVariant::SizePolicy:
141         dbg.nospace() << *v_cast<QSizePolicy>(d);
142         break;
143     default:
144         dbg.nospace() << "QMetaType::Type(" << d->type << ")";
145     }
146 }
147 #endif
148
149 static const QVariant::Handler widgets_handler = {
150     construct,
151     clear,
152     isNull,
153 #ifndef QT_NO_DATASTREAM
154     0,
155     0,
156 #endif
157     compare,
158     convert,
159     0,
160 #if !defined(QT_NO_DEBUG_STREAM)
161     streamDebug
162 #else
163     0
164 #endif
165 };
166
167 #define QT_IMPL_METATYPEINTERFACE_WIDGETS_TYPES(MetaTypeName, MetaTypeId, RealName) \
168     QT_METATYPE_INTERFACE_INIT(RealName),
169
170 static const QMetaTypeInterface qVariantWidgetsHelper[] = {
171     QT_FOR_EACH_STATIC_WIDGETS_CLASS(QT_IMPL_METATYPEINTERFACE_WIDGETS_TYPES)
172 };
173
174 #undef QT_IMPL_METATYPEINTERFACE_WIDGETS_TYPES
175
176 }  // namespace
177
178 extern Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeWidgetsHelper;
179
180 void qRegisterWidgetsVariant()
181 {
182     qMetaTypeWidgetsHelper = qVariantWidgetsHelper;
183     QVariantPrivate::registerHandler(QModulesPrivate::Widgets, &widgets_handler);
184 }
185 Q_CONSTRUCTOR_FUNCTION(qRegisterWidgetsVariant)
186
187 void qUnregisterWidgetsVariant()
188 {
189     QVariantPrivate::unregisterHandler(QModulesPrivate::Widgets);
190     qMetaTypeWidgetsHelper = 0;
191 }
192 Q_DESTRUCTOR_FUNCTION(qUnregisterWidgetsVariant)
193
194
195 QT_END_NAMESPACE