Wed, 15 Jun 2022 13:05:53 +0300
rename LibraryManager -> LibrariesModel
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
7 | 19 | #include <QSettings> |
20 | #include "libraries.h" | |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
21 | #include "settings.h" |
7 | 22 | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
23 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
24 | * @brief Constructs a new library manager |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
25 | * @param parent Parent object |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
26 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
27 | LibrariesModel::LibrariesModel(QObject* parent): |
7 | 28 | QAbstractTableModel{parent} |
29 | { | |
30 | } | |
31 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
32 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
33 | * @brief Yields a begin-terator for the libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
34 | * @return iterator |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
35 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
36 | QVector<Library>::const_iterator LibrariesModel::begin() const |
7 | 37 | { |
38 | return this->libraries.begin(); | |
39 | } | |
40 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
41 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
42 | * @brief Yields an end-iterator for the libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
43 | * @return iterator |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
44 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
45 | QVector<Library>::const_iterator LibrariesModel::end() const |
7 | 46 | { |
47 | return this->libraries.end(); | |
48 | } | |
49 | ||
50 | /** | |
51 | * @brief Searches the libraries for the specified file name. | |
52 | * @param fileName File to search for | |
53 | * @return Full path to the file, or empty string if not found. | |
54 | */ | |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
55 | QString LibrariesModel::findFile(QString fileName) const |
7 | 56 | { |
12 | 57 | QString path; |
7 | 58 | fileName.replace("\\", "/"); |
59 | bool found = false; | |
60 | for (const Library& library : this->libraries) | |
61 | { | |
12 | 62 | for (const QString& subdirectory : {"parts", "p"}) |
7 | 63 | { |
12 | 64 | QDir directory = library.path; |
65 | directory.cd(subdirectory); | |
66 | QFileInfo fileInfo = directory.absoluteFilePath(fileName); | |
67 | if (fileInfo.exists() && fileInfo.isFile()) | |
68 | { | |
69 | path = fileInfo.absoluteFilePath(); | |
70 | found = true; | |
71 | break; | |
72 | } | |
73 | } | |
74 | if (found) | |
75 | { | |
7 | 76 | break; |
77 | } | |
78 | } | |
79 | return path; | |
80 | } | |
81 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
82 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
83 | * @brief Adds a new library to the end of the libraries list. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
84 | * @param library Library to add |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
85 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
86 | void LibrariesModel::addLibrary(const Library& library) |
7 | 87 | { |
112 | 88 | Q_EMIT layoutAboutToBeChanged(); |
7 | 89 | libraries.append(library); |
112 | 90 | Q_EMIT layoutChanged(); |
7 | 91 | } |
92 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
93 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
94 | * @brief Removes a library by index. Does nothing if the index is not valid. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
95 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
96 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
97 | void LibrariesModel::removeLibrary(const int libraryIndex) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
98 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
99 | Q_ASSERT(isValidIndex(libraryIndex)); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
100 | if (isValidIndex(libraryIndex)) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
101 | { |
112 | 102 | Q_EMIT layoutAboutToBeChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
103 | libraries.remove(libraryIndex); |
112 | 104 | Q_EMIT layoutChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
105 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
106 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
107 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
108 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
109 | * @brief Gets a library by index. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
110 | * @warning Index is assumed to be valid. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
111 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
112 | * @return const reference |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
113 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
114 | const Library& LibrariesModel::library(int libraryIndex) const |
7 | 115 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
116 | Q_ASSERT(isValidIndex(libraryIndex)); |
7 | 117 | return this->libraries[libraryIndex]; |
118 | } | |
119 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
120 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
121 | * @brief Changes the path of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
122 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
123 | * @param path New path |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
124 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
125 | void LibrariesModel::setLibraryPath(int libraryIndex, const QDir& path) |
7 | 126 | { |
127 | if (this->isValidIndex(libraryIndex)) | |
128 | { | |
129 | this->libraries[libraryIndex].path = path; | |
130 | this->signalLibraryChange(libraryIndex); | |
131 | } | |
132 | } | |
133 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
134 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
135 | * @brief Changes the role of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
136 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
137 | * @param role New role |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
138 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
139 | void LibrariesModel::setLibraryRole(int libraryIndex, const Library::Role role) |
7 | 140 | { |
141 | if (this->isValidIndex(libraryIndex)) | |
142 | { | |
143 | this->libraries[libraryIndex].role = role; | |
144 | this->signalLibraryChange(libraryIndex); | |
145 | } | |
146 | } | |
147 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
148 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
149 | * @brief Restores the libraries from the specified settings object. All unsaved |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
150 | * changes are lost. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
151 | * @param settings Settings object to restore from. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
152 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
153 | void LibrariesModel::restoreFromSettings() |
7 | 154 | { |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
155 | this->libraries = setting<Setting::Libraries>(); |
7 | 156 | } |
157 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
158 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
159 | * @brief Saves the libraries to the specified settings object. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
160 | * @param settings Settings object to modify. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
161 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
162 | void LibrariesModel::storeToSettings() |
7 | 163 | { |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
164 | setSetting<Setting::Libraries>(this->libraries); |
7 | 165 | } |
166 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
167 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
168 | * @returns the amount of libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
169 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
170 | int LibrariesModel::count() const |
7 | 171 | { |
172 | return this->libraries.size(); | |
173 | } | |
174 | ||
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
175 | void LibrariesModel::moveLibrary(const int libraryFromIndex, const int libraryToIndex) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
176 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
177 | if (isValidIndex(libraryFromIndex) and |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
178 | (isValidIndex(libraryToIndex) or libraryToIndex == count()) and |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
179 | libraryFromIndex != libraryToIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
180 | { |
112 | 181 | Q_EMIT layoutAboutToBeChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
182 | const Library library = this->library(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
183 | if (libraryToIndex > libraryFromIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
184 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
185 | this->libraries.insert(libraryToIndex, library); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
186 | this->libraries.removeAt(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
187 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
188 | else if (libraryToIndex < libraryFromIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
189 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
190 | this->libraries.removeAt(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
191 | this->libraries.insert(libraryToIndex, library); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
192 | } |
112 | 193 | Q_EMIT layoutChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
194 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
195 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
196 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
197 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
198 | * @brief Checks whether the specified index points to a valid library. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
199 | * @param libraryIndex Index to check |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
200 | * @returns whether or not it is valid |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
201 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
202 | bool LibrariesModel::isValidIndex(const int libraryIndex) const |
7 | 203 | { |
204 | return libraryIndex >= 0 && libraryIndex < this->libraries.size(); | |
205 | } | |
206 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
207 | /** |
26 | 208 | * @brief Iterates over libraries and loads LDConfig.ldr from each of them. |
209 | * @param errors Where to stream any encountered errors | |
210 | * @return color table | |
211 | */ | |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
212 | ColorTable LibrariesModel::loadColorTable(QTextStream& errors) const |
26 | 213 | { |
205 | 214 | ColorTable result; |
26 | 215 | for (const Library& library : this->libraries) |
216 | { | |
217 | const QString path = library.path.filePath("LDConfig.ldr"); | |
218 | QFile file{path}; | |
219 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) | |
220 | { | |
205 | 221 | const auto loadedTable = ::loadColorTable(file, errors); |
222 | if (loadedTable) { | |
223 | result = std::move(*loadedTable); | |
224 | } | |
225 | break; | |
26 | 226 | } |
227 | } | |
228 | return result; | |
229 | } | |
230 | ||
231 | /** | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
232 | * @brief Gets a human-readable string for the specified role |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
233 | * @param role Role to get a string for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
234 | * @returns string |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
235 | */ |
7 | 236 | QString Library::libraryRoleName(const Role role) |
237 | { | |
238 | switch (role) | |
239 | { | |
240 | case Library::OfficialLibrary: | |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
241 | return LibrariesModel::tr("Official library"); |
7 | 242 | case Library::UnofficialLibrary: |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
243 | return LibrariesModel::tr("Unofficial library"); |
7 | 244 | case Library::WorkingLibrary: |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
245 | return LibrariesModel::tr("Working library"); |
7 | 246 | } |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
12
diff
changeset
|
247 | return "???"; |
7 | 248 | } |
249 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
250 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
251 | * @brief Overload necessary to implement QAbstractTableModel |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
252 | * @param index |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
253 | * @return Item flags |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
254 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
255 | Qt::ItemFlags LibrariesModel::flags(const QModelIndex& index) const |
7 | 256 | { |
257 | Q_UNUSED(index); | |
258 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; | |
259 | } | |
260 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
261 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
262 | * @brief Returns data needed to represent the libraries in a table. This function |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
263 | * decides how data is represented in a table view. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
264 | * @param index Index to get data for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
265 | * @param role Role of the data (role as in Qt model-view role, not LDraw library role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
266 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
267 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
268 | QVariant LibrariesModel::data(const QModelIndex& index, int role) const |
7 | 269 | { |
270 | if (role != Qt::DisplayRole) | |
271 | { | |
272 | return {}; | |
273 | } | |
274 | else | |
275 | { | |
276 | const int row = index.row(); | |
277 | const Column column = static_cast<Column>(index.column()); | |
278 | const Library& library = this->library(row); | |
279 | switch (column) | |
280 | { | |
281 | case RoleColumn: | |
282 | return Library::libraryRoleName(library.role); | |
283 | case PathColumn: | |
284 | return library.path.absolutePath(); | |
285 | } | |
286 | return {}; | |
287 | } | |
288 | } | |
289 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
290 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
291 | * @brief Returns header texts for a table view. We only implement column headers here. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
292 | * @param section Index of the column (can also be a row but we only have column headers) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
293 | * @param orientation Orientation (we only support horizontal orientation) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
294 | * @param role Role of the data (role as in Qt model-view role, not LDraw library role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
295 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
296 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
297 | QVariant LibrariesModel::headerData(int section, Qt::Orientation orientation, int role) const |
7 | 298 | { |
299 | if (orientation == Qt::Horizontal and role == Qt::DisplayRole) | |
300 | { | |
301 | switch(static_cast<Column>(section)) | |
302 | { | |
303 | case PathColumn: | |
304 | return tr("Path"); | |
305 | case RoleColumn: | |
306 | return tr("Role"); | |
307 | } | |
308 | return {}; | |
309 | } | |
310 | else | |
311 | { | |
312 | return {}; | |
313 | } | |
314 | } | |
315 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
316 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
317 | * @brief Overload necessary to implement QAbstractTableModel. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
318 | * @returns how many rows there are in the table. Since there is one row per library, this returns |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
319 | * the amount of libraries. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
320 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
321 | int LibrariesModel::rowCount(const QModelIndex&) const |
7 | 322 | { |
323 | return this->count(); | |
324 | } | |
325 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
326 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
327 | * @brief LibraryManager::columnCount |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
328 | * @returns how many columns there are in the table. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
329 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
330 | int LibrariesModel::columnCount(const QModelIndex&) const |
7 | 331 | { |
332 | return 2; | |
333 | } | |
334 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
335 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
336 | * @brief Signals view objects that the specified library has changed. This is necessary |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
337 | * to update the table widget know when libraries are changed. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
338 | * @param libraryIndex Index of the library. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
339 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
340 | void LibrariesModel::signalLibraryChange(int libraryIndex) |
7 | 341 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
342 | Q_ASSERT(isValidIndex(libraryIndex)); |
7 | 343 | const QModelIndex topLeft = this->index(libraryIndex, 0); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
344 | const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); |
112 | 345 | Q_EMIT dataChanged(topLeft, bottomRight); |
7 | 346 | } |
347 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
348 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
349 | * @brief Overload for operator<< to allow serializing a library into the configuration file. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
350 | * @param stream Stream to write the library into |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
351 | * @param library Library to write into the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
352 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
353 | */ |
7 | 354 | QDataStream& operator<<(QDataStream& stream, const Library& library) |
355 | { | |
356 | const QString path = library.path.absolutePath(); | |
357 | const int role = static_cast<int>(library.role); | |
358 | return stream << path << role; | |
359 | } | |
360 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
361 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
362 | * @brief Overload for operator>> to allow serializing a library into the configuration file. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
363 | * @param stream Stream to read the library from |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
364 | * @param library Library to obtain from the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
365 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
366 | */ |
7 | 367 | QDataStream& operator>>(QDataStream& stream, Library& library) |
368 | { | |
369 | QString path; | |
370 | int role; | |
371 | QDataStream& result = stream >> path >> role; | |
100 | 372 | library.path.setPath(path); |
7 | 373 | library.role = static_cast<Library::Role>(role); |
374 | return result; | |
375 | } | |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
376 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
377 | bool operator==(const Library& one, const Library& other) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
378 | { |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
379 | return one.role == other.role and one.path == other.path; |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
380 | } |