Repository navigation
Expand file tree
/
Copy pathleaffilterproxymodel.cpp
More file actions
82 lines (67 loc) · 2.67 KB
/
Copy pathleaffilterproxymodel.cpp
File metadata and controls
82 lines (67 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "leaffilterproxymodel.h"
// with courtesy of Andre - https://forum.qt.io/topic/7606/qsortfilterproxymodel-filteracceptsrow-filter-parent-node-and-child-node/4
LeafFilterProxyModel::LeafFilterProxyModel(QObject *w_parent) :
QSortFilterProxyModel(w_parent)
{
//connect(this, SIGNAL(callParentSignal()), SLOT(emitFetchMore())); // local signal
connect(this, SIGNAL(callParentFetchSignal()), this->parent(), SLOT(fetchMorePatients()), Qt::UniqueConnection); // signal the parent
}
bool LeafFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if (filterAcceptsRowItself(source_row, source_parent))
return true;
//accept if any of the parents is accepted on it's own merits
QModelIndex parent = source_parent;
while (parent.isValid()) {
if (filterAcceptsRowItself(parent.row(), parent.parent()))
return true;
parent = parent.parent();
}
//accept if any of the children is accepted on it's own merits
if (hasAcceptedChildren(source_row, source_parent)) {
return true;
}
return false;
}
bool LeafFilterProxyModel::filterAcceptsRowItself(int source_row, const QModelIndex &source_parent) const
{
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
bool LeafFilterProxyModel::hasAcceptedChildren(int source_row, const QModelIndex &source_parent) const
{
QModelIndex item = sourceModel()->index(source_row,0,source_parent);
if (!item.isValid()) {
//qDebug() << "item invalid" << source_parent << source_row;
return false;
}
//check if there are children
int childCount = item.model()->rowCount(item);
if (childCount == 0)
return false;
for (int i = 0; i < childCount; ++i) {
if (filterAcceptsRowItself(i, item))
return true;
//recursive call -> NOTICE that this is depth-first searching, you're probably better off with breadth first search...
if (hasAcceptedChildren(i, item))
return true;
}
return false;
}
bool LeafFilterProxyModel::canFetchMore(const QModelIndex &parent) const{
//qDebug() << "canFetchMore is called on parent " << parent.row();
//qDebug() << parent.isValid();
if (parent.row() == -1){
//return !parent.isValid(); // return true only when not expanding existing parent
return false;
}
return false;
}
void LeafFilterProxyModel::fetchMore(const QModelIndex &parent){
//mainwindow->loadMorePatients();
//mainwindow->updatePatientTreeModel();
//emitFetchMore();
emit callParentFetchSignal();
}
void LeafFilterProxyModel::emitFetchMore(){
qDebug() << "got the local signal!";
}