diff options
Diffstat (limited to 'utils/wpseditor/gui/src/QPropertyEditor/Property.cpp')
-rw-r--r-- | utils/wpseditor/gui/src/QPropertyEditor/Property.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/Property.cpp b/utils/wpseditor/gui/src/QPropertyEditor/Property.cpp new file mode 100644 index 0000000000..0746d15140 --- /dev/null +++ b/utils/wpseditor/gui/src/QPropertyEditor/Property.cpp | |||
@@ -0,0 +1,136 @@ | |||
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 | ||
12 | // | ||
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. | ||
17 | // | ||
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/>. | ||
20 | // | ||
21 | // **************************************************************************************** | ||
22 | |||
23 | #include "Property.h" | ||
24 | #include "ColorCombo.h" | ||
25 | |||
26 | #include <Qt/qmetaobject.h> | ||
27 | #include <Qt/qspinbox.h> | ||
28 | |||
29 | #include <limits.h> | ||
30 | |||
31 | Property::Property(const QString& name /*= QString()*/, QObject* propertyObject /*= 0*/, QObject* parent /*= 0*/) : QObject(parent), | ||
32 | m_propertyObject(propertyObject) { | ||
33 | setObjectName(name); | ||
34 | } | ||
35 | |||
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(); | ||
41 | } | ||
42 | |||
43 | void Property::setValue(const QVariant &value) { | ||
44 | if (m_propertyObject) | ||
45 | m_propertyObject->setProperty(qPrintable(objectName()), value); | ||
46 | } | ||
47 | |||
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; | ||
53 | } | ||
54 | |||
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; | ||
77 | } | ||
78 | return editor; | ||
79 | } | ||
80 | |||
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; | ||
86 | ; | ||
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; | ||
100 | } | ||
101 | return false; | ||
102 | } | ||
103 | |||
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(); | ||
116 | } | ||
117 | } | ||
118 | |||
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; | ||
126 | } | ||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | void Property::setValue(double value) { | ||
131 | setValue(QVariant(value)); | ||
132 | } | ||
133 | |||
134 | void Property::setValue(int value) { | ||
135 | setValue(QVariant(value)); | ||
136 | } | ||