Add wpseditor, the Google Summer of Code 2008 project of Rostislav Chekan. Closes...
[kugel-rb.git] / utils / wpseditor / gui / src / QPropertyEditor / Property.cpp
blob0746d1514073fde0d058fe2e0a3f70db9b9093a6
1 // ****************************************************************************************
2 //
3 // QPropertyEditor Library
4 // --------------------------------------
5 // Copyright (C) 2007 Volker Wiendl
6 //
7 // This file is part of the Horde3D Scene Editor.
8 //
9 // The QPropertyEditor Library is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation version 3 of the License
13 // The Horde3D Scene Editor is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 // ****************************************************************************************
23 #include "Property.h"
24 #include "ColorCombo.h"
26 #include <Qt/qmetaobject.h>
27 #include <Qt/qspinbox.h>
29 #include <limits.h>
31 Property::Property(const QString& name /*= QString()*/, QObject* propertyObject /*= 0*/, QObject* parent /*= 0*/) : QObject(parent),
32 m_propertyObject(propertyObject) {
33 setObjectName(name);
36 QVariant Property::value(int /*role = Qt::UserRole*/) const {
37 if (m_propertyObject)
38 return m_propertyObject->property(qPrintable(objectName()));
39 else
40 return QVariant();
43 void Property::setValue(const QVariant &value) {
44 if (m_propertyObject)
45 m_propertyObject->setProperty(qPrintable(objectName()), value);
48 bool Property::isReadOnly() {
49 if (m_propertyObject && m_propertyObject->metaObject()->property(m_propertyObject->metaObject()->indexOfProperty(qPrintable(objectName()))).isWritable())
50 return false;
51 else
52 return true;
55 QWidget* Property::createEditor(QWidget *parent, const QStyleOptionViewItem &option) {
56 (void)option;
57 QWidget* editor = 0;
58 switch (value().type()) {
59 case QVariant::Color:
60 editor = new ColorCombo(parent);
61 break;
62 case QVariant::Int:
63 editor = new QSpinBox(parent);
64 editor->setProperty("minimum", -INT_MAX);
65 editor->setProperty("maximum", INT_MAX);
66 connect(editor, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
67 break;
68 case QMetaType::Float:
69 case QVariant::Double:
70 editor = new QDoubleSpinBox(parent);
71 editor->setProperty("minimum", -INT_MAX);
72 editor->setProperty("maximum", INT_MAX);
73 connect(editor, SIGNAL(valueChanged(double)), this, SLOT(setValue(double)));
74 break;
75 default:
76 return editor;
78 return editor;
81 bool Property::setEditorData(QWidget *editor, const QVariant &data) {
82 switch (value().type()) {
83 case QVariant::Color:
84 static_cast<ColorCombo*>(editor)->setColor(data.value<QColor>());
85 return true;
87 case QVariant::Int:
88 editor->blockSignals(true);
89 static_cast<QSpinBox*>(editor)->setValue(data.toInt());
90 editor->blockSignals(false);
91 return true;
92 case QMetaType::Float:
93 case QVariant::Double:
94 editor->blockSignals(true);
95 static_cast<QDoubleSpinBox*>(editor)->setValue(data.toDouble());
96 editor->blockSignals(false);
97 return true;
98 default:
99 return false;
101 return false;
104 QVariant Property::editorData(QWidget *editor) {
105 switch (value().type()) {
106 case QVariant::Color:
107 return QVariant::fromValue(static_cast<ColorCombo*>(editor)->color());
108 case QVariant::Int:
109 return QVariant(static_cast<QSpinBox*>(editor)->value());
110 case QMetaType::Float:
111 case QVariant::Double:
112 return QVariant(static_cast<QDoubleSpinBox*>(editor)->value());
113 break;
114 default:
115 return QVariant();
119 Property* Property::findPropertyObject(QObject* propertyObject) {
120 if (m_propertyObject == propertyObject)
121 return this;
122 for (int i=0; i<children().size(); ++i) {
123 Property* child = static_cast<Property*>(children()[i])->findPropertyObject(propertyObject);
124 if (child)
125 return child;
127 return 0;
130 void Property::setValue(double value) {
131 setValue(QVariant(value));
134 void Property::setValue(int value) {
135 setValue(QVariant(value));