47 QModelIndex lookup(ldraw::id_t id) const; |
47 QModelIndex lookup(ldraw::id_t id) const; |
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) const; |
52 const R* get(ldraw::Id<R> id, QModelIndex* index_out = nullptr) const; |
53 signals: |
53 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); |
59 void append(ModelObjectPointer&& object); |
59 void append(ModelObjectPointer&& object); |
60 template<typename T, typename... Args> |
60 template<typename T, typename... Args> |
61 ldraw::Id<T> insert(int position, Args&&... args); |
61 ldraw::Id<T> insert(std::size_t position, Args&&... args); |
|
62 void remove(int position); |
62 ldraw::Object* objectAt(const QModelIndex& index); |
63 ldraw::Object* objectAt(const QModelIndex& index); |
63 const ldraw::Object* objectAt(const QModelIndex& index) const; |
64 const ldraw::Object* objectAt(const QModelIndex& index) const; |
64 template<typename T> |
65 template<typename T> |
65 T* objectAt(ldraw::Id<T> id) |
66 T* objectAt(ldraw::Id<T> id) |
66 { |
67 { |
107 { |
108 { |
108 emit layoutAboutToBeChanged(); |
109 emit layoutAboutToBeChanged(); |
109 this->body.push_back(std::make_unique<T>(args...)); |
110 this->body.push_back(std::make_unique<T>(args...)); |
110 ldraw::Object* pointer = this->body.back().get(); |
111 ldraw::Object* pointer = this->body.back().get(); |
111 this->objectsById[pointer->id] = pointer; |
112 this->objectsById[pointer->id] = pointer; |
112 emit objectAdded(pointer->id, this->body.size() - 1); |
113 emit objectAdded(pointer->id, static_cast<int>(this->body.size() - 1)); |
113 emit layoutChanged(); |
114 emit layoutChanged(); |
114 return ldraw::Id<T>{pointer->id}; |
115 return ldraw::Id<T>{pointer->id.value}; |
115 } |
116 } |
116 |
117 |
117 template<typename T, typename... Args> |
118 template<typename T, typename... Args> |
118 ldraw::Id<T> Model::insert(int position, Args&&... args) |
119 ldraw::Id<T> Model::insert(std::size_t position, Args&&... args) |
119 { |
120 { |
120 emit layoutAboutToBeChanged(); |
121 emit layoutAboutToBeChanged(); |
121 this->body.insert(position, std::make_unique<T>(args...)); |
122 this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...)); |
122 ldraw::Object* pointer = this->body[position].get(); |
123 ldraw::Object* pointer = this->body[position].get(); |
123 this->objectsById[pointer->id] = pointer; |
124 this->objectsById[pointer->id] = pointer; |
124 emit objectAdded(pointer->id, position); |
125 emit objectAdded(pointer->id, static_cast<int>(position)); |
125 emit layoutChanged(); |
126 emit layoutChanged(); |
126 return ldraw::Id<T>{pointer->id}; |
127 return ldraw::Id<T>{pointer->id.value}; |
127 } |
128 } |
128 |
129 |
129 template<typename R> |
130 template<typename R> |
130 const R* Model::get(ldraw::Id<R> id) const |
131 const R* Model::get(ldraw::Id<R> id, QModelIndex* index_out) const |
131 { |
132 { |
132 return this->objectAt(id); |
133 QModelIndex index = this->lookup(id); |
|
134 if (index_out != nullptr) |
|
135 { |
|
136 *index_out = index; |
|
137 } |
|
138 return static_cast<const R*>(this->objectAt(index)); |
133 } |
139 } |