Unified category/feed accessors in model.

This commit is contained in:
Martin Rotter 2015-11-04 08:42:52 +01:00
parent 7dd09cfd89
commit d48873ab45
6 changed files with 25 additions and 45 deletions

View File

@ -312,10 +312,6 @@ QList<Message> FeedsModel::messagesForFeeds(const QList<Feed*> &feeds) {
return messages; return messages;
} }
QList<Category*> FeedsModel::allCategories() {
return categoriesForItem(m_rootItem);
}
int FeedsModel::columnCount(const QModelIndex &parent) const { int FeedsModel::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent) Q_UNUSED(parent)
@ -543,7 +539,7 @@ QList<Feed*> FeedsModel::allFeeds() {
} }
QList<Feed*> FeedsModel::feedsForItem(RootItem *root) { QList<Feed*> FeedsModel::feedsForItem(RootItem *root) {
QList<RootItem*> children = root->getRecursiveChildren(); QList<RootItem*> children = root->getSubTree();
QList<Feed*> feeds; QList<Feed*> feeds;
foreach (RootItem *child, children) { foreach (RootItem *child, children) {
@ -555,22 +551,18 @@ QList<Feed*> FeedsModel::feedsForItem(RootItem *root) {
return feeds; return feeds;
} }
QList<Category*> FeedsModel::allCategories() {
return categoriesForItem(m_rootItem);
}
QList<Category*> FeedsModel::categoriesForItem(RootItem *root) { QList<Category*> FeedsModel::categoriesForItem(RootItem *root) {
QList<RootItem*> children = root->getSubTree();
QList<Category*> categories; QList<Category*> categories;
QList<RootItem*> parents;
parents.append(root); foreach (RootItem *child, children) {
if (child->kind() == RootItemKind::Category) {
while (!parents.isEmpty()) { categories.append(child->toCategory());
RootItem *item = parents.takeFirst();
if (item->kind() == RootItemKind::Category) {
// This item is category, add it to the output list and
// scan its children.
categories.append( item->toCategory());
} }
parents.append(item->childItems());
} }
return categories; return categories;

View File

@ -20,7 +20,7 @@
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include "core/messagesmodel.h" #include "core/message.h"
#include "core/rootitem.h" #include "core/rootitem.h"

View File

@ -58,7 +58,7 @@ RootItem *FeedsSelection::selectedItem() const {
QString FeedsSelection::generateListOfIds() { QString FeedsSelection::generateListOfIds() {
if (m_selectedItem != NULL && if (m_selectedItem != NULL &&
(m_selectedItem->kind() == RootItemKind::Feed || m_selectedItem->kind() == RootItemKind::Category)) { (m_selectedItem->kind() == RootItemKind::Feed || m_selectedItem->kind() == RootItemKind::Category)) {
QList<RootItem*> children = m_selectedItem->getRecursiveChildren(); QList<RootItem*> children = m_selectedItem->getSubTree();
QStringList stringy_ids; QStringList stringy_ids;
foreach (RootItem *child, children) { foreach (RootItem *child, children) {

View File

@ -74,36 +74,20 @@ int RootItem::countOfAllMessages() const {
return total_count; return total_count;
} }
QList<RootItem*> RootItem::getRecursiveChildren() { QList<RootItem*> RootItem::getSubTree() {
QList<RootItem*> children; QList<RootItem*> children;
if (childCount() == 0) { // Root itself is a CATEGORY or ROOT item.
// Root itself has no children, it is either feed or QList<RootItem*> traversable_items;
// empty category?
children.append(this);
}
else {
// Root itself is a CATEGORY or ROOT item.
QList<RootItem*> traversable_items;
traversable_items.append(this); traversable_items.append(this);
// Iterate all nested categories. // Iterate all nested categories.
while (!traversable_items.isEmpty()) { while (!traversable_items.isEmpty()) {
RootItem *active_item = traversable_items.takeFirst(); RootItem *active_item = traversable_items.takeFirst();
children.append(active_item);
foreach (RootItem *child, active_item->childItems()) { children.append(active_item);
if (child->childCount() == 0) { traversable_items.append(active_item->childItems());
// This child is feed or empty category.
children.append(child);
}
else {
// This child is category, add its child feeds too.
traversable_items.append(child);
}
}
}
} }
return children; return children;

View File

@ -138,7 +138,9 @@ class RootItem {
m_childItems.clear(); m_childItems.clear();
} }
QList<RootItem*> getRecursiveChildren(); // Returns flat list of all items from subtree where this item is a root.
// Returned list includes this item too.
QList<RootItem*> getSubTree();
// Removes particular child at given index. // Removes particular child at given index.
// NOTE: Child is NOT freed from the memory. // NOTE: Child is NOT freed from the memory.

View File

@ -330,12 +330,14 @@ void FeedsView::markAllFeedsRead() {
void FeedsView::fetchMetadataForSelectedFeed() { void FeedsView::fetchMetadataForSelectedFeed() {
// TODO: fix // TODO: fix
/*
StandardFeed *selected_feed = (StandardFeed*) selectedFeed(); StandardFeed *selected_feed = (StandardFeed*) selectedFeed();
if (selected_feed != NULL) { if (selected_feed != NULL) {
selected_feed->fetchMetadataForItself(); selected_feed->fetchMetadataForItself();
m_sourceModel->reloadChangedLayout(QModelIndexList() << m_proxyModel->mapToSource(selectionModel()->selectedRows(0).at(0))); m_sourceModel->reloadChangedLayout(QModelIndexList() << m_proxyModel->mapToSource(selectionModel()->selectedRows(0).at(0)));
} }
*/
} }
void FeedsView::clearAllReadMessages() { void FeedsView::clearAllReadMessages() {