Mon, 27 Mar 2017 14:56:05 +0300
LibraryCollection now derives from QObject and QVector<Library>
--- a/src/documentmanager.cpp Sun Mar 12 11:03:44 2017 +0200 +++ b/src/documentmanager.cpp Mon Mar 27 14:56:05 2017 +0300 @@ -192,11 +192,6 @@ return path; } -QString DocumentManager::findDocumentPath (QString relativePath) -{ - return m_libraries.find(relativePath); -} - void DocumentManager::loadFileContents(QIODevice* input, Model& model, int* numWarnings, bool* ok) { if (numWarnings) @@ -235,7 +230,7 @@ if (search) { print ("Opening %1...\n", path.toLower()); - fullpath = findDocumentPath(path.toLower()); + fullpath = m_libraries.find(path.toLower()); if (fullpath.isEmpty()) return nullptr;
--- a/src/documentmanager.h Sun Mar 12 11:03:44 2017 +0200 +++ b/src/documentmanager.h Mon Mar 27 14:56:05 2017 +0300 @@ -39,7 +39,6 @@ void clear(); LDDocument* createNew(); LDDocument* findDocumentByName (QString name); - QString findDocumentPath (QString relpath); LDDocument* getDocumentByName (QString filename); bool isSafeToCloseAll(); void loadFileContents(QIODevice* fp, Model& model, int* numWarnings, bool* ok);
--- a/src/librarycollection.cpp Sun Mar 12 11:03:44 2017 +0200 +++ b/src/librarycollection.cpp Mon Mar 27 14:56:05 2017 +0300 @@ -22,7 +22,7 @@ void LibraryCollection::addLibrary(QDir dir, QString name) { - m_libraries.append({dir, name}); + Super::append({dir, name}); } /* @@ -34,7 +34,7 @@ // slashes so that Qt understands them on all platforms. relativePath.replace('\\', '/'); - for (const Library& library : m_libraries) + for (const Library& library : *this) { for (const QString& directoryName : directoryNames) { @@ -52,7 +52,7 @@ * 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 +QString LibraryCollection::find(const QString &name, Library::FileType type) const { return find(FileSpec {name, type}); } @@ -63,7 +63,7 @@ */ QString LibraryCollection::find(const FileSpec& spec) const { - for (const Library& library : m_libraries) + for (const Library& library : *this) { try { @@ -71,7 +71,7 @@ if (subdirectory.exists(spec.name)) return QDir::cleanPath(subdirectory.absoluteFilePath(spec.name)); } - catch (SubdirectoryNotFoundError&) {} + catch (Library::SubdirectoryNotFoundError&) {} } return ""; @@ -80,7 +80,7 @@ /* * Returns the appropriate subdirectory for the given file type. */ -QDir LibraryCollection::Library::subdirectory(FileType type) const +QDir Library::subdirectory(FileType type) const { QString subdirectory = subdirectoryName(type); @@ -93,7 +93,7 @@ /* * Returns the appropriate subdirectory name for the given file type. */ -QString LibraryCollection::Library::subdirectoryName(FileType type) +QString Library::subdirectoryName(FileType type) { switch (type) { @@ -114,4 +114,4 @@ } return ""; -} \ No newline at end of file +}
--- a/src/librarycollection.h Sun Mar 12 11:03:44 2017 +0200 +++ b/src/librarycollection.h Mon Mar 27 14:56:05 2017 +0300 @@ -20,13 +20,10 @@ #include <QDir> #include "main.h" -/* - * Models a collection of libraries. A library is a folder containing directories named "p" and - * "parts" that contains part and subpart files. - */ -class LibraryCollection +struct Library { -public: + class SubdirectoryNotFoundError : public std::exception {}; + enum FileType { Part, @@ -36,30 +33,33 @@ LowResolutionPrimitive, }; + QDir path; + QString name; + + QDir subdirectory(FileType type) const; + static QString subdirectoryName(FileType type); +}; + +/* + * Models a collection of libraries. A library is a folder containing directories named "p" and + * "parts" that contains part and subpart files. + */ +class LibraryCollection : public QObject, private QVector<Library> +{ + Q_OBJECT + using Super = QVector<Library>; + +public: struct FileSpec { QString name; - FileType type; + Library::FileType type; }; void addLibrary(QDir dir, QString name); QString find(QString relativePath) const; - QString find(const QString& name, FileType type) const; + QString find(const QString& name, Library::FileType type) const; QString find(const FileSpec& spec) const; static const QStringList directoryNames; - -private: - class SubdirectoryNotFoundError : public std::exception {}; - - struct Library - { - QDir path; - QString name; - - QDir subdirectory(FileType type) const; - static QString subdirectoryName(FileType type); - }; - - QVector<Library> m_libraries; };