From 26211cb22970d9d0dbcdd12c57d1f1252b925b6d Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 16 Dec 2013 12:53:48 +0100 Subject: [PATCH] Work on feed models & representation. --- src/core/feedsmodel.cpp | 31 +++++++++++++++++++++++++++++ src/core/feedsmodel.h | 23 +++++++++++++++------ src/core/feedsmodelcategory.cpp | 33 +++++++++++++++++++++++++++++++ src/core/feedsmodelcategory.h | 8 ++++++++ src/core/feedsmodelrootitem.h | 6 +++--- src/core/feedsmodelstandardfeed.h | 1 + src/core/feedsproxymodel.cpp | 20 +++++++++++++++++++ src/core/feedsproxymodel.h | 4 ++++ src/core/messagesproxymodel.cpp | 1 - 9 files changed, 117 insertions(+), 10 deletions(-) diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index 77db96424..c291c6401 100644 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -238,6 +238,37 @@ void FeedsModel::loadFromDatabase() { assembleFeeds(feeds); } +QList FeedsModel::feedsForIndex(const QModelIndex &index) { + QList feeds; + FeedsModelRootItem *item = itemForIndex(index); + + switch (item->kind()) { + case FeedsModelRootItem::Category: + feeds.append(static_cast(item)->feeds()); + break; + + case FeedsModelRootItem::Feed: + // This item is feed (it SURELY subclasses FeedsModelFeed),add it. + feeds.append(static_cast(item)); + break; + + default: + break; + } + + return feeds; +} + +QList FeedsModel::feedsForIndexes(const QModelIndexList &indexes) { + QList feeds; + + foreach (const QModelIndex &index, indexes) { + feeds.append(feedsForIndex(index)); + } + + return feeds; +} + QHash FeedsModel::getCategories(FeedsModelRootItem *root) { QHash categories; diff --git a/src/core/feedsmodel.h b/src/core/feedsmodel.h index c93e60650..afb4958b9 100644 --- a/src/core/feedsmodel.h +++ b/src/core/feedsmodel.h @@ -30,19 +30,30 @@ class FeedsModel : public QAbstractItemModel { int columnCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const; - // Returns feed/category which lies at the specified index. - FeedsModelRootItem *itemForIndex(const QModelIndex &index); - - // Loads feed/categories from the database. - void loadFromDatabase(); - // Returns all categories. QHash getCategories(); // Returns categories from the subtree with given root node. QHash getCategories(FeedsModelRootItem *root); + // Returns list of feeds which belong to given indexes. + // NOTE: If index is "category", then all child feeds are contained in the + // result. + // NOTE: This is particularly useful for displaying messages of selected feeds. + QList feedsForIndexes(const QModelIndexList &indexes); + + // Returns feeds contained within single index. + QList feedsForIndex(const QModelIndex &index); + protected: + // Loads feed/categories from the database. + void loadFromDatabase(); + + // TODO: Otestovat metody itemForIndex, feedsForIndex, feedsForIndexes. + + // Returns feed/category which lies at the specified index. + FeedsModelRootItem *itemForIndex(const QModelIndex &index); + // Takes lists of feeds/categories and assembles // them into the tree structure. void assembleCategories(CategoryAssignment categories); diff --git a/src/core/feedsmodelcategory.cpp b/src/core/feedsmodelcategory.cpp index 2bcbe5544..76adec502 100755 --- a/src/core/feedsmodelcategory.cpp +++ b/src/core/feedsmodelcategory.cpp @@ -1,4 +1,8 @@ +#include + #include "core/feedsmodelcategory.h" +#include "core/feedsmodelstandardcategory.h" +#include "core/feedsmodelstandardfeed.h" FeedsModelCategory::FeedsModelCategory(FeedsModelRootItem *parent_item) @@ -8,6 +12,35 @@ FeedsModelCategory::FeedsModelCategory(FeedsModelRootItem *parent_item) FeedsModelCategory::~FeedsModelCategory() { } +QList FeedsModelCategory::feeds() { + QList feeds; + QQueue categories; + + categories.enqueue(this); + + while (!categories.isEmpty()) { + FeedsModelCategory *active_category = categories.dequeue(); + + foreach (FeedsModelRootItem *child, active_category->childItems()) { + switch (child->kind()) { + case FeedsModelRootItem::Feed: + feeds.append(static_cast(child)); + break; + + case FeedsModelRootItem::Category: + // This is category, so add it to traversed categories. + categories.enqueue(static_cast(child)); + break; + + default: + break; + } + } + } + + return feeds; +} + int FeedsModelCategory::countOfAllMessages() const { int total_count = 0; diff --git a/src/core/feedsmodelcategory.h b/src/core/feedsmodelcategory.h index 99311ba62..5047b8739 100755 --- a/src/core/feedsmodelcategory.h +++ b/src/core/feedsmodelcategory.h @@ -6,6 +6,8 @@ #include "core/feedsmodelrootitem.h" +class FeedsModelFeed; + // Base class for all categories contained in FeedsModel. // NOTE: This class should be derived to create PARTICULAR category types. // NOTE: This class should not be instantiated directly. @@ -23,6 +25,12 @@ class FeedsModelCategory : public FeedsModelRootItem { explicit FeedsModelCategory(FeedsModelRootItem *parent_item = NULL); virtual ~FeedsModelCategory(); + // TODO: Otestovat metodu feeds. + + // Returns list of ALL feeds situated under this category. + // NOTE: This is recursive. + virtual QList feeds(); + // Counts of messages. // NOTE: Counts of messages in categories include // counts of messages from all children. diff --git a/src/core/feedsmodelrootitem.h b/src/core/feedsmodelrootitem.h index 4781ea1c5..5d106d71f 100755 --- a/src/core/feedsmodelrootitem.h +++ b/src/core/feedsmodelrootitem.h @@ -11,9 +11,9 @@ class FeedsModelRootItem { public: // Describes the kind of the item. enum Kind { - RootItem, - Feed, - Category + RootItem = 1001, + Feed = 1002, + Category = 1003 }; // Constructors and destructors. diff --git a/src/core/feedsmodelstandardfeed.h b/src/core/feedsmodelstandardfeed.h index 8553b8f53..4fd153dbc 100644 --- a/src/core/feedsmodelstandardfeed.h +++ b/src/core/feedsmodelstandardfeed.h @@ -37,6 +37,7 @@ class FeedsModelStandardFeed : public FeedsModelFeed { QString language() const; void setLanguage(const QString &language); + // Loads standard feed object from given SQL record. static FeedsModelStandardFeed *loadFromRecord(const QSqlRecord &record); private: diff --git a/src/core/feedsproxymodel.cpp b/src/core/feedsproxymodel.cpp index d27781601..3027479dd 100755 --- a/src/core/feedsproxymodel.cpp +++ b/src/core/feedsproxymodel.cpp @@ -56,3 +56,23 @@ bool FeedsProxyModel::lessThan(const QModelIndex &left, return false; } } + +QModelIndexList FeedsProxyModel::mapListFromSource(const QModelIndexList &indexes) { + QModelIndexList mapped_indexes; + + foreach (const QModelIndex &index, indexes) { + mapped_indexes << mapFromSource(index); + } + + return mapped_indexes; +} + +QModelIndexList FeedsProxyModel::mapListToSource(const QModelIndexList &indexes) { + QModelIndexList source_indexes; + + foreach (const QModelIndex &index, indexes) { + source_indexes << mapToSource(index); + } + + return source_indexes; +} diff --git a/src/core/feedsproxymodel.h b/src/core/feedsproxymodel.h index e8743a5b7..42e1c5def 100755 --- a/src/core/feedsproxymodel.h +++ b/src/core/feedsproxymodel.h @@ -17,6 +17,10 @@ class FeedsProxyModel : public QSortFilterProxyModel { // Access to the source model. FeedsModel *sourceModel(); + // Maps list of indexes. + QModelIndexList mapListToSource(const QModelIndexList &indexes); + QModelIndexList mapListFromSource(const QModelIndexList &indexes); + protected: // Compares two rows of data. bool lessThan(const QModelIndex &left, const QModelIndex &right) const; diff --git a/src/core/messagesproxymodel.cpp b/src/core/messagesproxymodel.cpp index 6e129ce19..9d93eaf77 100644 --- a/src/core/messagesproxymodel.cpp +++ b/src/core/messagesproxymodel.cpp @@ -52,4 +52,3 @@ QModelIndexList MessagesProxyModel::mapListToSource(const QModelIndexList &index return source_indexes; } -