File size: 8,284 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2023 David Carter <dcarter@david.carter.ca> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include <QMutexLocker>
#include <App/Application.h>
#include "Exceptions.h"
#include "ExternalManager.h"
#include "MaterialLibrary.h"
#include "MaterialManagerExternal.h"
using namespace Materials;
/* TRANSLATOR Material::Materials */
QMutex MaterialManagerExternal::_mutex;
LRU::Cache<std::string, std::shared_ptr<Material>>
MaterialManagerExternal::_cache(DEFAULT_CACHE_SIZE);
TYPESYSTEM_SOURCE(Materials::MaterialManagerExternal, Base::BaseClass)
MaterialManagerExternal::MaterialManagerExternal()
{
initCache();
}
void MaterialManagerExternal::initCache()
{
QMutexLocker locker(&_mutex);
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Mod/Material/ExternalInterface");
auto cacheSize = hGrp->GetInt("MaterialCacheSize", DEFAULT_CACHE_SIZE);
_cache.capacity(cacheSize);
_cache.monitor();
}
void MaterialManagerExternal::cleanup()
{}
void MaterialManagerExternal::refresh()
{
resetCache();
}
//=====
//
// Library management
//
//=====
std::shared_ptr<std::list<std::shared_ptr<MaterialLibrary>>> MaterialManagerExternal::getLibraries()
{
auto libraryList = std::make_shared<std::list<std::shared_ptr<MaterialLibrary>>>();
try {
auto externalLibraries = ExternalManager::getManager()->libraries();
for (auto& entry : *externalLibraries) {
auto library = std::make_shared<MaterialLibrary>(*entry);
libraryList->push_back(library);
}
}
catch (const LibraryNotFound& e) {
}
catch (const ConnectionError& e) {
}
return libraryList;
}
std::shared_ptr<std::list<std::shared_ptr<MaterialLibrary>>>
MaterialManagerExternal::getMaterialLibraries()
{
auto libraryList = std::make_shared<std::list<std::shared_ptr<MaterialLibrary>>>();
try {
auto externalLibraries = ExternalManager::getManager()->materialLibraries();
for (auto& entry : *externalLibraries) {
auto library = std::make_shared<MaterialLibrary>(*entry);
libraryList->push_back(library);
}
}
catch (const LibraryNotFound& e) {
}
catch (const ConnectionError& e) {
}
return libraryList;
}
std::shared_ptr<MaterialLibrary> MaterialManagerExternal::getLibrary(const QString& name) const
{
try {
auto lib = ExternalManager::getManager()->getLibrary(name);
auto library = std::make_shared<MaterialLibrary>(*lib);
return library;
}
catch (const LibraryNotFound& e) {
throw LibraryNotFound(e);
}
catch (const ConnectionError& e) {
throw LibraryNotFound(e.what());
}
catch (...) {
throw LibraryNotFound("Unknown exception");
}
}
void MaterialManagerExternal::createLibrary(const QString& libraryName,
const QByteArray& icon,
bool readOnly)
{
ExternalManager::getManager()->createLibrary(libraryName, icon, readOnly);
}
void MaterialManagerExternal::renameLibrary(const QString& libraryName, const QString& newName)
{
ExternalManager::getManager()->renameLibrary(libraryName, newName);
}
void MaterialManagerExternal::changeIcon(const QString& libraryName, const QByteArray& icon)
{
ExternalManager::getManager()->changeIcon(libraryName, icon);
}
void MaterialManagerExternal::removeLibrary(const QString& libraryName)
{
ExternalManager::getManager()->removeLibrary(libraryName);
}
std::shared_ptr<std::vector<LibraryObject>>
MaterialManagerExternal::libraryMaterials(const QString& libraryName)
{
return ExternalManager::getManager()->libraryMaterials(libraryName);
}
std::shared_ptr<std::vector<LibraryObject>>
MaterialManagerExternal::libraryMaterials(const QString& libraryName,
const MaterialFilter& filter,
const MaterialFilterOptions& options)
{
return ExternalManager::getManager()->libraryMaterials(libraryName, filter, options);
}
//=====
//
// Folder management
//
//=====
void MaterialManagerExternal::createFolder(const MaterialLibrary& library,
const QString& path)
{
ExternalManager::getManager()->createFolder(library.getName(), path);
}
void MaterialManagerExternal::renameFolder(const MaterialLibrary& library,
const QString& oldPath,
const QString& newPath)
{
ExternalManager::getManager()->renameFolder(library.getName(), oldPath, newPath);
}
void MaterialManagerExternal::deleteRecursive(const MaterialLibrary& library,
const QString& path)
{
ExternalManager::getManager()->deleteRecursive(library.getName(), path);
}
//=====
//
// Material management
//
//=====
std::shared_ptr<Material> MaterialManagerExternal::materialNotFound(const QString& uuid) const
{
// Setting the cache value to nullptr prevents repeated lookups
_cache.emplace(uuid.toStdString(), nullptr);
return nullptr;
}
std::shared_ptr<Material> MaterialManagerExternal::getMaterial(const QString& uuid) const
{
if (_cache.contains(uuid.toStdString())) {
return _cache.lookup(uuid.toStdString());
}
try {
auto material = ExternalManager::getManager()->getMaterial(uuid);
_cache.emplace(uuid.toStdString(), material);
return material;
}
catch (const MaterialNotFound& e) {
return materialNotFound(uuid);
}
catch (const ConnectionError& e) {
return materialNotFound(uuid);
}
catch (...) {
return materialNotFound(uuid);
}
}
void MaterialManagerExternal::addMaterial(const QString& libraryName,
const QString& path,
const Material& material)
{
_cache.erase(material.getUUID().toStdString());
ExternalManager::getManager()->addMaterial(libraryName, path, material);
}
void MaterialManagerExternal::migrateMaterial(const QString& libraryName,
const QString& path,
const Material& material)
{
_cache.erase(material.getUUID().toStdString());
ExternalManager::getManager()->migrateMaterial(libraryName, path, material);
}
//=====
//
// Cache management
//
//=====
void MaterialManagerExternal::resetCache()
{
_cache.clear();
}
double MaterialManagerExternal::materialHitRate()
{
auto hitRate = _cache.stats().hit_rate();
if (std::isnan(hitRate)) {
return 0;
}
return hitRate;
}
|