KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
objecthelper.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDAB State Machine Editor Library.
3
4 SPDX-FileCopyrightText: 2014 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: Kevin Funk <kevin.funk@kdab.com>
6
7 SPDX-License-Identifier: LGPL-2.1-only OR LicenseRef-KDAB-KDStateMachineEditor
8
9 Licensees holding valid commercial KDAB State Machine Editor Library
10 licenses may use this file in accordance with the KDAB State Machine Editor
11 Library 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
16#include "objecthelper.h"
17
18#include <QMetaObject>
19#include <QPointF>
20#include <QRectF>
21#include <QSizeF>
22
23using namespace KDSME;
24
25namespace {
26
27QString stripNameSpace(const QString &className)
28{
29 const int pos = static_cast<int>(className.lastIndexOf(QLatin1String("::")));
30 if (pos != -1)
31 return className.mid(pos + 2);
32 return className;
33}
34}
35
36QString ObjectHelper::addressToString(const void *p)
37{
38 return (QLatin1String("0x") + QString::number(reinterpret_cast<quint64>(p), 16));
39}
40
41QString ObjectHelper::className(const QObject *object, ObjectHelper::DisplayOption option)
42{
43 QString className = QString::fromLatin1(object->metaObject()->className());
44 return option == StripNameSpace ? stripNameSpace(className) : std::move(className);
45}
46
47QString ObjectHelper::displayString(const QObject *object, DisplayOption option)
48{
49 if (!object) {
50 return QStringLiteral("QObject(0x0)");
51 }
52 if (object->objectName().isEmpty()) {
53 return QString::fromLatin1("%1 (%2)").arg(addressToString(object)).arg(className(object, option)); // clazy:exclude=qstring-arg
54 }
55 return object->objectName();
56}
57
58const char *ObjectHelper::enumToString(const QMetaObject *metaObject, const char *name, int value)
59{
60 const int index = metaObject->indexOfEnumerator(name);
61 const QMetaEnum metaEnum = metaObject->enumerator(index);
62 return metaEnum.valueToKey(value);
63}
64
65int ObjectHelper::stringToEnum(const QMetaObject *metaObject, const char *name, const char *key)
66{
67 const int index = metaObject->indexOfEnumerator(name);
68 const QMetaEnum metaEnum = metaObject->enumerator(index);
69 return metaEnum.keyToValue(key);
70}
71
72QString ObjectHelper::toString(const QPointF &point) // clazy:exclude=function-args-by-value
73{
74 return QStringLiteral("(%1,%2)").arg(point.x()).arg(point.y());
75}
76
77QString ObjectHelper::toString(const QSizeF &size) // clazy:exclude=function-args-by-value
78{
79 return QStringLiteral("(%1,%2)").arg(size.width()).arg(size.height());
80}
81
82QString ObjectHelper::toString(const QRectF &rect)
83{
84 return QStringLiteral("(pos=%1,size=%2)")
85 .arg(toString(rect.topLeft())) // clazy:exclude=qstring-arg
86 .arg(toString(rect.size()));
87}
88
89bool ObjectHelper::descendantOf(const QObject *ascendant, const QObject *object)
90{
91 const QObject *parent = object->parent();
92 if (!parent) {
93 return false;
94 }
95 if (parent == ascendant) {
96 return true;
97 }
98 return descendantOf(ascendant, parent);
99}
100
101int ObjectHelper::depth(const QObject *root, const QObject *object)
102{
103 if (!object)
104 return -1;
105
106 int depth = 0;
107 const QObject *current = object;
108 while (current && current != root) {
109 ++depth;
110 current = current->parent();
111 }
112 return depth;
113}
KDSME_CORE_EXPORT QString toString(const QPointF &point)
KDSME_CORE_EXPORT QString displayString(const QObject *object, DisplayOption option=NoStrip)
KDSME_CORE_EXPORT int depth(const QObject *root, const QObject *object)
KDSME_CORE_EXPORT QString className(const QObject *object, DisplayOption option=NoStrip)
KDSME_CORE_EXPORT int stringToEnum(const QMetaObject *metaObject, const char *name, const char *key)
KDSME_CORE_EXPORT bool descendantOf(const QObject *ascendant, const QObject *object)
KDSME_CORE_EXPORT QString addressToString(const void *p)
KDSME_CORE_EXPORT const char * enumToString(const QMetaObject *metaObject, const char *name, int value)

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8