FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
qobject_utils.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (30 Mar. 2015)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 class QObject;
19 
20 #include "core.h"
21 #include <vector>
22 
23 namespace qtcore {
24 
26 {
27 public:
28  static void forwardSignal(
29  const QObject* sender, const QObject* resender, const char* signal);
30 
31  template<typename PARENT_TYPE>
32  static const PARENT_TYPE* constFindParent(const QObject* object);
33 
34  template<typename PARENT_TYPE>
35  static PARENT_TYPE* findParent(QObject* object);
36 
37  template<typename QOBJECT, typename ENUM>
38  static ENUM toQEnumValue(const char* enumName, int value);
39 
40  template<typename QOBJECT, typename ENUM>
41  static std::vector<ENUM> allQEnumValues(const char* enumName);
42 };
43 
44 } // namespace qtcore
45 
46 // --
47 // -- Implementation
48 // --
49 
50 #include <QtCore/QObject>
51 #include <QtCore/QMetaObject>
52 #include <QtCore/QMetaEnum>
53 #include <cassert>
54 
55 namespace qtcore {
56 
57 template<typename PARENT_TYPE>
58 const PARENT_TYPE* QObjectUtils::constFindParent(const QObject* object)
59 {
60  return findParent<PARENT_TYPE>(const_cast<QObject*>(object));
61 }
62 
63 template<typename PARENT_TYPE>
64 PARENT_TYPE* QObjectUtils::findParent(QObject* object)
65 {
66  QObject* it = object;
67  while (it != NULL && qobject_cast<PARENT_TYPE*>(it) == NULL)
68  it = it->parent();
69  return qobject_cast<PARENT_TYPE*>(it);
70 }
71 
73 template<typename QOBJECT, typename ENUM>
74 ENUM QObjectUtils::toQEnumValue(const char* enumName, int value)
75 {
76  const QMetaObject& metaObj = QOBJECT::staticMetaObject;
77  const int enumIndex = metaObj.indexOfEnumerator(enumName);
78  if (enumIndex != -1) {
79  const QMetaEnum metaEnum = metaObj.enumerator(enumIndex);
80  if (metaEnum.valueToKey(value) != nullptr)
81  return static_cast<ENUM>(value);
82  }
83  assert(false);
84  return static_cast<ENUM>(value);
85 }
86 
93 template<typename QOBJECT, typename ENUM>
94 std::vector<ENUM> QObjectUtils::allQEnumValues(const char* enumName)
95 {
96  std::vector<ENUM> enumValueVec;
97  const int enumIndex = QOBJECT::staticMetaObject.indexOfEnumerator(enumName);
98  if (enumIndex != -1) {
99  const QMetaEnum metaEnum = QOBJECT::staticMetaObject.enumerator(enumIndex);
100  enumValueVec.reserve(metaEnum.keyCount());
101  for (int i = 0; i < metaEnum.keyCount(); ++i) {
102  const int enumValueAsInt = metaEnum.value(i);
103  if (enumValueAsInt != -1)
104  enumValueVec.push_back(static_cast<ENUM>(enumValueAsInt));
105  }
106  }
107  return enumValueVec;
108 }
109 
110 } // namespace qtcore
Provides a collection of tools around QObject.
Definition: qobject_utils.h:25
static ENUM toQEnumValue(const char *enumName, int value)
Safe cast of an integer to a Q_ENUM value.
Definition: qobject_utils.h:74
#define QTTOOLS_CORE_EXPORT
Definition: core.h:27
Definition: grid_numbering.cpp:19
static PARENT_TYPE * findParent(QObject *object)
Definition: qobject_utils.h:64
static const PARENT_TYPE * constFindParent(const QObject *object)
Definition: qobject_utils.h:58
static std::vector< ENUM > allQEnumValues(const char *enumName)
All enumerator values of QOBJECT::ENUM returned in a single array.
Definition: qobject_utils.h:94