Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qguivariant.cpp
Go to the documentation of this file.
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 "qbitarray.h"
31#include "qbytearray.h"
32#include "qdatastream.h"
33#include "qdatetime.h"
34#include "qdebug.h"
35#include "qline.h"
36#include "qlist.h"
37#include "qlocale.h"
38#include "qmap.h"
39#include "qpoint.h"
40#include "qrect.h"
41#include "qsize.h"
42#include "qstring.h"
43#include "qstringlist.h"
44#include "qurl.h"
45#include "quuid.h"
46#include "qvariant.h"
47
48#include <float.h>
49
50#include <private/qmetatype_p.h>
51
53
54namespace {
56{
57#define QT_IMPL_METATYPEINTERFACE_GUI_TYPES(MetaTypeName, MetaTypeId, RealName)
58 QT_METATYPE_INTERFACE_INIT(RealName),
59
61 {
62 switch (type) {
63 QT_FOR_EACH_STATIC_GUI_CLASS(QT_METATYPE_CONVERT_ID_TO_TYPE)
64 default: return nullptr;
65 }
66 }
67#undef QT_IMPL_METATYPEINTERFACE_GUI_TYPES
68
69 static bool convert(const void *from, int fromTypeId, void *to, int toTypeId)
70 {
71 Q_ASSERT(fromTypeId != toTypeId);
72
73 bool onlyCheck = (from == nullptr && to == nullptr);
74 // either two nullptrs from canConvert, or two valid pointers
75 Q_ASSERT(onlyCheck || (bool(from) && bool(to)));
76
77#if QT_CONFIG(shortcut)
78 using Int = int;
79#endif
80 switch (makePair(toTypeId, fromTypeId)) {
81 QMETATYPE_CONVERTER(QByteArray, QColor,
82 result = source.name(source.alpha() != 255 ?
83 QColor::HexArgb : QColor::HexRgb).toLatin1();
84 return true;
85 );
86 QMETATYPE_CONVERTER(QColor, QByteArray,
87 result = QColor::fromString(QLatin1StringView(source));
88 return result.isValid();
89 );
90 QMETATYPE_CONVERTER(QString, QColor,
91 result = source.name(source.alpha() != 255 ?
92 QColor::HexArgb : QColor::HexRgb);
93 return true;
94 );
95 QMETATYPE_CONVERTER(QColor, QString,
96 result = QColor::fromString(source);
97 return result.isValid();
98 );
99#if QT_CONFIG(shortcut)
100 QMETATYPE_CONVERTER(QString, QKeySequence,
101 result = source.toString(QKeySequence::NativeText);
102 return true;
103 );
104 QMETATYPE_CONVERTER(QKeySequence, QString, result = source; return true;);
105 QMETATYPE_CONVERTER(Int, QKeySequence,
106 result = source.isEmpty() ? 0 : source[0].toCombined();
107 return true;
108 );
109 QMETATYPE_CONVERTER(QKeySequence, Int, result = source; return true;);
110#endif
111 QMETATYPE_CONVERTER(QString, QFont, result = source.toString(); return true;);
112 QMETATYPE_CONVERTER(QFont, QString, return result.fromString(source););
113 QMETATYPE_CONVERTER(QPixmap, QImage, result = QPixmap::fromImage(source); return true;);
114 QMETATYPE_CONVERTER(QImage, QPixmap, result = source.toImage(); return true;);
115 QMETATYPE_CONVERTER(QPixmap, QBitmap, result = source; return true;);
116 QMETATYPE_CONVERTER(QBitmap, QPixmap, result = QBitmap::fromPixmap(source); return true;);
117 QMETATYPE_CONVERTER(QImage, QBitmap, result = source.toImage(); return true;);
118 QMETATYPE_CONVERTER(QBitmap, QImage, result = QBitmap::fromImage(source); return true;);
119 QMETATYPE_CONVERTER(QPixmap, QBrush, result = source.texture(); return true;);
120 QMETATYPE_CONVERTER(QBrush, QPixmap, result = source; return true;);
121 QMETATYPE_CONVERTER(QColor, QBrush,
122 if (source.style() == Qt::SolidPattern) {
123 result = source.color();
124 return true;
125 }
126 return false;
127 );
128 QMETATYPE_CONVERTER(QBrush, QColor, result = source; return true;);
129 default:
130 break;
131 }
132 return false;
133 }
134};
135} // namespace used to hide QVariant handler
136
138{
139 qMetaTypeGuiHelper = QMetaTypeModuleHelper{
140 &QVariantGuiHelper::interfaceForType,
141 &QVariantGuiHelper::convert,
142 };
143}
144Q_CONSTRUCTOR_FUNCTION(qRegisterGuiVariant)
145
146QT_END_NAMESPACE
void qRegisterGuiVariant()
static const QtPrivate::QMetaTypeInterface * interfaceForType(int type)
static bool convert(const void *from, int fromTypeId, void *to, int toTypeId)