KDStateMachineEditor  1.1.0
A framework for creating Qt State Machine metacode using a graphical user interface
objecthelper.h
1 /*
2  This file is part of the KDAB State Machine Editor Library.
3 
4  Copyright (C) 2014-2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com.
5  All rights reserved.
6  Author: Kevin Funk <kevin.funk@kdab.com>
7 
8  Licensees holding valid commercial KDAB State Machine Editor Library
9  licenses may use this file in accordance with the KDAB State Machine Editor
10  Library License Agreement provided with the Software.
11 
12  This file may be distributed and/or modified under the terms of the
13  GNU Lesser General Public License version 2.1 as published by the
14  Free Software Foundation and appearing in the file LICENSE.LGPL.txt included.
15 
16  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
17  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 
19  Contact info@kdab.com if any conditions of this licensing are not
20  clear to you.
21 */
22 
23 #ifndef KDSME_UTIL_OBJECTHELPER_H
24 #define KDSME_UTIL_OBJECTHELPER_H
25 
26 #include "kdsme_core_export.h"
27 
28 #include <QObject>
29 #include <QMetaEnum>
30 #include <QList>
31 
32 #include <algorithm>
33 
34 QT_BEGIN_NAMESPACE
35 class QPointF;
36 QT_END_NAMESPACE
37 
38 namespace KDSME {
39 
40 namespace ObjectHelper {
41 
42  KDSME_CORE_EXPORT QString addressToString(const void *p);
43 
44  enum DisplayOption {
45  NoStrip,
46  StripNameSpace,
47  };
48  KDSME_CORE_EXPORT QString className(const QObject* object, DisplayOption option = NoStrip);
49  KDSME_CORE_EXPORT QString displayString(const QObject *object, DisplayOption option = NoStrip);
50 
51  KDSME_CORE_EXPORT const char* enumToString(const QMetaObject* metaObject, const char* name, int value);
52  KDSME_CORE_EXPORT int stringToEnum(const QMetaObject* metaObject, const char* name, const char* key);
53 
54  KDSME_CORE_EXPORT QString toString(const QPointF& point);
55  KDSME_CORE_EXPORT QString toString(const QSizeF& size);
56  KDSME_CORE_EXPORT QString toString(const QRectF& rect);
57 
65  KDSME_CORE_EXPORT bool descendantOf(const QObject *ascendant, const QObject *object);
66 
72  KDSME_CORE_EXPORT int depth(const QObject* root, const QObject* object);
73 
79  template<class FilterType, class ItemType>
80  QList<FilterType> copy_if_type(const QList<ItemType> list) {
81  QList<FilterType> filteredList;
82  Q_FOREACH (const ItemType object, list) {
83  if (FilterType filterObject = qobject_cast<FilterType>(object))
84  filteredList << filterObject;
85  }
86  return filteredList;
87  }
88 
94  template<class C, class P>
95  C filter(const C& source, const P& predicate)
96  {
97  C d = source;
98  // filter elements
99  d.erase(std::remove_if(std::begin(d), std::end(d), predicate), std::end(d));
100  return d;
101  }
102 
110  template <typename T>
111  T *findParentOfType(QObject *object) {
112  if (!object) {
113  return nullptr;
114  }
115  if (qobject_cast<T*>(object)) {
116  return static_cast<T*>(object);
117  }
118  return findParentOfType<T>(object->parent());
119  }
120 
121 }
122 
123 }
124 
125 #endif
Definition: abstractexporter.h:33

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
https://github.com/KDAB/KDStateMachineEditor