From f6e6cd8a5c8ef2d8d7e331834dff026646be543e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 20 Aug 2017 13:55:56 -0600 Subject: Filter enabled folders using a krecursivefilterproxymodel The KRecursiveFilterProxyModel is necessary until Qt 5.10 when QSortFilterProxyModel will support recursive filtering. For the recursive filtering to work we need to make all data available, so we trigger fetchMore on all added indexes. --- framework/src/domain/folderlistmodel.cpp | 20 ++++++++++++++++++-- framework/src/domain/folderlistmodel.h | 7 +++---- 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'framework/src/domain') diff --git a/framework/src/domain/folderlistmodel.cpp b/framework/src/domain/folderlistmodel.cpp index 0abc70c5..1fe2abe6 100644 --- a/framework/src/domain/folderlistmodel.cpp +++ b/framework/src/domain/folderlistmodel.cpp @@ -26,11 +26,20 @@ using namespace Sink; using namespace Sink::ApplicationDomain; -FolderListModel::FolderListModel(QObject *parent) : QSortFilterProxyModel() +FolderListModel::FolderListModel(QObject *parent) : KRecursiveFilterProxyModel() { setDynamicSortFilter(true); sort(0, Qt::AscendingOrder); + //Automatically fetch all folders, otherwise the recursive filtering does not work. + QObject::connect(this, &QSortFilterProxyModel::sourceModelChanged, [this] () { + QObject::connect(sourceModel(), &QAbstractItemModel::rowsInserted, sourceModel(), [this] (QModelIndex parent, int first, int last) { + for (int row = first; row <= last; row++) { + auto idx = sourceModel()->index(row, 0, parent); + sourceModel()->fetchMore(idx); + } + }); + }); } FolderListModel::~FolderListModel() @@ -99,7 +108,6 @@ void FolderListModel::setAccountId(const QVariant &accountId) auto query = Query(); query.resourceFilter(account); query.setFlags(Sink::Query::LiveQuery | Sink::Query::UpdateStatus); - query.filter(true); query.request() .request() .request() @@ -144,6 +152,14 @@ bool FolderListModel::lessThan(const QModelIndex &left, const QModelIndex &right return leftPriority < rightPriority; } +bool FolderListModel::acceptRow(int sourceRow, const QModelIndex &sourceParent) const +{ + auto index = sourceModel()->index(sourceRow, 0, sourceParent); + const auto folder = index.data(Sink::Store::DomainObjectRole).value(); + const auto enabled = folder->getEnabled(); + return enabled; +} + void FolderListModel::setFolderId(const QVariant &folderId) { const auto folder = folderId.toString().toUtf8(); diff --git a/framework/src/domain/folderlistmodel.h b/framework/src/domain/folderlistmodel.h index 0e412202..738cf4a0 100644 --- a/framework/src/domain/folderlistmodel.h +++ b/framework/src/domain/folderlistmodel.h @@ -20,16 +20,14 @@ #pragma once -#include -#include +#include #include -#include namespace Sink { class Query; } -class FolderListModel : public QSortFilterProxyModel +class FolderListModel : public KRecursiveFilterProxyModel { Q_OBJECT @@ -69,6 +67,7 @@ public: QVariant folderId() const; protected: bool lessThan(const QModelIndex &left, const QModelIndex &right) const Q_DECL_OVERRIDE; + bool acceptRow(int sourceRow, const QModelIndex &sourceParent) const Q_DECL_OVERRIDE; private: void runQuery(const Sink::Query &query); -- cgit v1.2.3