Sun, 12 Mar 2017 11:03:44 +0200
More work on library collections
/* * LDForge: LDraw parts authoring CAD * Copyright (C) 2013 - 2017 Teemu Piippo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "librarycollection.h" const QStringList LibraryCollection::directoryNames = {"parts", "p"}; void LibraryCollection::addLibrary(QDir dir, QString name) { m_libraries.append({dir, name}); } /* * Searches the libraries for a file by relative path. */ QString LibraryCollection::find(QString relativePath) const { // LDraw uses backslashes for directory separators. We need to convert them to forward // slashes so that Qt understands them on all platforms. relativePath.replace('\\', '/'); for (const Library& library : m_libraries) { for (const QString& directoryName : directoryNames) { QDir dir = library.path; if (dir.exists() and dir.cd(directoryName) and dir.exists(relativePath)) return QDir::cleanPath(dir.absoluteFilePath(relativePath)); } } return ""; } /* * Searches the libraries for a file. This overload takes the parameters of a FileSpec and * calls another overload with a ready spec. */ QString LibraryCollection::find(const QString &name, FileType type) const { return find(FileSpec {name, type}); } /* * Searches the libraries for a file by the given file specification. The type attribute * specifies what kind of file we are looking for. */ QString LibraryCollection::find(const FileSpec& spec) const { for (const Library& library : m_libraries) { try { QDir subdirectory = library.subdirectory(spec.type); if (subdirectory.exists(spec.name)) return QDir::cleanPath(subdirectory.absoluteFilePath(spec.name)); } catch (SubdirectoryNotFoundError&) {} } return ""; } /* * Returns the appropriate subdirectory for the given file type. */ QDir LibraryCollection::Library::subdirectory(FileType type) const { QString subdirectory = subdirectoryName(type); if (path.exists(subdirectory)) return path.absolutePath() + "/" + subdirectory; else throw SubdirectoryNotFoundError {}; } /* * Returns the appropriate subdirectory name for the given file type. */ QString LibraryCollection::Library::subdirectoryName(FileType type) { switch (type) { case Part: return "/parts/"; case Subpart: return "/parts/s/"; case Primitive: return "/p/"; case HighResolutionPrimitive: return "/p/48/"; case LowResolutionPrimitive: return "/p/8/"; } return ""; }