src/librarycollection.cpp

changeset 1209
c2723022b173
child 1210
0b3b35f560f2
equal deleted inserted replaced
1208:8d4b8a9df724 1209:c2723022b173
1 #include "librarycollection.h"
2
3 const QStringList LibraryCollection::directoryNames = {"parts", "p"};
4
5 /*
6 * Searches the libraries for a file by relative path.
7 */
8 QString LibraryCollection::find(QString relativePath) const
9 {
10 // LDraw uses backslashes for directory separators. We need to convert them to forward
11 // slashes so that Qt understands them on all platforms.
12 relativePath.replace('\\', '/');
13
14 for (const Library& library : m_libraries)
15 {
16 for (const QString& directoryName : directoryNames)
17 {
18 QDir dir = library.path;
19
20 if (dir.exists() and dir.cd(directoryName) and dir.exists(relativePath))
21 return QDir::cleanPath(dir.absoluteFilePath(relativePath));
22 }
23 }
24
25 return "";
26 }
27
28 /*
29 * Searches the libraries for a file. This overload takes the parameters of a FileSpec and
30 * calls another overload with a ready spec.
31 */
32 QString LibraryCollection::find(const QString &name, FileType type) const
33 {
34 return find(FileSpec {name, type});
35 }
36
37 /*
38 * Searches the libraries for a file by the given file specification. The type attribute
39 * specifies what kind of file we are looking for.
40 */
41 QString LibraryCollection::find(const FileSpec& spec) const
42 {
43 for (const Library& library : m_libraries)
44 {
45 try
46 {
47 QDir subdirectory = library.subdirectory(spec.type);
48 if (subdirectory.exists(spec.name))
49 return QDir::cleanPath(subdirectory.absoluteFilePath(spec.name));
50 }
51 catch (SubdirectoryNotFoundError&) {}
52 }
53
54 return "";
55 }
56
57 /*
58 * Returns the appropriate subdirectory for the given file type.
59 */
60 QDir LibraryCollection::Library::subdirectory(FileType type) const
61 {
62 QString subdirectory = subdirectoryName(type);
63
64 if (path.exists(subdirectory))
65 return path.absolutePath() + "/" + subdirectory;
66 else
67 throw SubdirectoryNotFoundError {};
68 }
69
70 /*
71 * Returns the appropriate subdirectory name for the given file type.
72 */
73 QString LibraryCollection::Library::subdirectoryName(FileType type)
74 {
75 switch (type)
76 {
77 case Part:
78 return "/parts/";
79
80 case Subpart:
81 return "/parts/s/";
82
83 case Primitive:
84 return "/p/";
85
86 case HighResolutionPrimitive:
87 return "/p/48/";
88
89 case LowResolutionPrimitive:
90 return "/p/8/";
91 }
92
93 return "";
94 }

mercurial