192 }, |
192 }, |
193 element); |
193 element); |
194 } |
194 } |
195 |
195 |
196 QString modelElementToString(const ModelElement& element); |
196 QString modelElementToString(const ModelElement& element); |
197 struct ModelId |
197 struct ElementId |
198 { |
198 { |
199 std::int32_t value; |
199 std::int32_t value; |
200 constexpr auto operator<=>(const ModelId& other) const = default; |
200 constexpr auto operator<=>(const ElementId& other) const = default; |
201 }; |
201 }; |
202 |
202 |
203 constexpr auto qHash(ModelId id) |
203 constexpr auto qHash(ElementId id) |
204 { |
204 { |
205 return qHash(id.value); |
205 return qHash(id.value); |
206 } |
206 } |
207 |
207 |
208 class Model : public QAbstractListModel |
208 class Model : public QAbstractListModel |
209 { |
209 { |
210 Q_OBJECT |
210 Q_OBJECT |
211 struct Entry { |
211 struct Entry { |
212 ModelElement data; |
212 ModelElement data; |
213 ModelId id; |
213 ElementId id; |
214 }; |
214 }; |
215 std::vector<Entry> body; |
215 std::vector<Entry> body; |
216 std::map<ModelId, std::size_t> positions; |
216 std::map<ElementId, std::size_t> positions; |
217 ModelId runningId = {1}; |
217 ElementId runningId = {1}; |
218 public: |
218 public: |
219 explicit Model(QObject* parent); |
219 explicit Model(QObject* parent); |
220 virtual ~Model(); |
220 virtual ~Model(); |
221 ModelId append(const ModelElement& value); |
221 ElementId append(const ModelElement& value); |
222 const ModelElement& at(std::size_t position) const; |
222 const ModelElement& at(std::size_t position) const; |
223 ModelId idAt(std::size_t position) const; |
223 ElementId idAt(std::size_t position) const; |
224 void assignAt(std::size_t position, const ModelElement& element); |
224 void assignAt(std::size_t position, const ModelElement& element); |
225 std::optional<std::size_t> find(ModelId id) const; |
225 std::optional<std::size_t> find(ElementId id) const; |
226 void remove(std::size_t index); |
226 void remove(std::size_t index); |
227 int rowCount(const QModelIndex&) const override; |
227 int rowCount(const QModelIndex&) const override; |
228 QVariant data(const QModelIndex& index, int role) const override; |
228 QVariant data(const QModelIndex& index, int role) const override; |
229 const ModelElement& operator[](std::size_t index) const; |
229 const ModelElement& operator[](std::size_t index) const; |
230 std::size_t size() const; |
230 std::size_t size() const; |