RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
enumproperty.cpp
Go to the documentation of this file.
1 // *************************************************************************************************
2 //
3 // QPropertyEditor v 0.3
4 //
5 // --------------------------------------
6 // Copyright (C) 2007 Volker Wiendl
7 // Acknowledgements to Roman alias banal from qt-apps.org for the Enum enhancement
8 //
9 //
10 // The QPropertyEditor Library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation version 3 of the License
13 //
14 // The Horde3D Scene Editor is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 // *************************************************************************************************
23 #include "EnumProperty.h"
24 
26 // Constructor
28 EnumProperty::EnumProperty(const QString &name /* = QString()*/,
29  QObject *propertyObject /* = 0*/, QObject *parent /* = 0*/)
30 : Property(name, propertyObject, parent)
31 {
32  // get the meta property object
33  const QMetaObject* meta = propertyObject->metaObject();
34  QMetaProperty prop = meta->property(meta->indexOfProperty(qPrintable(name)));
35 
36  // if it is indeed an enum type, fill the QStringList member with the keys
37  if(prop.isEnumType()){
38  QMetaEnum qenum = prop.enumerator();
39  for(int i=0; i < qenum.keyCount(); i++){
40  m_enum << qenum.key(i);
41  }
42  }
43 }
44 
46 // value
48 QVariant EnumProperty::value(int role /* = Qt::UserRole */) const {
49  if(role == Qt::DisplayRole){
50  if (m_propertyObject){
51  // resolve the value to the corresponding enum key
52  int index = m_propertyObject->property(qPrintable(objectName())).toInt();
53 
54  const QMetaObject* meta = m_propertyObject->metaObject();
55  QMetaProperty prop = meta->property(meta->indexOfProperty(qPrintable(objectName())));
56  return QVariant(prop.enumerator().valueToKey(index));
57  } else{
58  return QVariant();
59  }
60  } else {
61  return Property::value(role);
62  }
63 }
64 
66 // createEditor
68 QWidget* EnumProperty::createEditor(QWidget* parent, const QStyleOptionViewItem&){
69  // create a QComboBox and fill it with the QStringList values
70  QComboBox* editor = new QComboBox(parent);
71  editor->addItems(m_enum);
72 
73  connect(editor, SIGNAL(currentIndexChanged(int)),
74  this, SLOT(valueChanged(int)));
75  return editor;
76 }
77 
79 // setEditorData
81 bool EnumProperty::setEditorData(QWidget *editor, const QVariant &data)
82 {
83  QComboBox* combo = 0;
84  if(combo = qobject_cast<QComboBox*>(editor)){
85  int value = data.toInt();
86  const QMetaObject* meta = m_propertyObject->metaObject();
87  QMetaProperty prop = meta->property(meta->indexOfProperty(qPrintable(objectName())));
88 
89  int index = combo->findText(prop.enumerator().valueToKey(value));
90  if(index == -1)
91  return false;
92 
93  combo->setCurrentIndex(index);
94  } else {
95  return false;
96  }
97 
98  return true;
99 }
100 
102 // editorData
104 QVariant EnumProperty::editorData(QWidget *editor)
105 {
106  QComboBox* combo = 0;
107  if(combo = qobject_cast<QComboBox*>(editor)){
108  return QVariant(combo->currentText());
109  } else {
110  return QVariant();
111  }
112 }
113 
115 // valueChanged
117 void EnumProperty::valueChanged(int value)
118 {
119  setValue(QVariant(value));
120 }
virtual void setValue(const QVariant &value)
Definition: property.cpp:51
QObject * m_propertyObject
Definition: property.h:142
EnumProperty(const QString &name=QString(), QObject *propertyObject=0, QObject *parent=0)
virtual QVariant editorData(QWidget *editor)
QStringList m_enum
Definition: enumproperty.h:63
virtual QVariant value(int role=Qt::UserRole) const
virtual bool setEditorData(QWidget *editor, const QVariant &data)
virtual QVariant value(int role=Qt::UserRole) const
Definition: property.cpp:43
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option)