From 68cc35e3fdb5f578cff513c25ff28a5fd65de410 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Sun, 10 Nov 2013 03:30:04 +0200 Subject: [PATCH] Hide empty folders after filtering. Closes #74. --- src/torrentcontentfiltermodel.cpp | 26 ++++++++++++++++++++++++-- src/torrentcontentfiltermodel.h | 1 + 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/torrentcontentfiltermodel.cpp b/src/torrentcontentfiltermodel.cpp index f68fa2479..24341d449 100644 --- a/src/torrentcontentfiltermodel.cpp +++ b/src/torrentcontentfiltermodel.cpp @@ -75,8 +75,8 @@ QModelIndex TorrentContentFilterModel::parent(const QModelIndex& child) const bool TorrentContentFilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const { if (m_model->itemType(m_model->index(source_row, 0, source_parent)) == TorrentContentModelItem::FolderType) { - // always accept folders, since we want to filter their children - return true; + // accept folders if they have at least one filtered item + return hasFiltered(m_model->index(source_row, 0, source_parent)); } return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); } @@ -114,3 +114,25 @@ void TorrentContentFilterModel::selectNone() } emit dataChanged(index(0,0), index(rowCount(), columnCount())); } + +bool TorrentContentFilterModel::hasFiltered(const QModelIndex& folder) const { + // this should be called only with folders + // check if the folder name itself matches the filter string + QString name = folder.data().toString(); + if (name.contains(filterRegExp())) + return true; + for (int child = 0; child < m_model->rowCount(folder); child++) { + QModelIndex childIndex = m_model->index(child, 0, folder); + if (m_model->hasChildren(childIndex)) { + if (hasFiltered(childIndex)) + return true; + else + continue; + } + name = childIndex.data().toString(); + if (name.contains(filterRegExp())) + return true; + } + + return false; +} diff --git a/src/torrentcontentfiltermodel.h b/src/torrentcontentfiltermodel.h index 0fba55239..0a924f9a4 100644 --- a/src/torrentcontentfiltermodel.h +++ b/src/torrentcontentfiltermodel.h @@ -62,6 +62,7 @@ public slots: private: TorrentContentModel* m_model; + bool hasFiltered(const QModelIndex& folder) const; }; #endif // TORRENTCONTENTFILTERMODEL_H