summaryrefslogtreecommitdiffstats
path: root/qtinterface/tqvariant.cpp
blob: 981922e622b699b2b6b8d9dbf11466f9e282a107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*

Copyright (C) 2010 Timothy Pearson <kb9vqf@pearsoncomputing.net>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.

*/

#include <tqt.h>
#include <tqvariant.h>

#include <Qt/qbitmap.h>

#ifdef USE_QT4

// This is taken straight from <private/qvariant_p.h>
#ifdef Q_CC_SUN // Sun CC picks the wrong overload, so introduce awful hack

template <typename T>
inline T *v_cast(const QVariant::Private *nd, T * = 0)
{
    QVariant::Private *d = const_cast<QVariant::Private *>(nd);
    return ((sizeof(T) > sizeof(QVariant::Private::Data))
            ? static_cast<T *>(d->data.shared->ptr)
            : static_cast<T *>(static_cast<void *>(&d->data.c)));
}

#else // every other compiler in this world

template <typename T>
inline const T *v_cast(const QVariant::Private *d, T * = 0)
{
    return ((sizeof(T) > sizeof(QVariant::Private::Data))
            ? static_cast<const T *>(d->data.shared->ptr)
            : static_cast<const T *>(static_cast<const void *>(&d->data.c)));
}

template <typename T>
inline T *v_cast(QVariant::Private *d, T * = 0)
{
    return ((sizeof(T) > sizeof(QVariant::Private::Data))
            ? static_cast<T *>(d->data.shared->ptr)
            : static_cast<T *>(static_cast<void *>(&d->data.c)));
}

#endif

template <typename T>
inline T qVariantToHelper(const QVariant::Private &d, QVariant::Type t,
                          const QVariant::Handler *handler, T * = 0)
{
    if (d.type == t)
        return *v_cast<T>(&d);

    T ret;
    handler->convert(&d, t, &ret, 0);
    return ret;
}

QFont QVariant::toFont() const {
	return qVariantToHelper<QFont>(d, Font, handler);
}

QColor QVariant::toColor() const {
	return qVariantToHelper<QColor>(d, Color, handler);
}

QCursor QVariant::toCursor() const {
	return qVariantToHelper<QCursor>(d, Cursor, handler);
}

QBitmap QVariant::toBitmap() const {
	return qVariantToHelper<QBitmap>(d, Bitmap, handler);
}

Q3PointArray QVariant::toPointArray() const {
	return qVariantToHelper<Q3PointArray>(d, PointArray, handler);
}

QRegion QVariant::toRegion() const {
	return qVariantToHelper<QRegion>(d, Region, handler);
}

QPixmap QVariant::toPixmap() const {
	return qVariantToHelper<QPixmap>(d, Pixmap, handler);
}

QImage QVariant::toImage() const {
	return qVariantToHelper<QImage>(d, Image, handler);
}

QBrush QVariant::toBrush() const {
	return qVariantToHelper<QBrush>(d, Brush, handler);
}

QPalette QVariant::toPalette() const {
	return qVariantToHelper<QPalette>(d, Palette, handler);
}

QColorGroup QVariant::toColorGroup() const {
	return qVariantToHelper<QColorGroup>(d, ColorGroup, handler);
}

QIconSet QVariant::toIconSet() const {
	return qVariantToHelper<QIconSet>(d, IconSet, handler);
}

QFont QVariant::asFont() {
	return toFont();
}

QColor QVariant::asColor() {
	return toColor();
}

#endif // USE_QT4