--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/libraries.cpp Sat Oct 05 23:47:03 2019 +0300 @@ -0,0 +1,196 @@ +#include <QSettings> +#include "libraries.h" + +LibraryManager::LibraryManager(QObject* parent): + QAbstractTableModel{parent} +{ +} + +LibraryManager::LibraryManager(QSettings* settings, QObject* parent) : + QAbstractTableModel{parent} +{ + this->restoreFromSettings(settings); +} + +QVector<Library>::const_iterator LibraryManager::begin() const +{ + return this->libraries.begin(); +} + +QVector<Library>::const_iterator LibraryManager::end() const +{ + return this->libraries.end(); +} + +/** + * @brief Searches the libraries for the specified file name. + * @param fileName File to search for + * @return Full path to the file, or empty string if not found. + */ +QFileInfo LibraryManager::findFile(QString fileName) const +{ + QFileInfo path; + fileName.replace("\\", "/"); + bool found = false; + for (const Library& library : this->libraries) + { + path = library.path.absoluteFilePath(fileName); + if (path.exists() && path.isFile()) + { + found = true; + break; + } + } + if (not found) + path = {}; + return path; +} + +void LibraryManager::addLibrary(const Library& library) +{ + emit layoutAboutToBeChanged(); + libraries.append(library); + emit layoutChanged(); +} + +const Library& LibraryManager::library(int libraryIndex) const +{ + return this->libraries[libraryIndex]; +} + +void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path) +{ + if (this->isValidIndex(libraryIndex)) + { + this->libraries[libraryIndex].path = path; + this->signalLibraryChange(libraryIndex); + } +} + +void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role) +{ + if (this->isValidIndex(libraryIndex)) + { + this->libraries[libraryIndex].role = role; + this->signalLibraryChange(libraryIndex); + } +} + +void LibraryManager::restoreFromSettings(QSettings* settings) +{ + this->libraries = settings->value("libraries").value<Libraries>(); +} + +void LibraryManager::storeToSettings(QSettings* settings) +{ + QVariant librariesValue = QVariant::fromValue(this->libraries); + settings->setValue("libraries", librariesValue); +} + +int LibraryManager::count() const +{ + return this->libraries.size(); +} + +bool LibraryManager::isValidIndex(int libraryIndex) +{ + return libraryIndex >= 0 && libraryIndex < this->libraries.size(); +} + +QString Library::libraryRoleName(const Role role) +{ + switch (role) + { + case Library::OfficialLibrary: + return LibraryManager::tr("Official library"); + case Library::UnofficialLibrary: + return LibraryManager::tr("Unofficial library"); + case Library::WorkingLibrary: + return LibraryManager::tr("Working library"); + default: + return "???"; + } +} + +Qt::ItemFlags LibraryManager::flags(const QModelIndex& index) const +{ + Q_UNUSED(index); + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} + +QVariant LibraryManager::data(const QModelIndex& index, int role) const +{ + if (role != Qt::DisplayRole) + { + return {}; + } + else + { + const int row = index.row(); + const Column column = static_cast<Column>(index.column()); + const Library& library = this->library(row); + switch (column) + { + case RoleColumn: + return Library::libraryRoleName(library.role); + case PathColumn: + return library.path.absolutePath(); + } + return {}; + } +} + +QVariant LibraryManager::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Horizontal and role == Qt::DisplayRole) + { + switch(static_cast<Column>(section)) + { + case PathColumn: + return tr("Path"); + case RoleColumn: + return tr("Role"); + } + return {}; + } + else + { + return {}; + } +} + +int LibraryManager::rowCount(const QModelIndex& parent) const +{ + Q_UNUSED(parent) + return this->count(); +} + +int LibraryManager::columnCount(const QModelIndex& parent) const +{ + Q_UNUSED(parent) + return 2; +} + +void LibraryManager::signalLibraryChange(int libraryIndex) +{ + const QModelIndex topLeft = this->index(libraryIndex, 0); + const QModelIndex bottomRight = this->index(libraryIndex, columnCount() - 1); + emit dataChanged(topLeft, bottomRight); +} + +QDataStream& operator<<(QDataStream& stream, const Library& library) +{ + const QString path = library.path.absolutePath(); + const int role = static_cast<int>(library.role); + return stream << path << role; +} + +QDataStream& operator>>(QDataStream& stream, Library& library) +{ + QString path; + int role; + QDataStream& result = stream >> path >> role; + library.path = path; + library.role = static_cast<Library::Role>(role); + return result; +}