Sun, 03 Nov 2019 12:56:42 +0200
added saving of splitter state and recent files
7 | 1 | #include <QSettings> |
2 | #include "libraries.h" | |
3 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
4 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
5 | * @brief Constructs a new library manager |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
6 | * @param parent Parent object |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
7 | */ |
7 | 8 | LibraryManager::LibraryManager(QObject* parent): |
9 | QAbstractTableModel{parent} | |
10 | { | |
11 | } | |
12 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
13 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
14 | * @brief Constructs a library manager from settings |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
15 | * @param settings Settings to construct from |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
16 | * @param parent Parent object |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
17 | */ |
7 | 18 | LibraryManager::LibraryManager(QSettings* settings, QObject* parent) : |
19 | QAbstractTableModel{parent} | |
20 | { | |
21 | this->restoreFromSettings(settings); | |
22 | } | |
23 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
24 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
25 | * @brief Yields a begin-terator for the libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
26 | * @return iterator |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
27 | */ |
7 | 28 | QVector<Library>::const_iterator LibraryManager::begin() const |
29 | { | |
30 | return this->libraries.begin(); | |
31 | } | |
32 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
33 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
34 | * @brief Yields an end-iterator for the libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
35 | * @return iterator |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
36 | */ |
7 | 37 | QVector<Library>::const_iterator LibraryManager::end() const |
38 | { | |
39 | return this->libraries.end(); | |
40 | } | |
41 | ||
42 | /** | |
43 | * @brief Searches the libraries for the specified file name. | |
44 | * @param fileName File to search for | |
45 | * @return Full path to the file, or empty string if not found. | |
46 | */ | |
47 | QFileInfo LibraryManager::findFile(QString fileName) const | |
48 | { | |
49 | QFileInfo path; | |
50 | fileName.replace("\\", "/"); | |
51 | bool found = false; | |
52 | for (const Library& library : this->libraries) | |
53 | { | |
54 | path = library.path.absoluteFilePath(fileName); | |
55 | if (path.exists() && path.isFile()) | |
56 | { | |
57 | found = true; | |
58 | break; | |
59 | } | |
60 | } | |
61 | if (not found) | |
62 | path = {}; | |
63 | return path; | |
64 | } | |
65 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
66 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
67 | * @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
|
68 | * @param library Library to add |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
69 | */ |
7 | 70 | void LibraryManager::addLibrary(const Library& library) |
71 | { | |
72 | emit layoutAboutToBeChanged(); | |
73 | libraries.append(library); | |
74 | emit layoutChanged(); | |
75 | } | |
76 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
77 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
78 | * @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
|
79 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
80 | */ |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
81 | void LibraryManager::removeLibrary(const int libraryIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
82 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
83 | Q_ASSERT(isValidIndex(libraryIndex)); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
84 | if (isValidIndex(libraryIndex)) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
85 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
86 | emit layoutAboutToBeChanged(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
87 | libraries.remove(libraryIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
88 | emit layoutChanged(); |
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 | |
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 Gets a library by index. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
94 | * @warning Index is assumed to be valid. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
95 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
96 | * @return const reference |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
97 | */ |
7 | 98 | const Library& LibraryManager::library(int libraryIndex) const |
99 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
100 | Q_ASSERT(isValidIndex(libraryIndex)); |
7 | 101 | return this->libraries[libraryIndex]; |
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 Changes the path of the specified library |
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 | * @param path New path |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
108 | */ |
7 | 109 | void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path) |
110 | { | |
111 | if (this->isValidIndex(libraryIndex)) | |
112 | { | |
113 | this->libraries[libraryIndex].path = path; | |
114 | this->signalLibraryChange(libraryIndex); | |
115 | } | |
116 | } | |
117 | ||
8
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 Changes the role of the specified library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
120 | * @param libraryIndex Index of the library |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
121 | * @param role New role |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
122 | */ |
7 | 123 | void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role) |
124 | { | |
125 | if (this->isValidIndex(libraryIndex)) | |
126 | { | |
127 | this->libraries[libraryIndex].role = role; | |
128 | this->signalLibraryChange(libraryIndex); | |
129 | } | |
130 | } | |
131 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
132 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
133 | * @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
|
134 | * changes are lost. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
135 | * @param settings Settings object to restore from. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
136 | */ |
7 | 137 | void LibraryManager::restoreFromSettings(QSettings* settings) |
138 | { | |
139 | this->libraries = settings->value("libraries").value<Libraries>(); | |
140 | } | |
141 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
142 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
143 | * @brief Saves the libraries to the specified settings object. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
144 | * @param settings Settings object to modify. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
145 | */ |
7 | 146 | void LibraryManager::storeToSettings(QSettings* settings) |
147 | { | |
148 | QVariant librariesValue = QVariant::fromValue(this->libraries); | |
149 | settings->setValue("libraries", librariesValue); | |
150 | } | |
151 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
152 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
153 | * @returns the amount of libraries |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
154 | */ |
7 | 155 | int LibraryManager::count() const |
156 | { | |
157 | return this->libraries.size(); | |
158 | } | |
159 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
160 | void LibraryManager::moveLibrary(const int libraryFromIndex, const int libraryToIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
161 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
162 | if (isValidIndex(libraryFromIndex) and |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
163 | (isValidIndex(libraryToIndex) or libraryToIndex == count()) and |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
164 | libraryFromIndex != libraryToIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
165 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
166 | emit layoutAboutToBeChanged(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
167 | const Library library = this->library(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
168 | if (libraryToIndex > libraryFromIndex) |
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 | this->libraries.insert(libraryToIndex, library); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
171 | this->libraries.removeAt(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
172 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
173 | else if (libraryToIndex < libraryFromIndex) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
174 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
175 | this->libraries.removeAt(libraryFromIndex); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
176 | this->libraries.insert(libraryToIndex, library); |
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 | emit layoutChanged(); |
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 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
181 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
182 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
183 | * @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
|
184 | * @param libraryIndex Index to check |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
185 | * @returns whether or not it is valid |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
186 | */ |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
187 | bool LibraryManager::isValidIndex(const int libraryIndex) const |
7 | 188 | { |
189 | return libraryIndex >= 0 && libraryIndex < this->libraries.size(); | |
190 | } | |
191 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
192 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
193 | * @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
|
194 | * @param role Role to get a string for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
195 | * @returns string |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
196 | */ |
7 | 197 | QString Library::libraryRoleName(const Role role) |
198 | { | |
199 | switch (role) | |
200 | { | |
201 | case Library::OfficialLibrary: | |
202 | return LibraryManager::tr("Official library"); | |
203 | case Library::UnofficialLibrary: | |
204 | return LibraryManager::tr("Unofficial library"); | |
205 | case Library::WorkingLibrary: | |
206 | return LibraryManager::tr("Working library"); | |
207 | default: | |
208 | return "???"; | |
209 | } | |
210 | } | |
211 | ||
8
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 | * @brief Overload necessary to implement QAbstractTableModel |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
214 | * @param index |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
215 | * @return Item flags |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
216 | */ |
7 | 217 | Qt::ItemFlags LibraryManager::flags(const QModelIndex& index) const |
218 | { | |
219 | Q_UNUSED(index); | |
220 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; | |
221 | } | |
222 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
223 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
224 | * @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
|
225 | * decides how data is represented in a table view. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
226 | * @param index Index to get data for |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
227 | * @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
|
228 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
229 | */ |
7 | 230 | QVariant LibraryManager::data(const QModelIndex& index, int role) const |
231 | { | |
232 | if (role != Qt::DisplayRole) | |
233 | { | |
234 | return {}; | |
235 | } | |
236 | else | |
237 | { | |
238 | const int row = index.row(); | |
239 | const Column column = static_cast<Column>(index.column()); | |
240 | const Library& library = this->library(row); | |
241 | switch (column) | |
242 | { | |
243 | case RoleColumn: | |
244 | return Library::libraryRoleName(library.role); | |
245 | case PathColumn: | |
246 | return library.path.absolutePath(); | |
247 | } | |
248 | return {}; | |
249 | } | |
250 | } | |
251 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
252 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
253 | * @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
|
254 | * @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
|
255 | * @param orientation Orientation (we only support horizontal orientation) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
256 | * @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
|
257 | * @returns variant |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
258 | */ |
7 | 259 | QVariant LibraryManager::headerData(int section, Qt::Orientation orientation, int role) const |
260 | { | |
261 | if (orientation == Qt::Horizontal and role == Qt::DisplayRole) | |
262 | { | |
263 | switch(static_cast<Column>(section)) | |
264 | { | |
265 | case PathColumn: | |
266 | return tr("Path"); | |
267 | case RoleColumn: | |
268 | return tr("Role"); | |
269 | } | |
270 | return {}; | |
271 | } | |
272 | else | |
273 | { | |
274 | return {}; | |
275 | } | |
276 | } | |
277 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
278 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
279 | * @brief Overload necessary to implement QAbstractTableModel. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
280 | * @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
|
281 | * the amount of libraries. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
282 | */ |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
283 | int LibraryManager::rowCount(const QModelIndex&) const |
7 | 284 | { |
285 | return this->count(); | |
286 | } | |
287 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
288 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
289 | * @brief LibraryManager::columnCount |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
290 | * @returns how many columns there are in the table. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
291 | */ |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
292 | int LibraryManager::columnCount(const QModelIndex&) const |
7 | 293 | { |
294 | return 2; | |
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 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
|
299 | * 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
|
300 | * @param libraryIndex Index of the library. |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
301 | */ |
7 | 302 | void LibraryManager::signalLibraryChange(int libraryIndex) |
303 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
304 | Q_ASSERT(isValidIndex(libraryIndex)); |
7 | 305 | const QModelIndex topLeft = this->index(libraryIndex, 0); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
306 | const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); |
7 | 307 | emit dataChanged(topLeft, bottomRight); |
308 | } | |
309 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
310 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
311 | * @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
|
312 | * @param stream Stream to write the library into |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
313 | * @param library Library to write into the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
314 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
315 | */ |
7 | 316 | QDataStream& operator<<(QDataStream& stream, const Library& library) |
317 | { | |
318 | const QString path = library.path.absolutePath(); | |
319 | const int role = static_cast<int>(library.role); | |
320 | return stream << path << role; | |
321 | } | |
322 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
323 | /** |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
324 | * @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
|
325 | * @param stream Stream to read the library from |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
326 | * @param library Library to obtain from the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
327 | * @returns the stream |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
328 | */ |
7 | 329 | QDataStream& operator>>(QDataStream& stream, Library& library) |
330 | { | |
331 | QString path; | |
332 | int role; | |
333 | QDataStream& result = stream >> path >> role; | |
334 | library.path = path; | |
335 | library.role = static_cast<Library::Role>(role); | |
336 | return result; | |
337 | } |