1 /*
2 enumutil.cpp
3
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
6
7 Copyright (C) 2016-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
9
10 Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11 accordance with GammaRay Commercial License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29 #include "enumutil.h"
30 #include "enumrepositoryserver.h"
31
32 #include <QDebug>
33 #include <QMetaEnum>
34
35 using namespace GammaRay;
36
37 namespace GammaRay {
38 class ProtectedExposer : public QObject
39 {
40 public:
41 using QObject::staticQtMetaObject;
42 };
43 }
44
metaObjectForClass(const QByteArray & name)45 static const QMetaObject *metaObjectForClass(const QByteArray &name)
46 {
47 if (name.isEmpty())
48 return nullptr;
49 auto mo = QMetaType::metaObjectForType(QMetaType::type(name));
50 if (mo)
51 return mo;
52 mo = QMetaType::metaObjectForType(QMetaType::type(name + '*')); // try pointer version, more likely for QObjects
53 return mo;
54 }
55
metaEnum(const QVariant & value,const char * typeName,const QMetaObject * metaObject)56 QMetaEnum EnumUtil::metaEnum(const QVariant &value, const char *typeName, const QMetaObject *metaObject)
57 {
58 QByteArray fullTypeName(typeName);
59 if (fullTypeName.isEmpty())
60 fullTypeName = value.typeName();
61
62 // split class name and enum name
63 QByteArray className;
64 QByteArray enumTypeName(fullTypeName);
65 const int pos = enumTypeName.lastIndexOf("::");
66 if (pos >= 0) {
67 className = enumTypeName.left(pos);
68 enumTypeName = enumTypeName.mid(pos + 2);
69 }
70
71 const QMetaObject *mo = &ProtectedExposer::staticQtMetaObject;
72 int enumIndex = mo->indexOfEnumerator(enumTypeName);
73 if (enumIndex < 0 && metaObject) {
74 mo = metaObject;
75 enumIndex = mo->indexOfEnumerator(enumTypeName);
76 }
77 if (enumIndex < 0 && (mo = QMetaType::metaObjectForType(QMetaType::type(fullTypeName)))) {
78 enumIndex = mo->indexOfEnumerator(enumTypeName);
79 }
80 if (enumIndex < 0 && (mo = metaObjectForClass(className))) {
81 enumIndex = mo->indexOfEnumerator(enumTypeName);
82 }
83
84 // attempt to recover namespaces from semi-qualified type names
85 if (enumIndex < 0 && metaObject) {
86 QByteArray n(metaObject->className());
87 const int pos = n.lastIndexOf("::");
88 if (pos > 0) {
89 n = n.left(pos + 2) + fullTypeName;
90 return metaEnum(value, n, nullptr);
91 }
92 }
93
94 if (enumIndex < 0)
95 return {};
96 return mo->enumerator(enumIndex);
97 }
98
enumToInt(const QVariant & value,const QMetaEnum & metaEnum)99 int EnumUtil::enumToInt(const QVariant &value, const QMetaEnum &metaEnum)
100 {
101 // QVariant has no implicit QFlag to int conversion as of Qt 5.7
102 if (metaEnum.isFlag() && QMetaType::sizeOf(value.userType()) == sizeof(int)) // int should be enough, QFlag has that hardcoded
103 return value.constData() ? *static_cast<const int*>(value.constData()) : 0;
104 return value.toInt();
105 }
106
enumToString(const QVariant & value,const char * typeName,const QMetaObject * metaObject)107 QString EnumUtil::enumToString(const QVariant &value, const char *typeName, const QMetaObject *metaObject)
108 {
109 const auto me = metaEnum(value, typeName, metaObject);
110 if (me.isValid()) {
111 return me.isFlag() ? QString::fromUtf8(me.valueToKeys(enumToInt(value, me))) : QString::fromUtf8(me.valueToKey(enumToInt(value, me)));
112 }
113 if (EnumRepositoryServer::isEnum(value.userType())) {
114 const auto ev = EnumRepositoryServer::valueFromVariant(value);
115 const auto def = EnumRepositoryServer::definitionForId(ev.id());
116 return def.valueToString(ev);
117 }
118 return QString();
119 }
120