1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4// Gui types
5#include "qbitmap.h"
6#include "qbrush.h"
7#include "qcolor.h"
8#include "qcolorspace.h"
9#include "qcursor.h"
10#include "qfont.h"
11#include "qimage.h"
12#if QT_CONFIG(shortcut)
13# include "qkeysequence.h"
14#endif
15#include "qtransform.h"
16#include "qpalette.h"
17#include "qpen.h"
18#include "qpixmap.h"
19#include "qpolygon.h"
20#include "qregion.h"
21#include "qtextformat.h"
22#include "qmatrix4x4.h"
23#include "qvector2d.h"
24#include "qvector3d.h"
25#include "qvector4d.h"
26#include "qquaternion.h"
27#include "qicon.h"
28
29// Core types
30#include "qvariant.h"
31#include "qbitarray.h"
32#include "qbytearray.h"
33#include "qdatastream.h"
34#include "qdebug.h"
35#include "qmap.h"
36#include "qdatetime.h"
37#include "qlist.h"
38#include "qstring.h"
39#include "qstringlist.h"
40#include "qurl.h"
41#include "qlocale.h"
42#include "quuid.h"
43
44#ifndef QT_NO_GEOM_VARIANT
45#include "qsize.h"
46#include "qpoint.h"
47#include "qrect.h"
48#include "qline.h"
49#endif
50
51#include <float.h>
52
53#include <private/qmetatype_p.h>
54
55QT_BEGIN_NAMESPACE
56
57namespace {
58
59static const struct : QMetaTypeModuleHelper
60{
61#define QT_IMPL_METATYPEINTERFACE_GUI_TYPES(MetaTypeName, MetaTypeId, RealName) \
62 QT_METATYPE_INTERFACE_INIT(RealName),
63
64 const QtPrivate::QMetaTypeInterface *interfaceForType(int type) const override {
65 switch (type) {
66 QT_FOR_EACH_STATIC_GUI_CLASS(QT_METATYPE_CONVERT_ID_TO_TYPE)
67 default: return nullptr;
68 }
69 }
70#undef QT_IMPL_METATYPEINTERFACE_GUI_TYPES
71
72 bool convert(const void *from, int fromTypeId, void *to, int toTypeId) const override
73 {
74 Q_ASSERT(fromTypeId != toTypeId);
75
76 bool onlyCheck = (from == nullptr && to == nullptr);
77 // either two nullptrs from canConvert, or two valid pointers
78 Q_ASSERT(onlyCheck || (bool(from) && bool(to)));
79
80 using Int = int;
81 switch (makePair(from: toTypeId, to: fromTypeId)) {
82 QMETATYPE_CONVERTER(QByteArray, QColor,
83 result = source.name(source.alpha() != 255 ?
84 QColor::HexArgb : QColor::HexRgb).toLatin1();
85 return true;
86 );
87 QMETATYPE_CONVERTER(QColor, QByteArray,
88 result = QColor::fromString(QLatin1StringView(source));
89 return result.isValid();
90 );
91 QMETATYPE_CONVERTER(QString, QColor,
92 result = source.name(source.alpha() != 255 ?
93 QColor::HexArgb : QColor::HexRgb);
94 return true;
95 );
96 QMETATYPE_CONVERTER(QColor, QString,
97 result = QColor::fromString(source);
98 return result.isValid();
99 );
100#if QT_CONFIG(shortcut)
101 QMETATYPE_CONVERTER(QString, QKeySequence,
102 result = source.toString(QKeySequence::NativeText);
103 return true;
104 );
105 QMETATYPE_CONVERTER(QKeySequence, QString, result = source; return true;);
106 QMETATYPE_CONVERTER(Int, QKeySequence,
107 result = source.isEmpty() ? 0 : source[0].toCombined();
108 return true;
109 );
110 QMETATYPE_CONVERTER(QKeySequence, Int, result = source; return true;);
111#endif
112 QMETATYPE_CONVERTER(QString, QFont, result = source.toString(); return true;);
113 QMETATYPE_CONVERTER(QFont, QString, return result.fromString(source););
114 QMETATYPE_CONVERTER(QPixmap, QImage, result = QPixmap::fromImage(source); return true;);
115 QMETATYPE_CONVERTER(QImage, QPixmap, result = source.toImage(); return true;);
116 QMETATYPE_CONVERTER(QPixmap, QBitmap, result = source; return true;);
117 QMETATYPE_CONVERTER(QBitmap, QPixmap, result = QBitmap::fromPixmap(source); return true;);
118 QMETATYPE_CONVERTER(QImage, QBitmap, result = source.toImage(); return true;);
119 QMETATYPE_CONVERTER(QBitmap, QImage, result = QBitmap::fromImage(source); return true;);
120 QMETATYPE_CONVERTER(QPixmap, QBrush, result = source.texture(); return true;);
121 QMETATYPE_CONVERTER(QBrush, QPixmap, result = source; return true;);
122 QMETATYPE_CONVERTER(QColor, QBrush,
123 if (source.style() == Qt::SolidPattern) {
124 result = source.color();
125 return true;
126 }
127 return false;
128 );
129 QMETATYPE_CONVERTER(QBrush, QColor, result = source; return true;);
130 default:
131 break;
132 }
133 return false;
134 }
135} qVariantGuiHelper;
136
137} // namespace used to hide QVariant handler
138
139void qRegisterGuiVariant()
140{
141 qMetaTypeGuiHelper = &qVariantGuiHelper;
142}
143Q_CONSTRUCTOR_FUNCTION(qRegisterGuiVariant)
144
145QT_END_NAMESPACE
146

source code of qtbase/src/gui/kernel/qguivariant.cpp