Cutelee 6.1.0
customtyperegistry.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2010 Michael Jansen <kde@michael-jansen.biz>
5 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either version
10 2.1 of the Licence, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22#include "customtyperegistry_p.h"
23
24#include "metaenumvariable_p.h"
25#include "safestring.h"
26
27#include <QtCore/QLoggingCategory>
28#include <QtCore/QQueue>
29#include <QtCore/QStack>
30#include <QtCore/QStringList>
31
32Q_LOGGING_CATEGORY(CUTELEE_CUSTOMTYPE, "cutelee.customtype")
33
34using namespace Cutelee;
35
36CustomTypeRegistry::CustomTypeRegistry()
37{
38 // Cutelee Types
39 registerBuiltInMetatype<SafeString>();
40 registerBuiltInMetatype<MetaEnumVariable>();
41#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
42 QMetaType::registerComparators<MetaEnumVariable>();
43#endif
44}
45
46void CustomTypeRegistry::registerLookupOperator(int id,
47 MetaType::LookupFunction f)
48{
49 CustomTypeInfo &info = types[id];
50 info.lookupFunction = f;
51}
52
53QVariant CustomTypeRegistry::lookup(const QVariant &object,
54 const QString &property) const
55{
56 if (!object.isValid())
57 return QVariant();
58
59 const auto id = object.userType();
60 MetaType::LookupFunction lf;
61 {
62 auto it = types.constFind(id);
63 if (it == types.constEnd()) {
64 qCWarning(CUTELEE_CUSTOMTYPE)
65#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
66 << "Don't know how to handle metatype" << QMetaType::typeName(id);
67#else
68 << "Don't know how to handle metatype" << QMetaType(id).name();
69#endif
70 // :TODO: Print out error message
71 return QVariant();
72 }
73
74 const CustomTypeInfo &info = it.value();
75 if (!info.lookupFunction) {
76 qCWarning(CUTELEE_CUSTOMTYPE)
77#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
78 << "No lookup function for metatype" << QMetaType::typeName(id);
79#else
80 << "No lookup function for metatype" << QMetaType(id).name();
81#endif
82 lf = 0;
83 // :TODO: Print out error message
84 return QVariant();
85 }
86
87 lf = info.lookupFunction;
88 }
89
90 return lf(object, property);
91}
92
93bool CustomTypeRegistry::lookupAlreadyRegistered(int id) const
94{
95 auto it = types.constFind(id);
96 if (it != types.constEnd()) {
97 return it.value().lookupFunction != 0;
98 }
99 return false;
100}
The Cutelee namespace holds all public Cutelee API.
Definition Mainpage.dox:8