From 7647edcdc25416f76f7be47608316391c65317a8 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Fri, 9 Oct 2020 13:47:35 +0200 Subject: [PATCH] Refactor models --- src/CMakeLists.txt | 4 ++-- src/{entryListModel.cpp => entriesmodel.cpp} | 23 ++++++++------------ src/{entryListModel.h => entriesmodel.h} | 12 ++++------ src/feed.cpp | 3 +++ src/feed.h | 4 ++++ src/{feedListModel.cpp => feedsmodel.cpp} | 16 +++++++------- src/{feedListModel.h => feedsmodel.h} | 4 ++-- src/main.cpp | 8 +++---- src/qml/EntryListPage.qml | 5 +---- src/qml/FeedListDelegate.qml | 2 +- src/qml/FeedListPage.qml | 4 ++-- 11 files changed, 40 insertions(+), 45 deletions(-) rename src/{entryListModel.cpp => entriesmodel.cpp} (73%) rename src/{entryListModel.h => entriesmodel.h} (72%) rename src/{feedListModel.cpp => feedsmodel.cpp} (83%) rename src/{feedListModel.h => feedsmodel.h} (86%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bdd86598..a46c6413 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,7 @@ set(alligator_SRCS main.cpp - feedListModel.cpp - entryListModel.cpp + feedsmodel.cpp + entriesmodel.cpp fetcher.cpp database.cpp entry.cpp diff --git a/src/entryListModel.cpp b/src/entriesmodel.cpp similarity index 73% rename from src/entryListModel.cpp rename to src/entriesmodel.cpp index 3ec4c631..e5cba8e1 100644 --- a/src/entryListModel.cpp +++ b/src/entriesmodel.cpp @@ -9,11 +9,12 @@ #include #include "database.h" -#include "entryListModel.h" +#include "entriesmodel.h" #include "fetcher.h" -EntryListModel::EntryListModel(QObject *parent) - : QAbstractListModel(parent) +EntriesModel::EntriesModel(Feed *feed) + : QAbstractListModel(feed) + , m_feed(feed) { connect(&Fetcher::instance(), &Fetcher::feedUpdated, this, [this](QString url) { if (m_feed->url() == url) { @@ -27,7 +28,7 @@ EntryListModel::EntryListModel(QObject *parent) }); } -QVariant EntryListModel::data(const QModelIndex &index, int role) const +QVariant EntriesModel::data(const QModelIndex &index, int role) const { if (role != 0) return QVariant(); @@ -36,14 +37,14 @@ QVariant EntryListModel::data(const QModelIndex &index, int role) const return QVariant::fromValue(m_entries[index.row()]); } -QHash EntryListModel::roleNames() const +QHash EntriesModel::roleNames() const { QHash roleNames; roleNames[0] = "entry"; return roleNames; } -int EntryListModel::rowCount(const QModelIndex &parent) const +int EntriesModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) QSqlQuery query; @@ -55,18 +56,12 @@ int EntryListModel::rowCount(const QModelIndex &parent) const return query.value(0).toInt(); } -void EntryListModel::loadEntry(int index) const +void EntriesModel::loadEntry(int index) const { m_entries[index] = new Entry(m_feed, index); } -Feed *EntryListModel::feed() const +Feed *EntriesModel::feed() const { return m_feed; } - -void EntryListModel::setFeed(Feed *feed) -{ - m_feed = feed; - Q_EMIT feedChanged(feed); -} diff --git a/src/entryListModel.h b/src/entriesmodel.h similarity index 72% rename from src/entryListModel.h rename to src/entriesmodel.h index 16a9b409..06b2b562 100644 --- a/src/entryListModel.h +++ b/src/entriesmodel.h @@ -12,26 +12,22 @@ #include #include "entry.h" +#include "feed.h" -class EntryListModel : public QAbstractListModel +class EntriesModel : public QAbstractListModel { Q_OBJECT - Q_PROPERTY(Feed *feed READ feed WRITE setFeed NOTIFY feedChanged) + Q_PROPERTY(Feed *feed READ feed CONSTANT) public: - explicit EntryListModel(QObject *parent = nullptr); + explicit EntriesModel(Feed *feed); QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QHash roleNames() const override; int rowCount(const QModelIndex &parent) const override; Feed *feed() const; - void setFeed(Feed *feed); - -Q_SIGNALS: - void feedChanged(Feed *feed); - private: void loadEntry(int index) const; diff --git a/src/feed.cpp b/src/feed.cpp index c244c154..08663d15 100644 --- a/src/feed.cpp +++ b/src/feed.cpp @@ -7,6 +7,7 @@ #include #include "database.h" +#include "entriesmodel.h" #include "feed.h" #include "fetcher.h" @@ -70,6 +71,8 @@ Feed::Feed(int index) if(url == m_image) Q_EMIT imageChanged(url); }); + + m_entries = new EntriesModel(this); } Feed::~Feed() diff --git a/src/feed.h b/src/feed.h index 61196a03..ce14f8f4 100644 --- a/src/feed.h +++ b/src/feed.h @@ -12,6 +12,8 @@ #include "author.h" +class EntriesModel; + class Feed : public QObject { Q_OBJECT @@ -32,6 +34,7 @@ class Feed : public QObject Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged) Q_PROPERTY(int errorId READ errorId WRITE setErrorId NOTIFY errorIdChanged) Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged) + Q_PROPERTY(EntriesModel *entries MEMBER m_entries CONSTANT) public: Feed(int index); @@ -104,6 +107,7 @@ private: bool m_notify; int m_errorId; QString m_errorString; + EntriesModel *m_entries; bool m_refreshing = false; }; diff --git a/src/feedListModel.cpp b/src/feedsmodel.cpp similarity index 83% rename from src/feedListModel.cpp rename to src/feedsmodel.cpp index 5530c9b4..6b550ffe 100644 --- a/src/feedListModel.cpp +++ b/src/feedsmodel.cpp @@ -11,10 +11,10 @@ #include #include "database.h" -#include "feedListModel.h" +#include "feedsmodel.h" #include "fetcher.h" -FeedListModel::FeedListModel(QObject *parent) +FeedsModel::FeedsModel(QObject *parent) : QAbstractListModel(parent) { connect(&Database::instance(), &Database::feedAdded, this, [this]() { @@ -36,14 +36,14 @@ FeedListModel::FeedListModel(QObject *parent) }); } -QHash FeedListModel::roleNames() const +QHash FeedsModel::roleNames() const { QHash roleNames; roleNames[0] = "feed"; return roleNames; } -int FeedListModel::rowCount(const QModelIndex &parent) const +int FeedsModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) QSqlQuery query; @@ -54,7 +54,7 @@ int FeedListModel::rowCount(const QModelIndex &parent) const return query.value(0).toInt(); } -QVariant FeedListModel::data(const QModelIndex &index, int role) const +QVariant FeedsModel::data(const QModelIndex &index, int role) const { if (role != 0) return QVariant(); @@ -63,12 +63,12 @@ QVariant FeedListModel::data(const QModelIndex &index, int role) const return QVariant::fromValue(m_feeds[index.row()]); } -void FeedListModel::loadFeed(int index) const +void FeedsModel::loadFeed(int index) const { m_feeds += new Feed(index); } -void FeedListModel::removeFeed(int index) +void FeedsModel::removeFeed(int index) { m_feeds[index]->remove(); delete m_feeds[index]; @@ -77,7 +77,7 @@ void FeedListModel::removeFeed(int index) endRemoveRows(); } -void FeedListModel::refreshAll() +void FeedsModel::refreshAll() { for (auto &feed : m_feeds) { feed->refresh(); diff --git a/src/feedListModel.h b/src/feedsmodel.h similarity index 86% rename from src/feedListModel.h rename to src/feedsmodel.h index e87e0248..026aa3fe 100644 --- a/src/feedListModel.h +++ b/src/feedsmodel.h @@ -13,12 +13,12 @@ #include "feed.h" -class FeedListModel : public QAbstractListModel +class FeedsModel : public QAbstractListModel { Q_OBJECT public: - explicit FeedListModel(QObject *parent = nullptr); + explicit FeedsModel(QObject *parent = nullptr); QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QHash roleNames() const override; int rowCount(const QModelIndex &parent) const override; diff --git a/src/main.cpp b/src/main.cpp index 993c6489..c3ad352c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,8 +23,8 @@ #include "alligatorsettings.h" #include "database.h" -#include "entryListModel.h" -#include "feedListModel.h" +#include "entriesmodel.h" +#include "feedsmodel.h" #include "fetcher.h" #ifdef Q_OS_ANDROID @@ -44,8 +44,8 @@ int main(int argc, char *argv[]) QCoreApplication::setApplicationName(QStringLiteral("Alligator")); QCoreApplication::setApplicationVersion(QStringLiteral("0.1")); - qmlRegisterType("org.kde.alligator", 1, 0, "FeedListModel"); - qmlRegisterType("org.kde.alligator", 1, 0, "EntryListModel"); + qmlRegisterType("org.kde.alligator", 1, 0, "FeedsModel"); + qmlRegisterUncreatableType("org.kde.alligator", 1, 0, "EntriesModel", QStringLiteral("Get from Feed")); qmlRegisterSingletonType("org.kde.alligator", 1, 0, "Fetcher", [](QQmlEngine *engine, QJSEngine *) -> QObject * { engine->setObjectOwnership(&Fetcher::instance(), QQmlEngine::CppOwnership); return &Fetcher::instance(); diff --git a/src/qml/EntryListPage.qml b/src/qml/EntryListPage.qml index a0fb9b38..b2e6fb31 100644 --- a/src/qml/EntryListPage.qml +++ b/src/qml/EntryListPage.qml @@ -61,10 +61,7 @@ Kirigami.ScrollablePage { ListView { id: entryList visible: count !== 0 - model: EntryListModel { - id: entryListModel - feed: page.feed - } + model: page.feed.entries header: EntryListHeader { } diff --git a/src/qml/FeedListDelegate.qml b/src/qml/FeedListDelegate.qml index 5b459b1c..01ea9171 100644 --- a/src/qml/FeedListDelegate.qml +++ b/src/qml/FeedListDelegate.qml @@ -36,7 +36,7 @@ Kirigami.SwipeListItem { onTriggered: { if(pageStack.depth > 1 && model.feed.url === lastFeed) pageStack.pop() - feedListModel.removeFeed(index) + feedsModel.removeFeed(index) } } diff --git a/src/qml/FeedListPage.qml b/src/qml/FeedListPage.qml index d80f7dae..83a44960 100644 --- a/src/qml/FeedListPage.qml +++ b/src/qml/FeedListPage.qml @@ -69,8 +69,8 @@ Kirigami.ScrollablePage { id: feedList visible: count !== 0 anchors.fill: parent - model: FeedListModel { - id: feedListModel + model: FeedsModel { + id: feedsModel } delegate: FeedListDelegate { }