Work on model.

This commit is contained in:
Martin Rotter 2015-11-03 13:49:15 +01:00
parent a104844814
commit dad3cc8555
3 changed files with 39 additions and 17 deletions

View File

@ -231,14 +231,14 @@ bool FeedsModel::removeItem(const QModelIndex &index) {
return false;
}
void FeedsModel::assignNodeToNewParent(RootItem *item, RootItem *parent) {
// Get index of parent item (parent standard category).
QModelIndex parent_index = indexForItem(parent);
void FeedsModel::assignNodeToNewParent(RootItem *item, RootItem *new_parent) {
QModelIndex parent_index = indexForItem(new_parent);
int new_index_of_item = new_parent->childCount();
// TODO: todle jde sloučit s metodou reassignNodeToNewParent.
// TODO: sloučit do funkce reassignNodeToNewParent.
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
parent->appendChild(item);
beginInsertRows(parent_index, new_index_of_item, new_index_of_item);
new_parent->appendChild(item);
endInsertRows();
}
@ -246,18 +246,18 @@ void FeedsModel::reassignNodeToNewParent(RootItem *original_node, RootItem *new_
RootItem *original_parent = original_node->parent();
if (original_parent != new_parent) {
// User edited category and set it new parent item,
// User edited item and set it new parent item,
// se we need to move the item in the model too.
int original_index_of_feed = original_parent->childItems().indexOf(original_node);
int new_index_of_feed = new_parent->childCount();
int original_index_of_item = original_parent->childItems().indexOf(original_node);
int new_index_of_item = new_parent->childCount();
// Remove the original item from the model...
beginRemoveRows(indexForItem(original_parent), original_index_of_feed, original_index_of_feed);
beginRemoveRows(indexForItem(original_parent), original_index_of_item, original_index_of_item);
original_parent->removeChild(original_node);
endRemoveRows();
// ... and insert it under the new parent.
beginInsertRows(indexForItem(new_parent), new_index_of_feed, new_index_of_feed);
beginInsertRows(indexForItem(new_parent), new_index_of_item, new_index_of_item);
new_parent->appendChild(original_node);
endInsertRows();
}
@ -334,6 +334,10 @@ QList<Message> FeedsModel::messagesForFeeds(const QList<Feed*> &feeds) {
return messages;
}
QList<Category*> FeedsModel::allCategories() {
return categoriesForItem(m_rootItem);
}
int FeedsModel::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent)
@ -572,3 +576,16 @@ QList<Feed*> FeedsModel::feedsForItem(RootItem *root) {
return feeds;
}
QList<Category*> FeedsModel::categoriesForItem(RootItem *root) {
QList<RootItem*> children = root->getRecursiveChildren();
QList<Category*> categories;
foreach (RootItem *child, children) {
if (child->kind() == RootItemKind::Category) {
categories.append(child->toCategory());
}
}
return categories;
}

View File

@ -23,8 +23,6 @@
#include "core/messagesmodel.h"
#include "core/rootitem.h"
#include <QIcon>
class Category;
class Feed;
@ -64,7 +62,7 @@ class FeedsModel : public QAbstractItemModel {
bool removeItem(const QModelIndex &index);
// Assigns item to the new parent.
void assignNodeToNewParent(RootItem *item, RootItem *parent);
void assignNodeToNewParent(RootItem *item, RootItem *new_parent);
// Checks if new parent node is different from one used by original node.
// If it is, then it reassigns original_node to new parent.
@ -82,11 +80,18 @@ class FeedsModel : public QAbstractItemModel {
// in "newspaper" mode.
QList<Message> messagesForFeeds(const QList<Feed*> &feeds);
// Returns list of all categories contained in the model.
QList<Category*> allCategories();
// Get list of categories from tree with particular item
// as root.
QList<Category*> categoriesForItem(RootItem *root);
// Returns list of all feeds contained in the model.
QList<Feed*> allFeeds();
// Get list of feeds from tree with particular item
// as root. If root itself is a feed, then it is returned.
// as root.
QList<Feed*> feedsForItem(RootItem *root);
// Returns list of ALL CHILD feeds which belong to given parent indexes.

View File

@ -112,8 +112,8 @@ void FeedsView::saveExpandedStates() {
// TODO: doědlat
// Iterate all categories and save their expand statuses.
/*foreach (Category *category, sourceModel()->allCategories().values()) {
/*
foreach (Category *category, sourceModel()->allCategories().values()) {
settings->setValue(GROUP(Categories),
QString::number(category->id()),
isExpanded(model()->mapFromSource(sourceModel()->indexForItem(category))));