Fixed data model issues

This commit is contained in:
Digital Artifex
2026-06-16 16:58:55 -04:00
parent d5314b9066
commit 5f31bc9fc2
2 changed files with 229 additions and 47 deletions
+121 -25
View File
@@ -1,33 +1,72 @@
#include "newestpacksmodel.h" #include "newestpacksmodel.h"
#include "common/coreservices.h"
#include "common/logging.h"
#include "common/wallpapercache.h" #include "common/wallpapercache.h"
NewestPacksModel::NewestPacksModel(QObject *parent) : QAbstractListModel{parent} NewestPacksModel::NewestPacksModel(QObject *parent) : QAbstractListModel{parent}
{ {
m_paginator = new NewestPacksPaginator(this);
PaginationNotifier *notifier = static_cast<PaginationNotifier*>(m_paginator);
QObject::connect(
notifier,
&PaginationNotifier::resultsPerPageChanged,
this,
&NewestPacksModel::resultsPerPageChanged
);
QObject::connect(
notifier,
&PaginationNotifier::totalResultsChanged,
this,
&NewestPacksModel::totalResultsChanged
);
QObject::connect(
notifier,
&PaginationNotifier::totalPagesChanged,
this,
&NewestPacksModel::totalPagesChanged
);
QObject::connect(
notifier,
&PaginationNotifier::pageChanged,
this,
&NewestPacksModel::pageChanged
);
QObject::connect(
notifier,
&PaginationNotifier::dataChanged,
this,
&NewestPacksModel::restDataModel
);
} }
auto NewestPacksModel::rowCount(const QModelIndex &) const -> int auto NewestPacksModel::rowCount(const QModelIndex &) const -> int
{ {
return m_wallpaperCache.count(); if(m_paginator != nullptr)
{
return m_paginator->count();
}
return 0;
} }
auto NewestPacksModel::data(const QModelIndex &index, int role) const -> QVariant auto NewestPacksModel::data(const QModelIndex &index, int role) const -> QVariant
{ {
if(index.row() < 0) if(index.row() < 0 || m_paginator == nullptr)
{ {
return {}; return {};
} }
WallpaperCache dataPoint = m_wallpaperCache.at(index.row()); WallpaperCache dataPoint = m_data.at(index.row());
QVariant data; QVariant data;
switch(static_cast<DataRole>(role)) switch(static_cast<DataRole>(role))
{ {
case UriRole: case UuidRole:
data = dataPoint.uri; data = dataPoint.uuid;
break; break;
case AuthorRole: case AuthorRole:
data = dataPoint.author; data = dataPoint.author;
@@ -68,8 +107,10 @@ auto NewestPacksModel::index(int row, int column, const QModelIndex &parent) con
{ {
Q_UNUSED(parent) Q_UNUSED(parent)
WallpaperCache data = m_wallpaperCache.at(row); if(row < 0 || row >= m_data.count())
return createIndex(row, column, &data); return {};
return createIndex(row, column, &m_data[row]);
} }
auto NewestPacksModel::columnCount(const QModelIndex &) const -> int auto NewestPacksModel::columnCount(const QModelIndex &) const -> int
@@ -108,6 +149,25 @@ auto NewestPacksModel::resetState() -> void
setState(Idle); setState(Idle);
} }
auto NewestPacksModel::restDataModel() -> void
{
//invalidate previous model data
beginRemoveRows(QModelIndex(), 0, m_data.count());
m_data.clear();
endRemoveRows();
//signal new data
beginInsertRows(QModelIndex(), 0, m_paginator->count() - 1);
auto dataset = m_paginator->data();
for(const WallpaperCache& data : std::as_const(dataset))
{
m_data.append(data);
}
endInsertRows();
}
auto NewestPacksModel::roleNames() const -> QHash<int, QByteArray> auto NewestPacksModel::roleNames() const -> QHash<int, QByteArray>
{ {
return m_dataRoles; return m_dataRoles;
@@ -129,30 +189,66 @@ auto NewestPacksModel::setErrorString(const QString &errorString) -> void
emit errorStringChanged(); emit errorStringChanged();
} }
auto NewestPacksModel::resultsPerPage() const -> qint64 auto NewestPacksModel::resultsPerPage() const -> qsizetype
{ {
return m_resultsPerPage; if(m_paginator != nullptr)
{
return m_paginator->resultsPerPage();
}
return 0;
} }
auto NewestPacksModel::setResultsPerPage(qint64 resultsPerPage) -> void auto NewestPacksModel::setResultsPerPage(qsizetype resultsPerPage) -> void
{ {
if (m_resultsPerPage == resultsPerPage) if(m_paginator != nullptr)
return; {
m_paginator->setResultsPerPage(resultsPerPage);
m_resultsPerPage = resultsPerPage; }
emit resultsPerPageChanged();
} }
qint64 NewestPacksModel::page() const auto NewestPacksModel::totalResults() const -> qsizetype
{ {
return m_page; if(m_paginator != nullptr)
{
return m_paginator->totalResults();
}
return 0;
} }
void NewestPacksModel::setPage(qint64 page) auto NewestPacksModel::nextPage() const -> void
{ {
if (m_page == page) if(m_paginator != nullptr)
return; {
m_paginator->next();
m_page = page; }
emit pageChanged(); }
auto NewestPacksModel::previousPage() const -> void
{
if(m_paginator != nullptr)
{
m_paginator->previous();
}
}
qsizetype NewestPacksModel::page() const
{
if(m_paginator != nullptr)
{
return m_paginator->page();
}
return 0;
}
auto NewestPacksModel::totalPages() const -> qsizetype
{
if(m_paginator != nullptr)
{
return m_paginator->totalPages();
}
return 0;
} }
+108 -22
View File
@@ -38,12 +38,18 @@
#include "paginators/newestpackspaginator.h" #include "paginators/newestpackspaginator.h"
/**
* @brief The NewestPacksModel class
*/
class KOMPLEX_EXPORT NewestPacksModel : public QAbstractListModel class KOMPLEX_EXPORT NewestPacksModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
QML_ELEMENT QML_ELEMENT
public: public:
/**
* @brief Used to control the UI state
*/
enum State enum State
{ {
Idle, Idle,
@@ -52,8 +58,11 @@ public:
}; };
Q_ENUM(State) Q_ENUM(State)
/**
* @brief Data roles used by the view to request data
*/
enum DataRole { enum DataRole {
UriRole = Qt::UserRole + 1, UuidRole = Qt::UserRole + 1,
AuthorRole, AuthorRole,
AuthorIdRole, AuthorIdRole,
DescriptionRole, DescriptionRole,
@@ -70,59 +79,139 @@ public:
explicit NewestPacksModel(QObject *parent = nullptr); explicit NewestPacksModel(QObject *parent = nullptr);
auto rowCount(const QModelIndex &parent = QModelIndex()) const -> int override; auto rowCount(const QModelIndex &parent = QModelIndex()) const -> int override;
auto data(const QModelIndex &index, int role = Qt::DisplayRole) const -> QVariant override;
/**
* @brief data
* Used by the view to pull data from the model.
* Required by QAbstractListModel
* @param index
* @param role
* @return
*/
auto data(
const QModelIndex &index,
int role = Qt::DisplayRole
) const -> QVariant override;
/**
* @brief index
* Used by the view to create an index for the data point.
* Required by QAbstractListModel
* @param row
* @param column
* @param parent
* @return
*/
auto index( auto index(
int row, int row,
int column, int column,
const QModelIndex &parent = QModelIndex() const QModelIndex &parent = QModelIndex()
) const -> QModelIndex override; ) const -> QModelIndex override;
/**
* @brief columnCount
* Required by QAbstractListModel, but just returns 0
* since we dont use cols
* @param parent
* @return
*/
auto columnCount( auto columnCount(
const QModelIndex &parent = QModelIndex() const QModelIndex &parent = QModelIndex()
) const -> int override; ) const -> int override;
/**
* @brief parent
* @param index
* @return
*/
auto parent(const QModelIndex &index) const -> QModelIndex override; auto parent(const QModelIndex &index) const -> QModelIndex override;
/**
* @brief setData
* Required by QAbstractListModel but not used
* @param index
* @param value
* @param role
* @return true always
*/
auto setData( auto setData(
const QModelIndex &index, const QModelIndex &index,
const QVariant &value, const QVariant &value,
int role = Qt::EditRole int role = Qt::EditRole
) -> bool override; ) -> bool override;
/**
* @brief state
* Current Model State
* @return
*/
auto state() const -> State; auto state() const -> State;
auto setState(State state) -> void;
auto resetState() -> void; /**
* @brief roleNames
* Used to tell the view what data roles are available
* and their QML friendly names
* @return
*/
auto roleNames() const -> QHash<int, QByteArray> override; auto roleNames() const -> QHash<int, QByteArray> override;
auto errorString() const -> QString; auto errorString() const -> QString;
auto resultsPerPage() const -> qsizetype;
auto setResultsPerPage(qsizetype resultsPerPage) -> void;
auto totalResults() const -> qsizetype;
auto page() const -> qsizetype;
auto totalPages() const -> qsizetype;
Q_INVOKABLE auto nextPage() const -> void;
Q_INVOKABLE auto previousPage() const -> void;
protected:
/**
* @brief setErrorString
* @param errorString
*/
auto setErrorString(const QString &errorString) -> void; auto setErrorString(const QString &errorString) -> void;
auto resultsPerPage() const -> qint64; /**
auto setResultsPerPage(qint64 resultsPerPage) -> void; * @brief setState
* @param state
*/
auto setState(State state) -> void;
qint64 page() const; /**
void setPage(qint64 page); * @brief resetState
*/
auto resetState() -> void;
auto restDataModel() -> void;
signals: signals:
auto installedWallpapersChanged() -> void;
auto stateChanged() -> void; auto stateChanged() -> void;
auto errorStringChanged() -> void; auto errorStringChanged() -> void;
auto resultsPerPageChanged() -> void; auto resultsPerPageChanged() -> void;
void pageChanged(); auto pageChanged() -> void;
auto totalResultsChanged() -> void;
auto totalPagesChanged() -> void;
private: private:
static inline const QHash<int, QByteArray> m_dataRoles = static inline const QHash<int, QByteArray> m_dataRoles =
{ {
{ {
static_cast<int>(UriRole), static_cast<int>(UuidRole),
QByteArray("uri") QByteArray("uuid")
}, },
{ {
static_cast<int>(AuthorRole), static_cast<int>(AuthorRole),
QByteArray("author") QByteArray("author")
}, },
{
static_cast<int>(AuthorIdRole),
QByteArray("authorId")
},
{ {
static_cast<int>(DescriptionRole), static_cast<int>(DescriptionRole),
QByteArray("description") QByteArray("description")
@@ -156,23 +245,20 @@ private:
QByteArray("type") QByteArray("type")
} }
}; };
static inline const QString m_endpointUri = QString("");
State m_state = Idle; State m_state = Idle;
QString m_errorString = QString(); QString m_errorString = QString();
QString m_installDirectoryUri; NewestPacksPaginator *m_paginator = nullptr;
qint64 m_page = 0; mutable QList<WallpaperCache> m_data;
qint64 m_resultsPerPage = 0;
NewestPacksPaginator m_wallpaperCache;
Q_PROPERTY(State state READ state WRITE setState RESET resetState NOTIFY stateChanged FINAL) 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_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged FINAL)
Q_PROPERTY(qint64 resultsPerPage READ resultsPerPage WRITE setResultsPerPage NOTIFY resultsPerPageChanged FINAL) Q_PROPERTY(qsizetype resultsPerPage READ resultsPerPage WRITE setResultsPerPage NOTIFY resultsPerPageChanged FINAL)
Q_PROPERTY(qint64 page READ page WRITE setPage NOTIFY pageChanged FINAL) Q_PROPERTY(qsizetype totalResults READ totalResults NOTIFY totalResultsChanged FINAL)
Q_PROPERTY(qsizetype totalPages READ totalPages NOTIFY totalPagesChanged FINAL)
Q_PROPERTY(qsizetype page READ page NOTIFY pageChanged FINAL)
}; };
Q_DECLARE_METATYPE(NewestPacksModel) Q_DECLARE_METATYPE(NewestPacksModel)