src/model.h

changeset 149
008989bc7d6e
parent 148
e1ced2523cad
equal deleted inserted replaced
148:e1ced2523cad 149:008989bc7d6e
68 void apply(Fn f) const; 68 void apply(Fn f) const;
69 void save(QIODevice *device) const; 69 void save(QIODevice *device) const;
70 void makeUnofficial(); 70 void makeUnofficial();
71 Q_SIGNALS: 71 Q_SIGNALS:
72 void objectAdded(ldraw::id_t id, int position); 72 void objectAdded(ldraw::id_t id, int position);
73 void objectModified(ldraw::id_t id, int position); 73 void objectStateChanged(int position, const QByteArray& stateBefore, const QByteArray& stateAfter);
74 private: 74 private:
75 using ModelObjectPointer = std::unique_ptr<ldraw::Object>; 75 using ModelObjectPointer = std::unique_ptr<ldraw::Object>;
76 template<typename T, typename... Args> 76 template<typename T, typename... Args>
77 ldraw::Id<T> append(Args&&... args); 77 ldraw::Id<T> append(Args&&... args);
78 void append(ModelObjectPointer&& object); 78 void append(ModelObjectPointer&& object);
153 return ldraw::NULL_ID; 153 return ldraw::NULL_ID;
154 } 154 }
155 } 155 }
156 156
157 template<typename T, typename... Args> 157 template<typename T, typename... Args>
158 ldraw::Id<T> Model::append(Args&&... args) 158 T* Model::append(Args&&... args)
159 { 159 {
160 const int position = static_cast<int>(this->body.size()); 160 const int position = static_cast<int>(this->body.size());
161 Q_EMIT beginInsertRows({}, position, position); 161 Q_EMIT beginInsertRows({}, position, position);
162 this->body.push_back(std::make_unique<T>(args...)); 162 this->body.push_back(std::make_unique<T>(args...));
163 ldraw::Object* pointer = this->body.back().get(); 163 ldraw::Object* pointer = this->body.back().get();
167 this->needRecache = true; 167 this->needRecache = true;
168 return ldraw::Id<T>{pointer->id.value}; 168 return ldraw::Id<T>{pointer->id.value};
169 } 169 }
170 170
171 template<typename T, typename... Args> 171 template<typename T, typename... Args>
172 ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args) 172 T* Model::insert(const std::size_t position, Args&&... args)
173 { 173 {
174 Q_EMIT beginInsertRows({}, position, position); 174 Q_EMIT beginInsertRows({}, position, position);
175 this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...)); 175 this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...));
176 ldraw::Object* pointer = this->body[position].get(); 176 ldraw::Object* pointer = this->body[position].get();
177 this->objectsById[pointer->id] = pointer; 177 this->objectsById[pointer->id] = pointer;

mercurial