Work on feed models & representation.

This commit is contained in:
Martin Rotter 2013-12-16 12:53:48 +01:00
parent ec11ccbfa4
commit 26211cb229
9 changed files with 117 additions and 10 deletions

View File

@ -238,6 +238,37 @@ void FeedsModel::loadFromDatabase() {
assembleFeeds(feeds);
}
QList<FeedsModelFeed*> FeedsModel::feedsForIndex(const QModelIndex &index) {
QList<FeedsModelFeed*> feeds;
FeedsModelRootItem *item = itemForIndex(index);
switch (item->kind()) {
case FeedsModelRootItem::Category:
feeds.append(static_cast<FeedsModelCategory*>(item)->feeds());
break;
case FeedsModelRootItem::Feed:
// This item is feed (it SURELY subclasses FeedsModelFeed),add it.
feeds.append(static_cast<FeedsModelFeed*>(item));
break;
default:
break;
}
return feeds;
}
QList<FeedsModelFeed*> FeedsModel::feedsForIndexes(const QModelIndexList &indexes) {
QList<FeedsModelFeed*> feeds;
foreach (const QModelIndex &index, indexes) {
feeds.append(feedsForIndex(index));
}
return feeds;
}
QHash<int, FeedsModelCategory *> FeedsModel::getCategories(FeedsModelRootItem *root) {
QHash<int, FeedsModelCategory*> categories;

View File

@ -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<int, FeedsModelCategory*> getCategories();
// Returns categories from the subtree with given root node.
QHash<int, FeedsModelCategory*> 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<FeedsModelFeed*> feedsForIndexes(const QModelIndexList &indexes);
// Returns feeds contained within single index.
QList<FeedsModelFeed*> 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);

View File

@ -1,4 +1,8 @@
#include <QQueue>
#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<FeedsModelFeed*> FeedsModelCategory::feeds() {
QList<FeedsModelFeed*> feeds;
QQueue<FeedsModelCategory*> 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<FeedsModelFeed*>(child));
break;
case FeedsModelRootItem::Category:
// This is category, so add it to traversed categories.
categories.enqueue(static_cast<FeedsModelCategory*>(child));
break;
default:
break;
}
}
}
return feeds;
}
int FeedsModelCategory::countOfAllMessages() const {
int total_count = 0;

View File

@ -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<FeedsModelFeed*> feeds();
// Counts of messages.
// NOTE: Counts of messages in categories include
// counts of messages from all children.

View File

@ -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.

View File

@ -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:

View File

@ -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;
}

View File

@ -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;

View File

@ -52,4 +52,3 @@ QModelIndexList MessagesProxyModel::mapListToSource(const QModelIndexList &index
return source_indexes;
}