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 "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
56
57namespace {
58
59// NOLINTNEXTLINE(cppcoreguidelines-virtual-class-destructor): this is not a base class
61{
62#define QT_IMPL_METATYPEINTERFACE_GUI_TYPES(MetaTypeName, MetaTypeId, RealName)
63 QT_METATYPE_INTERFACE_INIT(RealName),
64
65 const QtPrivate::QMetaTypeInterface *interfaceForType(int type) const override {
66 switch (type) {
67 QT_FOR_EACH_STATIC_GUI_CLASS(QT_METATYPE_CONVERT_ID_TO_TYPE)
68 default: return nullptr;
69 }
70 }
71#undef QT_IMPL_METATYPEINTERFACE_GUI_TYPES
72
73 bool convert(const void *from, int fromTypeId, void *to, int toTypeId) const override
74 {
75 Q_ASSERT(fromTypeId != toTypeId);
76
77 bool onlyCheck = (from == nullptr && to == nullptr);
78 // either two nullptrs from canConvert, or two valid pointers
79 Q_ASSERT(onlyCheck || (bool(from) && bool(to)));
80
81#if QT_CONFIG(shortcut)
82 using Int = int;
83#endif
84 switch (makePair(toTypeId, fromTypeId)) {
85 QMETATYPE_CONVERTER(QByteArray, QColor,
86 result = source.name(source.alpha() != 255 ?
87 QColor::HexArgb : QColor::HexRgb).toLatin1();
88 return true;
89 );
90 QMETATYPE_CONVERTER(QColor, QByteArray,
91 result = QColor::fromString(QLatin1StringView(source));
92 return result.isValid();
93 );
94 QMETATYPE_CONVERTER(QString, QColor,
95 result = source.name(source.alpha() != 255 ?
96 QColor::HexArgb : QColor::HexRgb);
97 return true;
98 );
99 QMETATYPE_CONVERTER(QColor, QString,
100 result = QColor::fromString(source);
101 return result.isValid();
102 );
103#if QT_CONFIG(shortcut)
104 QMETATYPE_CONVERTER(QString, QKeySequence,
105 result = source.toString(QKeySequence::NativeText);
106 return true;
107 );
108 QMETATYPE_CONVERTER(QKeySequence, QString, result = source; return true;);
109 QMETATYPE_CONVERTER(Int, QKeySequence,
110 result = source.isEmpty() ? 0 : source[0].toCombined();
111 return true;
112 );
113 QMETATYPE_CONVERTER(QKeySequence, Int, result = source; return true;);
114#endif
115 QMETATYPE_CONVERTER(QString, QFont, result = source.toString(); return true;);
116 QMETATYPE_CONVERTER(QFont, QString, return result.fromString(source););
117 QMETATYPE_CONVERTER(QPixmap, QImage, result = QPixmap::fromImage(source); return true;);
118 QMETATYPE_CONVERTER(QImage, QPixmap, result = source.toImage(); return true;);
119 QMETATYPE_CONVERTER(QPixmap, QBitmap, result = source; return true;);
120 QMETATYPE_CONVERTER(QBitmap, QPixmap, result = QBitmap::fromPixmap(source); return true;);
121 QMETATYPE_CONVERTER(QImage, QBitmap, result = source.toImage(); return true;);
122 QMETATYPE_CONVERTER(QBitmap, QImage, result = QBitmap::fromImage(source); return true;);
123 QMETATYPE_CONVERTER(QPixmap, QBrush, result = source.texture(); return true;);
124 QMETATYPE_CONVERTER(QBrush, QPixmap, result = source; return true;);
125 QMETATYPE_CONVERTER(QColor, QBrush,
126 if (source.style() == Qt::SolidPattern) {
127 result = source.color();
128 return true;
129 }
130 return false;
131 );
132 QMETATYPE_CONVERTER(QBrush, QColor, result = source; return true;);
133 default:
134 break;
135 }
136 return false;
137 }
138};
139
141
142} // namespace used to hide QVariant handler
143
145{
146 qMetaTypeGuiHelper = &qVariantGuiHelper;
147}
148Q_CONSTRUCTOR_FUNCTION(qRegisterGuiVariant)
149
150QT_END_NAMESPACE
Combined button and popup list for selecting options.
static constexpr QVariantGuiHelper qVariantGuiHelper
void qRegisterGuiVariant()
bool convert(const void *from, int fromTypeId, void *to, int toTypeId) const override
const QtPrivate::QMetaTypeInterface * interfaceForType(int type) const override