22 |
22 |
23 /** |
23 /** |
24 * @brief Constructs a new library manager |
24 * @brief Constructs a new library manager |
25 * @param parent Parent object |
25 * @param parent Parent object |
26 */ |
26 */ |
27 LibraryManager::LibraryManager(QObject* parent): |
27 LibrariesModel::LibrariesModel(QObject* parent): |
28 QAbstractTableModel{parent} |
28 QAbstractTableModel{parent} |
29 { |
29 { |
30 } |
30 } |
31 |
31 |
32 /** |
32 /** |
33 * @brief Yields a begin-terator for the libraries |
33 * @brief Yields a begin-terator for the libraries |
34 * @return iterator |
34 * @return iterator |
35 */ |
35 */ |
36 QVector<Library>::const_iterator LibraryManager::begin() const |
36 QVector<Library>::const_iterator LibrariesModel::begin() const |
37 { |
37 { |
38 return this->libraries.begin(); |
38 return this->libraries.begin(); |
39 } |
39 } |
40 |
40 |
41 /** |
41 /** |
42 * @brief Yields an end-iterator for the libraries |
42 * @brief Yields an end-iterator for the libraries |
43 * @return iterator |
43 * @return iterator |
44 */ |
44 */ |
45 QVector<Library>::const_iterator LibraryManager::end() const |
45 QVector<Library>::const_iterator LibrariesModel::end() const |
46 { |
46 { |
47 return this->libraries.end(); |
47 return this->libraries.end(); |
48 } |
48 } |
49 |
49 |
50 /** |
50 /** |
51 * @brief Searches the libraries for the specified file name. |
51 * @brief Searches the libraries for the specified file name. |
52 * @param fileName File to search for |
52 * @param fileName File to search for |
53 * @return Full path to the file, or empty string if not found. |
53 * @return Full path to the file, or empty string if not found. |
54 */ |
54 */ |
55 QString LibraryManager::findFile(QString fileName) const |
55 QString LibrariesModel::findFile(QString fileName) const |
56 { |
56 { |
57 QString path; |
57 QString path; |
58 fileName.replace("\\", "/"); |
58 fileName.replace("\\", "/"); |
59 bool found = false; |
59 bool found = false; |
60 for (const Library& library : this->libraries) |
60 for (const Library& library : this->libraries) |
81 |
81 |
82 /** |
82 /** |
83 * @brief Adds a new library to the end of the libraries list. |
83 * @brief Adds a new library to the end of the libraries list. |
84 * @param library Library to add |
84 * @param library Library to add |
85 */ |
85 */ |
86 void LibraryManager::addLibrary(const Library& library) |
86 void LibrariesModel::addLibrary(const Library& library) |
87 { |
87 { |
88 Q_EMIT layoutAboutToBeChanged(); |
88 Q_EMIT layoutAboutToBeChanged(); |
89 libraries.append(library); |
89 libraries.append(library); |
90 Q_EMIT layoutChanged(); |
90 Q_EMIT layoutChanged(); |
91 } |
91 } |
92 |
92 |
93 /** |
93 /** |
94 * @brief Removes a library by index. Does nothing if the index is not valid. |
94 * @brief Removes a library by index. Does nothing if the index is not valid. |
95 * @param libraryIndex Index of the library |
95 * @param libraryIndex Index of the library |
96 */ |
96 */ |
97 void LibraryManager::removeLibrary(const int libraryIndex) |
97 void LibrariesModel::removeLibrary(const int libraryIndex) |
98 { |
98 { |
99 Q_ASSERT(isValidIndex(libraryIndex)); |
99 Q_ASSERT(isValidIndex(libraryIndex)); |
100 if (isValidIndex(libraryIndex)) |
100 if (isValidIndex(libraryIndex)) |
101 { |
101 { |
102 Q_EMIT layoutAboutToBeChanged(); |
102 Q_EMIT layoutAboutToBeChanged(); |
109 * @brief Gets a library by index. |
109 * @brief Gets a library by index. |
110 * @warning Index is assumed to be valid. |
110 * @warning Index is assumed to be valid. |
111 * @param libraryIndex Index of the library |
111 * @param libraryIndex Index of the library |
112 * @return const reference |
112 * @return const reference |
113 */ |
113 */ |
114 const Library& LibraryManager::library(int libraryIndex) const |
114 const Library& LibrariesModel::library(int libraryIndex) const |
115 { |
115 { |
116 Q_ASSERT(isValidIndex(libraryIndex)); |
116 Q_ASSERT(isValidIndex(libraryIndex)); |
117 return this->libraries[libraryIndex]; |
117 return this->libraries[libraryIndex]; |
118 } |
118 } |
119 |
119 |
120 /** |
120 /** |
121 * @brief Changes the path of the specified library |
121 * @brief Changes the path of the specified library |
122 * @param libraryIndex Index of the library |
122 * @param libraryIndex Index of the library |
123 * @param path New path |
123 * @param path New path |
124 */ |
124 */ |
125 void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path) |
125 void LibrariesModel::setLibraryPath(int libraryIndex, const QDir& path) |
126 { |
126 { |
127 if (this->isValidIndex(libraryIndex)) |
127 if (this->isValidIndex(libraryIndex)) |
128 { |
128 { |
129 this->libraries[libraryIndex].path = path; |
129 this->libraries[libraryIndex].path = path; |
130 this->signalLibraryChange(libraryIndex); |
130 this->signalLibraryChange(libraryIndex); |
134 /** |
134 /** |
135 * @brief Changes the role of the specified library |
135 * @brief Changes the role of the specified library |
136 * @param libraryIndex Index of the library |
136 * @param libraryIndex Index of the library |
137 * @param role New role |
137 * @param role New role |
138 */ |
138 */ |
139 void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role) |
139 void LibrariesModel::setLibraryRole(int libraryIndex, const Library::Role role) |
140 { |
140 { |
141 if (this->isValidIndex(libraryIndex)) |
141 if (this->isValidIndex(libraryIndex)) |
142 { |
142 { |
143 this->libraries[libraryIndex].role = role; |
143 this->libraries[libraryIndex].role = role; |
144 this->signalLibraryChange(libraryIndex); |
144 this->signalLibraryChange(libraryIndex); |
148 /** |
148 /** |
149 * @brief Restores the libraries from the specified settings object. All unsaved |
149 * @brief Restores the libraries from the specified settings object. All unsaved |
150 * changes are lost. |
150 * changes are lost. |
151 * @param settings Settings object to restore from. |
151 * @param settings Settings object to restore from. |
152 */ |
152 */ |
153 void LibraryManager::restoreFromSettings() |
153 void LibrariesModel::restoreFromSettings() |
154 { |
154 { |
155 this->libraries = setting<Setting::Libraries>(); |
155 this->libraries = setting<Setting::Libraries>(); |
156 } |
156 } |
157 |
157 |
158 /** |
158 /** |
159 * @brief Saves the libraries to the specified settings object. |
159 * @brief Saves the libraries to the specified settings object. |
160 * @param settings Settings object to modify. |
160 * @param settings Settings object to modify. |
161 */ |
161 */ |
162 void LibraryManager::storeToSettings() |
162 void LibrariesModel::storeToSettings() |
163 { |
163 { |
164 setSetting<Setting::Libraries>(this->libraries); |
164 setSetting<Setting::Libraries>(this->libraries); |
165 } |
165 } |
166 |
166 |
167 /** |
167 /** |
168 * @returns the amount of libraries |
168 * @returns the amount of libraries |
169 */ |
169 */ |
170 int LibraryManager::count() const |
170 int LibrariesModel::count() const |
171 { |
171 { |
172 return this->libraries.size(); |
172 return this->libraries.size(); |
173 } |
173 } |
174 |
174 |
175 void LibraryManager::moveLibrary(const int libraryFromIndex, const int libraryToIndex) |
175 void LibrariesModel::moveLibrary(const int libraryFromIndex, const int libraryToIndex) |
176 { |
176 { |
177 if (isValidIndex(libraryFromIndex) and |
177 if (isValidIndex(libraryFromIndex) and |
178 (isValidIndex(libraryToIndex) or libraryToIndex == count()) and |
178 (isValidIndex(libraryToIndex) or libraryToIndex == count()) and |
179 libraryFromIndex != libraryToIndex) |
179 libraryFromIndex != libraryToIndex) |
180 { |
180 { |
197 /** |
197 /** |
198 * @brief Checks whether the specified index points to a valid library. |
198 * @brief Checks whether the specified index points to a valid library. |
199 * @param libraryIndex Index to check |
199 * @param libraryIndex Index to check |
200 * @returns whether or not it is valid |
200 * @returns whether or not it is valid |
201 */ |
201 */ |
202 bool LibraryManager::isValidIndex(const int libraryIndex) const |
202 bool LibrariesModel::isValidIndex(const int libraryIndex) const |
203 { |
203 { |
204 return libraryIndex >= 0 && libraryIndex < this->libraries.size(); |
204 return libraryIndex >= 0 && libraryIndex < this->libraries.size(); |
205 } |
205 } |
206 |
206 |
207 /** |
207 /** |
208 * @brief Iterates over libraries and loads LDConfig.ldr from each of them. |
208 * @brief Iterates over libraries and loads LDConfig.ldr from each of them. |
209 * @param errors Where to stream any encountered errors |
209 * @param errors Where to stream any encountered errors |
210 * @return color table |
210 * @return color table |
211 */ |
211 */ |
212 ColorTable LibraryManager::loadColorTable(QTextStream& errors) const |
212 ColorTable LibrariesModel::loadColorTable(QTextStream& errors) const |
213 { |
213 { |
214 ColorTable result; |
214 ColorTable result; |
215 for (const Library& library : this->libraries) |
215 for (const Library& library : this->libraries) |
216 { |
216 { |
217 const QString path = library.path.filePath("LDConfig.ldr"); |
217 const QString path = library.path.filePath("LDConfig.ldr"); |
236 QString Library::libraryRoleName(const Role role) |
236 QString Library::libraryRoleName(const Role role) |
237 { |
237 { |
238 switch (role) |
238 switch (role) |
239 { |
239 { |
240 case Library::OfficialLibrary: |
240 case Library::OfficialLibrary: |
241 return LibraryManager::tr("Official library"); |
241 return LibrariesModel::tr("Official library"); |
242 case Library::UnofficialLibrary: |
242 case Library::UnofficialLibrary: |
243 return LibraryManager::tr("Unofficial library"); |
243 return LibrariesModel::tr("Unofficial library"); |
244 case Library::WorkingLibrary: |
244 case Library::WorkingLibrary: |
245 return LibraryManager::tr("Working library"); |
245 return LibrariesModel::tr("Working library"); |
246 } |
246 } |
247 return "???"; |
247 return "???"; |
248 } |
248 } |
249 |
249 |
250 /** |
250 /** |
251 * @brief Overload necessary to implement QAbstractTableModel |
251 * @brief Overload necessary to implement QAbstractTableModel |
252 * @param index |
252 * @param index |
253 * @return Item flags |
253 * @return Item flags |
254 */ |
254 */ |
255 Qt::ItemFlags LibraryManager::flags(const QModelIndex& index) const |
255 Qt::ItemFlags LibrariesModel::flags(const QModelIndex& index) const |
256 { |
256 { |
257 Q_UNUSED(index); |
257 Q_UNUSED(index); |
258 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
258 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
259 } |
259 } |
260 |
260 |
263 * decides how data is represented in a table view. |
263 * decides how data is represented in a table view. |
264 * @param index Index to get data for |
264 * @param index Index to get data for |
265 * @param role Role of the data (role as in Qt model-view role, not LDraw library role) |
265 * @param role Role of the data (role as in Qt model-view role, not LDraw library role) |
266 * @returns variant |
266 * @returns variant |
267 */ |
267 */ |
268 QVariant LibraryManager::data(const QModelIndex& index, int role) const |
268 QVariant LibrariesModel::data(const QModelIndex& index, int role) const |
269 { |
269 { |
270 if (role != Qt::DisplayRole) |
270 if (role != Qt::DisplayRole) |
271 { |
271 { |
272 return {}; |
272 return {}; |
273 } |
273 } |
292 * @param section Index of the column (can also be a row but we only have column headers) |
292 * @param section Index of the column (can also be a row but we only have column headers) |
293 * @param orientation Orientation (we only support horizontal orientation) |
293 * @param orientation Orientation (we only support horizontal orientation) |
294 * @param role Role of the data (role as in Qt model-view role, not LDraw library role) |
294 * @param role Role of the data (role as in Qt model-view role, not LDraw library role) |
295 * @returns variant |
295 * @returns variant |
296 */ |
296 */ |
297 QVariant LibraryManager::headerData(int section, Qt::Orientation orientation, int role) const |
297 QVariant LibrariesModel::headerData(int section, Qt::Orientation orientation, int role) const |
298 { |
298 { |
299 if (orientation == Qt::Horizontal and role == Qt::DisplayRole) |
299 if (orientation == Qt::Horizontal and role == Qt::DisplayRole) |
300 { |
300 { |
301 switch(static_cast<Column>(section)) |
301 switch(static_cast<Column>(section)) |
302 { |
302 { |
316 /** |
316 /** |
317 * @brief Overload necessary to implement QAbstractTableModel. |
317 * @brief Overload necessary to implement QAbstractTableModel. |
318 * @returns how many rows there are in the table. Since there is one row per library, this returns |
318 * @returns how many rows there are in the table. Since there is one row per library, this returns |
319 * the amount of libraries. |
319 * the amount of libraries. |
320 */ |
320 */ |
321 int LibraryManager::rowCount(const QModelIndex&) const |
321 int LibrariesModel::rowCount(const QModelIndex&) const |
322 { |
322 { |
323 return this->count(); |
323 return this->count(); |
324 } |
324 } |
325 |
325 |
326 /** |
326 /** |
327 * @brief LibraryManager::columnCount |
327 * @brief LibraryManager::columnCount |
328 * @returns how many columns there are in the table. |
328 * @returns how many columns there are in the table. |
329 */ |
329 */ |
330 int LibraryManager::columnCount(const QModelIndex&) const |
330 int LibrariesModel::columnCount(const QModelIndex&) const |
331 { |
331 { |
332 return 2; |
332 return 2; |
333 } |
333 } |
334 |
334 |
335 /** |
335 /** |
336 * @brief Signals view objects that the specified library has changed. This is necessary |
336 * @brief Signals view objects that the specified library has changed. This is necessary |
337 * to update the table widget know when libraries are changed. |
337 * to update the table widget know when libraries are changed. |
338 * @param libraryIndex Index of the library. |
338 * @param libraryIndex Index of the library. |
339 */ |
339 */ |
340 void LibraryManager::signalLibraryChange(int libraryIndex) |
340 void LibrariesModel::signalLibraryChange(int libraryIndex) |
341 { |
341 { |
342 Q_ASSERT(isValidIndex(libraryIndex)); |
342 Q_ASSERT(isValidIndex(libraryIndex)); |
343 const QModelIndex topLeft = this->index(libraryIndex, 0); |
343 const QModelIndex topLeft = this->index(libraryIndex, 0); |
344 const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); |
344 const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); |
345 Q_EMIT dataChanged(topLeft, bottomRight); |
345 Q_EMIT dataChanged(topLeft, bottomRight); |