File indexing completed on 2025-02-09 04:28:37

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2010 Michael Jansen <kde@michael-jansen.biz>
0005   SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
0006 
0007   SPDX-License-Identifier: LGPL-2.1-or-later
0008 
0009 */
0010 
0011 #include "customtyperegistry_p.h"
0012 
0013 #include "metaenumvariable_p.h"
0014 #include "safestring.h"
0015 
0016 #include <QLoggingCategory>
0017 #include <QQueue>
0018 #include <QStack>
0019 #include <QStringList>
0020 
0021 Q_LOGGING_CATEGORY(KTEXTTEMPLATE_CUSTOMTYPE, "kf.texttemplate.customtyperegistry")
0022 
0023 using namespace KTextTemplate;
0024 
0025 CustomTypeRegistry::CustomTypeRegistry()
0026 {
0027     // KTextTemplate Types
0028     registerBuiltInMetatype<SafeString>();
0029     registerBuiltInMetatype<MetaEnumVariable>();
0030 }
0031 
0032 void CustomTypeRegistry::registerLookupOperator(int id, MetaType::LookupFunction f)
0033 {
0034     CustomTypeInfo &info = types[id];
0035     info.lookupFunction = f;
0036 }
0037 
0038 QVariant CustomTypeRegistry::lookup(const QVariant &object, const QString &property) const
0039 {
0040     if (!object.isValid())
0041         return {};
0042     const auto id = object.userType();
0043     MetaType::LookupFunction lf;
0044 
0045     {
0046         auto it = types.constFind(id);
0047         if (it == types.constEnd()) {
0048             qCWarning(KTEXTTEMPLATE_CUSTOMTYPE) << "Don't know how to handle metatype" << QMetaType(id).name();
0049             // :TODO: Print out error message
0050             return {};
0051         }
0052 
0053         const CustomTypeInfo &info = it.value();
0054         if (!info.lookupFunction) {
0055             qCWarning(KTEXTTEMPLATE_CUSTOMTYPE) << "No lookup function for metatype" << QMetaType(id).name();
0056             lf = nullptr;
0057             // :TODO: Print out error message
0058             return {};
0059         }
0060 
0061         lf = info.lookupFunction;
0062     }
0063 
0064     return lf(object, property);
0065 }
0066 
0067 bool CustomTypeRegistry::lookupAlreadyRegistered(int id) const
0068 {
0069     auto it = types.constFind(id);
0070     if (it != types.constEnd()) {
0071         return it.value().lookupFunction != nullptr;
0072     }
0073     return false;
0074 }