diff -r 37f936073cac -r e1ced2523cad src/model.cpp --- a/src/model.cpp Sun Oct 24 11:33:32 2021 +0300 +++ b/src/model.cpp Tue Nov 02 15:43:57 2021 +0200 @@ -23,22 +23,14 @@ #include #include "model.h" #include "modeleditcontext.h" +#include "documentmanager.h" /** * @brief Constructs a model * @param parent QObject parent to pass forward */ -Model::Model(QObject* parent) : - Model{"", parent} {} - -/** - * @brief Constructs a model - * @param path Path that was used to open the model - * @param parent QObject parent to pass forward - */ -Model::Model(const QString& path, QObject *parent) : - QAbstractListModel{parent}, - storedPath{path} +Model::Model(QObject *parent) : + QAbstractListModel{parent} { connect(this, &Model::dataChanged, [&](){ this->needRecache = true; }); } @@ -150,10 +142,14 @@ if (this->needRecache) { this->cachedPolygons.clear(); - ldraw::GetPolygonsContext context{documents}; - for (int i = 0; i < this->size(); i += 1) + const std::optional modelId = documents->findIdForModel(this); + if (modelId.has_value()) { - this->getObjectPolygons(i, this->cachedPolygons, &context); + ldraw::GetPolygonsContext context{modelId.value(), documents}; + for (int i = 0; i < this->size(); i += 1) + { + this->getObjectPolygons(i, this->cachedPolygons, &context); + } } this->needRecache = false; } @@ -188,15 +184,7 @@ return this->objectAt(index)->id; } -/** - * @brief Gets the path to the model - * @return path - */ -const QString& Model::path() const -{ - return this->storedPath; -} - +#if 0 /** * @brief Sets the path to the model * @param path New path to use @@ -220,6 +208,7 @@ } } } +#endif /** * @brief Gets the GL polygons of the object at the specified position in the model @@ -304,39 +293,13 @@ /** * @brief Attempts the save the model - * @param errors Where to write any errors - * @returns whether it succeeded */ -bool Model::save(QTextStream &errors) const +void Model::save(QIODevice* device) const { - QSaveFile file{this->path()}; - file.setDirectWriteFallback(true); - if (file.open(QSaveFile::WriteOnly)) + QTextStream out{device}; + for (const ModelObjectPointer& object : this->body) { - QTextStream out{&file}; - for (const ModelObjectPointer& object : this->body) - { - out << object.get()->toLDrawCode() << "\r\n"; - } - const bool commitSucceeded = file.commit(); - if (not commitSucceeded) - { - errors << tr("Could not save to %1: %2") - .arg(this->path()) - .arg(file.errorString()); - return false; - } - else - { - return true; - } - } - else - { - errors << tr("Could not open %1 for writing: %2") - .arg(file.fileName()) - .arg(file.errorString()); - return false; + out << object.get()->toLDrawCode() << "\r\n"; } }