From b8f437c20364c6f970706a99a7499a22ef471b1b Mon Sep 17 00:00:00 2001 From: Digital Artifex <7929434+DigitalArtifex@users.noreply.github.com> Date: Sat, 6 Jun 2026 22:44:02 -0400 Subject: [PATCH] Added NewestPacksModel --- KomplexHubPlugin/newestpacksmodel.cpp | 133 ++++++++++++++++++++++ KomplexHubPlugin/newestpacksmodel.h | 153 ++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100644 KomplexHubPlugin/newestpacksmodel.cpp create mode 100644 KomplexHubPlugin/newestpacksmodel.h diff --git a/KomplexHubPlugin/newestpacksmodel.cpp b/KomplexHubPlugin/newestpacksmodel.cpp new file mode 100644 index 0000000..e7a2815 --- /dev/null +++ b/KomplexHubPlugin/newestpacksmodel.cpp @@ -0,0 +1,133 @@ +#include "newestpacksmodel.h" +#include "common/coreservices.h" +#include "common/logging.h" + +NewestPacksModel::NewestPacksModel(QObject *parent) : QAbstractListModel{parent} +{ + +} + +auto NewestPacksModel::rowCount(const QModelIndex &) const -> int +{ + return m_wallpaperCache.count(); +} + +auto NewestPacksModel::data(const QModelIndex &index, int role) const -> QVariant +{ + if(index.row() < 0) + { + return {}; + } + + WallpaperCache *dataPoint = m_wallpaperCache.at(index.row()); + + if(dataPoint == nullptr) + { + return {}; + } + + QVariant data; + + switch(static_cast(role)) + { + case UriRole: + data = dataPoint->uri; + break; + case AuthorRole: + data = dataPoint->author; + break; + case DescriptionRole: + data = dataPoint->description; + break; + case NameRole: + data = dataPoint->name; + break; + case ThumbnailRole: + data = dataPoint->thumbnail; + break; + case CreatedDateRole: + data = dataPoint->createdDate; + break; + case AuthorIdRole: + data = dataPoint->authorId; + break; + case PriceRole: + data = dataPoint->price; + break; + case CurrencyRole: + data = dataPoint->currency; + break; + case DownloadCountRole: + data = dataPoint->downloadCount; + break; + case TypeRole: + data = dataPoint->type; + break; + } + + return data; +} + +auto NewestPacksModel::index(int row, int column, const QModelIndex &parent) const -> QModelIndex +{ + Q_UNUSED(parent) + + return createIndex(row, column, m_wallpaperCache.at(row)); +} + +auto NewestPacksModel::columnCount(const QModelIndex &) const -> int +{ + return 0; +} + +auto NewestPacksModel::parent(const QModelIndex &) const -> QModelIndex +{ + return {}; +} + +auto NewestPacksModel::setData(const QModelIndex &, const QVariant &, int) -> bool +{ + return true; +} + +auto NewestPacksModel::state() const -> NewestPacksModel::State +{ + return m_state; +} + +auto NewestPacksModel::setState(State state) -> void +{ + if (m_state == state) + { + return; + } + + m_state = state; + emit stateChanged(); +} + +auto NewestPacksModel::resetState() -> void +{ + setState(Idle); +} + +auto NewestPacksModel::roleNames() const -> QHash +{ + return m_dataRoles; +} + +auto NewestPacksModel::errorString() const -> QString +{ + return m_errorString; +} + +auto NewestPacksModel::setErrorString(const QString &errorString) -> void +{ + if (m_errorString == errorString) + { + return; + } + + m_errorString = errorString; + emit errorStringChanged(); +} diff --git a/KomplexHubPlugin/newestpacksmodel.h b/KomplexHubPlugin/newestpacksmodel.h new file mode 100644 index 0000000..20201a8 --- /dev/null +++ b/KomplexHubPlugin/newestpacksmodel.h @@ -0,0 +1,153 @@ +#ifndef NEWESTPACKSMODEL_H +#define NEWESTPACKSMODEL_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "paginators/newestpackspaginator.h" +#include "common/wallpapercache.h" + +class KOMPLEX_EXPORT NewestPacksModel : public QAbstractListModel +{ + Q_OBJECT + QML_ELEMENT + +public: + enum State + { + Idle, + Loading, + Error + }; + Q_ENUM(State) + + enum DataRole { + UriRole = Qt::UserRole + 1, + AuthorRole, + AuthorIdRole, + DescriptionRole, + NameRole, + ThumbnailRole, + CreatedDateRole, + PriceRole, + CurrencyRole, + DownloadCountRole, + TypeRole + }; + Q_ENUM(DataRole) + + explicit NewestPacksModel(QObject *parent = nullptr); + + auto rowCount(const QModelIndex &parent = QModelIndex()) const -> int override; + auto data(const QModelIndex &index, int role = Qt::DisplayRole) const -> QVariant override; + + auto index( + int row, + int column, + const QModelIndex &parent = QModelIndex() + ) const -> QModelIndex override; + + auto columnCount( + const QModelIndex &parent = QModelIndex() + ) const -> int override; + + auto parent(const QModelIndex &index) const -> QModelIndex override; + + auto setData( + const QModelIndex &index, + const QVariant &value, + int role = Qt::EditRole + ) -> bool override; + + auto state() const -> State; + auto setState(State state) -> void; + auto resetState() -> void; + auto roleNames() const -> QHash override; + + auto errorString() const -> QString; + auto setErrorString(const QString &errorString) -> void; + +signals: + auto installedWallpapersChanged() -> void; + + auto stateChanged() -> void; + + auto errorStringChanged() -> void; + +private: + static inline const QHash m_dataRoles = + { + { + static_cast(UriRole), + QByteArray("uri") + }, + { + static_cast(AuthorRole), + QByteArray("author") + }, + { + static_cast(DescriptionRole), + QByteArray("description") + }, + { + static_cast(NameRole), + QByteArray("name") + }, + { + static_cast(ThumbnailRole), + QByteArray("thumbnail") + }, + { + static_cast(CreatedDateRole), + QByteArray("createdDate") + }, + { + static_cast(PriceRole), + QByteArray("price") + }, + { + static_cast(CurrencyRole), + QByteArray("currency") + }, + { + static_cast(DownloadCountRole), + QByteArray("downloadCount") + }, + { + static_cast(TypeRole), + QByteArray("type") + } + }; + + static inline const QString m_endpointUri = QString(""); + State m_state = Idle; + + QString m_errorString = QString(); + + QString m_installDirectoryUri; + + qint64 m_page = 0; + qint64 m_resultsPerPage = 0; + + NewestPacksPaginator m_wallpaperCache; + + Q_PROPERTY(State state READ state WRITE setState RESET resetState NOTIFY stateChanged FINAL) + Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged FINAL) +}; +Q_DECLARE_METATYPE(NewestPacksModel) + +#endif // NEWESTPACKSMODEL_H