KTextTemplate

customtyperegistry.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2010 Michael Jansen <kde@michael-jansen.biz>
5 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
6
7 SPDX-License-Identifier: LGPL-2.1-or-later
8
9*/
10
11#include "customtyperegistry_p.h"
12
13#include "metaenumvariable_p.h"
14#include "safestring.h"
15
16#include <QLoggingCategory>
17#include <QQueue>
18#include <QStack>
19#include <QStringList>
20
21Q_LOGGING_CATEGORY(KTEXTTEMPLATE_CUSTOMTYPE, "kf.texttemplate.customtyperegistry")
22
23using namespace KTextTemplate;
24
25CustomTypeRegistry::CustomTypeRegistry()
26{
27 // KTextTemplate Types
28 registerBuiltInMetatype<SafeString>();
29 registerBuiltInMetatype<MetaEnumVariable>();
30}
31
32void CustomTypeRegistry::registerLookupOperator(int id, MetaType::LookupFunction f)
33{
34 CustomTypeInfo &info = types[id];
35 info.lookupFunction = f;
36}
37
38QVariant CustomTypeRegistry::lookup(const QVariant &object, const QString &property) const
39{
40 if (!object.isValid())
41 return {};
42 const auto id = object.userType();
43 MetaType::LookupFunction lf;
44
45 {
46 auto it = types.constFind(id);
47 if (it == types.constEnd()) {
48 qCWarning(KTEXTTEMPLATE_CUSTOMTYPE) << "Don't know how to handle metatype" << QMetaType(id).name();
49 // :TODO: Print out error message
50 return {};
51 }
52
53 const CustomTypeInfo &info = it.value();
54 if (!info.lookupFunction) {
55 qCWarning(KTEXTTEMPLATE_CUSTOMTYPE) << "No lookup function for metatype" << QMetaType(id).name();
56 lf = nullptr;
57 // :TODO: Print out error message
58 return {};
59 }
60
61 lf = info.lookupFunction;
62 }
63
64 return lf(object, property);
65}
66
67bool CustomTypeRegistry::lookupAlreadyRegistered(int id) const
68{
69 auto it = types.constFind(id);
70 if (it != types.constEnd()) {
71 return it.value().lookupFunction != nullptr;
72 }
73 return false;
74}
bool isValid(QStringView ifopt)
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
const char * name() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.