Add roles to EpisodeModel in preparation for filtering

This commit is contained in:
Bart De Vries 2021-09-08 13:58:50 +02:00
parent bb8cd2807c
commit 6d5831a715
2 changed files with 29 additions and 7 deletions

View File

@ -30,16 +30,25 @@ EpisodeModel::EpisodeModel()
QVariant EpisodeModel::data(const QModelIndex &index, int role) const
{
if (role != 0)
return QVariant();
switch (role) {
case EntryRole:
return QVariant::fromValue(DataManager::instance().getEntry(m_entryIds[index.row()]));
case ReadRole:
return QVariant::fromValue(m_read[index.row()]);
case NewRole:
return QVariant::fromValue(m_new[index.row()]);
default:
return QVariant();
}
}
QHash<int, QByteArray> EpisodeModel::roleNames() const
{
QHash<int, QByteArray> roleNames;
roleNames[0] = "entry";
return roleNames;
return {
{EntryRole, "entry"},
{ReadRole, "read"},
{NewRole, "new"},
};
}
int EpisodeModel::rowCount(const QModelIndex &parent) const
@ -51,10 +60,14 @@ int EpisodeModel::rowCount(const QModelIndex &parent) const
void EpisodeModel::updateInternalState()
{
m_entryIds.clear();
m_read.clear();
m_new.clear();
QSqlQuery query;
query.prepare(QStringLiteral("SELECT id FROM Entries ORDER BY updated DESC;"));
query.prepare(QStringLiteral("SELECT id, read, new FROM Entries ORDER BY updated DESC;"));
Database::instance().execute(query);
while (query.next()) {
m_entryIds += query.value(QStringLiteral("id")).toString();
m_read += query.value(QStringLiteral("read")).toBool();
m_new += query.value(QStringLiteral("new")).toBool();
}
}

View File

@ -10,14 +10,21 @@
#include <QHash>
#include <QObject>
#include <QVariant>
#include <QVector>
class EpisodeModel : public QAbstractListModel
{
Q_OBJECT
public:
enum Roles {
EntryRole = Qt::UserRole,
ReadRole,
NewRole,
};
explicit EpisodeModel();
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant data(const QModelIndex &index, int role = Qt::UserRole) const override;
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent) const override;
@ -25,4 +32,6 @@ private:
void updateInternalState();
QStringList m_entryIds;
QVector<bool> m_read;
QVector<bool> m_new;
};