Sun, 19 Jan 2020 14:25:57 +0200
added debug and release to hgignore
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" | |
21 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
22 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
23 | * @brief Constructs a new library manager |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
24 | * @param parent Parent object |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
25 | */ |
7 | 26 | LibraryManager::LibraryManager(QObject* parent): |
27 | QAbstractTableModel{parent} | |
28 | { | |
29 | } | |
30 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
31 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
32 | * @brief Constructs a library manager from settings |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
33 | * @param settings Settings to construct from |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
34 | * @param parent Parent object |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
35 | */ |
7 | 36 | LibraryManager::LibraryManager(QSettings* settings, QObject* parent) : |
37 | QAbstractTableModel{parent} | |
38 | { | |
39 | this->restoreFromSettings(settings); | |
40 | } | |
41 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
42 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
43 | * @brief Yields a begin-terator for the libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
44 | * @return iterator |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
45 | */ |
7 | 46 | QVector<Library>::const_iterator LibraryManager::begin() const |
47 | { | |
48 | return this->libraries.begin(); | |
49 | } | |
50 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
51 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
52 | * @brief Yields an end-iterator for the libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
53 | * @return iterator |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
54 | */ |
7 | 55 | QVector<Library>::const_iterator LibraryManager::end() const |
56 | { | |
57 | return this->libraries.end(); | |
58 | } | |
59 | ||
60 | /** | |
61 | * @brief Searches the libraries for the specified file name. | |
62 | * @param fileName File to search for | |
63 | * @return Full path to the file, or empty string if not found. | |
64 | */ | |
12 | 65 | QString LibraryManager::findFile(QString fileName) const |
7 | 66 | { |
12 | 67 | QString path; |
7 | 68 | fileName.replace("\\", "/"); |
69 | bool found = false; | |
70 | for (const Library& library : this->libraries) | |
71 | { | |
12 | 72 | for (const QString& subdirectory : {"parts", "p"}) |
7 | 73 | { |
12 | 74 | QDir directory = library.path; |
75 | directory.cd(subdirectory); | |
76 | QFileInfo fileInfo = directory.absoluteFilePath(fileName); | |
77 | if (fileInfo.exists() && fileInfo.isFile()) | |
78 | { | |
79 | path = fileInfo.absoluteFilePath(); | |
80 | found = true; | |
81 | break; | |
82 | } | |
83 | } | |
84 | if (found) | |
85 | { | |
7 | 86 | break; |
87 | } | |
88 | } | |
89 | return path; | |
90 | } | |
91 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
92 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
93 | * @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
|
94 | * @param library Library to add |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
95 | */ |
7 | 96 | void LibraryManager::addLibrary(const Library& library) |
97 | { | |
98 | emit layoutAboutToBeChanged(); | |
99 | libraries.append(library); | |
100 | emit layoutChanged(); | |
101 | } | |
102 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
103 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
104 | * @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
|
105 | * @param libraryIndex Index of the library |
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 | void LibraryManager::removeLibrary(const int libraryIndex) |
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 | Q_ASSERT(isValidIndex(libraryIndex)); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
110 | if (isValidIndex(libraryIndex)) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
111 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
112 | emit layoutAboutToBeChanged(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
113 | libraries.remove(libraryIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
114 | emit layoutChanged(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
115 | } |
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 | * @brief Gets a library by index. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
120 | * @warning Index is assumed to be valid. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
121 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
122 | * @return const reference |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
123 | */ |
7 | 124 | const Library& LibraryManager::library(int libraryIndex) const |
125 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
126 | Q_ASSERT(isValidIndex(libraryIndex)); |
7 | 127 | return this->libraries[libraryIndex]; |
128 | } | |
129 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
130 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
131 | * @brief Changes the path of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
132 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
133 | * @param path New path |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
134 | */ |
7 | 135 | void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path) |
136 | { | |
137 | if (this->isValidIndex(libraryIndex)) | |
138 | { | |
139 | this->libraries[libraryIndex].path = path; | |
140 | this->signalLibraryChange(libraryIndex); | |
141 | } | |
142 | } | |
143 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
144 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
145 | * @brief Changes the role of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
146 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
147 | * @param role New role |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
148 | */ |
7 | 149 | void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role) |
150 | { | |
151 | if (this->isValidIndex(libraryIndex)) | |
152 | { | |
153 | this->libraries[libraryIndex].role = role; | |
154 | this->signalLibraryChange(libraryIndex); | |
155 | } | |
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 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
|
160 | * changes are lost. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
161 | * @param settings Settings object to restore from. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
162 | */ |
7 | 163 | void LibraryManager::restoreFromSettings(QSettings* settings) |
164 | { | |
165 | this->libraries = settings->value("libraries").value<Libraries>(); | |
166 | } | |
167 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
168 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
169 | * @brief Saves the libraries to the specified settings object. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
170 | * @param settings Settings object to modify. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
171 | */ |
7 | 172 | void LibraryManager::storeToSettings(QSettings* settings) |
173 | { | |
174 | QVariant librariesValue = QVariant::fromValue(this->libraries); | |
175 | settings->setValue("libraries", librariesValue); | |
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 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
192 | emit layoutAboutToBeChanged(); |
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 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
204 | emit layoutChanged(); |
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 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
219 | * @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
|
220 | * @param role Role to get a string for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
221 | * @returns string |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
222 | */ |
7 | 223 | QString Library::libraryRoleName(const Role role) |
224 | { | |
225 | switch (role) | |
226 | { | |
227 | case Library::OfficialLibrary: | |
228 | return LibraryManager::tr("Official library"); | |
229 | case Library::UnofficialLibrary: | |
230 | return LibraryManager::tr("Unofficial library"); | |
231 | case Library::WorkingLibrary: | |
232 | return LibraryManager::tr("Working library"); | |
233 | } | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
12
diff
changeset
|
234 | return "???"; |
7 | 235 | } |
236 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
237 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
238 | * @brief Overload necessary to implement QAbstractTableModel |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
239 | * @param index |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
240 | * @return Item flags |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
241 | */ |
7 | 242 | Qt::ItemFlags LibraryManager::flags(const QModelIndex& index) const |
243 | { | |
244 | Q_UNUSED(index); | |
245 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; | |
246 | } | |
247 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
248 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
249 | * @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
|
250 | * decides how data is represented in a table view. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
251 | * @param index Index to get data for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
252 | * @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
|
253 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
254 | */ |
7 | 255 | QVariant LibraryManager::data(const QModelIndex& index, int role) const |
256 | { | |
257 | if (role != Qt::DisplayRole) | |
258 | { | |
259 | return {}; | |
260 | } | |
261 | else | |
262 | { | |
263 | const int row = index.row(); | |
264 | const Column column = static_cast<Column>(index.column()); | |
265 | const Library& library = this->library(row); | |
266 | switch (column) | |
267 | { | |
268 | case RoleColumn: | |
269 | return Library::libraryRoleName(library.role); | |
270 | case PathColumn: | |
271 | return library.path.absolutePath(); | |
272 | } | |
273 | return {}; | |
274 | } | |
275 | } | |
276 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
277 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
278 | * @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
|
279 | * @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
|
280 | * @param orientation Orientation (we only support horizontal orientation) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
281 | * @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
|
282 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
283 | */ |
7 | 284 | QVariant LibraryManager::headerData(int section, Qt::Orientation orientation, int role) const |
285 | { | |
286 | if (orientation == Qt::Horizontal and role == Qt::DisplayRole) | |
287 | { | |
288 | switch(static_cast<Column>(section)) | |
289 | { | |
290 | case PathColumn: | |
291 | return tr("Path"); | |
292 | case RoleColumn: | |
293 | return tr("Role"); | |
294 | } | |
295 | return {}; | |
296 | } | |
297 | else | |
298 | { | |
299 | return {}; | |
300 | } | |
301 | } | |
302 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
303 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
304 | * @brief Overload necessary to implement QAbstractTableModel. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
305 | * @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
|
306 | * the amount of libraries. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
307 | */ |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
308 | int LibraryManager::rowCount(const QModelIndex&) const |
7 | 309 | { |
310 | return this->count(); | |
311 | } | |
312 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
313 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
314 | * @brief LibraryManager::columnCount |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
315 | * @returns how many columns there are in the table. |
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 | int LibraryManager::columnCount(const QModelIndex&) const |
7 | 318 | { |
319 | return 2; | |
320 | } | |
321 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
322 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
323 | * @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
|
324 | * 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
|
325 | * @param libraryIndex Index of the library. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
326 | */ |
7 | 327 | void LibraryManager::signalLibraryChange(int libraryIndex) |
328 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
329 | Q_ASSERT(isValidIndex(libraryIndex)); |
7 | 330 | const QModelIndex topLeft = this->index(libraryIndex, 0); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
331 | const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); |
7 | 332 | emit dataChanged(topLeft, bottomRight); |
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 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
|
337 | * @param stream Stream to write the library into |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
338 | * @param library Library to write into the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
339 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
340 | */ |
7 | 341 | QDataStream& operator<<(QDataStream& stream, const Library& library) |
342 | { | |
343 | const QString path = library.path.absolutePath(); | |
344 | const int role = static_cast<int>(library.role); | |
345 | return stream << path << role; | |
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 read the library from |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
351 | * @param library Library to obtain from 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, Library& library) |
355 | { | |
356 | QString path; | |
357 | int role; | |
358 | QDataStream& result = stream >> path >> role; | |
359 | library.path = path; | |
360 | library.role = static_cast<Library::Role>(role); | |
361 | return result; | |
362 | } |