Wed, 08 Jun 2022 20:41:21 +0300
Refactor colors.cpp/.h
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" | |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
21 | #include "configuration.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 | */ |
7 | 27 | LibraryManager::LibraryManager(QObject* parent): |
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 Constructs a library manager from settings |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
34 | * @param settings Settings to construct from |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
35 | * @param parent Parent object |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
36 | */ |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
37 | LibraryManager::LibraryManager(Configuration* settings, QObject* parent) : |
7 | 38 | QAbstractTableModel{parent} |
39 | { | |
40 | this->restoreFromSettings(settings); | |
41 | } | |
42 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
43 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
44 | * @brief Yields a begin-terator for the libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
45 | * @return iterator |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
46 | */ |
7 | 47 | QVector<Library>::const_iterator LibraryManager::begin() const |
48 | { | |
49 | return this->libraries.begin(); | |
50 | } | |
51 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
52 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
53 | * @brief Yields an end-iterator for the libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
54 | * @return iterator |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
55 | */ |
7 | 56 | QVector<Library>::const_iterator LibraryManager::end() const |
57 | { | |
58 | return this->libraries.end(); | |
59 | } | |
60 | ||
61 | /** | |
62 | * @brief Searches the libraries for the specified file name. | |
63 | * @param fileName File to search for | |
64 | * @return Full path to the file, or empty string if not found. | |
65 | */ | |
12 | 66 | QString LibraryManager::findFile(QString fileName) const |
7 | 67 | { |
12 | 68 | QString path; |
7 | 69 | fileName.replace("\\", "/"); |
70 | bool found = false; | |
71 | for (const Library& library : this->libraries) | |
72 | { | |
12 | 73 | for (const QString& subdirectory : {"parts", "p"}) |
7 | 74 | { |
12 | 75 | QDir directory = library.path; |
76 | directory.cd(subdirectory); | |
77 | QFileInfo fileInfo = directory.absoluteFilePath(fileName); | |
78 | if (fileInfo.exists() && fileInfo.isFile()) | |
79 | { | |
80 | path = fileInfo.absoluteFilePath(); | |
81 | found = true; | |
82 | break; | |
83 | } | |
84 | } | |
85 | if (found) | |
86 | { | |
7 | 87 | break; |
88 | } | |
89 | } | |
90 | return path; | |
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 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
|
95 | * @param library Library to add |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
96 | */ |
7 | 97 | void LibraryManager::addLibrary(const Library& library) |
98 | { | |
112 | 99 | Q_EMIT layoutAboutToBeChanged(); |
7 | 100 | libraries.append(library); |
112 | 101 | Q_EMIT layoutChanged(); |
7 | 102 | } |
103 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
104 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
105 | * @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
|
106 | * @param libraryIndex Index of the library |
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 | void LibraryManager::removeLibrary(const int libraryIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
109 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
110 | Q_ASSERT(isValidIndex(libraryIndex)); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
111 | if (isValidIndex(libraryIndex)) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
112 | { |
112 | 113 | Q_EMIT layoutAboutToBeChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
114 | libraries.remove(libraryIndex); |
112 | 115 | Q_EMIT layoutChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
116 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
117 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
118 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
119 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
120 | * @brief Gets a library by index. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
121 | * @warning Index is assumed to be valid. |
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 | * @return const reference |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
124 | */ |
7 | 125 | const Library& LibraryManager::library(int libraryIndex) const |
126 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
127 | Q_ASSERT(isValidIndex(libraryIndex)); |
7 | 128 | return this->libraries[libraryIndex]; |
129 | } | |
130 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
131 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
132 | * @brief Changes the path of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
133 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
134 | * @param path New path |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
135 | */ |
7 | 136 | void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path) |
137 | { | |
138 | if (this->isValidIndex(libraryIndex)) | |
139 | { | |
140 | this->libraries[libraryIndex].path = path; | |
141 | this->signalLibraryChange(libraryIndex); | |
142 | } | |
143 | } | |
144 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
145 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
146 | * @brief Changes the role of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
147 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
148 | * @param role New role |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
149 | */ |
7 | 150 | void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role) |
151 | { | |
152 | if (this->isValidIndex(libraryIndex)) | |
153 | { | |
154 | this->libraries[libraryIndex].role = role; | |
155 | this->signalLibraryChange(libraryIndex); | |
156 | } | |
157 | } | |
158 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
159 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
160 | * @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
|
161 | * changes are lost. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
162 | * @param settings Settings object to restore from. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
163 | */ |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
164 | void LibraryManager::restoreFromSettings(Configuration* settings) |
7 | 165 | { |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
166 | this->libraries = settings->libraries(); |
7 | 167 | } |
168 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
169 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
170 | * @brief Saves the libraries to the specified settings object. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
171 | * @param settings Settings object to modify. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
172 | */ |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
173 | void LibraryManager::storeToSettings(Configuration* settings) |
7 | 174 | { |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
175 | settings->setLibraries(this->libraries); |
7 | 176 | } |
177 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
178 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
179 | * @returns the amount of libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
180 | */ |
7 | 181 | int LibraryManager::count() const |
182 | { | |
183 | return this->libraries.size(); | |
184 | } | |
185 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
186 | void LibraryManager::moveLibrary(const int libraryFromIndex, const int libraryToIndex) |
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 | if (isValidIndex(libraryFromIndex) and |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
189 | (isValidIndex(libraryToIndex) or libraryToIndex == count()) and |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
190 | libraryFromIndex != libraryToIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
191 | { |
112 | 192 | Q_EMIT layoutAboutToBeChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
193 | const Library library = this->library(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
194 | if (libraryToIndex > libraryFromIndex) |
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 | this->libraries.insert(libraryToIndex, library); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
197 | this->libraries.removeAt(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
198 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
199 | else if (libraryToIndex < libraryFromIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
200 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
201 | this->libraries.removeAt(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
202 | this->libraries.insert(libraryToIndex, library); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
203 | } |
112 | 204 | Q_EMIT layoutChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
205 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
206 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
207 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
208 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
209 | * @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
|
210 | * @param libraryIndex Index to check |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
211 | * @returns whether or not it is valid |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
212 | */ |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
213 | bool LibraryManager::isValidIndex(const int libraryIndex) const |
7 | 214 | { |
215 | return libraryIndex >= 0 && libraryIndex < this->libraries.size(); | |
216 | } | |
217 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
218 | /** |
26 | 219 | * @brief Iterates over libraries and loads LDConfig.ldr from each of them. |
220 | * @param errors Where to stream any encountered errors | |
221 | * @return color table | |
222 | */ | |
205 | 223 | ColorTable LibraryManager::loadColorTable(QTextStream& errors) const |
26 | 224 | { |
205 | 225 | ColorTable result; |
26 | 226 | for (const Library& library : this->libraries) |
227 | { | |
228 | const QString path = library.path.filePath("LDConfig.ldr"); | |
229 | QFile file{path}; | |
230 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) | |
231 | { | |
205 | 232 | const auto loadedTable = ::loadColorTable(file, errors); |
233 | if (loadedTable) { | |
234 | result = std::move(*loadedTable); | |
235 | } | |
236 | break; | |
26 | 237 | } |
238 | } | |
239 | return result; | |
240 | } | |
241 | ||
242 | /** | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
243 | * @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
|
244 | * @param role Role to get a string for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
245 | * @returns string |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
246 | */ |
7 | 247 | QString Library::libraryRoleName(const Role role) |
248 | { | |
249 | switch (role) | |
250 | { | |
251 | case Library::OfficialLibrary: | |
252 | return LibraryManager::tr("Official library"); | |
253 | case Library::UnofficialLibrary: | |
254 | return LibraryManager::tr("Unofficial library"); | |
255 | case Library::WorkingLibrary: | |
256 | return LibraryManager::tr("Working library"); | |
257 | } | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
12
diff
changeset
|
258 | return "???"; |
7 | 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 Overload necessary to implement QAbstractTableModel |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
263 | * @param index |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
264 | * @return Item flags |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
265 | */ |
7 | 266 | Qt::ItemFlags LibraryManager::flags(const QModelIndex& index) const |
267 | { | |
268 | Q_UNUSED(index); | |
269 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; | |
270 | } | |
271 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
272 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
273 | * @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
|
274 | * decides how data is represented in a table view. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
275 | * @param index Index to get data for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
276 | * @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
|
277 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
278 | */ |
7 | 279 | QVariant LibraryManager::data(const QModelIndex& index, int role) const |
280 | { | |
281 | if (role != Qt::DisplayRole) | |
282 | { | |
283 | return {}; | |
284 | } | |
285 | else | |
286 | { | |
287 | const int row = index.row(); | |
288 | const Column column = static_cast<Column>(index.column()); | |
289 | const Library& library = this->library(row); | |
290 | switch (column) | |
291 | { | |
292 | case RoleColumn: | |
293 | return Library::libraryRoleName(library.role); | |
294 | case PathColumn: | |
295 | return library.path.absolutePath(); | |
296 | } | |
297 | return {}; | |
298 | } | |
299 | } | |
300 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
301 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
302 | * @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
|
303 | * @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
|
304 | * @param orientation Orientation (we only support horizontal orientation) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
305 | * @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
|
306 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
307 | */ |
7 | 308 | QVariant LibraryManager::headerData(int section, Qt::Orientation orientation, int role) const |
309 | { | |
310 | if (orientation == Qt::Horizontal and role == Qt::DisplayRole) | |
311 | { | |
312 | switch(static_cast<Column>(section)) | |
313 | { | |
314 | case PathColumn: | |
315 | return tr("Path"); | |
316 | case RoleColumn: | |
317 | return tr("Role"); | |
318 | } | |
319 | return {}; | |
320 | } | |
321 | else | |
322 | { | |
323 | return {}; | |
324 | } | |
325 | } | |
326 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
327 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
328 | * @brief Overload necessary to implement QAbstractTableModel. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
329 | * @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
|
330 | * the amount of libraries. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
331 | */ |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
332 | int LibraryManager::rowCount(const QModelIndex&) const |
7 | 333 | { |
334 | return this->count(); | |
335 | } | |
336 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
337 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
338 | * @brief LibraryManager::columnCount |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
339 | * @returns how many columns there are in the table. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
340 | */ |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
341 | int LibraryManager::columnCount(const QModelIndex&) const |
7 | 342 | { |
343 | return 2; | |
344 | } | |
345 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
346 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
347 | * @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
|
348 | * 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
|
349 | * @param libraryIndex Index of the library. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
350 | */ |
7 | 351 | void LibraryManager::signalLibraryChange(int libraryIndex) |
352 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
353 | Q_ASSERT(isValidIndex(libraryIndex)); |
7 | 354 | const QModelIndex topLeft = this->index(libraryIndex, 0); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
355 | const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); |
112 | 356 | Q_EMIT dataChanged(topLeft, bottomRight); |
7 | 357 | } |
358 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
359 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
360 | * @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
|
361 | * @param stream Stream to write the library into |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
362 | * @param library Library to write into the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
363 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
364 | */ |
7 | 365 | QDataStream& operator<<(QDataStream& stream, const Library& library) |
366 | { | |
367 | const QString path = library.path.absolutePath(); | |
368 | const int role = static_cast<int>(library.role); | |
369 | return stream << path << role; | |
370 | } | |
371 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
372 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
373 | * @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
|
374 | * @param stream Stream to read the library from |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
375 | * @param library Library to obtain from the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
376 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
377 | */ |
7 | 378 | QDataStream& operator>>(QDataStream& stream, Library& library) |
379 | { | |
380 | QString path; | |
381 | int role; | |
382 | QDataStream& result = stream >> path >> role; | |
100 | 383 | library.path.setPath(path); |
7 | 384 | library.role = static_cast<Library::Role>(role); |
385 | return result; | |
386 | } | |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
387 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
388 | bool operator==(const Library& one, const Library& other) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
389 | { |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
390 | return one.role == other.role and one.path == other.path; |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
391 | } |