-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLightModel.cpp
More file actions
238 lines (206 loc) · 6.11 KB
/
LightModel.cpp
File metadata and controls
238 lines (206 loc) · 6.11 KB
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
#include "LightModel.h"
#include <QJSValue>
#include <algorithm>
LightModel::LightModel(QObject* parent) : QAbstractListModel(parent)
{
}
int LightModel::updateCounter() const
{
return counter;
}
void LightModel::setUpdateCounter(int value)
{
if (counter != value)
{
counter = value;
emit updateCounterChanged(value);
}
}
void LightModel::append(std::unique_ptr<Light> light)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
lights.push_back(std::move(light));
endInsertRows();
}
void LightModel::append(QVariant light)
{
if (!light.canConvert<QJSValue>())
{
return;
}
QJSValue object = light.value<QJSValue>();
std::unique_ptr<Light> newLight = std::make_unique<Light>();
newLight->setName(object.property("name").toString());
append(std::move(newLight));
}
void LightModel::remove(quint32 index)
{
removeRows(index, 1);
}
bool LightModel::setLightProperty(int lightIndex, const QVariant& value, LightRole role)
{
if (lightIndex < 0 || lightIndex >= static_cast<int>(lights.size()))
{
return false;
}
Light& light = *lights[lightIndex];
if (role == Name && value.canConvert<QString>())
{
light.setName(value.value<QString>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == Position && value.canConvert<QVector3D>())
{
light.setPosition(value.value<QVector3D>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == Direction && value.canConvert<QVector3D>())
{
light.setDirection(value.value<QVector3D>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == LightType && value.canConvert<Light::LightType>())
{
light.setLightType(value.value<Light::LightType>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == Visibility && value.canConvert<bool>())
{
light.setVisibility(value.value<bool>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == Color && value.canConvert<QColor>())
{
light.setColor(value.value<QColor>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == Brightness && value.canConvert<float>())
{
light.setBrightness(value.value<float>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == AttenuationRadius && value.canConvert<float>())
{
light.setAttenuationRadius(value.value<float>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == InnerConeAngle && value.canConvert<float>())
{
light.setInnerConeAngle(value.value<float>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
else if (role == OuterConeAngle && value.canConvert<float>())
{
light.setOuterConeAngle(value.value<float>());
emit dataChanged(index(lightIndex), index(lightIndex), QVector<int>({role}));
setUpdateCounter(counter + 1);
return true;
}
return false;
}
QVariant LightModel::getLightProperty(int lightIndex, LightRole role) const
{
if (lightIndex < 0 || lightIndex >= static_cast<int>(lights.size()))
{
return QVariant();
}
const Light& light = *lights[lightIndex];
if (role == Name)
{
return light.name();
}
else if (role == Position)
{
return light.position();
}
else if (role == Direction)
{
return light.direction();
}
else if (role == LightType)
{
return light.lightType();
}
else if (role == Visibility)
{
return light.visibility();
}
else if (role == Color)
{
return light.color();
}
else if (role == Brightness)
{
return light.brightness();
}
else if (role == AttenuationRadius)
{
return light.attenuationRadius();
}
else if (role == InnerConeAngle)
{
return light.innerConeAngle();
}
else if (role == OuterConeAngle)
{
return light.outerConeAngle();
}
return QVariant();
}
int LightModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return lights.size();
}
QVariant LightModel::data(const QModelIndex& index, int role) const
{
return getLightProperty(index.row(), static_cast<LightRole>(role));
}
bool LightModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
return setLightProperty(index.row(), value, static_cast<LightRole>(role));
}
bool LightModel::removeRows(int row, int count, const QModelIndex& parent)
{
if (row < 0 || row + count > static_cast<int>(lights.size()))
{
return false;
}
beginRemoveRows(parent, row, row + count - 1);
lights.erase(lights.begin() + row, lights.begin() + row + count);
endRemoveRows();
return true;
}
QHash<int, QByteArray> LightModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Name] = "name";
roles[Position] = "position";
roles[Direction] = "direction";
roles[LightType] = "lightType";
roles[Visibility] = "visibility";
roles[Color] = "color";
roles[Brightness] = "brightness";
roles[AttenuationRadius] = "attenuationRadius";
roles[InnerConeAngle] = "innerConeAngle";
roles[OuterConeAngle] = "outerConeAngle";
return roles;
}