23 Model::EditContext::EditContext(Model& model) : |
23 Model::EditContext::EditContext(Model& model) : |
24 storedModel{model} |
24 storedModel{model} |
25 { |
25 { |
26 } |
26 } |
27 |
27 |
|
28 void Model::EditContext::markObjectAsModified(ldraw::id_t id, ldraw::Object *object) |
|
29 { |
|
30 if (not this->modifiedObjects.contains(id)) |
|
31 { |
|
32 QDataStream stream{&this->modifiedObjects[id], QIODevice::WriteOnly}; |
|
33 object->serialize(stream); |
|
34 } |
|
35 } |
|
36 |
28 Model::EditContext::~EditContext() |
37 Model::EditContext::~EditContext() |
29 { |
38 { |
30 for (ldraw::id_t id : this->modifiedObjects) |
39 mapapply(this->modifiedObjects, [&](MAP_CPARMS(this->modifiedObjects)) |
31 { |
40 { |
32 this->model().objectModified(id); |
41 ldraw::Object* object = this->model().objectAt(key); |
33 } |
42 if (object != nullptr) |
|
43 { |
|
44 QByteArray newState; |
|
45 QDataStream stream{&newState, QIODevice::WriteOnly}; |
|
46 object->serialize(stream); |
|
47 const int position = this->model().lookup(key).row(); |
|
48 Q_EMIT this->model().objectStateChanged(position, value, newState); |
|
49 } |
|
50 }); |
34 this->model().editFinished(); |
51 this->model().editFinished(); |
35 } |
52 } |
36 |
53 |
37 ldraw::id_t Model::EditContext::append(std::unique_ptr<ldraw::Object>&& object) |
54 ldraw::id_t Model::EditContext::append(std::unique_ptr<ldraw::Object>&& object) |
38 { |
55 { |
39 const ldraw::id_t id = object->id; |
56 const ldraw::id_t id = object->id; |
40 this->model().objectsById[id] = object.get(); |
57 this->model().objectsById[id] = object.get(); |
41 this->model().append(std::move(object)); |
58 this->model().append(std::move(object)); |
42 return id; |
59 return id; |
|
60 } |
|
61 |
|
62 void Model::EditContext::insertFromState(int position, QByteArray &state) |
|
63 { |
|
64 int typeInteger; |
|
65 QDataStream stream{&state, QIODevice::ReadOnly}; |
|
66 stream >> typeInteger; |
|
67 switch (static_cast<ldraw::Object::Type>(typeInteger)) |
|
68 { |
|
69 case ldraw::Object::Type::Empty: |
|
70 this->storedModel.insert<ldraw::Empty>(position); |
|
71 break; |
|
72 } |
43 } |
73 } |
44 |
74 |
45 void Model::EditContext::remove(int position) |
75 void Model::EditContext::remove(int position) |
46 { |
76 { |
47 this->model().remove(position); |
77 this->model().remove(position); |
54 -> ldraw::Object::SetPropertyResult |
84 -> ldraw::Object::SetPropertyResult |
55 { |
85 { |
56 ldraw::Object* const object = this->model().objectAt(id); |
86 ldraw::Object* const object = this->model().objectAt(id); |
57 if (object != nullptr) |
87 if (object != nullptr) |
58 { |
88 { |
|
89 this->markObjectAsModified(id, object); |
59 const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value}); |
90 const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value}); |
60 modifiedObjects.insert(id); |
|
61 return result; |
91 return result; |
62 } |
92 } |
63 else |
93 else |
64 { |
94 { |
65 return ldraw::Object::SetPropertyResult::PropertyNotHandled; |
95 return ldraw::Object::SetPropertyResult::PropertyNotHandled; |
69 void Model::EditContext::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) |
99 void Model::EditContext::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) |
70 { |
100 { |
71 ldraw::Object* object = this->model().objectAt(id); |
101 ldraw::Object* object = this->model().objectAt(id); |
72 if (object != nullptr) |
102 if (object != nullptr) |
73 { |
103 { |
|
104 this->markObjectAsModified(id, object); |
74 object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)}); |
105 object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)}); |
75 modifiedObjects.insert(id); |
|
76 } |
106 } |
77 } |
107 } |
78 |
108 |
79 void Model::EditContext::invertObject(ldraw::id_t id) |
109 void Model::EditContext::invertObject(ldraw::id_t id) |
80 { |
110 { |
81 auto it = this->model().objectsById.find(id); |
111 auto it = this->model().objectsById.find(id); |
82 if (it != this->model().objectsById.end()) |
112 if (it != this->model().objectsById.end()) |
83 { |
113 { |
84 ldraw::Object* object = it->second; |
114 ldraw::Object* object = it->second; |
|
115 this->markObjectAsModified(id, object); |
85 object->invert(); |
116 object->invert(); |
|
117 } |
|
118 } |
|
119 |
|
120 void Model::EditContext::deserialize(ldraw::id_t id, QDataStream &stream) |
|
121 { |
|
122 auto it = this->model().objectsById.find(id); |
|
123 if (it != this->model().objectsById.end()) |
|
124 { |
|
125 ldraw::Object* object = it->second; |
|
126 int typeInteger; |
|
127 stream >> typeInteger; |
|
128 const ldraw::Object::Type type = static_cast<ldraw::Object::Type>(typeInteger); |
|
129 if (object->typeIdentifier() == type) |
|
130 { |
|
131 this->markObjectAsModified(id, object); |
|
132 object->deserialize(stream); |
|
133 } |
|
134 } |
|
135 } |
|
136 |
|
137 ldraw::id_t Model::EditContext::resolve(int position) const |
|
138 { |
|
139 if (position >= 0 and position < this->storedModel.rowCount({})) |
|
140 { |
|
141 return this->storedModel.objectAt(this->storedModel.index(position))->id; |
|
142 } |
|
143 else |
|
144 { |
|
145 return ldraw::NULL_ID; |
86 } |
146 } |
87 } |
147 } |
88 |
148 |
89 Model& Model::EditContext::model() |
149 Model& Model::EditContext::model() |
90 { |
150 { |