diff -r 68443f5be176 -r 44679e468ba9 src/libraries.cpp --- a/src/libraries.cpp Sat Oct 05 23:47:03 2019 +0300 +++ b/src/libraries.cpp Sun Nov 03 12:17:41 2019 +0200 @@ -1,22 +1,39 @@ #include #include "libraries.h" +/** + * @brief Constructs a new library manager + * @param parent Parent object + */ LibraryManager::LibraryManager(QObject* parent): QAbstractTableModel{parent} { } +/** + * @brief Constructs a library manager from settings + * @param settings Settings to construct from + * @param parent Parent object + */ LibraryManager::LibraryManager(QSettings* settings, QObject* parent) : QAbstractTableModel{parent} { this->restoreFromSettings(settings); } +/** + * @brief Yields a begin-terator for the libraries + * @return iterator + */ QVector::const_iterator LibraryManager::begin() const { return this->libraries.begin(); } +/** + * @brief Yields an end-iterator for the libraries + * @return iterator + */ QVector::const_iterator LibraryManager::end() const { return this->libraries.end(); @@ -46,6 +63,10 @@ return path; } +/** + * @brief Adds a new library to the end of the libraries list. + * @param library Library to add + */ void LibraryManager::addLibrary(const Library& library) { emit layoutAboutToBeChanged(); @@ -53,11 +74,38 @@ emit layoutChanged(); } +/** + * @brief Removes a library by index. Does nothing if the index is not valid. + * @param libraryIndex Index of the library + */ +void LibraryManager::removeLibrary(const int libraryIndex) +{ + Q_ASSERT(isValidIndex(libraryIndex)); + if (isValidIndex(libraryIndex)) + { + emit layoutAboutToBeChanged(); + libraries.remove(libraryIndex); + emit layoutChanged(); + } +} + +/** + * @brief Gets a library by index. + * @warning Index is assumed to be valid. + * @param libraryIndex Index of the library + * @return const reference + */ const Library& LibraryManager::library(int libraryIndex) const { + Q_ASSERT(isValidIndex(libraryIndex)); return this->libraries[libraryIndex]; } +/** + * @brief Changes the path of the specified library + * @param libraryIndex Index of the library + * @param path New path + */ void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path) { if (this->isValidIndex(libraryIndex)) @@ -67,6 +115,11 @@ } } +/** + * @brief Changes the role of the specified library + * @param libraryIndex Index of the library + * @param role New role + */ void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role) { if (this->isValidIndex(libraryIndex)) @@ -76,27 +129,71 @@ } } +/** + * @brief Restores the libraries from the specified settings object. All unsaved + * changes are lost. + * @param settings Settings object to restore from. + */ void LibraryManager::restoreFromSettings(QSettings* settings) { this->libraries = settings->value("libraries").value(); } +/** + * @brief Saves the libraries to the specified settings object. + * @param settings Settings object to modify. + */ void LibraryManager::storeToSettings(QSettings* settings) { QVariant librariesValue = QVariant::fromValue(this->libraries); settings->setValue("libraries", librariesValue); } +/** + * @returns the amount of libraries + */ int LibraryManager::count() const { return this->libraries.size(); } -bool LibraryManager::isValidIndex(int libraryIndex) +void LibraryManager::moveLibrary(const int libraryFromIndex, const int libraryToIndex) +{ + if (isValidIndex(libraryFromIndex) and + (isValidIndex(libraryToIndex) or libraryToIndex == count()) and + libraryFromIndex != libraryToIndex) + { + emit layoutAboutToBeChanged(); + const Library library = this->library(libraryFromIndex); + if (libraryToIndex > libraryFromIndex) + { + this->libraries.insert(libraryToIndex, library); + this->libraries.removeAt(libraryFromIndex); + } + else if (libraryToIndex < libraryFromIndex) + { + this->libraries.removeAt(libraryFromIndex); + this->libraries.insert(libraryToIndex, library); + } + emit layoutChanged(); + } +} + +/** + * @brief Checks whether the specified index points to a valid library. + * @param libraryIndex Index to check + * @returns whether or not it is valid + */ +bool LibraryManager::isValidIndex(const int libraryIndex) const { return libraryIndex >= 0 && libraryIndex < this->libraries.size(); } +/** + * @brief Gets a human-readable string for the specified role + * @param role Role to get a string for + * @returns string + */ QString Library::libraryRoleName(const Role role) { switch (role) @@ -112,12 +209,24 @@ } } +/** + * @brief Overload necessary to implement QAbstractTableModel + * @param index + * @return Item flags + */ Qt::ItemFlags LibraryManager::flags(const QModelIndex& index) const { Q_UNUSED(index); return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } +/** + * @brief Returns data needed to represent the libraries in a table. This function + * decides how data is represented in a table view. + * @param index Index to get data for + * @param role Role of the data (role as in Qt model-view role, not LDraw library role) + * @returns variant + */ QVariant LibraryManager::data(const QModelIndex& index, int role) const { if (role != Qt::DisplayRole) @@ -140,6 +249,13 @@ } } +/** + * @brief Returns header texts for a table view. We only implement column headers here. + * @param section Index of the column (can also be a row but we only have column headers) + * @param orientation Orientation (we only support horizontal orientation) + * @param role Role of the data (role as in Qt model-view role, not LDraw library role) + * @returns variant + */ QVariant LibraryManager::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal and role == Qt::DisplayRole) @@ -159,25 +275,44 @@ } } -int LibraryManager::rowCount(const QModelIndex& parent) const +/** + * @brief Overload necessary to implement QAbstractTableModel. + * @returns how many rows there are in the table. Since there is one row per library, this returns + * the amount of libraries. + */ +int LibraryManager::rowCount(const QModelIndex&) const { - Q_UNUSED(parent) return this->count(); } -int LibraryManager::columnCount(const QModelIndex& parent) const +/** + * @brief LibraryManager::columnCount + * @returns how many columns there are in the table. + */ +int LibraryManager::columnCount(const QModelIndex&) const { - Q_UNUSED(parent) return 2; } +/** + * @brief Signals view objects that the specified library has changed. This is necessary + * to update the table widget know when libraries are changed. + * @param libraryIndex Index of the library. + */ void LibraryManager::signalLibraryChange(int libraryIndex) { + Q_ASSERT(isValidIndex(libraryIndex)); const QModelIndex topLeft = this->index(libraryIndex, 0); - const QModelIndex bottomRight = this->index(libraryIndex, columnCount() - 1); + const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1); emit dataChanged(topLeft, bottomRight); } +/** + * @brief Overload for operator<< to allow serializing a library into the configuration file. + * @param stream Stream to write the library into + * @param library Library to write into the stream + * @returns the stream + */ QDataStream& operator<<(QDataStream& stream, const Library& library) { const QString path = library.path.absolutePath(); @@ -185,6 +320,12 @@ return stream << path << role; } +/** + * @brief Overload for operator>> to allow serializing a library into the configuration file. + * @param stream Stream to read the library from + * @param library Library to obtain from the stream + * @returns the stream + */ QDataStream& operator>>(QDataStream& stream, Library& library) { QString path;