1 #include <QSettings> |
1 #include <QSettings> |
2 #include "libraries.h" |
2 #include "libraries.h" |
3 |
3 |
|
4 /** |
|
5 * @brief Constructs a new library manager |
|
6 * @param parent Parent object |
|
7 */ |
4 LibraryManager::LibraryManager(QObject* parent): |
8 LibraryManager::LibraryManager(QObject* parent): |
5 QAbstractTableModel{parent} |
9 QAbstractTableModel{parent} |
6 { |
10 { |
7 } |
11 } |
8 |
12 |
|
13 /** |
|
14 * @brief Constructs a library manager from settings |
|
15 * @param settings Settings to construct from |
|
16 * @param parent Parent object |
|
17 */ |
9 LibraryManager::LibraryManager(QSettings* settings, QObject* parent) : |
18 LibraryManager::LibraryManager(QSettings* settings, QObject* parent) : |
10 QAbstractTableModel{parent} |
19 QAbstractTableModel{parent} |
11 { |
20 { |
12 this->restoreFromSettings(settings); |
21 this->restoreFromSettings(settings); |
13 } |
22 } |
14 |
23 |
|
24 /** |
|
25 * @brief Yields a begin-terator for the libraries |
|
26 * @return iterator |
|
27 */ |
15 QVector<Library>::const_iterator LibraryManager::begin() const |
28 QVector<Library>::const_iterator LibraryManager::begin() const |
16 { |
29 { |
17 return this->libraries.begin(); |
30 return this->libraries.begin(); |
18 } |
31 } |
19 |
32 |
|
33 /** |
|
34 * @brief Yields an end-iterator for the libraries |
|
35 * @return iterator |
|
36 */ |
20 QVector<Library>::const_iterator LibraryManager::end() const |
37 QVector<Library>::const_iterator LibraryManager::end() const |
21 { |
38 { |
22 return this->libraries.end(); |
39 return this->libraries.end(); |
23 } |
40 } |
24 |
41 |
44 if (not found) |
61 if (not found) |
45 path = {}; |
62 path = {}; |
46 return path; |
63 return path; |
47 } |
64 } |
48 |
65 |
|
66 /** |
|
67 * @brief Adds a new library to the end of the libraries list. |
|
68 * @param library Library to add |
|
69 */ |
49 void LibraryManager::addLibrary(const Library& library) |
70 void LibraryManager::addLibrary(const Library& library) |
50 { |
71 { |
51 emit layoutAboutToBeChanged(); |
72 emit layoutAboutToBeChanged(); |
52 libraries.append(library); |
73 libraries.append(library); |
53 emit layoutChanged(); |
74 emit layoutChanged(); |
54 } |
75 } |
55 |
76 |
|
77 /** |
|
78 * @brief Removes a library by index. Does nothing if the index is not valid. |
|
79 * @param libraryIndex Index of the library |
|
80 */ |
|
81 void LibraryManager::removeLibrary(const int libraryIndex) |
|
82 { |
|
83 Q_ASSERT(isValidIndex(libraryIndex)); |
|
84 if (isValidIndex(libraryIndex)) |
|
85 { |
|
86 emit layoutAboutToBeChanged(); |
|
87 libraries.remove(libraryIndex); |
|
88 emit layoutChanged(); |
|
89 } |
|
90 } |
|
91 |
|
92 /** |
|
93 * @brief Gets a library by index. |
|
94 * @warning Index is assumed to be valid. |
|
95 * @param libraryIndex Index of the library |
|
96 * @return const reference |
|
97 */ |
56 const Library& LibraryManager::library(int libraryIndex) const |
98 const Library& LibraryManager::library(int libraryIndex) const |
57 { |
99 { |
|
100 Q_ASSERT(isValidIndex(libraryIndex)); |
58 return this->libraries[libraryIndex]; |
101 return this->libraries[libraryIndex]; |
59 } |
102 } |
60 |
103 |
|
104 /** |
|
105 * @brief Changes the path of the specified library |
|
106 * @param libraryIndex Index of the library |
|
107 * @param path New path |
|
108 */ |
61 void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path) |
109 void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path) |
62 { |
110 { |
63 if (this->isValidIndex(libraryIndex)) |
111 if (this->isValidIndex(libraryIndex)) |
64 { |
112 { |
65 this->libraries[libraryIndex].path = path; |
113 this->libraries[libraryIndex].path = path; |
66 this->signalLibraryChange(libraryIndex); |
114 this->signalLibraryChange(libraryIndex); |
67 } |
115 } |
68 } |
116 } |
69 |
117 |
|
118 /** |
|
119 * @brief Changes the role of the specified library |
|
120 * @param libraryIndex Index of the library |
|
121 * @param role New role |
|
122 */ |
70 void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role) |
123 void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role) |
71 { |
124 { |
72 if (this->isValidIndex(libraryIndex)) |
125 if (this->isValidIndex(libraryIndex)) |
73 { |
126 { |
74 this->libraries[libraryIndex].role = role; |
127 this->libraries[libraryIndex].role = role; |
75 this->signalLibraryChange(libraryIndex); |
128 this->signalLibraryChange(libraryIndex); |
76 } |
129 } |
77 } |
130 } |
78 |
131 |
|
132 /** |
|
133 * @brief Restores the libraries from the specified settings object. All unsaved |
|
134 * changes are lost. |
|
135 * @param settings Settings object to restore from. |
|
136 */ |
79 void LibraryManager::restoreFromSettings(QSettings* settings) |
137 void LibraryManager::restoreFromSettings(QSettings* settings) |
80 { |
138 { |
81 this->libraries = settings->value("libraries").value<Libraries>(); |
139 this->libraries = settings->value("libraries").value<Libraries>(); |
82 } |
140 } |
83 |
141 |
|
142 /** |
|
143 * @brief Saves the libraries to the specified settings object. |
|
144 * @param settings Settings object to modify. |
|
145 */ |
84 void LibraryManager::storeToSettings(QSettings* settings) |
146 void LibraryManager::storeToSettings(QSettings* settings) |
85 { |
147 { |
86 QVariant librariesValue = QVariant::fromValue(this->libraries); |
148 QVariant librariesValue = QVariant::fromValue(this->libraries); |
87 settings->setValue("libraries", librariesValue); |
149 settings->setValue("libraries", librariesValue); |
88 } |
150 } |
89 |
151 |
|
152 /** |
|
153 * @returns the amount of libraries |
|
154 */ |
90 int LibraryManager::count() const |
155 int LibraryManager::count() const |
91 { |
156 { |
92 return this->libraries.size(); |
157 return this->libraries.size(); |
93 } |
158 } |
94 |
159 |
95 bool LibraryManager::isValidIndex(int libraryIndex) |
160 void LibraryManager::moveLibrary(const int libraryFromIndex, const int libraryToIndex) |
|
161 { |
|
162 if (isValidIndex(libraryFromIndex) and |
|
163 (isValidIndex(libraryToIndex) or libraryToIndex == count()) and |
|
164 libraryFromIndex != libraryToIndex) |
|
165 { |
|
166 emit layoutAboutToBeChanged(); |
|
167 const Library library = this->library(libraryFromIndex); |
|
168 if (libraryToIndex > libraryFromIndex) |
|
169 { |
|
170 this->libraries.insert(libraryToIndex, library); |
|
171 this->libraries.removeAt(libraryFromIndex); |
|
172 } |
|
173 else if (libraryToIndex < libraryFromIndex) |
|
174 { |
|
175 this->libraries.removeAt(libraryFromIndex); |
|
176 this->libraries.insert(libraryToIndex, library); |
|
177 } |
|
178 emit layoutChanged(); |
|
179 } |
|
180 } |
|
181 |
|
182 /** |
|
183 * @brief Checks whether the specified index points to a valid library. |
|
184 * @param libraryIndex Index to check |
|
185 * @returns whether or not it is valid |
|
186 */ |
|
187 bool LibraryManager::isValidIndex(const int libraryIndex) const |
96 { |
188 { |
97 return libraryIndex >= 0 && libraryIndex < this->libraries.size(); |
189 return libraryIndex >= 0 && libraryIndex < this->libraries.size(); |
98 } |
190 } |
99 |
191 |
|
192 /** |
|
193 * @brief Gets a human-readable string for the specified role |
|
194 * @param role Role to get a string for |
|
195 * @returns string |
|
196 */ |
100 QString Library::libraryRoleName(const Role role) |
197 QString Library::libraryRoleName(const Role role) |
101 { |
198 { |
102 switch (role) |
199 switch (role) |
103 { |
200 { |
104 case Library::OfficialLibrary: |
201 case Library::OfficialLibrary: |
157 { |
273 { |
158 return {}; |
274 return {}; |
159 } |
275 } |
160 } |
276 } |
161 |
277 |
162 int LibraryManager::rowCount(const QModelIndex& parent) const |
278 /** |
163 { |
279 * @brief Overload necessary to implement QAbstractTableModel. |
164 Q_UNUSED(parent) |
280 * @returns how many rows there are in the table. Since there is one row per library, this returns |
|
281 * the amount of libraries. |
|
282 */ |
|
283 int LibraryManager::rowCount(const QModelIndex&) const |
|
284 { |
165 return this->count(); |
285 return this->count(); |
166 } |
286 } |
167 |
287 |
168 int LibraryManager::columnCount(const QModelIndex& parent) const |
288 /** |
169 { |
289 * @brief LibraryManager::columnCount |
170 Q_UNUSED(parent) |
290 * @returns how many columns there are in the table. |
|
291 */ |
|
292 int LibraryManager::columnCount(const QModelIndex&) const |
|
293 { |
171 return 2; |
294 return 2; |
172 } |
295 } |
173 |
296 |
|
297 /** |
|
298 * @brief Signals view objects that the specified library has changed. This is necessary |
|
299 * to update the table widget know when libraries are changed. |
|
300 * @param libraryIndex Index of the library. |
|
301 */ |
174 void LibraryManager::signalLibraryChange(int libraryIndex) |
302 void LibraryManager::signalLibraryChange(int libraryIndex) |
175 { |
303 { |
|
304 Q_ASSERT(isValidIndex(libraryIndex)); |
176 const QModelIndex topLeft = this->index(libraryIndex, 0); |
305 const QModelIndex topLeft = this->index(libraryIndex, 0); |
177 const QModelIndex bottomRight = this->index(libraryIndex, columnCount() - 1); |
306 const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); |
178 emit dataChanged(topLeft, bottomRight); |
307 emit dataChanged(topLeft, bottomRight); |
179 } |
308 } |
180 |
309 |
|
310 /** |
|
311 * @brief Overload for operator<< to allow serializing a library into the configuration file. |
|
312 * @param stream Stream to write the library into |
|
313 * @param library Library to write into the stream |
|
314 * @returns the stream |
|
315 */ |
181 QDataStream& operator<<(QDataStream& stream, const Library& library) |
316 QDataStream& operator<<(QDataStream& stream, const Library& library) |
182 { |
317 { |
183 const QString path = library.path.absolutePath(); |
318 const QString path = library.path.absolutePath(); |
184 const int role = static_cast<int>(library.role); |
319 const int role = static_cast<int>(library.role); |
185 return stream << path << role; |
320 return stream << path << role; |
186 } |
321 } |
187 |
322 |
|
323 /** |
|
324 * @brief Overload for operator>> to allow serializing a library into the configuration file. |
|
325 * @param stream Stream to read the library from |
|
326 * @param library Library to obtain from the stream |
|
327 * @returns the stream |
|
328 */ |
188 QDataStream& operator>>(QDataStream& stream, Library& library) |
329 QDataStream& operator>>(QDataStream& stream, Library& library) |
189 { |
330 { |
190 QString path; |
331 QString path; |
191 int role; |
332 int role; |
192 QDataStream& result = stream >> path >> role; |
333 QDataStream& result = stream >> path >> role; |