-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPropertyBrowser.cpp
256 lines (211 loc) · 8.6 KB
/
PropertyBrowser.cpp
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
// MapMaker (c) 2016 Andrey Fidrya. MIT license. See LICENSE for more information.
#include <QMouseEvent>
#include <QMenu>
#include <ThirdParty/qtpropertybrowser/src/QtStringPropertyManager>
#include <ThirdParty/qtpropertybrowser/src/QtIntPropertyManager>
#include <ThirdParty/qtpropertybrowser/src/QtBoolPropertyManager>
#include <ThirdParty/qtpropertybrowser/src/QtGroupPropertyManager>
#include <ThirdParty/qtpropertybrowser/src/QtCheckBoxFactory>
#include <ThirdParty/qtpropertybrowser/src/QtSpinBoxFactory>
#include "PropertyBrowser.h"
#include "Data/LevelObject.h"
#include "Dialogs/NewPropertyDialog/NewPropertyDialog.h"
#include "Utils/Utils.h"
PropertyBrowser::PropertyBrowser(QWidget *parent)
: QtTreePropertyBrowser(parent)
{
groupManager_ = new QtGroupPropertyManager(this);
intManager_ = new QtIntPropertyManager(this);
connect(intManager_, SIGNAL(valueChanged(QtProperty*,int)),
this, SLOT(onIntValueChanged(QtProperty*,int)));
boolManager_ = new QtBoolPropertyManager(this);
connect(boolManager_, SIGNAL(valueChanged(QtProperty*,bool)),
this, SLOT(onBoolValueChanged(QtProperty*,bool)));
stringManager_ = new QtStringPropertyManager(this);
connect(stringManager_, SIGNAL(valueChanged(QtProperty*,QString)),
this, SLOT(onStringValueChanged(QtProperty*,QString)));
customPropertyManager_ = new QtStringPropertyManager(this);
connect(customPropertyManager_, SIGNAL(valueChanged(QtProperty*,QString)),
this, SLOT(onCustomPropertyValueChanged(QtProperty*,QString)));
QtCheckBoxFactory *checkBoxFactory = new QtCheckBoxFactory(this);
QtSpinBoxFactory *spinBoxFactory = new QtSpinBoxFactory(this);
QtLineEditFactory *lineEditFactory = new QtLineEditFactory(this);
setFactoryForManager(boolManager_, checkBoxFactory);
setFactoryForManager(intManager_, spinBoxFactory);
setFactoryForManager(stringManager_, lineEditFactory);
setFactoryForManager(customPropertyManager_, lineEditFactory);
// Standard properties
standardGroup_ = groupManager_->addProperty("Standard");
name_ = stringManager_->addProperty("name");
name_->setToolTip("Object name");
name_->setEnabled(false);
standardGroup_->addSubProperty(name_);
x_ = intManager_->addProperty("x");
x_->setToolTip("X coordinate");
standardGroup_->addSubProperty(x_);
y_ = intManager_->addProperty("y");
y_->setToolTip("Y coordinate");
standardGroup_->addSubProperty(y_);
width_ = intManager_->addProperty("width");
width_->setToolTip("Width");
width_->setEnabled(false);
standardGroup_->addSubProperty(width_);
height_ = intManager_->addProperty("height");
height_->setToolTip("Height");
height_->setEnabled(false);
standardGroup_->addSubProperty(height_);
flipX_ = boolManager_->addProperty("flipX");
flipX_->setToolTip("Flips the object horizontally");
standardGroup_->addSubProperty(flipX_);
flipY_ = boolManager_->addProperty("flipY");
flipY_->setToolTip("Flips the object vertically");
standardGroup_->addSubProperty(flipY_);
// Custom properties
customGroup_ = groupManager_->addProperty("Custom");
// Final setup
//addProperty(standardGroup);
//addProperty(customGroup);
QAction *addPropertyAction = new QAction(tr("&Add property"), this);
addPropertyAction->setEnabled(false);
connect(addPropertyAction, SIGNAL(triggered(bool)),
this, SLOT(onAddProperty()));
connect(this, SIGNAL(hasLevelObject(bool)),
addPropertyAction, SLOT(setEnabled(bool)));
addAction(addPropertyAction);
setContextMenuPolicy(Qt::ActionsContextMenu);
}
void PropertyBrowser::setLevelObject(LevelObject *object)
{
if (levelObject_)
disconnectFromPropertiesOf(levelObject_);
levelObject_ = object;
// Remove all properties from browser
clear();
// Delete custom properties because they're recreated
// for different levelObjects
foreach (QtProperty *property, customGroup_->subProperties()) {
customGroup_->removeSubProperty(property);
delete property;
}
if (levelObject_)
connectToPropertiesOf(levelObject_);
emit hasLevelObject(levelObject_ != nullptr);
}
void PropertyBrowser::resetLevelObject()
{
setLevelObject(nullptr);
}
void PropertyBrowser::onAddProperty()
{
NewPropertyDialog *dialog = new NewPropertyDialog(this);
dialog->setWindowModality(Qt::WindowModal);
dialog->setWindowFlags((dialog->windowFlags() | Qt::CustomizeWindowHint) & ~Qt::WindowMaximizeButtonHint);
if (QDialog::Accepted == dialog->exec() && levelObject_) {
QtProperty *property = customPropertyManager_->addProperty(
dialog->key());
customGroup_->addSubProperty(property);
customPropertyManager_->setValue(property, dialog->value());
}
delete dialog;
}
void PropertyBrowser::onIntValueChanged(QtProperty *property, int val)
{
if (property->propertyName() == "x")
levelObject_->setX(val + levelObject_->size().width() / 2);
else if (property->propertyName() == "y")
levelObject_->setY(val + levelObject_->size().height() / 2);
else if (property->propertyName() == "width")
levelObject_->setWidth(val);
else if (property->propertyName() == "height")
levelObject_->setHeight(val);
}
void PropertyBrowser::onBoolValueChanged(QtProperty *property, bool val)
{
// TODO: evaluate possibility of using Q_PROPERTIES for automating this
if (property->propertyName() == "flipX")
levelObject_->setFlipX(val);
else if (property->propertyName() == "flipY")
levelObject_->setFlipY(val);
}
void PropertyBrowser::onStringValueChanged(QtProperty *property, const QString &val)
{
if (property->propertyName() == "name")
levelObject_->setName(val);
}
void PropertyBrowser::onCustomPropertyValueChanged(QtProperty *property, const QString &val)
{
levelObject_->setCustomProperty(property->propertyName(),
val);
}
void PropertyBrowser::updateName(const QString &name)
{
stringManager_->setValue(name_, name);
}
void PropertyBrowser::updatePosition(const QPointF &pos)
{
LevelObject *obj = qobject_cast<LevelObject *>(sender());
Q_ASSERT(obj);
updatePosition(obj, pos);
}
void PropertyBrowser::updatePosition(LevelObject *levelObject, const QPointF &pos)
{
intManager_->setValue(x_, pos.x() - levelObject->size().width() / 2);
intManager_->setValue(y_, pos.y() - levelObject->size().height() / 2);
}
void PropertyBrowser::updateSize(const QSizeF &size)
{
intManager_->setValue(width_, size.width());
intManager_->setValue(height_, size.height());
}
void PropertyBrowser::updateFlipX(bool flipX)
{
boolManager_->setValue(flipX_, flipX);
}
void PropertyBrowser::updateFlipY(bool flipY)
{
boolManager_->setValue(flipY_, flipY);
}
void PropertyBrowser::onCustomPropertyChanged(const QString &key, const QString &value)
{
foreach (QtProperty *property, customGroup_->subProperties()) {
if (property->propertyName() != key)
continue;
customPropertyManager_->setValue(property, value);
}
}
void PropertyBrowser::connectToPropertiesOf(LevelObject *object)
{
addProperty(standardGroup_);
addProperty(customGroup_);
LevelObject::Properties properties = object->customProperties();
for (LevelObject::Properties::iterator i = properties.begin();
i != properties.end(); ++i) {
QtProperty *property = customPropertyManager_->addProperty(i.key());
customGroup_->addSubProperty(property);
customPropertyManager_->setValue(property, i.value());
}
connect(object, SIGNAL(nameChanged(QString)),
this, SLOT(updateName(QString)));
updateName(levelObject_->name());
connect(object, SIGNAL(positionChanged(QPointF)),
this, SLOT(updatePosition(QPointF)));
updatePosition(object, levelObject_->position());
connect(object, SIGNAL(sizeChanged(QSizeF)),
this, SLOT(updateSize(QSizeF)));
updateSize(levelObject_->size());
connect(object, SIGNAL(flipXChanged(bool)),
this, SLOT(updateFlipX(bool)));
updateFlipX(levelObject_->flipX());
connect(object, SIGNAL(flipYChanged(bool)),
this, SLOT(updateFlipY(bool)));
updateFlipY(levelObject_->flipY());
connect(object, SIGNAL(customPropertyChanged(QString,QString)),
this, SLOT(onCustomPropertyChanged(QString,QString)));
connect(object, SIGNAL(destroyed(QObject*)),
this, SLOT(resetLevelObject()));
}
void PropertyBrowser::disconnectFromPropertiesOf(LevelObject *object)
{
disconnect(object, nullptr,
this, nullptr);
}