--- a/src/model.cpp Tue Nov 02 15:43:57 2021 +0200 +++ b/src/model.cpp Thu Mar 03 11:42:52 2022 +0200 @@ -32,7 +32,6 @@ Model::Model(QObject *parent) : QAbstractListModel{parent} { - connect(this, &Model::dataChanged, [&](){ this->needRecache = true; }); } /** @@ -132,36 +131,11 @@ } /** - * @brief Gets a list of GL polygons that are used to represent this model. - * @details Will build polygons if polygons are outdated. - * @param documents Documents to use to resolve subfile references - * @return vector of GL polygons - */ -std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const -{ - if (this->needRecache) - { - this->cachedPolygons.clear(); - const std::optional<ModelId> modelId = documents->findIdForModel(this); - if (modelId.has_value()) - { - ldraw::GetPolygonsContext context{modelId.value(), documents}; - for (int i = 0; i < this->size(); i += 1) - { - this->getObjectPolygons(i, this->cachedPolygons, &context); - } - } - this->needRecache = false; - } - return this->cachedPolygons; -} - -/** * @brief Finds the position of the specified object in the model * @param id Object id to look for * @return model index */ -QModelIndex Model::lookup(ldraw::id_t id) const +QModelIndex Model::find(ldraw::id_t id) const { // FIXME: This linear search will probably cause performance issues for (std::size_t i = 0; i < this->body.size(); i += 1) @@ -179,7 +153,7 @@ * @param index Position of the object in the model * @return id */ -ldraw::id_t Model::resolve(const QModelIndex& index) const +ldraw::id_t Model::idAt(const QModelIndex& index) const { return this->objectAt(index)->id; } @@ -211,21 +185,6 @@ #endif /** - * @brief Gets the GL polygons of the object at the specified position in the model - * @param index Index of object in the model - * @param polygons_out Vector to add polygons into - * @param context Context to use to resolve subfile references - */ -void Model::getObjectPolygons( - const int index, - std::vector<gl::Polygon>& polygons_out, - ldraw::GetPolygonsContext* context) const -{ - const ldraw::Object* object = this->body[unsigned_cast(index)].get(); - object->getPolygons(polygons_out, context); -} - -/** * @brief Called by the editing context to signal to the model that editing is done. */ void Model::editFinished() @@ -239,7 +198,7 @@ */ void Model::objectModified(ldraw::id_t id) { - const QModelIndex index = this->lookup(id); + const QModelIndex index = this->find(id); Q_EMIT this->dataChanged(index, index); } @@ -250,10 +209,10 @@ void Model::append(ModelObjectPointer&& object) { const int position = static_cast<int>(this->body.size()); - Q_EMIT beginInsertRows({}, position, position); + Q_EMIT this->beginInsertRows({}, position, position); this->body.push_back(std::move(object)); - Q_EMIT endInsertRows(); - this->needRecache = true; + Q_EMIT this->endInsertRows(); + Q_EMIT this->objectAdded(position); } /** @@ -264,10 +223,11 @@ { if (position >= 0 and position < signed_cast(this->body.size())) { - Q_EMIT beginRemoveRows({}, position, position); + Q_EMIT this->beginRemoveRows({}, position, position); + const ldraw::id_t id = this->body[position]->id; this->body.erase(std::begin(this->body) + position); - Q_EMIT endRemoveRows(); - this->needRecache = true; + Q_EMIT this->endRemoveRows(); + Q_EMIT this->objectRemoved(id); } } @@ -303,29 +263,42 @@ } } +ldraw::Object* Model::operator[](int index) +{ + if (index >= 0 and index < this->size()) + { + return this->body[index].get(); + } + else + { + throw std::out_of_range{"index out of range"}; + } +} + /** * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial */ -void Model::makeUnofficial() +void makeUnofficial(Model& model) { - if (this->body.size() >= 4) + if (model.size() >= 4) { - const ldraw::id_t id = this->body[3]->id; - if (this->isA<ldraw::MetaCommand>(id)) + const ldraw::Object* ldrawOrgLine = model[3]; + if (isA<ldraw::MetaCommand>(ldrawOrgLine)) { - const QString& body = this->body[3]->getProperty<ldraw::Property::Text>(); + const QString& body = ldrawOrgLine->getProperty<ldraw::Property::Text>(); if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_")) { + // Add Unofficial_ to part type QStringList tokens = body.split(" "); tokens[1] = "Unofficial_" + tokens[1]; - // Remove the UPDATE tag + // Remove the UPDATE tag if it's there if (tokens.size() >= 4 && tokens[2] == "UPDATE") { tokens.removeAt(3); tokens.removeAt(2); } - EditContext editor = this->edit(); - editor.setObjectProperty<ldraw::Property::Text>(id, tokens.join(" ")); + Model::EditContext editor = model.edit(); + editor.setObjectProperty<ldraw::Property::Text>(ldrawOrgLine->id, tokens.join(" ")); } } }