src/documentmanager.cpp

changeset 150
b6cbba6e29a1
parent 148
e1ced2523cad
child 151
e628fc2e0c72
equal deleted inserted replaced
148:e1ced2523cad 150:b6cbba6e29a1
43 { 43 {
44 const ModelId modelId{++this->modelIdCounter}; 44 const ModelId modelId{++this->modelIdCounter};
45 const QString name = makeNewModelName(); 45 const QString name = makeNewModelName();
46 this->openModels[modelId] = ModelInfo{ 46 this->openModels[modelId] = ModelInfo{
47 .model = std::make_unique<Model>(), 47 .model = std::make_unique<Model>(),
48 .id = modelId,
48 .opentype = OpenType::ManuallyOpened, 49 .opentype = OpenType::ManuallyOpened,
49 }; 50 };
51 this->makePolygonCacheForModel(modelId);
50 return modelId; 52 return modelId;
51 } 53 }
52 54
53 /** 55 /**
54 * @brief Looks for a model by name 56 * @brief Looks for a model by name
139 parser.parseBody(editor); 141 parser.parseBody(editor);
140 std::optional<ModelId> result; 142 std::optional<ModelId> result;
141 if (file.error() == QFile::NoError) 143 if (file.error() == QFile::NoError)
142 { 144 {
143 const ModelId modelId{++this->modelIdCounter}; 145 const ModelId modelId{++this->modelIdCounter};
144 this->openModels[modelId] = {std::move(newModel), path, openType}; 146 this->openModels[modelId] = {std::move(newModel), modelId, path, openType};
147 this->makePolygonCacheForModel(modelId);
145 result = modelId; 148 result = modelId;
146 } 149 }
147 else 150 else
148 { 151 {
149 errorStream << file.errorString(); 152 errorStream << file.errorString();
297 } 300 }
298 } 301 }
299 return result; 302 return result;
300 } 303 }
301 304
305 PolygonCache *DocumentManager::getPolygonCacheForModel(ModelId modelId)
306 {
307 auto it = this->polygonCaches.find(modelId);
308 if (it != this->polygonCaches.end())
309 {
310 return &it->second;
311 }
312 else
313 {
314 return nullptr;
315 }
316 }
317
302 /** 318 /**
303 * @brief Cleans up and erases models that are no longer required. 319 * @brief Cleans up and erases models that are no longer required.
304 */ 320 */
305 void DocumentManager::prune() 321 void DocumentManager::prune()
306 { 322 {
309 // Find models that are not edited by the user and are not needed by any other model 325 // Find models that are not edited by the user and are not needed by any other model
310 if (true 326 if (true
311 and it->second.opentype == OpenType::AutomaticallyOpened 327 and it->second.opentype == OpenType::AutomaticallyOpened
312 and not this->isReferencedByAnything(it->first) 328 and not this->isReferencedByAnything(it->first)
313 ) { 329 ) {
330 // Remove its polygon cache
331 const auto polygonCache = this->polygonCaches.find(it->first);
332 if (polygonCache != this->polygonCaches.end())
333 {
334 this->polygonCaches.erase(polygonCache);
335 }
314 // Remove the model 336 // Remove the model
315 this->openModels.erase(it); 337 this->openModels.erase(it);
316 // We need to start over now. It is possible that other models that previously 338 // We need to start over now. It is possible that other models that previously
317 // were referenced by the model we just erased have become prunable. 339 // were referenced by the model we just erased have become prunable.
318 // Moreover, our iterator is invalid now and we cannot continue in this for loop. 340 // Moreover, our iterator is invalid now and we cannot continue in this for loop.
341 } 363 }
342 } 364 }
343 } 365 }
344 } 366 }
345 return false; 367 return false;
368 }
369
370 void DocumentManager::makePolygonCacheForModel(const ModelId modelId)
371 {
372 Model* model = this->getModelById(modelId);
373 if (model != nullptr)
374 {
375 this->polygonCaches.emplace(
376 std::piecewise_construct,
377 std::forward_as_tuple(modelId),
378 std::forward_as_tuple(model));
379 }
346 } 380 }
347 381
348 static QString findFile(QString referenceName, const QString& path, const LibraryManager& libraries) 382 static QString findFile(QString referenceName, const QString& path, const LibraryManager& libraries)
349 { 383 {
350 // Try to find the file in the same place as the model itself 384 // Try to find the file in the same place as the model itself

mercurial