Materials: External Modules Part 1

Refactored code to support local and external material sources

This is the first PR in a series to support external modules. External
modules allow materials to be stored in external data sources such as
databases or web services. No new functionality is introduced in this
PR, rather it is a refactoring of code that will allow for changes to
be introduced in future PRs. Minor performance improvements have also
been made in the model and material managers.

The Python API has been enhanced for many data types to allow for
modification within Python.
This commit is contained in:
David Carter
2025-03-07 10:13:56 -05:00
committed by Chris Hennes
parent 3c4977a2d4
commit 00c57a9d08
80 changed files with 4372 additions and 1396 deletions

View File

@@ -26,118 +26,141 @@
#include <QDirIterator>
#include <QMutexLocker>
#include <App/Application.h>
#include <Base/Console.h>
#include "Model.h"
#include "ModelLoader.h"
#include "ModelManager.h"
#include "ModelManagerLocal.h"
using namespace Materials;
std::shared_ptr<std::list<std::shared_ptr<ModelLibrary>>> ModelManager::_libraryList = nullptr;
std::shared_ptr<std::map<QString, std::shared_ptr<Model>>> ModelManager::_modelMap = nullptr;
QMutex ModelManager::_mutex;
TYPESYSTEM_SOURCE(Materials::ModelManager, Base::BaseClass)
QMutex ModelManager::_mutex;
ModelManager* ModelManager::_manager = nullptr;
std::unique_ptr<ModelManagerLocal> ModelManager::_localManager;
ModelManager::ModelManager()
{}
ModelManager::~ModelManager()
{}
ModelManager& ModelManager::getManager()
{
initLibraries();
if (!_manager) {
initManagers();
}
return *_manager;
}
void ModelManager::initLibraries()
void ModelManager::initManagers()
{
QMutexLocker locker(&_mutex);
if (_modelMap == nullptr) {
_modelMap = std::make_shared<std::map<QString, std::shared_ptr<Model>>>();
if (_libraryList == nullptr) {
_libraryList = std::make_shared<std::list<std::shared_ptr<ModelLibrary>>>();
}
// Load the libraries
ModelLoader loader(_modelMap, _libraryList);
if (!_manager) {
// Can't use smart pointers for this since the constructor is private
_manager = new ModelManager();
}
if (!_localManager) {
_localManager = std::make_unique<ModelManagerLocal>();
}
}
bool ModelManager::isModel(const QString& file)
{
// if (!fs::is_regular_file(p))
// return false;
// check file extension
if (file.endsWith(QStringLiteral(".yml"))) {
return true;
}
return false;
return ModelManagerLocal::isModel(file);
}
void ModelManager::cleanup()
{
if (_libraryList) {
_libraryList->clear();
}
if (_modelMap) {
for (auto& it : *_modelMap) {
// This is needed to resolve cyclic dependencies
it.second->setLibrary(nullptr);
}
_modelMap->clear();
}
return ModelManagerLocal::cleanup();
}
void ModelManager::refresh()
{
_modelMap->clear();
_libraryList->clear();
_localManager->refresh();
}
// Load the libraries
ModelLoader loader(_modelMap, _libraryList);
std::shared_ptr<std::list<std::shared_ptr<ModelLibrary>>> ModelManager::getLibraries()
{
return _localManager->getLibraries();
}
std::shared_ptr<std::list<std::shared_ptr<ModelLibrary>>> ModelManager::getLocalLibraries()
{
return _localManager->getLibraries();
}
void ModelManager::createLibrary(const QString& libraryName, const QString& icon, bool readOnly)
{}
void ModelManager::createLocalLibrary(const QString& libraryName,
const QString& directory,
const QString& icon,
bool readOnly)
{
_localManager->createLibrary(libraryName, directory, icon, readOnly);
}
void ModelManager::renameLibrary(const QString& libraryName, const QString& newName)
{
_localManager->renameLibrary(libraryName, newName);
}
void ModelManager::changeIcon(const QString& libraryName, const QString& icon)
{
_localManager->changeIcon(libraryName, icon);
}
void ModelManager::removeLibrary(const QString& libraryName)
{
_localManager->removeLibrary(libraryName);
}
std::shared_ptr<std::vector<std::tuple<QString, QString, QString>>>
ModelManager::libraryModels(const QString& libraryName)
{
return _localManager->libraryModels(libraryName);
}
bool ModelManager::isLocalLibrary(const QString& libraryName)
{
return true;
}
std::shared_ptr<std::map<QString, std::shared_ptr<Model>>> ModelManager::getModels()
{
return _localManager->getModels();
}
std::shared_ptr<std::map<QString, std::shared_ptr<Model>>> ModelManager::getLocalModels()
{
return _localManager->getModels();
}
std::shared_ptr<Model> ModelManager::getModel(const QString& uuid) const
{
try {
if (_modelMap == nullptr) {
throw Uninitialized();
}
return _modelMap->at(uuid);
}
catch (std::out_of_range const&) {
throw ModelNotFound();
}
return _localManager->getModel(uuid);
}
std::shared_ptr<Model> ModelManager::getModelByPath(const QString& path) const
{
QString cleanPath = QDir::cleanPath(path);
for (auto& library : *_libraryList) {
if (cleanPath.startsWith(library->getDirectory())) {
return library->getModelByPath(cleanPath);
}
}
throw MaterialNotFound();
return _localManager->getModelByPath(path);
}
std::shared_ptr<Model> ModelManager::getModelByPath(const QString& path, const QString& lib) const
{
auto library = getLibrary(lib); // May throw LibraryNotFound
return library->getModelByPath(path); // May throw ModelNotFound
return _localManager->getModelByPath(path, lib);
}
std::shared_ptr<ModelLibrary> ModelManager::getLibrary(const QString& name) const
{
for (auto& library : *_libraryList) {
if (library->getName() == name) {
return library;
}
}
throw LibraryNotFound();
return _localManager->getLibrary(name);
}
bool ModelManager::passFilter(ModelFilter filter, Model::ModelType modelType)