~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/gui/ksortfilterproxymodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007-2008 Omat Holding B.V. <info@omat.nl>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Library General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Library General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public License
 
15
 * along with this library; see the file COPYING.LIB.  If not, write to
 
16
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "ksortfilterproxymodel.h"
 
21
 
 
22
/**
 
23
 * Private class that helps to provide binary compatibility between releases.
 
24
 * @internal
 
25
 */
 
26
//@cond PRIVATE
 
27
class KSortFilterProxyModelPrivate {
 
28
    public:
 
29
        KSortFilterProxyModelPrivate() { 
 
30
            showAllChildren = false;
 
31
        }
 
32
        ~KSortFilterProxyModelPrivate() {}
 
33
       
 
34
        bool showAllChildren;
 
35
};
 
36
 
 
37
KSortFilterProxyModel::KSortFilterProxyModel(QObject * parent)
 
38
   : QSortFilterProxyModel(parent), d_ptr( new KSortFilterProxyModelPrivate )
 
39
{
 
40
}
 
41
 
 
42
KSortFilterProxyModel::~KSortFilterProxyModel()
 
43
{
 
44
    delete d_ptr;
 
45
}
 
46
 
 
47
bool KSortFilterProxyModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
 
48
{
 
49
    if( filterRegExp().isEmpty() ) return true; //Shortcut for common case
 
50
 
 
51
    if( QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent) )
 
52
        return true;
 
53
 
 
54
    //one of our children might be accepted, so accept this row if one of our children are accepted.
 
55
    QModelIndex source_index = sourceModel()->index(source_row, 0, source_parent);
 
56
    for(int i = 0 ; i < sourceModel()->rowCount(source_index); i++) {
 
57
        if(filterAcceptsRow(i, source_index)) return true;
 
58
    }
 
59
 
 
60
    //one of our parents might be accepted, so accept this row if one of our parents is accepted.
 
61
    if(d_ptr->showAllChildren) {
 
62
        QModelIndex parent_index = source_parent;
 
63
        while(parent_index.isValid()) {
 
64
            int row = parent_index.row();
 
65
            parent_index = parent_index.parent();
 
66
            if(QSortFilterProxyModel::filterAcceptsRow(row, parent_index)) return true;
 
67
        }
 
68
    }
 
69
 
 
70
    return false;
 
71
}
 
72
 
 
73
bool KSortFilterProxyModel::showAllChildren() const
 
74
{
 
75
    return d_ptr->showAllChildren;
 
76
}
 
77
void KSortFilterProxyModel::setShowAllChildren(bool showAllChildren)
 
78
{
 
79
    if(showAllChildren == d_ptr->showAllChildren)
 
80
        return;
 
81
    d_ptr->showAllChildren = showAllChildren;
 
82
    invalidateFilter();
 
83
}
 
84