41 int size() const; |
41 int size() const; |
42 ldraw::id_t at(int index) const; |
42 ldraw::id_t at(int index) const; |
43 EditContext edit(); |
43 EditContext edit(); |
44 int rowCount(const QModelIndex&) const override; |
44 int rowCount(const QModelIndex&) const override; |
45 QVariant data(const QModelIndex& index, int role) const override; |
45 QVariant data(const QModelIndex& index, int role) const override; |
46 QVariant getHeaderProperty(const HeaderProperty property); |
46 ldraw::Object* findObjectById(const ldraw::id_t id); |
47 QVariant getObjectProperty(const int index, const ldraw::Property property) const; |
47 const ldraw::Object* findObjectById(const ldraw::id_t id) const; |
48 template<ldraw::Property Property> |
|
49 ldraw::PropertyType<Property> getObjectProperty(const ldraw::id_t id) const; |
|
50 QModelIndex find(ldraw::id_t id) const; |
48 QModelIndex find(ldraw::id_t id) const; |
51 ldraw::id_t idAt(const QModelIndex& index) const; |
49 ldraw::id_t idAt(const QModelIndex& index) const; |
52 template<typename R> |
50 template<typename R> |
53 const R* get(ldraw::Id<R> id) const; |
51 const R* get(ldraw::Id<R> id) const; |
54 template<typename R> |
52 template<typename R> |
57 QModelIndex index; |
55 QModelIndex index; |
58 const R* object; |
56 const R* object; |
59 }; |
57 }; |
60 template<typename R> |
58 template<typename R> |
61 Get2Result<R> get2(ldraw::Id<R> id) const; |
59 Get2Result<R> get2(ldraw::Id<R> id) const; |
62 template<typename R, typename Fn> |
|
63 void apply(Fn f) const; |
|
64 void save(QIODevice *device) const; |
|
65 ldraw::Object* operator[](int index); |
60 ldraw::Object* operator[](int index); |
|
61 const ldraw::Object* operator[](int index) const; |
66 Q_SIGNALS: |
62 Q_SIGNALS: |
67 void objectAdded(int position); |
63 void objectAdded(int position); |
68 void objectModified(int position); |
64 void objectModified(int position); |
69 void objectRemoved(ldraw::id_t id); |
65 void objectRemoved(ldraw::id_t id); |
70 private: |
66 private: |
73 ldraw::Id<T> append(Args&&... args); |
69 ldraw::Id<T> append(Args&&... args); |
74 void append(ModelObjectPointer&& object); |
70 void append(ModelObjectPointer&& object); |
75 template<typename T, typename... Args> |
71 template<typename T, typename... Args> |
76 ldraw::Id<T> insert(std::size_t position, Args&&... args); |
72 ldraw::Id<T> insert(std::size_t position, Args&&... args); |
77 void remove(int position); |
73 void remove(int position); |
78 ldraw::Object* objectAt(const QModelIndex& index); |
|
79 const ldraw::Object* objectAt(const QModelIndex& index) const; |
|
80 template<typename T> |
|
81 T* objectAt(ldraw::Id<T> id); |
|
82 template<typename T> |
|
83 const T* objectAt(ldraw::Id<T> id) const; |
|
84 void editFinished(); |
74 void editFinished(); |
85 void objectModified(ldraw::id_t id); |
75 void objectModified(ldraw::id_t id); |
86 bool modified = false; |
76 bool modified = false; |
87 LDHeader header; |
|
88 std::vector<ModelObjectPointer> body; |
77 std::vector<ModelObjectPointer> body; |
89 std::map<ldraw::id_t, ldraw::Object*> objectsById; |
78 std::map<ldraw::id_t, ldraw::Object*> objectsById; |
90 /** |
79 /** |
91 * @brief Amount of model edit contexts active |
80 * @brief Amount of model edit contexts active |
92 */ |
81 */ |
93 int editCounter = 0; |
82 int editCounter = 0; |
94 }; |
83 }; |
95 |
84 |
96 void makeUnofficial(Model& model); |
85 void makeUnofficial(Model& model); |
|
86 void save(const Model& model, QIODevice *device); |
97 |
87 |
98 /** |
88 /** |
99 * @brief Calls the specified function to all matching objects in the model |
89 * @brief Calls the specified function to all matching objects in the model |
100 * @tparam R Type of LDraw line type object to filter by |
90 * @tparam R Type of LDraw line type object to filter by |
101 * @param fn Function to call. |
91 * @param fn Function to call. |
102 */ |
92 */ |
103 template<typename R, typename Fn> |
93 template<typename R, typename Fn> |
104 void Model::apply(Fn f) const |
94 void applyToModel(const Model& model, Fn&& f) |
105 { |
95 { |
106 for (const ModelObjectPointer& object : this->body) |
96 for (int i = 0; i < model.size(); i += 1) |
107 { |
97 { |
108 const R* subobject = dynamic_cast<const R*>(object.get()); |
98 const ldraw::Object* object = model[i]; |
|
99 const R* subobject = dynamic_cast<const R*>(object); |
109 if (subobject != nullptr) |
100 if (subobject != nullptr) |
110 { |
101 { |
111 f(subobject); |
102 f(subobject); |
112 } |
103 } |
113 } |
104 } |
149 { |
140 { |
150 Get2Result<R> result; |
141 Get2Result<R> result; |
151 result.index = this->find(id); |
142 result.index = this->find(id); |
152 if (result.index.isValid()) |
143 if (result.index.isValid()) |
153 { |
144 { |
154 result.object = static_cast<const R*>(this->objectAt(result.index)); |
145 result.object = static_cast<const R*>((*this)[result.index.row()]); |
155 } |
146 } |
156 else |
147 else |
157 { |
148 { |
158 result.object = nullptr; |
149 result.object = nullptr; |
159 } |
150 } |
160 return result; |
151 return result; |
161 } |
152 } |
162 |
|
163 /** |
|
164 * @brief Gets an object pointer by id. Used by the editing context to actually modify objects. |
|
165 * @param id |
|
166 * @return object pointer |
|
167 */ |
|
168 template<typename T> |
|
169 T* Model::objectAt(ldraw::Id<T> id) |
|
170 { |
|
171 return static_cast<T*>(this->objectAt(this->find(id))); |
|
172 } |
|
173 |
|
174 template<typename T> |
|
175 const T* Model::objectAt(ldraw::Id<T> id) const |
|
176 { |
|
177 return static_cast<const T*>(this->objectAt(this->find(id))); |
|
178 } |
|
179 |
|
180 /** |
|
181 * @brief Gets an object property by id |
|
182 * @tparam Property Property to obtain |
|
183 * @param id ID of object |
|
184 * @returns property or default value |
|
185 */ |
|
186 template<ldraw::Property Property> |
|
187 ldraw::PropertyType<Property> Model::getObjectProperty(const ldraw::id_t id) const |
|
188 { |
|
189 const ldraw::Object* const object = this->objectAt(id); |
|
190 ldraw::PropertyType<Property> result; |
|
191 if (object != nullptr) |
|
192 { |
|
193 result = object->getProperty<Property>(); |
|
194 } |
|
195 return result; |
|
196 } |
|