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 QVariant EpisodeModel::data(const QModelIndex &index, int role) const
{ {
if (role != 0) 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(); return QVariant();
return QVariant::fromValue(DataManager::instance().getEntry(m_entryIds[index.row()])); }
} }
QHash<int, QByteArray> EpisodeModel::roleNames() const QHash<int, QByteArray> EpisodeModel::roleNames() const
{ {
QHash<int, QByteArray> roleNames; return {
roleNames[0] = "entry"; {EntryRole, "entry"},
return roleNames; {ReadRole, "read"},
{NewRole, "new"},
};
} }
int EpisodeModel::rowCount(const QModelIndex &parent) const int EpisodeModel::rowCount(const QModelIndex &parent) const
@ -51,10 +60,14 @@ int EpisodeModel::rowCount(const QModelIndex &parent) const
void EpisodeModel::updateInternalState() void EpisodeModel::updateInternalState()
{ {
m_entryIds.clear(); m_entryIds.clear();
m_read.clear();
m_new.clear();
QSqlQuery query; 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); Database::instance().execute(query);
while (query.next()) { while (query.next()) {
m_entryIds += query.value(QStringLiteral("id")).toString(); 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 <QHash>
#include <QObject> #include <QObject>
#include <QVariant> #include <QVariant>
#include <QVector>
class EpisodeModel : public QAbstractListModel class EpisodeModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
public: public:
enum Roles {
EntryRole = Qt::UserRole,
ReadRole,
NewRole,
};
explicit EpisodeModel(); 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; QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
@ -25,4 +32,6 @@ private:
void updateInternalState(); void updateInternalState();
QStringList m_entryIds; QStringList m_entryIds;
QVector<bool> m_read;
QVector<bool> m_new;
}; };