src/libraries.cpp

changeset 259
c27612f0eac0
parent 230
a1f3f7d9078b
child 264
76a025db4948
equal deleted inserted replaced
258:fe094d0687ad 259:c27612f0eac0
28 QAbstractTableModel{parent} 28 QAbstractTableModel{parent}
29 { 29 {
30 } 30 }
31 31
32 /** 32 /**
33 * @brief Yields a begin-terator for the libraries
34 * @return iterator
35 */
36 QVector<Library>::const_iterator LibrariesModel::begin() const
37 {
38 return this->libraries.begin();
39 }
40
41 /**
42 * @brief Yields an end-iterator for the libraries
43 * @return iterator
44 */
45 QVector<Library>::const_iterator LibrariesModel::end() const
46 {
47 return this->libraries.end();
48 }
49
50 /**
51 * @brief Searches the libraries for the specified file name. 33 * @brief Searches the libraries for the specified file name.
52 * @param fileName File to search for 34 * @param fileName File to search for
53 * @return Full path to the file, or empty string if not found. 35 * @return Full path to the file, or empty string if not found.
54 */ 36 */
55 QString LibrariesModel::findFile(QString fileName) const 37 QString LibrariesModel::findFile(QString fileName) const
61 { 43 {
62 for (const QString& subdirectory : {"parts", "p"}) 44 for (const QString& subdirectory : {"parts", "p"})
63 { 45 {
64 QDir directory = library.path; 46 QDir directory = library.path;
65 directory.cd(subdirectory); 47 directory.cd(subdirectory);
66 QFileInfo fileInfo = directory.absoluteFilePath(fileName); 48 QFileInfo fileInfo{directory.absoluteFilePath(fileName)};
67 if (fileInfo.exists() && fileInfo.isFile()) 49 if (fileInfo.exists() && fileInfo.isFile())
68 { 50 {
69 path = fileInfo.absoluteFilePath(); 51 path = fileInfo.absoluteFilePath();
70 found = true; 52 found = true;
71 break; 53 break;
84 * @param library Library to add 66 * @param library Library to add
85 */ 67 */
86 void LibrariesModel::addLibrary(const Library& library) 68 void LibrariesModel::addLibrary(const Library& library)
87 { 69 {
88 Q_EMIT layoutAboutToBeChanged(); 70 Q_EMIT layoutAboutToBeChanged();
89 libraries.append(library); 71 this->libraries.push_back(library);
90 Q_EMIT layoutChanged(); 72 Q_EMIT layoutChanged();
91 } 73 }
92 74
93 /** 75 /**
94 * @brief Removes a library by index. Does nothing if the index is not valid. 76 * @brief Removes a library by index. Does nothing if the index is not valid.
95 * @param libraryIndex Index of the library 77 * @param libraryIndex Index of the library
96 */ 78 */
97 void LibrariesModel::removeLibrary(const int libraryIndex) 79 void LibrariesModel::removeLibrary(const std::size_t libraryIndex)
98 { 80 {
99 Q_ASSERT(isValidIndex(libraryIndex)); 81 Q_ASSERT(isValidIndex(libraryIndex));
100 if (isValidIndex(libraryIndex)) 82 if (isValidIndex(libraryIndex))
101 { 83 {
102 Q_EMIT layoutAboutToBeChanged(); 84 Q_EMIT layoutAboutToBeChanged();
103 libraries.remove(libraryIndex); 85 this->libraries.erase(this->libraries.begin() + static_cast<long>(libraryIndex));
104 Q_EMIT layoutChanged(); 86 Q_EMIT layoutChanged();
105 } 87 }
106 } 88 }
107 89
108 /** 90 /**
109 * @brief Gets a library by index. 91 * @brief Gets a library by index.
110 * @warning Index is assumed to be valid. 92 * @warning Index is assumed to be valid.
111 * @param libraryIndex Index of the library 93 * @param libraryIndex Index of the library
112 * @return const reference 94 * @return const reference
113 */ 95 */
114 const Library& LibrariesModel::library(int libraryIndex) const 96 const Library& LibrariesModel::library(std::size_t libraryIndex) const
115 { 97 {
116 Q_ASSERT(isValidIndex(libraryIndex)); 98 Q_ASSERT(isValidIndex(libraryIndex));
117 return this->libraries[libraryIndex]; 99 return this->libraries[libraryIndex];
118 } 100 }
119 101
120 /** 102 /**
121 * @brief Changes the path of the specified library 103 * @brief Changes the path of the specified library
122 * @param libraryIndex Index of the library 104 * @param libraryIndex Index of the library
123 * @param path New path 105 * @param path New path
124 */ 106 */
125 void LibrariesModel::setLibraryPath(int libraryIndex, const QDir& path) 107 void LibrariesModel::setLibraryPath(std::size_t libraryIndex, const QDir& path)
126 { 108 {
127 if (this->isValidIndex(libraryIndex)) 109 if (this->isValidIndex(libraryIndex))
128 { 110 {
129 this->libraries[libraryIndex].path = path; 111 this->libraries[libraryIndex].path = path;
130 this->signalLibraryChange(libraryIndex); 112 this->signalLibraryChange(libraryIndex);
134 /** 116 /**
135 * @brief Changes the role of the specified library 117 * @brief Changes the role of the specified library
136 * @param libraryIndex Index of the library 118 * @param libraryIndex Index of the library
137 * @param role New role 119 * @param role New role
138 */ 120 */
139 void LibrariesModel::setLibraryRole(int libraryIndex, const Library::Role role) 121 void LibrariesModel::setLibraryRole(std::size_t libraryIndex, const Library::Role role)
140 { 122 {
141 if (this->isValidIndex(libraryIndex)) 123 if (this->isValidIndex(libraryIndex))
142 { 124 {
143 this->libraries[libraryIndex].role = role; 125 this->libraries[libraryIndex].role = role;
144 this->signalLibraryChange(libraryIndex); 126 this->signalLibraryChange(libraryIndex);
165 } 147 }
166 148
167 /** 149 /**
168 * @returns the amount of libraries 150 * @returns the amount of libraries
169 */ 151 */
170 int LibrariesModel::count() const 152 std::size_t LibrariesModel::count() const
171 { 153 {
172 return this->libraries.size(); 154 return this->libraries.size();
173 } 155 }
174 156
175 void LibrariesModel::moveLibrary(const int libraryFromIndex, const int libraryToIndex) 157 void LibrariesModel::moveLibrary(const std::size_t libraryFromIndex, const std::size_t libraryToIndex)
176 { 158 {
177 if (isValidIndex(libraryFromIndex) and 159 if (isValidIndex(libraryFromIndex) and
178 (isValidIndex(libraryToIndex) or libraryToIndex == count()) and 160 (isValidIndex(libraryToIndex) or libraryToIndex == count()) and
179 libraryFromIndex != libraryToIndex) 161 libraryFromIndex != libraryToIndex)
180 { 162 {
181 Q_EMIT layoutAboutToBeChanged(); 163 Q_EMIT layoutAboutToBeChanged();
182 const Library library = this->library(libraryFromIndex); 164 const Library library = this->library(libraryFromIndex);
183 if (libraryToIndex > libraryFromIndex) 165 const auto from = this->libraries.begin() + signed_cast(libraryFromIndex);
184 { 166 const auto to = this->libraries.begin() + signed_cast(libraryToIndex);
185 this->libraries.insert(libraryToIndex, library); 167 if (to > from)
186 this->libraries.removeAt(libraryFromIndex); 168 {
169 this->libraries.insert(to, library);
170 this->libraries.erase(from);
187 } 171 }
188 else if (libraryToIndex < libraryFromIndex) 172 else if (libraryToIndex < libraryFromIndex)
189 { 173 {
190 this->libraries.removeAt(libraryFromIndex); 174 this->libraries.erase(from);
191 this->libraries.insert(libraryToIndex, library); 175 this->libraries.insert(to, library);
192 } 176 }
193 Q_EMIT layoutChanged(); 177 Q_EMIT layoutChanged();
194 } 178 }
195 } 179 }
196 180
197 /** 181 /**
198 * @brief Checks whether the specified index points to a valid library. 182 * @brief Checks whether the specified index points to a valid library.
199 * @param libraryIndex Index to check 183 * @param libraryIndex Index to check
200 * @returns whether or not it is valid 184 * @returns whether or not it is valid
201 */ 185 */
202 bool LibrariesModel::isValidIndex(const int libraryIndex) const 186 bool LibrariesModel::isValidIndex(const std::size_t libraryIndex) const
203 { 187 {
204 return libraryIndex >= 0 && libraryIndex < this->libraries.size(); 188 return libraryIndex >= 0 && libraryIndex < this->libraries.size();
205 } 189 }
206 190
207 /** 191 /**
273 } 257 }
274 else 258 else
275 { 259 {
276 const int row = index.row(); 260 const int row = index.row();
277 const Column column = static_cast<Column>(index.column()); 261 const Column column = static_cast<Column>(index.column());
278 const Library& library = this->library(row); 262 const Library& library = this->library(static_cast<std::size_t>(row));
279 switch (column) 263 switch (column)
280 { 264 {
281 case RoleColumn: 265 case RoleColumn:
282 return Library::libraryRoleName(library.role); 266 return Library::libraryRoleName(library.role);
283 case PathColumn: 267 case PathColumn:
318 * @returns how many rows there are in the table. Since there is one row per library, this returns 302 * @returns how many rows there are in the table. Since there is one row per library, this returns
319 * the amount of libraries. 303 * the amount of libraries.
320 */ 304 */
321 int LibrariesModel::rowCount(const QModelIndex&) const 305 int LibrariesModel::rowCount(const QModelIndex&) const
322 { 306 {
323 return this->count(); 307 return static_cast<int>(this->count());
324 } 308 }
325 309
326 /** 310 /**
327 * @brief LibraryManager::columnCount 311 * @brief LibraryManager::columnCount
328 * @returns how many columns there are in the table. 312 * @returns how many columns there are in the table.
335 /** 319 /**
336 * @brief Signals view objects that the specified library has changed. This is necessary 320 * @brief Signals view objects that the specified library has changed. This is necessary
337 * to update the table widget know when libraries are changed. 321 * to update the table widget know when libraries are changed.
338 * @param libraryIndex Index of the library. 322 * @param libraryIndex Index of the library.
339 */ 323 */
340 void LibrariesModel::signalLibraryChange(int libraryIndex) 324 void LibrariesModel::signalLibraryChange(std::size_t libraryIndex)
341 { 325 {
342 Q_ASSERT(isValidIndex(libraryIndex)); 326 Q_ASSERT(isValidIndex(libraryIndex));
343 const QModelIndex topLeft = this->index(libraryIndex, 0); 327 const QModelIndex topLeft = this->index(static_cast<int>(libraryIndex), 0);
344 const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); 328 const QModelIndex bottomRight = this->index(static_cast<int>(libraryIndex), columnCount({}) - 1);
345 Q_EMIT dataChanged(topLeft, bottomRight); 329 Q_EMIT dataChanged(topLeft, bottomRight);
346 } 330 }
347 331
348 /** 332 /**
349 * @brief Overload for operator<< to allow serializing a library into the configuration file. 333 * @brief Overload for operator<< to allow serializing a library into the configuration file.
376 360
377 bool operator==(const Library& one, const Library& other) 361 bool operator==(const Library& one, const Library& other)
378 { 362 {
379 return one.role == other.role and one.path == other.path; 363 return one.role == other.role and one.path == other.path;
380 } 364 }
365
366 /*
367 QDataStream &operator<<(QDataStream& stream, const Libraries& libs)
368 {
369 const auto size = static_cast<std::uint32_t>(libs.size());
370 stream << size;
371 for (std::uint32_t i = 0; i < size; ++i) {
372 stream << libs[i];
373 }
374 return stream;
375 }
376
377 QDataStream &operator>>(QDataStream& stream, Libraries& libs)
378 {
379 std::uint32_t size;
380 stream >> size;
381 libs.resize(size);
382 for (std::uint32_t i = 0; i < size; ++i) {
383 stream >> libs[i];
384 }
385 return stream;
386 }
387 */

mercurial