From ef6721feee1127a196dc02fbcafa1e0310698ef7 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Sat, 28 Dec 2013 21:20:34 +0100 Subject: [PATCH] Ability to obtain index for item in feeds model. Added one feed. --- resources/misc/db_init.sql | 2 ++ src/core/feedsmodel.cpp | 41 +++++++++++++++++++++++++++++++++ src/core/feedsmodel.h | 14 +++++++---- src/gui/feedsview.cpp | 3 +++ src/gui/formcategorydetails.cpp | 2 -- 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/resources/misc/db_init.sql b/resources/misc/db_init.sql index f5234141a..c3333e177 100644 --- a/resources/misc/db_init.sql +++ b/resources/misc/db_init.sql @@ -72,6 +72,8 @@ INSERT INTO Feeds (title, description, date_created, category, encoding, url, ty -- ! INSERT INTO Feeds (title, description, date_created, category, encoding, url, type) VALUES ('LinuxInsider', 'LinuxInsider: Linux News & Information from Around the World.', '2013-12-20T08:00:00-05:00', 1, 'UTF-8', 'http://www.linuxinsider.com/perl/syndication/rssfull.pl', 2); -- ! +INSERT INTO Feeds (title, description, date_created, category, encoding, url, type) VALUES ('LXer: Linux News', 'The world is talking about GNU/Linux and Free/Open Source Software.', '2013-12-20T08:00:00-05:00', 1, 'UTF-8', 'http://lxer.com/module/newswire/headlines.rss', 2); +-- ! INSERT INTO Feeds (title, description, date_created, category, encoding, url, type) VALUES ('Recent Commits', 'Recent commits for RSS Guard project.', '2013-12-20T08:00:00-05:00', 2, 'UTF-8', 'https://github.com/martinrotter/rssguard/commits/master.atom', 3); -- ! INSERT INTO Feeds (title, description, date_created, category, encoding, url, type) VALUES ('Releases', 'Releases for RSS Guard.', '2013-12-20T08:00:00-05:00', 2, 'UTF-8', 'https://github.com/martinrotter/rssguard/releases.atom', 3); diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index 2539fc9dd..2d4bb8acc 100644 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -157,6 +157,47 @@ FeedsModelRootItem *FeedsModel::itemForIndex(const QModelIndex &index) const { } } +QModelIndex FeedsModel::indexForItem(FeedsModelRootItem *item) const { + if (item->kind() == FeedsModelRootItem::RootItem) { + // Root item lies on invalid index. + return QModelIndex(); + } + + QModelIndexList parents; + + // Start with invalid index (so that we start from the root + // item). + parents << QModelIndex(); + + while (!parents.isEmpty()) { + QModelIndex active_index = parents.takeFirst(); + int row_count = rowCount(active_index); + + // Iterate all childs of this parent. + for (int i = 0; i < row_count; i++) { + QModelIndex candidate_index = index(i, 0, active_index); + + // This index could be our target item. + FeedsModelRootItem *target_item = itemForIndex(candidate_index); + + if (target_item != NULL) { + if (FeedsModelRootItem::isEqual(target_item, item)) { + // We found our target index, it's good. + return candidate_index; + } + else if (hasChildren(candidate_index)) { + // This is not our target index but it has children, + // scan them too. + parents << candidate_index; + } + } + } + + } + + return QModelIndex(); +} + void FeedsModel::reloadChangedLayout(QModelIndexList list) { while (!list.isEmpty()) { QModelIndex ix = list.takeLast(); diff --git a/src/core/feedsmodel.h b/src/core/feedsmodel.h index d7fb69624..259b050d1 100644 --- a/src/core/feedsmodel.h +++ b/src/core/feedsmodel.h @@ -48,12 +48,20 @@ class FeedsModel : public QAbstractItemModel { // 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. + // 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); + // Returns feed/category which lies at the specified index or + // null if index is invalid. + FeedsModelRootItem *itemForIndex(const QModelIndex &index) const; + + // Returns QModelIndex on which lies given item. + QModelIndex indexForItem(FeedsModelRootItem *item) const; + // Access to root item. FeedsModelRootItem *rootItem() const; @@ -72,10 +80,6 @@ class FeedsModel : public QAbstractItemModel { protected: QStringList textualFeedIds(const QList &feeds); - // Returns feed/category which lies at the specified index or - // null if index is invalid. - FeedsModelRootItem *itemForIndex(const QModelIndex &index) const; - // Loads feed/categories from the database. void loadFromDatabase(); diff --git a/src/gui/feedsview.cpp b/src/gui/feedsview.cpp index f5d4f0e29..7135e05ad 100644 --- a/src/gui/feedsview.cpp +++ b/src/gui/feedsview.cpp @@ -141,7 +141,10 @@ void FeedsView::setupAppearance() { setUniformRowHeights(true); setAcceptDrops(false); setDragEnabled(false); + setAnimated(true); setSortingEnabled(true); + setItemsExpandable(true); + setExpandsOnDoubleClick(true); setIndentation(10); sortByColumn(0, Qt::AscendingOrder); setDragDropMode(QAbstractItemView::NoDragDrop); diff --git a/src/gui/formcategorydetails.cpp b/src/gui/formcategorydetails.cpp index f40b79648..c41d9b7e3 100644 --- a/src/gui/formcategorydetails.cpp +++ b/src/gui/formcategorydetails.cpp @@ -42,11 +42,9 @@ int FormCategoryDetails::exec(FeedsModelCategory *input_category, if (input_category == NULL) { // User is adding new category. - } else { // User is editing existing category. - } return result;