48 ldraw::id_t resolve(const QModelIndex& index) const; |
48 ldraw::id_t resolve(const QModelIndex& index) const; |
49 template<typename R> |
49 template<typename R> |
50 ldraw::Id<R> checkType(ldraw::id_t id) const; |
50 ldraw::Id<R> checkType(ldraw::id_t id) const; |
51 template<typename R> |
51 template<typename R> |
52 const R* get(ldraw::Id<R> id, QModelIndex* index_out = nullptr) const; |
52 const R* get(ldraw::Id<R> id, QModelIndex* index_out = nullptr) const; |
53 signals: |
53 Q_SIGNALS: |
54 void objectAdded(ldraw::id_t id, int position); |
54 void objectAdded(ldraw::id_t id, int position); |
55 private: |
55 private: |
56 using ModelObjectPointer = std::unique_ptr<ldraw::Object>; |
56 using ModelObjectPointer = std::unique_ptr<ldraw::Object>; |
57 template<typename T, typename... Args> |
57 template<typename T, typename... Args> |
58 ldraw::Id<T> append(Args&&... args); |
58 ldraw::Id<T> append(Args&&... args); |
105 |
105 |
106 template<typename T, typename... Args> |
106 template<typename T, typename... Args> |
107 ldraw::Id<T> Model::append(Args&&... args) |
107 ldraw::Id<T> Model::append(Args&&... args) |
108 { |
108 { |
109 const int position = static_cast<int>(this->body.size()); |
109 const int position = static_cast<int>(this->body.size()); |
110 emit beginInsertRows({}, position, position); |
110 Q_EMIT beginInsertRows({}, position, position); |
111 this->body.push_back(std::make_unique<T>(args...)); |
111 this->body.push_back(std::make_unique<T>(args...)); |
112 ldraw::Object* pointer = this->body.back().get(); |
112 ldraw::Object* pointer = this->body.back().get(); |
113 this->objectsById[pointer->id] = pointer; |
113 this->objectsById[pointer->id] = pointer; |
114 emit objectAdded(pointer->id, static_cast<int>(this->body.size() - 1)); |
114 Q_EMIT objectAdded(pointer->id, static_cast<int>(this->body.size() - 1)); |
115 emit endInsertRows(); |
115 Q_EMIT endInsertRows(); |
116 this->needRecache = true; |
116 this->needRecache = true; |
117 return ldraw::Id<T>{pointer->id.value}; |
117 return ldraw::Id<T>{pointer->id.value}; |
118 } |
118 } |
119 |
119 |
120 template<typename T, typename... Args> |
120 template<typename T, typename... Args> |
121 ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args) |
121 ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args) |
122 { |
122 { |
123 emit beginInsertRows({}, position, position); |
123 Q_EMIT beginInsertRows({}, position, position); |
124 this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...)); |
124 this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...)); |
125 ldraw::Object* pointer = this->body[position].get(); |
125 ldraw::Object* pointer = this->body[position].get(); |
126 this->objectsById[pointer->id] = pointer; |
126 this->objectsById[pointer->id] = pointer; |
127 emit objectAdded(pointer->id, static_cast<int>(position)); |
127 Q_EMIT objectAdded(pointer->id, static_cast<int>(position)); |
128 emit endInsertRows(); |
128 Q_EMIT endInsertRows(); |
129 this->needRecache = true; |
129 this->needRecache = true; |
130 return ldraw::Id<T>{pointer->id.value}; |
130 return ldraw::Id<T>{pointer->id.value}; |
131 } |
131 } |
132 |
132 |
133 template<typename R> |
133 template<typename R> |