MythTV  master
rtti.cpp
Go to the documentation of this file.
1 // Program Name: rtti.cpp
3 // Created : July 25, 2014
4 //
5 // Copyright (c) 2014 David Blain <dblain@mythtv.org>
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //
25 
26 #include "rtti.h"
27 
28 #include <QCoreApplication>
29 #include <QMetaEnum>
30 #include <QStringList>
31 
33 //
35 
36 DTC::Enum* Rtti::GetEnum( const QString &sFQN )
37 {
38  if (sFQN.isEmpty())
39  return nullptr;
40 
41  // ----------------------------------------------------------------------
42  // accept enum FQN in either class::enum or class.enum formats.
43  // ----------------------------------------------------------------------
44 
45  QString sType( sFQN );
46 
47  sType.replace( ".", "::" );
48 
49  int nLastIdx = sType.lastIndexOf( "::" );
50 
51  if (nLastIdx == -1)
52  return nullptr;
53 
54  QString sParentFQN = sType.mid( 0, nLastIdx );
55  QString sEnumName = sType.mid( nLastIdx+2 );
56 
57  // ----------------------------------------------------------------------
58  // Create Parent object so we can get to its metaObject
59  // ----------------------------------------------------------------------
60 
61  int nParentId = QMetaType::type( sParentFQN.toUtf8() );
62 
63  if ((nParentId == 0) && !sParentFQN.startsWith( "DTC::" ))
64  {
65  sParentFQN = "DTC::" + sParentFQN;
66  nParentId = QMetaType::type( sParentFQN.toUtf8() );
67  }
68 
69  auto *pParentClass = (QObject *)QMetaType::create( nParentId );
70  if (pParentClass == nullptr)
71  return nullptr;
72 
73  const QMetaObject *pMetaObject = pParentClass->metaObject();
74 
75  QMetaType::destroy( nParentId, pParentClass );
76 
77  // ----------------------------------------------------------------------
78  // Now look up enum
79  // ----------------------------------------------------------------------
80 
81  int nEnumIdx = pMetaObject->indexOfEnumerator( sEnumName.toUtf8() );
82 
83  if (nEnumIdx < 0 )
84  return nullptr;
85 
86  QMetaEnum metaEnum = pMetaObject->enumerator( nEnumIdx );
87 
88  // ----------------------------------------------------------------------
89  //
90  // ----------------------------------------------------------------------
91 
92  auto *pEnum = new DTC::Enum();
93 
94  pEnum->setType( sFQN );
95 
96  for( int nIdx = 0; nIdx < metaEnum.keyCount(); nIdx++)
97  {
98  QString sKey = metaEnum.key ( nIdx );
99  int nValue = metaEnum.value( nIdx );
100 
101  QString sFQNKey = sFQN + "." + sKey;
102 
103  if (sFQNKey.startsWith( "DTC::" ))
104  sFQNKey.remove( 0, 5 );
105 
106  sFQNKey.replace( "::", "." );
107 
108  QString sDesc = QCoreApplication::translate( "Enums",
109  sFQNKey.toUtf8());
110 
111  DTC::EnumItem *pEnumItem = pEnum->AddNewEnum();
112 
113  pEnumItem->setKey ( sKey );
114  pEnumItem->setValue( nValue );
115  pEnumItem->setDesc ( sDesc );
116 
117  }
118 
119  return pEnum;
120 }
rtti.h
Rtti::GetEnum
DTC::Enum * GetEnum(const QString &FQN) override
Definition: rtti.cpp:36
DTC::EnumItem
Definition: enumItem.h:21
DTC::Enum
Definition: enum.h:25