src/librarycollection.cpp

Sun, 12 Mar 2017 11:03:44 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 12 Mar 2017 11:03:44 +0200
changeset 1210
0b3b35f560f2
parent 1209
c2723022b173
child 1211
83b8ed708909
permissions
-rw-r--r--

More work on library collections

1210
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
1 /*
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
2 * LDForge: LDraw parts authoring CAD
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
4 *
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
5 * This program is free software: you can redistribute it and/or modify
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
6 * it under the terms of the GNU General Public License as published by
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
8 * (at your option) any later version.
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
9 *
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
10 * This program is distributed in the hope that it will be useful,
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
13 * GNU General Public License for more details.
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
14 *
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
15 * You should have received a copy of the GNU General Public License
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
17 */
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
18
1209
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "librarycollection.h"
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 const QStringList LibraryCollection::directoryNames = {"parts", "p"};
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
1210
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
23 void LibraryCollection::addLibrary(QDir dir, QString name)
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
24 {
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
25 m_libraries.append({dir, name});
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
26 }
0b3b35f560f2 More work on library collections
Teemu Piippo <teemu@hecknology.net>
parents: 1209
diff changeset
27
1209
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 /*
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 * Searches the libraries for a file by relative path.
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 */
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 QString LibraryCollection::find(QString relativePath) const
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 // LDraw uses backslashes for directory separators. We need to convert them to forward
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 // slashes so that Qt understands them on all platforms.
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 relativePath.replace('\\', '/');
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 for (const Library& library : m_libraries)
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 for (const QString& directoryName : directoryNames)
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 QDir dir = library.path;
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 if (dir.exists() and dir.cd(directoryName) and dir.exists(relativePath))
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 return QDir::cleanPath(dir.absoluteFilePath(relativePath));
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 return "";
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 /*
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 * Searches the libraries for a file. This overload takes the parameters of a FileSpec and
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 * calls another overload with a ready spec.
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 */
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 QString LibraryCollection::find(const QString &name, FileType type) const
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 return find(FileSpec {name, type});
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 /*
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 * Searches the libraries for a file by the given file specification. The type attribute
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 * specifies what kind of file we are looking for.
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 */
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 QString LibraryCollection::find(const FileSpec& spec) const
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 for (const Library& library : m_libraries)
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 try
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 QDir subdirectory = library.subdirectory(spec.type);
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 if (subdirectory.exists(spec.name))
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 return QDir::cleanPath(subdirectory.absoluteFilePath(spec.name));
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 catch (SubdirectoryNotFoundError&) {}
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 return "";
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 /*
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 * Returns the appropriate subdirectory for the given file type.
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 */
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 QDir LibraryCollection::Library::subdirectory(FileType type) const
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 QString subdirectory = subdirectoryName(type);
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 if (path.exists(subdirectory))
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 return path.absolutePath() + "/" + subdirectory;
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 else
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 throw SubdirectoryNotFoundError {};
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 /*
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 * Returns the appropriate subdirectory name for the given file type.
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 */
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 QString LibraryCollection::Library::subdirectoryName(FileType type)
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 switch (type)
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 {
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 case Part:
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 return "/parts/";
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 case Subpart:
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 return "/parts/s/";
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 case Primitive:
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 return "/p/";
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 case HighResolutionPrimitive:
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 return "/p/48/";
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 case LowResolutionPrimitive:
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 return "/p/8/";
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 }
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 return "";
c2723022b173 Laid groundwork for library collection support.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117 }

mercurial