aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/designercore/projectstorage/commontypecache.h
blob: 35658c005f10e134aabf4c10b5f25c0547e7703e (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "projectstoragetypes.h"

#include <modelfwd.h>

#include <algorithm>
#include <tuple>
#include <type_traits>

QT_BEGIN_NAMESPACE
class QColor;
class QDateTime;
class QString;
class QUrl;
class QVariant;
class QVector2D;
class QVector3D;
class QVector4D;
QT_END_NAMESPACE

namespace QmlDesigner::Storage::Info {

inline constexpr char Affector3D[] = "Affector3D";
inline constexpr char Attractor3D[] = "Attractor3D";
inline constexpr char BakedLightmap[] = "BakedLightmap";
inline constexpr char BoolType[] = "bool";
inline constexpr char BorderImage[] = "BorderImage";
inline constexpr char Buffer[] = "Buffer";
inline constexpr char Camera[] = "Camera";
inline constexpr char Command[] = "Command";
inline constexpr char Component[] = "Component";
inline constexpr char Connections[] = "Connections";
inline constexpr char Control[] = "Control";
inline constexpr char CubeMapTexture[] = "CubeMapTexture";
inline constexpr char DefaultMaterial[] = "DefaultMaterial";
inline constexpr char Dialog[] = "Dialog";
inline constexpr char DirectionalLight[] = "DirectionalLight";
inline constexpr char DoubleType[] = "double";
inline constexpr char Effect[] = "Effect";
inline constexpr char FloatType[] = "float";
inline constexpr char FlowActionArea[] = "FlowActionArea";
inline constexpr char FlowDecision[] = "FlowDecision";
inline constexpr char FlowItem[] = "FlowItem";
inline constexpr char FlowTransition[] = "FlowTransition";
inline constexpr char FlowView[] = "FlowView";
inline constexpr char FlowWildcard[] = "FlowWildcard";
inline constexpr char GridView[] = "GridView";
inline constexpr char GroupItem[] = "GroupItem";
inline constexpr char Image[] = "Image";
inline constexpr char InstanceListEntry[] = "InstanceListEntry";
inline constexpr char InstanceList[] = "InstanceList";
inline constexpr char IntType[] = "int";
inline constexpr char Item[] = "Item";
inline constexpr char JsonListModel[] = "JsonListModel";
inline constexpr char KeyframeGroup[] = "KeyframeGroup";
inline constexpr char Keyframe[] = "Keyframe";
inline constexpr char Layout[] = "Layout";
inline constexpr char Light[] = "Light";
inline constexpr char ListElement[] = "ListElement";
inline constexpr char ListModel[] = "ListModel";
inline constexpr char ListView[] = "ListView";
inline constexpr char Loader[] = "Loader";
inline constexpr char Material[] = "Material";
inline constexpr char Model[] = "Model";
inline constexpr char MouseArea[] = "MouseArea";
inline constexpr char Node[] = "Node";
inline constexpr char OrthographicCamera[] = "OrthographicCamera";
inline constexpr char Particle3D[] = "Particle3D";
inline constexpr char ParticleEmitter3D[] = "ParticleEmitter3D";
inline constexpr char Pass[] = "Pass";
inline constexpr char PathView[] = "PathView";
inline constexpr char Path[] = "Path";
inline constexpr char PauseAnimation[] = "PauseAnimation";
inline constexpr char PerspectiveCamera[] = "PerspectiveCamera";
inline constexpr char Picture[] = "Picture";
inline constexpr char PointLight[] = "PointLight";
inline constexpr char Popup[] = "Popup";
inline constexpr char Positioner[] = "Positioner";
inline constexpr char PrincipledMaterial[] = "PrincipledMaterial";
inline constexpr char PropertyAnimation[] = "PropertyAnimation";
inline constexpr char PropertyChanges[] = "PropertyChanges";
inline constexpr char QML[] = "QML";
inline constexpr char QML_cppnative[] = "QML-cppnative";
inline constexpr char QQuick3DParticleAbstractShape[] = "QQuick3DParticleAbstractShape";
inline constexpr char QQuickStateOperation[] = "QQuickStateOperation";
inline constexpr char QtMultimedia[] = "QtMultimedia";
inline constexpr char QtObject[] = "QtObject";
inline constexpr char QtQml[] = "QtQml";
inline constexpr char QtQml_Models[] = "QtQml.Models";
inline constexpr char QtQml_XmlListModel[] = "QtQml.XmlListModel";
inline constexpr char QtQuick3D[] = "QtQuick3D";
inline constexpr char QtQuick3D_Particles3D[] = "QtQuick3D.Particles3D";
inline constexpr char QtQuick3D_Particles3D_cppnative[] = "QtQuick3D.Particles3D-cppnative";
inline constexpr char QtQuick[] = "QtQuick";
inline constexpr char QtQuick_Controls[] = "QtQuick.Controls";
inline constexpr char QtQuick_Dialogs[] = "QtQuick.Dialogs";
inline constexpr char QtQuick_Extras[] = "QtQuick.Extras";
inline constexpr char QtQuick_Layouts[] = "QtQuick.Layouts";
inline constexpr char QtQuick_Studio_Components[] = "QtQuick.Studio.Components";
inline constexpr char QtQuick_Templates[] = "QtQuick.Templates";
inline constexpr char QtQuick_Timeline[] = "QtQuick.Timeline";
inline constexpr char QtQuick_Window[] = "QtQuick.Window";
inline constexpr char QtQuick_cppnative[] = "QtQuick-cppnative";
inline constexpr char Qt_SafeRenderer[] = "Qt.SafeRenderer";
inline constexpr char Rectangle[] = "Rectangle";
inline constexpr char Repeater[] = "Repeater";
inline constexpr char SafePicture[] = "SafePicture";
inline constexpr char SafeRendererPicture[] = "SafeRendererPicture";
inline constexpr char SceneEnvironment[] = "SceneEnvironment";
inline constexpr char Shader[] = "Shader";
inline constexpr char SoundEffect[] = "SoundEffect";
inline constexpr char SpecularGlossyMaterial[] = "SpecularGlossyMaterial";
inline constexpr char SplitView[] = "SplitView";
inline constexpr char SpotLight[] = "SpotLight";
inline constexpr char SpriteParticle3D[] = "SpriteParticle3D";
inline constexpr char StateGroup[] = "StateGroup";
inline constexpr char State[] = "State";
inline constexpr char SwipeView[] = "SwipeView";
inline constexpr char TabBar[] = "TabBar";
inline constexpr char TextArea[] = "TextArea";
inline constexpr char TextEdit[] = "TextEdit";
inline constexpr char Text[] = "Text";
inline constexpr char TextureInput[] = "TextureInput";
inline constexpr char Texture[] = "Texture";
inline constexpr char TimelineAnimation[] = "TimelineAnimation";
inline constexpr char Timeline[] = "Timeline";
inline constexpr char Transition[] = "Transition";
inline constexpr char UIntType[] = "uint";
inline constexpr char View3D[] = "View3D";
inline constexpr char Window[] = "Window";
inline constexpr char XmlListModelRole[] = "XmlListModelRole";
inline constexpr char color[] = "color";
inline constexpr char date[] = "date";
inline constexpr char font[] = "font";
inline constexpr char string[] = "string";
inline constexpr char url[] = "url";
inline constexpr char var[] = "var";
inline constexpr char vector2d[] = "vector2d";
inline constexpr char vector3d[] = "vector3d";
inline constexpr char vector4d[] = "vector4d";

struct BaseCacheType
{
    QmlDesigner::ModuleId moduleId;
    QmlDesigner::TypeId typeId;
};

template<const char *moduleName_, const char *typeName_>
struct CacheType : public BaseCacheType
{
};

template<typename ProjectStorage>
class CommonTypeCache
{
    using CommonTypes = std::tuple<CacheType<FlowView, FlowActionArea>,
                                   CacheType<FlowView, FlowDecision>,
                                   CacheType<FlowView, FlowItem>,
                                   CacheType<FlowView, FlowTransition>,
                                   CacheType<FlowView, FlowView>,
                                   CacheType<FlowView, FlowWildcard>,
                                   CacheType<QML, BoolType>,
                                   CacheType<QML, Component>,
                                   CacheType<QML, DoubleType>,
                                   CacheType<QML, IntType>,
                                   CacheType<QML, QtObject>,
                                   CacheType<QML, date>,
                                   CacheType<QML, string>,
                                   CacheType<QML, url>,
                                   CacheType<QML, var>,
                                   CacheType<QML_cppnative, FloatType>,
                                   CacheType<QML_cppnative, UIntType>,
                                   CacheType<QtQml, Connections>,
                                   CacheType<QtMultimedia, SoundEffect>,
                                   CacheType<QtQml_Models, ListElement>,
                                   CacheType<QtQml_Models, ListModel>,
                                   CacheType<QtQml_XmlListModel, XmlListModelRole>,
                                   CacheType<QtQuick, BorderImage>,
                                   CacheType<QtQuick, GridView>,
                                   CacheType<QtQuick, Image>,
                                   CacheType<QtQuick, Item>,
                                   CacheType<QtQuick, ListView>,
                                   CacheType<QtQuick, Loader>,
                                   CacheType<QtQuick, MouseArea>,
                                   CacheType<QtQuick, Path>,
                                   CacheType<QtQuick, PathView>,
                                   CacheType<QtQuick, PauseAnimation>,
                                   CacheType<QtQuick, Positioner>,
                                   CacheType<QtQuick, PropertyAnimation>,
                                   CacheType<QtQuick, PropertyChanges>,
                                   CacheType<QtQuick, Rectangle>,
                                   CacheType<QtQuick, Repeater>,
                                   CacheType<QtQuick, State>,
                                   CacheType<QtQuick, StateGroup>,
                                   CacheType<QtQuick, Text>,
                                   CacheType<QtQuick, TextEdit>,
                                   CacheType<QtQuick, Transition>,
                                   CacheType<QtQuick, color>,
                                   CacheType<QtQuick, font>,
                                   CacheType<QtQuick, vector2d>,
                                   CacheType<QtQuick, vector3d>,
                                   CacheType<QtQuick, vector4d>,
                                   CacheType<QtQuick3D, BakedLightmap>,
                                   CacheType<QtQuick3D, Buffer>,
                                   CacheType<QtQuick3D, Camera>,
                                   CacheType<QtQuick3D, Command>,
                                   CacheType<QtQuick3D, CubeMapTexture>,
                                   CacheType<QtQuick3D, DefaultMaterial>,
                                   CacheType<QtQuick3D, DirectionalLight>,
                                   CacheType<QtQuick3D, Effect>,
                                   CacheType<QtQuick3D, InstanceList>,
                                   CacheType<QtQuick3D, InstanceListEntry>,
                                   CacheType<QtQuick3D, Light>,
                                   CacheType<QtQuick3D, Material>,
                                   CacheType<QtQuick3D, Model>,
                                   CacheType<QtQuick3D, Node>,
                                   CacheType<QtQuick3D, OrthographicCamera>,
                                   CacheType<QtQuick3D, Pass>,
                                   CacheType<QtQuick3D, PerspectiveCamera>,
                                   CacheType<QtQuick3D, PointLight>,
                                   CacheType<QtQuick3D, PrincipledMaterial>,
                                   CacheType<QtQuick3D, SceneEnvironment>,
                                   CacheType<QtQuick3D, Shader>,
                                   CacheType<QtQuick3D, SpecularGlossyMaterial>,
                                   CacheType<QtQuick3D, SpotLight>,
                                   CacheType<QtQuick3D, Texture>,
                                   CacheType<QtQuick3D, TextureInput>,
                                   CacheType<QtQuick3D, View3D>,
                                   CacheType<QtQuick3D_Particles3D, Affector3D>,
                                   CacheType<QtQuick3D_Particles3D, Attractor3D>,
                                   CacheType<QtQuick3D_Particles3D, Model>,
                                   CacheType<QtQuick3D_Particles3D, Particle3D>,
                                   CacheType<QtQuick3D_Particles3D, ParticleEmitter3D>,
                                   CacheType<QtQuick3D_Particles3D, SpriteParticle3D>,
                                   CacheType<QtQuick3D_Particles3D_cppnative, QQuick3DParticleAbstractShape>,
                                   CacheType<QtQuick_Controls, Control>,
                                   CacheType<QtQuick_Controls, Popup>,
                                   CacheType<QtQuick_Controls, SplitView>,
                                   CacheType<QtQuick_Controls, SwipeView>,
                                   CacheType<QtQuick_Controls, TabBar>,
                                   CacheType<QtQuick_Controls, TextArea>,
                                   CacheType<QtQuick_Dialogs, Dialog>,
                                   CacheType<QtQuick_Extras, Picture>,
                                   CacheType<QtQuick_Layouts, Layout>,
                                   CacheType<QtQuick_Studio_Components, GroupItem>,
                                   CacheType<QtQuick_Studio_Components, JsonListModel>,
                                   CacheType<QtQuick_Templates, Control>,
                                   CacheType<QtQuick_Timeline, Keyframe>,
                                   CacheType<QtQuick_Timeline, KeyframeGroup>,
                                   CacheType<QtQuick_Timeline, Timeline>,
                                   CacheType<QtQuick_Timeline, TimelineAnimation>,
                                   CacheType<QtQuick_cppnative, QQuickStateOperation>,
                                   CacheType<Qt_SafeRenderer, SafePicture>,
                                   CacheType<Qt_SafeRenderer, SafeRendererPicture>,
                                   CacheType<QtQuick_Window, Window>>;

public:
    CommonTypeCache(const ProjectStorage &projectStorage)
        : m_projectStorage{projectStorage}
    {
        m_typesWithoutProperties.fill(TypeId{});
    }

    CommonTypeCache(const CommonTypeCache &) = delete;
    CommonTypeCache &operator=(const CommonTypeCache &) = delete;
    CommonTypeCache(CommonTypeCache &&) = default;
    CommonTypeCache &operator=(CommonTypeCache &&) = default;

    void resetTypeIds()
    {
        std::apply([](auto &...type) { ((type.typeId = QmlDesigner::TypeId{}), ...); }, m_types);

        updateTypeIdsWithoutProperties();
    }

    void clearForTestsOnly()
    {
        std::apply([](auto &...type) { ((type.typeId = QmlDesigner::TypeId{}), ...); }, m_types);
        std::fill(std::begin(m_typesWithoutProperties), std ::end(m_typesWithoutProperties), TypeId{});
    }

    template<const char *moduleName, const char *typeName>
    TypeId typeId() const
    {
        auto &type = std::get<CacheType<moduleName, typeName>>(m_types);
        if (type.typeId)
            return type.typeId;

        return refreshTypedId(type, moduleName, typeName);
    }

    template<const char *typeName>
    TypeId builtinTypeId() const
    {
        return typeId<QML, typeName>();
    }

    template<typename Type>
    TypeId builtinTypeId() const
    {
        if constexpr (std::is_same_v<Type, double>)
            return typeId<QML, DoubleType>();
        else if constexpr (std::is_same_v<Type, int>)
            return typeId<QML, IntType>();
        else if constexpr (std::is_same_v<Type, uint>)
            return typeId<QML_cppnative, UIntType>();
        else if constexpr (std::is_same_v<Type, bool>)
            return typeId<QML, BoolType>();
        else if constexpr (std::is_same_v<Type, float>)
            return typeId<QML_cppnative, FloatType>();
        else if constexpr (std::is_same_v<Type, QString>)
            return typeId<QML, string>();
        else if constexpr (std::is_same_v<Type, QDateTime>)
            return typeId<QML, date>();
        else if constexpr (std::is_same_v<Type, QUrl>)
            return typeId<QML, url>();
        else if constexpr (std::is_same_v<Type, QVariant>)
            return typeId<QML, var>();
        else if constexpr (std::is_same_v<Type, QColor>)
            return typeId<QtQuick, color>();
        else if constexpr (std::is_same_v<Type, QVector2D>)
            return typeId<QtQuick, vector2d>();
        else if constexpr (std::is_same_v<Type, QVector3D>)
            return typeId<QtQuick, vector3d>();
        else if constexpr (std::is_same_v<Type, QVector4D>)
            return typeId<QtQuick, vector4d>();
        else
            static_assert(!std::is_same_v<Type, Type>, "built-in type not supported");
        return TypeId{};
    }

    const TypeIdsWithoutProperties &typeIdsWithoutProperties() const
    {
        return m_typesWithoutProperties;
    }

private:
    TypeId refreshTypedId(BaseCacheType &type,
                          ::Utils::SmallStringView moduleName,
                          ::Utils::SmallStringView typeName) const
    {
        if (!type.moduleId)
            type.moduleId = m_projectStorage.moduleId(moduleName);

        type.typeId = m_projectStorage.typeId(type.moduleId, typeName, Storage::Version{});

        return type.typeId;
    }

    TypeId refreshTypedIdWithoutTransaction(BaseCacheType &type,
                                            ::Utils::SmallStringView moduleName,
                                            ::Utils::SmallStringView typeName) const
    {
        if (!type.moduleId)
            type.moduleId = m_projectStorage.fetchModuleIdUnguarded(moduleName);

        type.typeId = m_projectStorage.fetchTypeIdByModuleIdAndExportedName(type.moduleId, typeName);

        return type.typeId;
    }

    template<std::size_t size>
    void setupTypeIdsWithoutProperties(const TypeId (&typeIds)[size])
    {
        static_assert(size == std::tuple_size_v<TypeIdsWithoutProperties>,
                      "array size must match type id count!");
        std::copy(std::begin(typeIds), std::end(typeIds), std::begin(m_typesWithoutProperties));
    }

    template<const char *moduleName, const char *typeName>
    TypeId typeIdWithoutTransaction() const
    {
        auto &type = std::get<CacheType<moduleName, typeName>>(m_types);
        if (type.typeId)
            return type.typeId;

        return refreshTypedIdWithoutTransaction(type, moduleName, typeName);
    }

    void updateTypeIdsWithoutProperties()
    {
        setupTypeIdsWithoutProperties({typeIdWithoutTransaction<QML, BoolType>(),
                                       typeIdWithoutTransaction<QML, IntType>(),
                                       typeIdWithoutTransaction<QML_cppnative, UIntType>(),
                                       typeIdWithoutTransaction<QML, DoubleType>(),
                                       typeIdWithoutTransaction<QML_cppnative, FloatType>(),
                                       typeIdWithoutTransaction<QML, date>(),
                                       typeIdWithoutTransaction<QML, string>(),
                                       typeIdWithoutTransaction<QML, url>()});
    }

private:
    const ProjectStorage &m_projectStorage;
    mutable CommonTypes m_types;
    TypeIdsWithoutProperties m_typesWithoutProperties;
};

} // namespace QmlDesigner::Storage::Info