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 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
62  int nParentId = QMetaType::type( sParentFQN.toUtf8() );
63 
64  if ((nParentId == 0) && !sParentFQN.startsWith( "DTC::" ))
65  {
66  sParentFQN = "DTC::" + sParentFQN;
67  nParentId = QMetaType::type( sParentFQN.toUtf8() );
68  }
69 
70  auto *pParentClass = (QObject *)QMetaType::create( nParentId );
71  if (pParentClass == nullptr)
72  return nullptr;
73 
74  const QMetaObject *pMetaObject = pParentClass->metaObject();
75 
76  QMetaType::destroy( nParentId, pParentClass );
77 #else
78  const QMetaObject *pMetaObject =
79  QMetaType::fromName( sParentFQN.toUtf8() ).metaObject();
80  if (pMetaObject == nullptr)
81  pMetaObject = QMetaType::fromName( "DTC::" + sParentFQN.toUtf8() ).metaObject();
82 #endif
83 
84  // ----------------------------------------------------------------------
85  // Now look up enum
86  // ----------------------------------------------------------------------
87 
88  int nEnumIdx = pMetaObject->indexOfEnumerator( sEnumName.toUtf8() );
89 
90  if (nEnumIdx < 0 )
91  return nullptr;
92 
93  QMetaEnum metaEnum = pMetaObject->enumerator( nEnumIdx );
94 
95  // ----------------------------------------------------------------------
96  //
97  // ----------------------------------------------------------------------
98 
99  auto *pEnum = new DTC::Enum();
100 
101  pEnum->setType( sFQN );
102 
103  for( int nIdx = 0; nIdx < metaEnum.keyCount(); nIdx++)
104  {
105  QString sKey = metaEnum.key ( nIdx );
106  int nValue = metaEnum.value( nIdx );
107 
108  QString sFQNKey = sFQN + "." + sKey;
109 
110  if (sFQNKey.startsWith( "DTC::" ))
111  sFQNKey.remove( 0, 5 );
112 
113  sFQNKey.replace( "::", "." );
114 
115  QString sDesc = QCoreApplication::translate( "Enums",
116  sFQNKey.toUtf8());
117 
118  DTC::EnumItem *pEnumItem = pEnum->AddNewEnum();
119 
120  pEnumItem->setKey ( sKey );
121  pEnumItem->setValue( nValue );
122  pEnumItem->setDesc ( sDesc );
123 
124  }
125 
126  return pEnum;
127 }
rtti.h
Rtti::GetEnum
DTC::Enum * GetEnum(const QString &FQN) override
Definition: rtti.cpp:36
DTC::EnumItem
Definition: enumItem.h:20
DTC::Enum
Definition: enum.h:24