src/libraries.cpp

changeset 265
b2b7af293c46
parent 264
76a025db4948
child 300
3a4b132b8353
equal deleted inserted replaced
264:76a025db4948 265:b2b7af293c46
74 74
75 /** 75 /**
76 * @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.
77 * @param libraryIndex Index of the library 77 * @param libraryIndex Index of the library
78 */ 78 */
79 void LibrariesModel::removeLibrary(const std::size_t libraryIndex) 79 void LibrariesModel::removeLibrary(const qsizetype libraryIndex)
80 { 80 {
81 Q_ASSERT(isValidIndex(libraryIndex)); 81 Q_ASSERT(this->isValidIndex(libraryIndex));
82 if (isValidIndex(libraryIndex)) 82 if (this->isValidIndex(libraryIndex))
83 { 83 {
84 Q_EMIT layoutAboutToBeChanged(); 84 Q_EMIT layoutAboutToBeChanged();
85 this->libraries.erase(this->libraries.begin() + static_cast<long>(libraryIndex)); 85 this->libraries.erase(this->libraries.begin() + static_cast<long>(libraryIndex));
86 Q_EMIT layoutChanged(); 86 Q_EMIT layoutChanged();
87 } 87 }
91 * @brief Gets a library by index. 91 * @brief Gets a library by index.
92 * @warning Index is assumed to be valid. 92 * @warning Index is assumed to be valid.
93 * @param libraryIndex Index of the library 93 * @param libraryIndex Index of the library
94 * @return const reference 94 * @return const reference
95 */ 95 */
96 const Library& LibrariesModel::library(std::size_t libraryIndex) const 96 const Library& LibrariesModel::library(qsizetype libraryIndex) const
97 { 97 {
98 Q_ASSERT(isValidIndex(libraryIndex)); 98 Q_ASSERT(this->isValidIndex(libraryIndex));
99 return this->libraries[libraryIndex]; 99 return this->libraries[libraryIndex];
100 } 100 }
101 101
102 /** 102 /**
103 * @brief Changes the path of the specified library 103 * @brief Changes the path of the specified library
104 * @param libraryIndex Index of the library 104 * @param libraryIndex Index of the library
105 * @param path New path 105 * @param path New path
106 */ 106 */
107 void LibrariesModel::setLibraryPath(std::size_t libraryIndex, const QDir& path) 107 void LibrariesModel::setLibraryPath(qsizetype libraryIndex, const QDir& path)
108 { 108 {
109 if (this->isValidIndex(libraryIndex)) 109 if (this->isValidIndex(libraryIndex))
110 { 110 {
111 this->libraries[libraryIndex].path = path; 111 this->libraries[libraryIndex].path = path;
112 this->signalLibraryChange(libraryIndex); 112 this->signalLibraryChange(libraryIndex);
116 /** 116 /**
117 * @brief Changes the role of the specified library 117 * @brief Changes the role of the specified library
118 * @param libraryIndex Index of the library 118 * @param libraryIndex Index of the library
119 * @param role New role 119 * @param role New role
120 */ 120 */
121 void LibrariesModel::setLibraryRole(std::size_t libraryIndex, const Library::Role role) 121 void LibrariesModel::setLibraryRole(qsizetype libraryIndex, const Library::Role role)
122 { 122 {
123 if (this->isValidIndex(libraryIndex)) 123 if (this->isValidIndex(libraryIndex))
124 { 124 {
125 this->libraries[libraryIndex].role = role; 125 this->libraries[libraryIndex].role = role;
126 this->signalLibraryChange(libraryIndex); 126 this->signalLibraryChange(libraryIndex);
147 } 147 }
148 148
149 /** 149 /**
150 * @returns the amount of libraries 150 * @returns the amount of libraries
151 */ 151 */
152 std::size_t LibrariesModel::count() const 152 qsizetype LibrariesModel::count() const
153 { 153 {
154 return this->libraries.size(); 154 return this->libraries.size();
155 } 155 }
156 156
157 void LibrariesModel::moveLibrary(const std::size_t libraryFromIndex, const std::size_t libraryToIndex) 157 void LibrariesModel::moveLibrary(const qsizetype libraryFromIndex, const qsizetype libraryToIndex)
158 { 158 {
159 if (isValidIndex(libraryFromIndex) and 159 if (isValidIndex(libraryFromIndex) and
160 (isValidIndex(libraryToIndex) or libraryToIndex == count()) and 160 (isValidIndex(libraryToIndex) or libraryToIndex == count()) and
161 libraryFromIndex != libraryToIndex) 161 libraryFromIndex != libraryToIndex)
162 { 162 {
163 Q_EMIT layoutAboutToBeChanged(); 163 Q_EMIT layoutAboutToBeChanged();
164 const Library library = this->library(libraryFromIndex); 164 const Library library = this->library(libraryFromIndex);
165 const auto from = this->libraries.begin() + signed_cast(libraryFromIndex); 165 if (libraryToIndex > libraryFromIndex)
166 const auto to = this->libraries.begin() + signed_cast(libraryToIndex); 166 {
167 if (to > from) 167 this->libraries.insert(libraryToIndex, library);
168 { 168 this->libraries.removeAt(libraryFromIndex);
169 this->libraries.insert(to, library);
170 this->libraries.erase(from);
171 } 169 }
172 else if (libraryToIndex < libraryFromIndex) 170 else if (libraryToIndex < libraryFromIndex)
173 { 171 {
174 this->libraries.erase(from); 172 this->libraries.removeAt(libraryFromIndex);
175 this->libraries.insert(to, library); 173 this->libraries.insert(libraryToIndex, library);
176 } 174 }
177 Q_EMIT layoutChanged(); 175 Q_EMIT layoutChanged();
178 } 176 }
179 } 177 }
180 178
181 /** 179 /**
182 * @brief Checks whether the specified index points to a valid library. 180 * @brief Checks whether the specified index points to a valid library.
183 * @param libraryIndex Index to check 181 * @param libraryIndex Index to check
184 * @returns whether or not it is valid 182 * @returns whether or not it is valid
185 */ 183 */
186 bool LibrariesModel::isValidIndex(const std::size_t libraryIndex) const 184 bool LibrariesModel::isValidIndex(const qsizetype libraryIndex) const
187 { 185 {
188 return libraryIndex >= 0 && libraryIndex < this->libraries.size(); 186 return libraryIndex >= 0 && libraryIndex < this->libraries.size();
189 } 187 }
190 188
191 /** 189 /**
255 { 253 {
256 return {}; 254 return {};
257 } 255 }
258 else 256 else
259 { 257 {
260 const int row = index.row();
261 const Column column = static_cast<Column>(index.column()); 258 const Column column = static_cast<Column>(index.column());
262 const Library& library = this->library(static_cast<std::size_t>(row)); 259 const Library& library = this->library(index.row());
263 switch (column) 260 switch (column)
264 { 261 {
265 case RoleColumn: 262 case RoleColumn:
266 return Library::libraryRoleName(library.role); 263 return Library::libraryRoleName(library.role);
267 case PathColumn: 264 case PathColumn:
319 /** 316 /**
320 * @brief Signals view objects that the specified library has changed. This is necessary 317 * @brief Signals view objects that the specified library has changed. This is necessary
321 * to update the table widget know when libraries are changed. 318 * to update the table widget know when libraries are changed.
322 * @param libraryIndex Index of the library. 319 * @param libraryIndex Index of the library.
323 */ 320 */
324 void LibrariesModel::signalLibraryChange(std::size_t libraryIndex) 321 void LibrariesModel::signalLibraryChange(const qsizetype libraryIndex)
325 { 322 {
326 Q_ASSERT(isValidIndex(libraryIndex)); 323 Q_ASSERT(isValidIndex(libraryIndex));
327 const QModelIndex topLeft = this->index(static_cast<int>(libraryIndex), 0); 324 const QModelIndex topLeft = this->index(static_cast<int>(libraryIndex), 0);
328 const QModelIndex bottomRight = this->index(static_cast<int>(libraryIndex), columnCount({}) - 1); 325 const QModelIndex bottomRight = this->index(static_cast<int>(libraryIndex), columnCount({}) - 1);
329 Q_EMIT dataChanged(topLeft, bottomRight); 326 Q_EMIT dataChanged(topLeft, bottomRight);
360 357
361 bool operator==(const Library& one, const Library& other) 358 bool operator==(const Library& one, const Library& other)
362 { 359 {
363 return one.role == other.role and one.path == other.path; 360 return one.role == other.role and one.path == other.path;
364 } 361 }
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