diff -r e1ced2523cad -r b6cbba6e29a1 src/model.h --- a/src/model.h Tue Nov 02 15:43:57 2021 +0200 +++ b/src/model.h Thu Mar 03 11:42:52 2022 +0200 @@ -37,23 +37,18 @@ class EditContext; Model(QObject* parent = nullptr); Model(const Model&) = delete; + int size() const; ldraw::id_t at(int index) const; EditContext edit(); int rowCount(const QModelIndex&) const override; QVariant data(const QModelIndex& index, int role) const override; QVariant getHeaderProperty(const HeaderProperty property); - const QString& getName() const; QVariant getObjectProperty(const int index, const ldraw::Property property) const; template ldraw::PropertyType getObjectProperty(const ldraw::id_t id) const; - std::vector getPolygons(class DocumentManager* documents) const; - QModelIndex lookup(ldraw::id_t id) const; - ldraw::id_t resolve(const QModelIndex& index) const; - template - ldraw::Id checkType(ldraw::id_t id) const; - template - bool isA(ldraw::id_t id) const; + QModelIndex find(ldraw::id_t id) const; + ldraw::id_t idAt(const QModelIndex& index) const; template const R* get(ldraw::Id id) const; template @@ -67,10 +62,11 @@ template void apply(Fn f) const; void save(QIODevice *device) const; - void makeUnofficial(); + ldraw::Object* operator[](int index); Q_SIGNALS: - void objectAdded(ldraw::id_t id, int position); - void objectModified(ldraw::id_t id, int position); + void objectAdded(int position); + void objectModified(int position); + void objectRemoved(ldraw::id_t id); private: using ModelObjectPointer = std::unique_ptr; template @@ -85,38 +81,19 @@ T* objectAt(ldraw::Id id); template const T* objectAt(ldraw::Id id) const; - void getObjectPolygons( - const int index, - std::vector& polygons_out, - ldraw::GetPolygonsContext* context) const; void editFinished(); void objectModified(ldraw::id_t id); bool modified = false; LDHeader header; std::vector body; std::map objectsById; - mutable std::vector cachedPolygons; - mutable bool needRecache = true; /** * @brief Amount of model edit contexts active */ int editCounter = 0; }; -/** - * @brief Checks whether the id is exactly of the specified type - * @tparam R Type of LDraw line type object to test for - * @param id Id of object to test - * @returns whether the type of the object specified by @c id is the same type as R. Returns false if it is a subclass. - */ -template -bool Model::isA(ldraw::id_t id) const -{ - const ldraw::Object* object = this->objectAt(this->lookup(id)); - const std::type_info& a = typeid(*object); - const std::type_info& b = typeid(R); - return a == b; -} +void makeUnofficial(Model& model); /** * @brief Calls the specified function to all matching objects in the model @@ -136,24 +113,6 @@ } } -/** - * \brief Checks type of object behind id - * Checks whether the specified id refers to an object of the specified type. - * \returns id casted to subclass if appropriate, null id otherwise - */ -template -ldraw::Id Model::checkType(ldraw::id_t id) const -{ - if (dynamic_cast(this->objectAt(this->lookup(id))) != nullptr) - { - return ldraw::Id{id.value}; - } - else - { - return ldraw::NULL_ID; - } -} - template ldraw::Id Model::append(Args&&... args) { @@ -162,9 +121,8 @@ this->body.push_back(std::make_unique(args...)); ldraw::Object* pointer = this->body.back().get(); this->objectsById[pointer->id] = pointer; - Q_EMIT objectAdded(pointer->id, static_cast(this->body.size() - 1)); + Q_EMIT objectAdded(static_cast(this->body.size() - 1)); Q_EMIT endInsertRows(); - this->needRecache = true; return ldraw::Id{pointer->id.value}; } @@ -175,9 +133,8 @@ this->body.insert(std::begin(this->body) + position, std::make_unique(args...)); ldraw::Object* pointer = this->body[position].get(); this->objectsById[pointer->id] = pointer; - Q_EMIT objectAdded(pointer->id, static_cast(position)); + Q_EMIT objectAdded(static_cast(position)); Q_EMIT endInsertRows(); - this->needRecache = true; return ldraw::Id{pointer->id.value}; } @@ -191,7 +148,7 @@ Model::Get2Result Model::get2(const ldraw::Id id) const { Get2Result result; - result.index = this->lookup(id); + result.index = this->find(id); if (result.index.isValid()) { result.object = static_cast(this->objectAt(result.index)); @@ -211,13 +168,13 @@ template T* Model::objectAt(ldraw::Id id) { - return static_cast(this->objectAt(this->lookup(id))); + return static_cast(this->objectAt(this->find(id))); } template const T* Model::objectAt(ldraw::Id id) const { - return static_cast(this->objectAt(this->lookup(id))); + return static_cast(this->objectAt(this->find(id))); } /**