summaryrefslogtreecommitdiff
path: root/utils/wpseditor/gui/src/QPropertyEditor
diff options
context:
space:
mode:
Diffstat (limited to 'utils/wpseditor/gui/src/QPropertyEditor')
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp73
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.h49
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/Property.cpp136
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/Property.h157
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditor.pro26
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditorWidget.cpp56
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditorWidget.h113
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/QPropertyModel.cpp236
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/QPropertyModel.h105
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp105
-rw-r--r--utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.h78
11 files changed, 1134 insertions, 0 deletions
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp b/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp
new file mode 100644
index 0000000000..f5eeb030dc
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp
@@ -0,0 +1,73 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23//
24// This class is based on the Color Editor Factory Example by Trolltech
25//
26// *************************************************************************************************
27
28#include "ColorCombo.h"
29
30#include <Qt/qcolordialog.h>
31
32ColorCombo::ColorCombo(QWidget* parent /*= 0*/) : QComboBox(parent) {
33 QStringList colorNames = QColor::colorNames();
34 for (int i = 0; i < colorNames.size(); ++i) {
35 QColor color(colorNames[i]);
36 insertItem(i, colorNames[i]);
37 setItemData(i, color, Qt::DecorationRole);
38 }
39 addItem(tr("Custom"), QVariant((int)QVariant::UserType));
40 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(currentChanged(int)));
41}
42
43
44ColorCombo::~ColorCombo() {}
45
46
47QColor ColorCombo::color() const {
48 return qVariantValue<QColor>(itemData(currentIndex(), Qt::DecorationRole));
49}
50
51void ColorCombo::setColor(QColor color) {
52 m_init = color;
53 setCurrentIndex(findData(color, int(Qt::DecorationRole)));
54 if (currentIndex() == -1) {
55 addItem(color.name());
56 setItemData(count()-1, color, Qt::DecorationRole);
57 setCurrentIndex(count()-1);
58 }
59}
60
61void ColorCombo::currentChanged(int index) {
62 if (itemData(index).isValid() && itemData(index) == QVariant((int)QVariant::UserType)) {
63 QColor color = QColorDialog::getColor(m_init, this);
64 if (color.isValid()) {
65 if (findData(color, int(Qt::DecorationRole)) == -1) {
66 addItem(color.name());
67 setItemData(count()-1, color, Qt::DecorationRole);
68 }
69 setCurrentIndex(findData(color, int(Qt::DecorationRole)));
70 } else
71 setCurrentIndex(findData(m_init));
72 }
73}
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.h b/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.h
new file mode 100644
index 0000000000..530b05bbc5
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.h
@@ -0,0 +1,49 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23//
24// This class is based on the Color Editor Factory Example by Trolltech
25//
26// *************************************************************************************************
27
28#ifndef COLORCOMBO_H_
29#define COLORCOMBO_H_
30
31#include <Qt/qcombobox.h>
32
33class ColorCombo : public QComboBox {
34 Q_OBJECT
35public:
36 ColorCombo(QWidget* parent = 0);
37 virtual ~ColorCombo();
38
39 QColor color() const;
40 void setColor(QColor c);
41
42private slots:
43 void currentChanged(int index);
44
45private:
46 QColor m_init;
47
48};
49#endif
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
31Property::Property(const QString& name /*= QString()*/, QObject* propertyObject /*= 0*/, QObject* parent /*= 0*/) : QObject(parent),
32 m_propertyObject(propertyObject) {
33 setObjectName(name);
34}
35
36QVariant 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
43void Property::setValue(const QVariant &value) {
44 if (m_propertyObject)
45 m_propertyObject->setProperty(qPrintable(objectName()), value);
46}
47
48bool 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
55QWidget* 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
81bool 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
104QVariant 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
119Property* 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
130void Property::setValue(double value) {
131 setValue(QVariant(value));
132}
133
134void Property::setValue(int value) {
135 setValue(QVariant(value));
136}
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/Property.h b/utils/wpseditor/gui/src/QPropertyEditor/Property.h
new file mode 100644
index 0000000000..52d6842987
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/Property.h
@@ -0,0 +1,157 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// *************************************************************************************************
24
25#ifndef PROPERTY_H_
26#define PROPERTY_H_
27
28#include <Qt/qwidget.h>
29#include <Qt/qstyleoption.h>
30#include <Qt/qvariant.h>
31
32/**
33 * The Property class is the base class for all properties in the QPropertyEditor
34 * You can implement custom properties inherited from this class to further enhence the
35 * functionality of the QPropertyEditor
36 */
37class Property : public QObject {
38 Q_OBJECT
39
40public:
41
42 /**
43 * Constructor
44 *
45 * @param name the name of the property within the propertyObject (will be used in the QPropertyEditorWidget view too)
46 * @param propertyObject the object that contains the property
47 * @param parent optional parent object
48 */
49 Property(const QString& name = QString(), QObject* propertyObject = 0, QObject* parent = 0);
50
51 /**
52 * The value stored by this property
53 * @return QVariant the data converted to a QVariant
54 */
55 virtual QVariant value(int role = Qt::UserRole) const;
56 /**
57 * Sets the value stored by this property
58 * @param value the data converted to a QVariant
59 */
60 virtual void setValue(const QVariant& value);
61
62 /**
63 * Returns the QObject which contains the property managed by this instance
64 * @return QObject* pointer to the QObject that contains user defined properties
65 */
66 QObject* propertyObject() {
67 return m_propertyObject;
68 }
69
70 /**
71 * Flag if property is used for indicating a group or really manages a property
72 * @return bool true if this property is only used to display a category in the QPropertyEditorWidget
73 */
74 bool isRoot() {
75 return m_propertyObject == 0;
76 }
77
78 /**
79 * Flag if the property can be set
80 * @return bool true if this property has no set method
81 */
82 bool isReadOnly();
83
84 /**
85 * Returns the row of this instance within the QPropertyModel
86 * @return int row within the QPropertyModel
87 */
88 int row() {
89 return parent()->children().indexOf(this);
90 }
91
92 /**
93 * returns optional settings for the editor widget that is used to manipulate the properties value
94 * @return QString a string that contains property settings for the editor widget (e.g. "minimum=1.0;maximum=10.0;")
95 */
96 QString editorHints() {
97 return m_hints;
98 }
99
100 /**
101 * Sets properties for the editor widget that is used to manipulate the data value managed by this instance
102 * @param hints a string containing property settings for the editor widget that manipulates this property
103 */
104 virtual void setEditorHints(const QString& hints) {
105 m_hints = hints;
106 }
107
108 /**
109 * Creates an editor for the data managed by this instance
110 * @param parent widget the newly created editor widget will be child of
111 * @param option currently not used
112 * @return QWidget* pointer to the editor widget
113 */
114 virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option);
115
116 /**
117 * Returns the data of the editor widget used to manipulate this instance
118 * @return QVariant the data converted to a QVariant
119 */
120 virtual QVariant editorData(QWidget *editor);
121
122 /**
123 * Changes the editor widget's data to a specific value
124 * @param editor the editor widget
125 * @param data the data to set in the editor widget
126 * @return bool true if editor widget was set to the given data successfully, false if the data can not be set in the editor (e.g. wrong datatype)
127 */
128 virtual bool setEditorData(QWidget *editor, const QVariant& data);
129
130 /**
131 * Tries to find the first property that manages the given propertyObject
132 * @param propertyObject
133 * @return Property
134 */
135 Property* findPropertyObject(QObject* propertyObject);
136
137private slots:
138 /**
139 * This slot is used to immediately set the properties when the editor widget's value of a double or float
140 * property has changed
141 * @param value the new value
142 */
143 void setValue(double value);
144 /**
145 * This slot is used to immediately set the properties when the editor widget's value of an integer
146 * property has changed
147 * @param value the new value
148 */
149 void setValue(int value);
150
151private:
152 QObject* m_propertyObject;
153 QString m_hints;
154
155};
156
157#endif
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditor.pro b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditor.pro
new file mode 100644
index 0000000000..85fd29ee2c
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditor.pro
@@ -0,0 +1,26 @@
1TEMPLATE = lib
2CONFIG += staticlib debug_and_release
3SOURCES = ColorCombo.cpp \
4 Property.cpp \
5 QPropertyEditorWidget.cpp \
6 QPropertyModel.cpp \
7 QVariantDelegate.cpp
8HEADERS = ColorCombo.h \
9 Property.h \
10 QPropertyEditorWidget.h \
11 QPropertyModel.h \
12 QVariantDelegate.h
13INCLUDEPATH += .
14DESTDIR = ../../lib
15UI_DIR = .
16CONFIG(debug, debug|release) {
17 TARGET = QPropertyEditord
18 OBJECTS_DIR = ../../build/QPropertyEditor/debug
19 MOC_DIR = ../../build/QPropertyEditor/debug
20}
21CONFIG(release, debug|release) {
22 TARGET = QPropertyEditor
23 OBJECTS_DIR = ../../build/QPropertyEditor/release
24 MOC_DIR = ../../build/QPropertyEditor/release
25 DEFINES += QT_NO_DEBUG
26}
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditorWidget.cpp b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditorWidget.cpp
new file mode 100644
index 0000000000..fc4b90c227
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditorWidget.cpp
@@ -0,0 +1,56 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// *************************************************************************************************
24
25#include "QPropertyEditorWidget.h"
26#include "QPropertyModel.h"
27#include "QVariantDelegate.h"
28#include "Property.h"
29
30QPropertyEditorWidget::QPropertyEditorWidget(QWidget* parent /*= 0*/) : QTreeView(parent) {
31 m_model = new QPropertyModel(this);
32 setModel(m_model);
33 setItemDelegate(new QVariantDelegate(this));
34}
35
36
37QPropertyEditorWidget::~QPropertyEditorWidget() {}
38
39void QPropertyEditorWidget::addObject(QObject* propertyObject) {
40 m_model->addItem(propertyObject);
41 expandToDepth(0);
42}
43
44void QPropertyEditorWidget::setObject(QObject* propertyObject) {
45 m_model->clear();
46 if (propertyObject)
47 addObject(propertyObject);
48}
49
50void QPropertyEditorWidget::updateObject(QObject* propertyObject) {
51 m_model->updateItem(propertyObject);
52}
53
54void QPropertyEditorWidget::setCustomPropertyCB(UserTypeCB callback) {
55 m_model->setCustomPropertyCB(callback);
56}
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditorWidget.h b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditorWidget.h
new file mode 100644
index 0000000000..2dab87722a
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyEditorWidget.h
@@ -0,0 +1,113 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// *************************************************************************************************
24
25#ifndef QPROPERTYEDITORWIDGET_H_
26#define QPROPERTYEDITORWIDGET_H_
27
28#include <Qt/qtreeview.h>
29
30class QPropertyModel;
31class Property;
32
33/**
34 * \mainpage QPropertyEditor
35 *
36 * \section intro_sec Introduction
37 *
38 * The main purpose for the QPropertyEditor is the visualization and manipulation of properties defined via the Q_PROPERTY macro in
39 * QObject based classes.
40 */
41
42/**
43 * \brief The QPropertyEditorWidget offers an easy to use mechanism to visualize properties of a class inherited from QObject.
44 *
45 * Qt provides a nice way to define class properties by using the Q_PROPERTY macro. The purpose of the QPropertyEditor
46 * is to visualize these properties in an easy way.
47 *
48 * To use the property editor, all you have to do is to create a class that defines it's properties by using Q_PROPERTY
49 * and to add this class by using the addObject() method of this QPropertyEditorWidget class.
50 * The QPropertyEditorWidget is inherited from QTreeView and will display the properties in a tree with two columns: Name and Value
51 *
52 * For basic data types the build in editor widgets of Qt will be used. The QPropertyEditor itself only defines an additional
53 * editor for QColor (based on the Color Editor Factory Example from Trolltech). But it can easily be extended by yourself
54 * either within the library or for special datatypes also outside of the library in your application.
55 */
56class QPropertyEditorWidget : public QTreeView {
57 Q_OBJECT
58public:
59
60 /**
61 * A typedef for a callback used to create user defined properties for custom datatypes
62 */
63 typedef Property* (*UserTypeCB)(const QString& name, QObject* propertyObject, Property* parent);
64
65 /**
66 * \brief Constructor
67 *
68 * Creates a new editor widget based on QTreeView
69 * @param parent optional parent widget
70 */
71 QPropertyEditorWidget(QWidget* parent = 0);
72
73 /// Destructor
74 virtual ~QPropertyEditorWidget();
75
76 /**
77 * Adds the user properties of the given class to the QPropertyModel associated with this view
78 *
79 * @param propertyObject the class inherited from QObject that contains user properties that should be
80 * managed by the QPropertyModel associated with this view
81 */
82 void addObject(QObject* propertyObject);
83
84 /**
85 * Similar to the addObject() method this method adds the properties of the given class to the QPropertyModel
86 * associated with this view. But in contrast to addObject() it will clear the model before, removing all
87 * previously added objects.
88 *
89 * @param propertyObject the class inherited from QObject that contains user properties that should be
90 * managed by the QPropertyModel associated with this view
91 */
92 void setObject(QObject* propertyObject);
93
94 /**
95 * Updates the view for the given object. This can be usefull if a property was changed programmatically instead
96 * of using the view. In this case the view normally will display the new property values only after the user clicked
97 * on it. To overcome this problem you can call updateObject with the object whose property was changed.
98 */
99 void updateObject(QObject* propertyObject);
100
101 /**
102 * If you define custom datatypes outside of this library the QPropertyModel will check if you
103 * also defined a callback that is responsible to create custom property classes inherited from Property to handle
104 * these datatypes. With this method you can set such a callback that will create custom properties for custom datatypes.
105 */
106 void setCustomPropertyCB(UserTypeCB callback);
107
108private:
109 /// The Model for this view
110 QPropertyModel* m_model;
111
112};
113#endif
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/QPropertyModel.cpp b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyModel.cpp
new file mode 100644
index 0000000000..b147cd089d
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyModel.cpp
@@ -0,0 +1,236 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// *************************************************************************************************
24
25#include "QPropertyModel.h"
26
27#include "Property.h"
28
29#include <Qt/qapplication.h>
30#include <Qt/qmetaobject.h>
31#include <Qt/qitemeditorfactory.h>
32
33struct PropertyPair {
34 PropertyPair(const QMetaObject* obj, QMetaProperty property) : Property(property), Object(obj) {}
35
36 QMetaProperty Property;
37 const QMetaObject* Object;
38
39 bool operator==(const PropertyPair& other) const {
40 return QString(other.Property.name()) == QString(Property.name());
41 }
42};
43
44
45QPropertyModel::QPropertyModel(QObject* parent /*= 0*/) : QAbstractItemModel(parent), m_userCallback(0) {
46 m_rootItem = new Property("Root",0, this);
47}
48
49
50QPropertyModel::~QPropertyModel() {}
51
52QModelIndex QPropertyModel::index ( int row, int column, const QModelIndex & parent /*= QModelIndex()*/ ) const {
53 Property *parentItem = m_rootItem;
54 if (parent.isValid())
55 parentItem = static_cast<Property*>(parent.internalPointer());
56 if (row >= parentItem->children().size())
57 return QModelIndex();
58 return createIndex(row, column, parentItem->children().at(row));
59
60}
61
62QModelIndex QPropertyModel::parent ( const QModelIndex & index ) const {
63 if (!index.isValid())
64 return QModelIndex();
65
66 Property *childItem = static_cast<Property*>(index.internalPointer());
67 Property *parentItem = qobject_cast<Property*>(childItem->parent());
68
69 if (!parentItem || parentItem == m_rootItem)
70 return QModelIndex();
71
72 return createIndex(parentItem->row(), 0, parentItem);
73}
74
75int QPropertyModel::rowCount ( const QModelIndex & parent /*= QModelIndex()*/ ) const {
76 Property *parentItem = m_rootItem;
77 if (parent.isValid())
78 parentItem = static_cast<Property*>(parent.internalPointer());
79 return parentItem->children().size();
80}
81
82int QPropertyModel::columnCount ( const QModelIndex & parent /*= QModelIndex()*/ ) const {
83 (void)parent;
84 return 2;
85}
86
87QVariant QPropertyModel::data ( const QModelIndex & index, int role /*= Qt::DisplayRole*/ ) const {
88 if (!index.isValid())
89 return QVariant();
90
91 Property *item = static_cast<Property*>(index.internalPointer());
92 switch (role) {
93 case Qt::ToolTipRole:
94 case Qt::DecorationRole:
95 case Qt::DisplayRole:
96 case Qt::EditRole:
97 if (index.column() == 0)
98 return item->objectName();
99 if (index.column() == 1)
100 return item->value(role);
101 case Qt::BackgroundRole:
102 if (item->isRoot()) return QApplication::palette("QTreeView").brush(QPalette::Normal, QPalette::Button).color();
103 break;
104 };
105 return QVariant();
106}
107
108// edit methods
109bool QPropertyModel::setData ( const QModelIndex & index, const QVariant & value, int role /*= Qt::EditRole*/ ) {
110 if (index.isValid() && role == Qt::EditRole) {
111 Property *item = static_cast<Property*>(index.internalPointer());
112 item->setValue(value);
113 emit dataChanged(index, index);
114 return true;
115 }
116 return false;
117}
118
119Qt::ItemFlags QPropertyModel::flags ( const QModelIndex & index ) const {
120 if (!index.isValid())
121 return Qt::ItemIsEnabled;
122 Property *item = static_cast<Property*>(index.internalPointer());
123 // only allow change of value attribute
124 if (item->isRoot())
125 return Qt::ItemIsEnabled;
126 else if (item->isReadOnly())
127 return Qt::ItemIsDragEnabled | Qt::ItemIsSelectable;
128 else
129 return Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
130}
131
132
133QVariant QPropertyModel::headerData ( int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/ ) const {
134 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
135 switch (section) {
136 case 0:
137 return tr("Name");
138 case 1:
139 return tr("Value");
140 }
141 }
142 return QVariant();
143}
144
145QModelIndex QPropertyModel::buddy ( const QModelIndex & index ) const {
146 if (index.isValid() && index.column() == 0)
147 return createIndex(index.row(), 1, index.internalPointer());
148 return index;
149}
150
151void QPropertyModel::addItem(QObject *propertyObject) {
152 // first create property <-> class hierarchy
153 QList<PropertyPair> propertyMap;
154 QList<const QMetaObject*> classList;
155 const QMetaObject* metaObject = propertyObject->metaObject();
156 do {
157 int count = metaObject->propertyCount();
158 for (int i=0; i<count; ++i) {
159 QMetaProperty property = metaObject->property(i);
160 if (property.isUser()) // Hide Qt specific properties
161 {
162 PropertyPair pair(metaObject, property);
163 int index = propertyMap.indexOf(pair);
164 if (index != -1)
165 propertyMap[index] = pair;
166 else
167 propertyMap.push_back(pair);
168 }
169 }
170 classList.push_front(metaObject);
171 } while ((metaObject = metaObject->superClass())!=0);
172
173 QList<const QMetaObject*> finalClassList;
174 // remove empty classes from hierarchy list
175 foreach(const QMetaObject* obj, classList) {
176 bool keep = false;
177 foreach(PropertyPair pair, propertyMap) {
178 if (pair.Object == obj) {
179 keep = true;
180 break;
181 }
182 }
183 if (keep)
184 finalClassList.push_back(obj);
185 }
186
187 // finally insert properties for classes containing them
188 int i=rowCount();
189 beginInsertRows(QModelIndex(), i, i + finalClassList.count());
190 foreach(const QMetaObject* metaObject, finalClassList) {
191 // Set default name of the hierarchy property to the class name
192 QString name = metaObject->className();
193 // Check if there is a special name for the class
194 int index = metaObject->indexOfClassInfo(qPrintable(name));
195 if (index != -1)
196 name = metaObject->classInfo(index).value();
197 // Create Property Item for class node
198 Property* propertyItem = new Property(name, 0, m_rootItem);
199 foreach(PropertyPair pair, propertyMap) {
200 // Check if the property is associated with the current class from the finalClassList
201 if (pair.Object == metaObject) {
202 QMetaProperty property(pair.Property);
203 Property* p = 0;
204 if (property.type() == QVariant::UserType && m_userCallback)
205 p = m_userCallback(property.name(), propertyObject, propertyItem);
206 else
207 p = new Property(property.name(), propertyObject, propertyItem);
208 int index = metaObject->indexOfClassInfo(property.name());
209 if (index != -1)
210 p->setEditorHints(metaObject->classInfo(index).value());
211 }
212 }
213 }
214 endInsertRows();
215}
216
217void QPropertyModel::updateItem ( QObject* propertyObject, const QModelIndex& parent /*= QModelIndex() */ ) {
218 Property *parentItem = m_rootItem;
219 if (parent.isValid())
220 parentItem = static_cast<Property*>(parent.internalPointer());
221 if (parentItem->propertyObject() != propertyObject)
222 parentItem = parentItem->findPropertyObject(propertyObject);
223 if (parentItem) // Indicate view that the data for the indices have changed
224 dataChanged(createIndex(parentItem->row(), 0, static_cast<Property*>(parentItem)), createIndex(parentItem->row(), 1, static_cast<Property*>(parentItem)));
225}
226
227void QPropertyModel::clear() {
228 beginRemoveRows(QModelIndex(), 0, rowCount());
229 delete m_rootItem;
230 m_rootItem = new Property("Root",0, this);
231 endRemoveRows();
232}
233
234void QPropertyModel::setCustomPropertyCB(QPropertyEditorWidget::UserTypeCB callback) {
235 m_userCallback = callback;
236}
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/QPropertyModel.h b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyModel.h
new file mode 100644
index 0000000000..8a52bbe87c
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/QPropertyModel.h
@@ -0,0 +1,105 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// *************************************************************************************************
24#ifndef QPROPERTYMODEL_H_
25#define QPROPERTYMODEL_H_
26
27#include <Qt/qabstractitemmodel.h>
28#include <Qt/qmap.h>
29
30#include "QPropertyEditorWidget.h"
31
32class Property;
33
34/**
35 * The QPropertyModel handles the user defined properties of QObjects
36 */
37class QPropertyModel : public QAbstractItemModel {
38 Q_OBJECT
39public:
40 /**
41 * Constructor
42 * @param parent optional parent object
43 */
44 QPropertyModel(QObject* parent = 0);
45 /// Destructor
46 virtual ~QPropertyModel();
47
48 /// QAbstractItemModel implementation
49 QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
50
51 /// QAbstractItemModel implementation
52 QModelIndex parent ( const QModelIndex & index ) const;
53 /// QAbstractItemModel implementation
54 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
55 /// QAbstractItemModel implementation
56 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
57 /// QAbstractItemModel implementation
58 QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
59
60 /// QAbstractItemModel implementation
61 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
62 /// QAbstractItemModel implementation
63 Qt::ItemFlags flags ( const QModelIndex & index ) const;
64
65 /// QAbstractItemModel implementation
66 QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
67
68 /// QAbstractItemModel implementation
69 QModelIndex buddy ( const QModelIndex & index ) const;
70
71 /**
72 * Adds the user properties of the given class to the QPropertyModel instance
73 *
74 * @param propertyObject the class inherited from QObject that contains user properties that should be
75 * managed by this instance
76 */
77 void addItem(QObject* propertyObject);
78
79 /**
80 * Creates a dataChanged signal for the given object
81 * @param propertyObject the instance of a QObject based class that should be updated
82 * @param parent optional model index the propertyObject is child of
83 */
84 void updateItem ( QObject* propertyObject, const QModelIndex& parent = QModelIndex() ) ;
85
86 /**
87 * Removes all objects from the model
88 */
89 void clear();
90
91 /**
92 * Sets custom callback that will be used to create Property instances for custom datatypes
93 */
94 void setCustomPropertyCB(QPropertyEditorWidget::UserTypeCB callback);
95
96private:
97
98 /// The Root Property for all objects
99 Property* m_rootItem;
100
101 /// Custom callback
102 QPropertyEditorWidget::UserTypeCB m_userCallback;
103
104};
105#endif
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp b/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp
new file mode 100644
index 0000000000..ebda9b2c31
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.cpp
@@ -0,0 +1,105 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// *************************************************************************************************
24
25#include "QVariantDelegate.h"
26
27#include "Property.h"
28
29#include <Qt/qabstractitemview.h>
30
31
32QVariantDelegate::QVariantDelegate(QObject* parent) : QItemDelegate(parent) {}
33
34
35QVariantDelegate::~QVariantDelegate() {}
36
37QWidget *QVariantDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option , const QModelIndex & index ) const {
38 QWidget* editor = 0;
39 Property* p = static_cast<Property*>(index.internalPointer());
40 switch (p->value().type()) {
41 case QVariant::Color:
42 case QVariant::Int:
43 case QMetaType::Float:
44 case QVariant::Double:
45 case QVariant::UserType:
46 editor = p->createEditor(parent, option);
47 if (editor) break; // if no editor could be created take default case
48 default:
49 editor = QItemDelegate::createEditor(parent, option, index);
50 }
51 parseEditorHints(editor, p->editorHints());
52 return editor;
53}
54
55void QVariantDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
56 QVariant data = index.model()->data(index, Qt::EditRole);
57 switch (data.type()) {
58 case QVariant::Color:
59 case QMetaType::Double:
60 case QMetaType::Float:
61 case QVariant::UserType:
62 if (static_cast<Property*>(index.internalPointer())->setEditorData(editor, data)) // if editor couldn't be recognized use default
63 break;
64 default:
65 QItemDelegate::setEditorData(editor, index);
66 break;
67 }
68}
69
70void QVariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
71 QVariant data = index.model()->data(index, Qt::EditRole);
72 switch (data.type()) {
73 case QVariant::Color:
74 case QMetaType::Double:
75 case QMetaType::Float:
76 case QVariant::UserType: {
77 QVariant data = static_cast<Property*>(index.internalPointer())->editorData(editor);
78 if (data.isValid()) {
79 model->setData(index, data , Qt::EditRole);
80 break;
81 }
82 }
83 default:
84 QItemDelegate::setModelData(editor, model, index);
85 break;
86 }
87}
88
89void QVariantDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index ) const {
90 return QItemDelegate::updateEditorGeometry(editor, option, index);
91}
92
93void QVariantDelegate::parseEditorHints(QWidget* editor, const QString& editorHints) const {
94 if (editor && !editorHints.isEmpty()) {
95 // Parse for property values
96 QRegExp rx("(.*)(=\\s*)(.*)(;{1})");
97 rx.setMinimal(true);
98 int pos = 0;
99 while ((pos = rx.indexIn(editorHints, pos)) != -1) {
100 qDebug("Setting %s to %s", qPrintable(rx.cap(1)), qPrintable(rx.cap(3)));
101 editor->setProperty(qPrintable(rx.cap(1).trimmed()), rx.cap(3).trimmed());
102 pos += rx.matchedLength();
103 }
104 }
105}
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.h b/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.h
new file mode 100644
index 0000000000..e06265af82
--- /dev/null
+++ b/utils/wpseditor/gui/src/QPropertyEditor/QVariantDelegate.h
@@ -0,0 +1,78 @@
1// *************************************************************************************************
2//
3// QPropertyEditor v 0.1
4//
5// --------------------------------------
6// Copyright (C) 2007 Volker Wiendl
7//
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or any later version.
13//
14// This library 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 GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// *************************************************************************************************
24
25#ifndef COLORSELECTIONBUTTON_H_
26#define COLORSELECTIONBUTTON_H_
27
28#include <Qt/qitemdelegate.h>
29
30
31/**
32 * This class is used to create the editor widgets for datatypes encapsulated in QVariant variables
33 */
34class QVariantDelegate : public QItemDelegate {
35 Q_OBJECT
36
37public:
38 /**
39 * Constructor
40 * @param parent optional parent object
41 */
42 QVariantDelegate(QObject* parent = 0);
43 /// Destructor
44 virtual ~QVariantDelegate();
45
46 /**
47 * Creates an editor widget as child of a given widget for a specific QModelIndex
48 *
49 * @param parent the parent widget for the editor
50 * @param option some style options that the editor should use
51 * @param index the index of the item the editor will be created for
52 * @return QWidget the editor widget
53 */
54 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
55
56 /**
57 * Tries to set the editor data based on the value stored at a specific QModelIndex
58 * @param editor the editor widget
59 * @param index the model index of the value that should be used in the editor
60 */
61 virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
62
63 /**
64 * Sets the data of a specific QModelIndex to tha value of the editor widget
65 * @param editor the editor widget that contains the new value
66 * @param model the model that contains the index
67 * @param index the index within the model whose data value should be set to the data value of the editor
68 */
69 virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
70
71 /// QItemDelegate implementation
72 virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
73
74protected:
75 void parseEditorHints(QWidget* editor, const QString& editorHints) const;
76
77};
78#endif