RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
property.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 "Property.h"
24 #include "ColorCombo.h"
25 
26 #include <QtCore/QMetaProperty>
27 #include <QtWidgets/QSpinBox>
28 
29 #include "Editor/EditorWindow.h"
30 
31 Property::Property(const QString& name /*= QString()*/, QObject* propertyObject /*= 0*/, QObject* parent /*= 0*/) : QObject(parent),
32 m_propertyObject(propertyObject)
33 {
34  setObjectName(name);
35 
37  {
38  connect(this, SIGNAL(propertyChanged(QObject*,QString,QVariant,QVariant)),
39  EditorWindow::currentInstance, SLOT(propertyChanged(QObject*,QString,QVariant,QVariant)));
40  }
41 }
42 
43 QVariant Property::value(int /*role = Qt::UserRole*/) const
44 {
45  if (m_propertyObject)
46  return m_propertyObject->property(qPrintable(objectName()));
47  else
48  return QVariant();
49 }
50 
51 void Property::setValue(const QVariant &value)
52 {
53  if (m_propertyObject)
54  {
55  QVariant old_value = m_propertyObject->property(qPrintable(objectName()));
56  m_propertyObject->setProperty(qPrintable(objectName()), value);
57  emit propertyChanged(m_propertyObject, objectName(), value, old_value);
58  }
59 }
60 
62 {
63  if( m_propertyObject->dynamicPropertyNames().contains( objectName().toLocal8Bit() ) )
64  return false;
65  if (m_propertyObject && m_propertyObject->metaObject()->property(m_propertyObject->metaObject()->indexOfProperty(qPrintable(objectName()))).isWritable())
66  return false;
67  else
68  return true;
69 }
70 
71 QWidget* Property::createEditor(QWidget *parent, const QStyleOptionViewItem& /*option*/)
72 {
73  QWidget* editor = 0;
74  switch(value().type())
75  {
76  case QVariant::Color:
77  editor = new ColorCombo(parent);
78  break;
79  case QVariant::Int:
80  editor = new QSpinBox(parent);
81  editor->setProperty("minimum", -INT_MAX);
82  editor->setProperty("maximum", INT_MAX);
83  connect(editor, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
84  break;
85  case QMetaType::Float:
86  case QVariant::Double:
87  editor = new QDoubleSpinBox(parent);
88  editor->setProperty("minimum", -INT_MAX);
89  editor->setProperty("maximum", INT_MAX);
90  connect(editor, SIGNAL(valueChanged(double)), this, SLOT(setValue(double)));
91  break;
92  default:
93  return editor;
94  }
95  return editor;
96 }
97 
98 bool Property::setEditorData(QWidget *editor, const QVariant &data)
99 {
100  switch(value().type())
101  {
102  case QVariant::Color:
103  static_cast<ColorCombo*>(editor)->setColor(data.value<QColor>());
104  return true;;
105  case QVariant::Int:
106  editor->blockSignals(true);
107  static_cast<QSpinBox*>(editor)->setValue(data.toInt());
108  editor->blockSignals(false);
109  return true;
110  case QMetaType::Float:
111  case QVariant::Double:
112  editor->blockSignals(true);
113  static_cast<QDoubleSpinBox*>(editor)->setValue(data.toDouble());
114  editor->blockSignals(false);
115  return true;
116  default:
117  return false;
118  }
119  return false;
120 }
121 
122 QVariant Property::editorData(QWidget *editor)
123 {
124  switch(value().type())
125  {
126  case QVariant::Color:
127  return QVariant::fromValue(static_cast<ColorCombo*>(editor)->color());
128  case QVariant::Int:
129  return QVariant(static_cast<QSpinBox*>(editor)->value());
130  case QMetaType::Float:
131  case QVariant::Double:
132  return QVariant(static_cast<QDoubleSpinBox*>(editor)->value());
133  break;
134  default:
135  return QVariant();
136  }
137 }
138 
139 Property* Property::findPropertyObject(QObject* propertyObject)
140 {
141  if (m_propertyObject == propertyObject)
142  return this;
143  for (int i=0; i<children().size(); ++i)
144  {
145  Property* child = static_cast<Property*>(children()[i])->findPropertyObject(propertyObject);
146  if (child)
147  return child;
148  }
149  return 0;
150 }
151 
152 void Property::setValue(double value)
153 {
154  setValue(QVariant(value));
155 }
156 
157 void Property::setValue(int value)
158 {
159  setValue(QVariant(value));
160 }
void propertyChanged(QObject *, QString, QVariant, QVariant)
Property(const QString &name=QString(), QObject *propertyObject=0, QObject *parent=0)
Definition: property.cpp:31
virtual void setValue(const QVariant &value)
Definition: property.cpp:51
QObject * m_propertyObject
Definition: property.h:142
virtual QVariant editorData(QWidget *editor)
Definition: property.cpp:122
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option)
Definition: property.cpp:71
static EditorWindow * currentInstance
Definition: editorwindow.h:69
virtual bool setEditorData(QWidget *editor, const QVariant &data)
Definition: property.cpp:98
virtual QVariant value(int role=Qt::UserRole) const
Definition: property.cpp:43
bool isReadOnly()
Definition: property.cpp:61
Property * findPropertyObject(QObject *propertyObject)
Definition: property.cpp:139