1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
49 #include "qkeysequence.h"
50 #include "qtransform.h"
57 #include "qtextformat.h"
58 #include "qmatrix4x4.h"
59 #include "qvector2d.h"
60 #include "qvector3d.h"
61 #include "qvector4d.h"
62 #include "qquaternion.h"
67 #include "qbitarray.h"
68 #include "qbytearray.h"
69 #include "qdatastream.h"
72 #include "qdatetime.h"
73 #include "qeasingcurve.h"
76 #include "qstringlist.h"
81 #ifndef QT_NO_GEOM_VARIANT
90 #include "private/qvariant_p.h"
91 #include <private/qmetatype_p.h>
95 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler();
98 struct GuiTypesFilter {
101 static const bool IsAccepted = QModulesPrivate::QTypeModuleInfo<T>::IsGui && QtMetaTypePrivate::TypeDefinition<T>::IsAvailable;
105 static void construct(QVariant::Private *x, const void *copy)
107 const int type = x->type;
108 QVariantConstructor<GuiTypesFilter> constructor(x, copy);
109 QMetaTypeSwitcher::switcher<void>(constructor, type, 0);
112 static void clear(QVariant::Private *d)
114 QVariantDestructor<GuiTypesFilter> destructor(d);
115 QMetaTypeSwitcher::switcher<void>(destructor, d->type, 0);
118 // This class is a hack that customizes access to QPolygon
119 template<class Filter>
120 class QGuiVariantIsNull : public QVariantIsNull<Filter> {
121 typedef QVariantIsNull<Filter> Base;
123 QGuiVariantIsNull(const QVariant::Private *d)
124 : QVariantIsNull<Filter>(d)
127 bool delegate(const T *p) { return Base::delegate(p); }
128 bool delegate(const QPolygon*) { return v_cast<QPolygon>(Base::m_d)->isEmpty(); }
129 bool delegate(const void *p) { return Base::delegate(p); }
131 static bool isNull(const QVariant::Private *d)
133 QGuiVariantIsNull<GuiTypesFilter> isNull(d);
134 return QMetaTypeSwitcher::switcher<bool>(isNull, d->type, 0);
137 // This class is a hack that customizes access to QPixmap, QBitmap, QCursor and QIcon
138 template<class Filter>
139 class QGuiVariantComparator : public QVariantComparator<Filter> {
140 typedef QVariantComparator<Filter> Base;
142 QGuiVariantComparator(const QVariant::Private *a, const QVariant::Private *b)
143 : QVariantComparator<Filter>(a, b)
146 bool delegate(const T *p)
148 return Base::delegate(p);
150 bool delegate(const QPixmap*)
152 return v_cast<QPixmap>(Base::m_a)->cacheKey() == v_cast<QPixmap>(Base::m_b)->cacheKey();
154 bool delegate(const QBitmap*)
156 return v_cast<QBitmap>(Base::m_a)->cacheKey() == v_cast<QBitmap>(Base::m_b)->cacheKey();
159 bool delegate(const QCursor*)
161 return v_cast<QCursor>(Base::m_a)->shape() == v_cast<QCursor>(Base::m_b)->shape();
165 bool delegate(const QIcon *)
170 bool delegate(const void *p) { return Base::delegate(p); }
173 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
175 QGuiVariantComparator<GuiTypesFilter> comparator(a, b);
176 return QMetaTypeSwitcher::switcher<bool>(comparator, a->type, 0);
179 static bool convert(const QVariant::Private *d, int t,
180 void *result, bool *ok)
183 case QVariant::ByteArray:
184 if (d->type == QVariant::Color) {
185 *static_cast<QByteArray *>(result) = v_cast<QColor>(d)->name().toLatin1();
189 case QVariant::String: {
190 QString *str = static_cast<QString *>(result);
192 #ifndef QT_NO_SHORTCUT
193 case QVariant::KeySequence:
194 *str = (*v_cast<QKeySequence>(d)).toString(QKeySequence::NativeText);
198 *str = v_cast<QFont>(d)->toString();
200 case QVariant::Color:
201 *str = v_cast<QColor>(d)->name();
208 case QVariant::Pixmap:
209 if (d->type == QVariant::Image) {
210 *static_cast<QPixmap *>(result) = QPixmap::fromImage(*v_cast<QImage>(d));
212 } else if (d->type == QVariant::Bitmap) {
213 *static_cast<QPixmap *>(result) = *v_cast<QBitmap>(d);
215 } else if (d->type == QVariant::Brush) {
216 if (v_cast<QBrush>(d)->style() == Qt::TexturePattern) {
217 *static_cast<QPixmap *>(result) = v_cast<QBrush>(d)->texture();
222 case QVariant::Image:
223 if (d->type == QVariant::Pixmap) {
224 *static_cast<QImage *>(result) = v_cast<QPixmap>(d)->toImage();
226 } else if (d->type == QVariant::Bitmap) {
227 *static_cast<QImage *>(result) = v_cast<QBitmap>(d)->toImage();
231 case QVariant::Bitmap:
232 if (d->type == QVariant::Pixmap) {
233 *static_cast<QBitmap *>(result) = *v_cast<QPixmap>(d);
235 } else if (d->type == QVariant::Image) {
236 *static_cast<QBitmap *>(result) = QBitmap::fromImage(*v_cast<QImage>(d));
240 #ifndef QT_NO_SHORTCUT
242 if (d->type == QVariant::KeySequence) {
243 const QKeySequence &seq = *v_cast<QKeySequence>(d);
244 *static_cast<int *>(result) = seq.isEmpty() ? 0 : seq[0];
250 if (d->type == QVariant::String) {
251 QFont *f = static_cast<QFont *>(result);
252 f->fromString(*v_cast<QString>(d));
256 case QVariant::Color:
257 if (d->type == QVariant::String) {
258 static_cast<QColor *>(result)->setNamedColor(*v_cast<QString>(d));
259 return static_cast<QColor *>(result)->isValid();
260 } else if (d->type == QVariant::ByteArray) {
261 static_cast<QColor *>(result)->setNamedColor(QString::fromLatin1(
262 *v_cast<QByteArray>(d)));
264 } else if (d->type == QVariant::Brush) {
265 if (v_cast<QBrush>(d)->style() == Qt::SolidPattern) {
266 *static_cast<QColor *>(result) = v_cast<QBrush>(d)->color();
271 case QVariant::Brush:
272 if (d->type == QVariant::Color) {
273 *static_cast<QBrush *>(result) = QBrush(*v_cast<QColor>(d));
275 } else if (d->type == QVariant::Pixmap) {
276 *static_cast<QBrush *>(result) = QBrush(*v_cast<QPixmap>(d));
280 #ifndef QT_NO_SHORTCUT
281 case QVariant::KeySequence: {
282 QKeySequence *seq = static_cast<QKeySequence *>(result);
284 case QVariant::String:
285 *seq = QKeySequence(*v_cast<QString>(d));
288 *seq = QKeySequence(d->data.i);
296 case QVariant::Icon: {
305 return qcoreVariantHandler()->convert(d, t, result, ok);
308 #if !defined(QT_NO_DEBUG_STREAM)
309 static void streamDebug(QDebug dbg, const QVariant &v)
311 QVariant::Private *d = const_cast<QVariant::Private *>(&v.data_ptr());
312 QVariantDebugStream<GuiTypesFilter> stream(dbg, d);
313 QMetaTypeSwitcher::switcher<void>(stream, d->type, 0);
317 const QVariant::Handler qt_gui_variant_handler = {
321 #ifndef QT_NO_DATASTREAM
328 #if !defined(QT_NO_DEBUG_STREAM)
335 #define QT_IMPL_METATYPEINTERFACE_GUI_TYPES(MetaTypeName, MetaTypeId, RealName) \
336 QT_METATYPE_INTERFACE_INIT(RealName),
338 static const QMetaTypeInterface qVariantGuiHelper[] = {
339 QT_FOR_EACH_STATIC_GUI_CLASS(QT_IMPL_METATYPEINTERFACE_GUI_TYPES)
342 #undef QT_IMPL_METATYPEINTERFACE_GUI_TYPES
343 } // namespace used to hide QVariant handler
345 extern Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeGuiHelper;
347 void qRegisterGuiVariant()
349 QVariantPrivate::registerHandler(QModulesPrivate::Gui, &qt_gui_variant_handler);
350 qMetaTypeGuiHelper = qVariantGuiHelper;
352 Q_CONSTRUCTOR_FUNCTION(qRegisterGuiVariant)