Sun, 03 Jul 2022 21:47:44 +0300
Fix normal of the plane used to find cylinder height
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> |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
20 | #include "src/libraries.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
21 | #include "src/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 | /** |
7 | 33 | * @brief Searches the libraries for the specified file name. |
34 | * @param fileName File to search for | |
35 | * @return Full path to the file, or empty string if not found. | |
36 | */ | |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
37 | QString LibrariesModel::findFile(QString fileName) const |
7 | 38 | { |
12 | 39 | QString path; |
7 | 40 | fileName.replace("\\", "/"); |
41 | bool found = false; | |
42 | for (const Library& library : this->libraries) | |
43 | { | |
12 | 44 | for (const QString& subdirectory : {"parts", "p"}) |
7 | 45 | { |
12 | 46 | QDir directory = library.path; |
47 | directory.cd(subdirectory); | |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
48 | QFileInfo fileInfo{directory.absoluteFilePath(fileName)}; |
12 | 49 | if (fileInfo.exists() && fileInfo.isFile()) |
50 | { | |
51 | path = fileInfo.absoluteFilePath(); | |
52 | found = true; | |
53 | break; | |
54 | } | |
55 | } | |
56 | if (found) | |
57 | { | |
7 | 58 | break; |
59 | } | |
60 | } | |
61 | return path; | |
62 | } | |
63 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
64 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
65 | * @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
|
66 | * @param library Library to add |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
67 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
68 | void LibrariesModel::addLibrary(const Library& library) |
7 | 69 | { |
112 | 70 | Q_EMIT layoutAboutToBeChanged(); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
71 | this->libraries.push_back(library); |
112 | 72 | Q_EMIT layoutChanged(); |
7 | 73 | } |
74 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
75 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
76 | * @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
|
77 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
78 | */ |
300
3a4b132b8353
Fix build warnings, size_type of QVector changes from Qt5 to Qt6 so we need an alias for it
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
265
diff
changeset
|
79 | void LibrariesModel::removeLibrary(const index_t libraryIndex) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
80 | { |
265 | 81 | Q_ASSERT(this->isValidIndex(libraryIndex)); |
82 | if (this->isValidIndex(libraryIndex)) | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
83 | { |
112 | 84 | Q_EMIT layoutAboutToBeChanged(); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
85 | this->libraries.erase(this->libraries.begin() + static_cast<long>(libraryIndex)); |
112 | 86 | Q_EMIT layoutChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
87 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
88 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
89 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
90 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
91 | * @brief Gets a library by index. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
92 | * @warning Index is assumed to be valid. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
93 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
94 | * @return const reference |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
95 | */ |
300
3a4b132b8353
Fix build warnings, size_type of QVector changes from Qt5 to Qt6 so we need an alias for it
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
265
diff
changeset
|
96 | const Library& LibrariesModel::library(index_t libraryIndex) const |
7 | 97 | { |
265 | 98 | Q_ASSERT(this->isValidIndex(libraryIndex)); |
7 | 99 | return this->libraries[libraryIndex]; |
100 | } | |
101 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
102 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
103 | * @brief Changes the path of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
104 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
105 | * @param path New path |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
106 | */ |
300
3a4b132b8353
Fix build warnings, size_type of QVector changes from Qt5 to Qt6 so we need an alias for it
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
265
diff
changeset
|
107 | void LibrariesModel::setLibraryPath(index_t libraryIndex, const QDir& path) |
7 | 108 | { |
109 | if (this->isValidIndex(libraryIndex)) | |
110 | { | |
111 | this->libraries[libraryIndex].path = path; | |
112 | this->signalLibraryChange(libraryIndex); | |
113 | } | |
114 | } | |
115 | ||
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 | * @brief Changes the role of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
118 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
119 | * @param role New role |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
120 | */ |
300
3a4b132b8353
Fix build warnings, size_type of QVector changes from Qt5 to Qt6 so we need an alias for it
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
265
diff
changeset
|
121 | void LibrariesModel::setLibraryRole(index_t libraryIndex, const Library::Role role) |
7 | 122 | { |
123 | if (this->isValidIndex(libraryIndex)) | |
124 | { | |
125 | this->libraries[libraryIndex].role = role; | |
126 | this->signalLibraryChange(libraryIndex); | |
127 | } | |
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 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
|
132 | * changes are lost. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
133 | * @param settings Settings object to restore from. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
134 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
135 | void LibrariesModel::restoreFromSettings() |
7 | 136 | { |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
137 | this->libraries = setting<Setting::Libraries>(); |
7 | 138 | } |
139 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
140 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
141 | * @brief Saves the libraries to the specified settings object. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
142 | * @param settings Settings object to modify. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
143 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
144 | void LibrariesModel::storeToSettings() |
7 | 145 | { |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
146 | setSetting<Setting::Libraries>(this->libraries); |
7 | 147 | } |
148 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
149 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
150 | * @returns the amount of libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
151 | */ |
300
3a4b132b8353
Fix build warnings, size_type of QVector changes from Qt5 to Qt6 so we need an alias for it
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
265
diff
changeset
|
152 | index_t LibrariesModel::count() const |
7 | 153 | { |
154 | return this->libraries.size(); | |
155 | } | |
156 | ||
300
3a4b132b8353
Fix build warnings, size_type of QVector changes from Qt5 to Qt6 so we need an alias for it
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
265
diff
changeset
|
157 | void LibrariesModel::moveLibrary(const index_t libraryFromIndex, const index_t libraryToIndex) |
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 | if (isValidIndex(libraryFromIndex) and |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
160 | (isValidIndex(libraryToIndex) or libraryToIndex == count()) and |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
161 | libraryFromIndex != libraryToIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
162 | { |
112 | 163 | Q_EMIT layoutAboutToBeChanged(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
164 | const Library library = this->library(libraryFromIndex); |
265 | 165 | if (libraryToIndex > libraryFromIndex) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
166 | { |
265 | 167 | this->libraries.insert(libraryToIndex, library); |
168 | this->libraries.removeAt(libraryFromIndex); | |
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 | else if (libraryToIndex < libraryFromIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
171 | { |
265 | 172 | this->libraries.removeAt(libraryFromIndex); |
173 | this->libraries.insert(libraryToIndex, library); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
174 | } |
112 | 175 | Q_EMIT layoutChanged(); |
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 | } |
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 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
180 | * @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
|
181 | * @param libraryIndex Index to check |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
182 | * @returns whether or not it is valid |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
183 | */ |
300
3a4b132b8353
Fix build warnings, size_type of QVector changes from Qt5 to Qt6 so we need an alias for it
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
265
diff
changeset
|
184 | bool LibrariesModel::isValidIndex(const index_t libraryIndex) const |
7 | 185 | { |
186 | return libraryIndex >= 0 && libraryIndex < this->libraries.size(); | |
187 | } | |
188 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
189 | /** |
26 | 190 | * @brief Iterates over libraries and loads LDConfig.ldr from each of them. |
191 | * @param errors Where to stream any encountered errors | |
192 | * @return color table | |
193 | */ | |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
194 | ColorTable LibrariesModel::loadColorTable(QTextStream& errors) const |
26 | 195 | { |
205 | 196 | ColorTable result; |
26 | 197 | for (const Library& library : this->libraries) |
198 | { | |
199 | const QString path = library.path.filePath("LDConfig.ldr"); | |
200 | QFile file{path}; | |
201 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) | |
202 | { | |
205 | 203 | const auto loadedTable = ::loadColorTable(file, errors); |
204 | if (loadedTable) { | |
205 | result = std::move(*loadedTable); | |
206 | } | |
207 | break; | |
26 | 208 | } |
209 | } | |
210 | return result; | |
211 | } | |
212 | ||
213 | /** | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
214 | * @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
|
215 | * @param role Role to get a string for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
216 | * @returns string |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
217 | */ |
7 | 218 | QString Library::libraryRoleName(const Role role) |
219 | { | |
220 | switch (role) | |
221 | { | |
222 | case Library::OfficialLibrary: | |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
223 | return LibrariesModel::tr("Official library"); |
7 | 224 | case Library::UnofficialLibrary: |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
225 | return LibrariesModel::tr("Unofficial library"); |
7 | 226 | case Library::WorkingLibrary: |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
227 | return LibrariesModel::tr("Working library"); |
7 | 228 | } |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
12
diff
changeset
|
229 | return "???"; |
7 | 230 | } |
231 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
232 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
233 | * @brief Overload necessary to implement QAbstractTableModel |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
234 | * @param index |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
235 | * @return Item flags |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
236 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
237 | Qt::ItemFlags LibrariesModel::flags(const QModelIndex& index) const |
7 | 238 | { |
239 | Q_UNUSED(index); | |
240 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; | |
241 | } | |
242 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
243 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
244 | * @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
|
245 | * decides how data is represented in a table view. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
246 | * @param index Index to get data for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
247 | * @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
|
248 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
249 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
250 | QVariant LibrariesModel::data(const QModelIndex& index, int role) const |
7 | 251 | { |
252 | if (role != Qt::DisplayRole) | |
253 | { | |
254 | return {}; | |
255 | } | |
256 | else | |
257 | { | |
258 | const Column column = static_cast<Column>(index.column()); | |
265 | 259 | const Library& library = this->library(index.row()); |
7 | 260 | switch (column) |
261 | { | |
262 | case RoleColumn: | |
263 | return Library::libraryRoleName(library.role); | |
264 | case PathColumn: | |
265 | return library.path.absolutePath(); | |
266 | } | |
267 | return {}; | |
268 | } | |
269 | } | |
270 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
271 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
272 | * @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
|
273 | * @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
|
274 | * @param orientation Orientation (we only support horizontal orientation) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
275 | * @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
|
276 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
277 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
278 | QVariant LibrariesModel::headerData(int section, Qt::Orientation orientation, int role) const |
7 | 279 | { |
280 | if (orientation == Qt::Horizontal and role == Qt::DisplayRole) | |
281 | { | |
282 | switch(static_cast<Column>(section)) | |
283 | { | |
284 | case PathColumn: | |
285 | return tr("Path"); | |
286 | case RoleColumn: | |
287 | return tr("Role"); | |
288 | } | |
289 | return {}; | |
290 | } | |
291 | else | |
292 | { | |
293 | return {}; | |
294 | } | |
295 | } | |
296 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
297 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
298 | * @brief Overload necessary to implement QAbstractTableModel. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
299 | * @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
|
300 | * the amount of libraries. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
301 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
302 | int LibrariesModel::rowCount(const QModelIndex&) const |
7 | 303 | { |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
304 | return static_cast<int>(this->count()); |
7 | 305 | } |
306 | ||
8
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 | * @brief LibraryManager::columnCount |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
309 | * @returns how many columns there are in the table. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
310 | */ |
230
a1f3f7d9078b
rename LibraryManager -> LibrariesModel
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
311 | int LibrariesModel::columnCount(const QModelIndex&) const |
7 | 312 | { |
313 | return 2; | |
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 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
|
318 | * 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
|
319 | * @param libraryIndex Index of the library. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
320 | */ |
300
3a4b132b8353
Fix build warnings, size_type of QVector changes from Qt5 to Qt6 so we need an alias for it
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
265
diff
changeset
|
321 | void LibrariesModel::signalLibraryChange(const index_t libraryIndex) |
7 | 322 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
323 | Q_ASSERT(isValidIndex(libraryIndex)); |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
324 | const QModelIndex topLeft = this->index(static_cast<int>(libraryIndex), 0); |
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
230
diff
changeset
|
325 | const QModelIndex bottomRight = this->index(static_cast<int>(libraryIndex), columnCount({}) - 1); |
112 | 326 | Q_EMIT dataChanged(topLeft, bottomRight); |
7 | 327 | } |
328 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
329 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
330 | * @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
|
331 | * @param stream Stream to write the library into |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
332 | * @param library Library to write into the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
333 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
334 | */ |
7 | 335 | QDataStream& operator<<(QDataStream& stream, const Library& library) |
336 | { | |
337 | const QString path = library.path.absolutePath(); | |
338 | const int role = static_cast<int>(library.role); | |
339 | return stream << path << role; | |
340 | } | |
341 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
342 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
343 | * @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
|
344 | * @param stream Stream to read the library from |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
345 | * @param library Library to obtain from the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
346 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
347 | */ |
7 | 348 | QDataStream& operator>>(QDataStream& stream, Library& library) |
349 | { | |
350 | QString path; | |
351 | int role; | |
352 | QDataStream& result = stream >> path >> role; | |
100 | 353 | library.path.setPath(path); |
7 | 354 | library.role = static_cast<Library::Role>(role); |
355 | return result; | |
356 | } | |
41
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
357 | |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
358 | bool operator==(const Library& one, const Library& other) |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
359 | { |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
360 | return one.role == other.role and one.path == other.path; |
0abada2a9802
added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
361 | } |