-
Notifications
You must be signed in to change notification settings - Fork 20
/
QtPropertyEditor.h
316 lines (260 loc) · 13.1 KB
/
QtPropertyEditor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* --------------------------------------------------------------------------------
* QObject property editor UI.
*
* Author: Marcel Paz Goldschen-Ohm
* Email: [email protected]
* -------------------------------------------------------------------------------- */
#ifndef __QtPropertyEditor_H__
#define __QtPropertyEditor_H__
#include <functional>
#include <QAbstractItemModel>
#include <QAction>
#include <QByteArray>
#include <QDialog>
#include <QDialogButtonBox>
#include <QHash>
#include <QList>
#include <QMetaProperty>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QStyledItemDelegate>
#include <QTableView>
#include <QTreeView>
#include <QVariant>
#include <QVBoxLayout>
#ifdef DEBUG
#include <iostream>
#include <QDebug>
#endif
namespace QtPropertyEditor
{
// List all object property names.
QList<QByteArray> getPropertyNames(QObject *object);
QList<QByteArray> getMetaPropertyNames(const QMetaObject &metaObject);
QList<QByteArray> getNoninheritedPropertyNames(QObject *object);
// Handle descendant properties such as "child.grandchild.property".
QObject* descendant(QObject *object, const QByteArray &pathToDescendantObject);
// Get the size of a QTableView widget.
QSize getTableSize(const QTableView *table);
/* --------------------------------------------------------------------------------
* Things that all QObject property models should be able to do.
* -------------------------------------------------------------------------------- */
class QtAbstractPropertyModel : public QAbstractItemModel
{
Q_OBJECT
public:
QtAbstractPropertyModel(QObject *parent = 0) : QAbstractItemModel(parent) {}
QList<QByteArray> propertyNames;
QHash<QByteArray, QString> propertyHeaders;
void setProperties(const QString &str);
void addProperty(const QString &str);
virtual QObject* objectAtIndex(const QModelIndex &index) const = 0;
virtual QByteArray propertyNameAtIndex(const QModelIndex &index) const = 0;
const QMetaProperty metaPropertyAtIndex(const QModelIndex &index) const;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
};
/* --------------------------------------------------------------------------------
* Property tree model for a QObject tree.
* Max tree depth can be specified (i.e. depth = 0 --> single object only).
* -------------------------------------------------------------------------------- */
class QtPropertyTreeModel : public QtAbstractPropertyModel
{
Q_OBJECT
public:
// Internal tree node.
struct Node
{
// Node traversal.
Node *parent = NULL;
QList<Node*> children;
// Node data.
QObject *object = NULL;
QByteArray propertyName;
Node(Node *parent = NULL) : parent(parent) {}
~Node() { qDeleteAll(children); }
void setObject(QObject *object, int maxChildDepth = -1, const QList<QByteArray> &propertyNames = QList<QByteArray>());
};
QtPropertyTreeModel(QObject *parent = NULL) : QtAbstractPropertyModel(parent) {}
// Getters.
QObject* object() const { return _root.object; }
int maxDepth() const { return _maxTreeDepth; }
// Setters.
void setObject(QObject *object) { beginResetModel(); _root.setObject(object, _maxTreeDepth, propertyNames); endResetModel(); }
void setMaxDepth(int i) { beginResetModel(); _maxTreeDepth = i; reset(); endResetModel(); }
void setProperties(const QString &str) { beginResetModel(); QtAbstractPropertyModel::setProperties(str); reset(); endResetModel(); }
void addProperty(const QString &str) { beginResetModel(); QtAbstractPropertyModel::addProperty(str); reset(); endResetModel(); }
// Model interface.
Node* nodeAtIndex(const QModelIndex &index) const;
QObject* objectAtIndex(const QModelIndex &index) const;
QByteArray propertyNameAtIndex(const QModelIndex &index) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
public slots:
void reset() { setObject(object()); }
protected:
Node _root;
int _maxTreeDepth = -1;
};
/* --------------------------------------------------------------------------------
* Property table model for a list of QObjects (rows are objects, columns are properties).
* -------------------------------------------------------------------------------- */
class QtPropertyTableModel : public QtAbstractPropertyModel
{
Q_OBJECT
public:
typedef std::function<QObject*()> ObjectCreatorFunction;
QtPropertyTableModel(QObject *parent = NULL) : QtAbstractPropertyModel(parent) {}
// Getters.
QObjectList objects() const { return _objects; }
ObjectCreatorFunction objectCreator() const { return _objectCreator; }
// Setters.
void setObjects(const QObjectList &objects) { beginResetModel(); _objects = objects; endResetModel(); }
template <class T>
void setObjects(const QList<T*> &objects);
template <class T>
void setChildObjects(QObject *parent);
void setObjectCreator(ObjectCreatorFunction creator) { _objectCreator = creator; }
void setProperties(const QString &str) { beginResetModel(); QtAbstractPropertyModel::setProperties(str); endResetModel(); }
void addProperty(const QString &str) { beginResetModel(); QtAbstractPropertyModel::addProperty(str); endResetModel(); }
// Model interface.
QObject* objectAtIndex(const QModelIndex &index) const;
QByteArray propertyNameAtIndex(const QModelIndex &index) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationRow);
void reorderChildObjectsToMatchRowOrder(int firstRow = 0);
// Default creator functions for convenience.
// Requires template class T to implement a default constructor T().
template <class T>
static QObject* defaultCreator() { return new T(); }
template <class T>
static QObject* defaultChildCreator(QObject *parent) { T *object = new T(); object->setParent(parent); return object; }
signals:
void rowCountChanged();
void rowOrderChanged();
protected:
QObjectList _objects;
ObjectCreatorFunction _objectCreator = NULL;
};
template <class T>
void QtPropertyTableModel::setObjects(const QList<T*> &objects)
{
beginResetModel();
_objects.clear();
foreach(T *object, objects) {
if(QObject *obj = qobject_cast<QObject*>(object))
_objects.append(obj);
}
endResetModel();
}
template <class T>
void QtPropertyTableModel::setChildObjects(QObject *parent)
{
beginResetModel();
_objects.clear();
foreach(T *derivedObject, parent->findChildren<T*>(QString(), Qt::FindDirectChildrenOnly)) {
if(QObject *object = qobject_cast<QObject*>(derivedObject))
_objects.append(object);
}
_objectCreator = std::bind(&QtPropertyTableModel::defaultChildCreator<T>, parent);
endResetModel();
}
/* --------------------------------------------------------------------------------
* Property editor delegate.
* -------------------------------------------------------------------------------- */
class QtPropertyDelegate: public QStyledItemDelegate
{
public:
QtPropertyDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE;
QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
protected:
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) Q_DECL_OVERRIDE;
};
/* --------------------------------------------------------------------------------
* User types for QVariant that will be handled by QtPropertyDelegate.
* User types need to be declared via Q_DECLARE_METATYPE (see below outside of namespace)
* and also registered via qRegisterMetaType (see static instantiation in .cpp file)
* -------------------------------------------------------------------------------- */
// For static registration of user types (see static instantiation in QtPropertyEditor.cpp).
template <typename Type> class MetaTypeRegistration
{
public:
inline MetaTypeRegistration()
{
qRegisterMetaType<Type>();
}
};
// For push buttons.
// See Q_DECLARE_METATYPE below and qRegisterMetaType in .cpp file.
class QtPushButtonActionWrapper
{
public:
QtPushButtonActionWrapper(QAction *action = NULL) : action(action) {}
QtPushButtonActionWrapper(const QtPushButtonActionWrapper &other) { action = other.action; }
~QtPushButtonActionWrapper() {}
QAction *action = NULL;
};
/* --------------------------------------------------------------------------------
* Tree editor for properties in a QObject tree.
* -------------------------------------------------------------------------------- */
class QtPropertyTreeEditor : public QTreeView
{
Q_OBJECT
public:
QtPropertyTreeEditor(QWidget *parent = NULL);
// Owns its own tree model for convenience. This means model will be deleted along with editor.
// However, you're not forced to use this model.
QtPropertyTreeModel treeModel;
public slots:
void resizeColumnsToContents();
protected:
QtPropertyDelegate _delegate;
};
/* --------------------------------------------------------------------------------
* Table editor for properties in a list of QObjects.
* -------------------------------------------------------------------------------- */
class QtPropertyTableEditor : public QTableView
{
Q_OBJECT
public:
QtPropertyTableEditor(QWidget *parent = NULL);
// Owns its own table model for convenience. This means model will be deleted along with editor.
// However, you're not forced to use this model.
QtPropertyTableModel tableModel;
bool isDynamic() const { return _isDynamic; }
void setIsDynamic(bool b);
QSize sizeHint() const Q_DECL_OVERRIDE { return getTableSize(this); }
public slots:
void horizontalHeaderContextMenu(QPoint pos);
void verticalHeaderContextMenu(QPoint pos);
void appendRow();
void insertSelectedRows();
void removeSelectedRows();
void handleSectionMove(int logicalIndex, int oldVisualIndex, int newVisualIndex);
protected:
QtPropertyDelegate _delegate;
bool _isDynamic = true;
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
bool eventFilter(QObject* o, QEvent* e) Q_DECL_OVERRIDE;
};
} // QtPropertyEditor
Q_DECLARE_METATYPE(QtPropertyEditor::QtPushButtonActionWrapper);
#endif // __QtPropertyEditor_H__