~macslow/unity8/fix-1475678

« back to all changes in this revision

Viewing changes to plugins/Utils/qsortfilterproxymodelqml.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
// self
 
18
#include "qsortfilterproxymodelqml.h"
 
19
 
 
20
// Qt
 
21
#include <QDebug>
 
22
 
 
23
QSortFilterProxyModelQML::QSortFilterProxyModelQML(QObject *parent)
 
24
    : QSortFilterProxyModel(parent)
 
25
    , m_invertMatch(false)
 
26
{
 
27
    connect(this, SIGNAL(modelReset()), SIGNAL(countChanged()));
 
28
    connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(countChanged()));
 
29
    connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(countChanged()));
 
30
}
 
31
 
 
32
QHash<int, QByteArray> QSortFilterProxyModelQML::roleNames() const
 
33
{
 
34
    return sourceModel() ? sourceModel()->roleNames() : QHash<int, QByteArray>();
 
35
}
 
36
 
 
37
void
 
38
QSortFilterProxyModelQML::setModel(QAbstractItemModel *itemModel)
 
39
{
 
40
    if (itemModel == NULL) {
 
41
        return;
 
42
    }
 
43
 
 
44
    if (itemModel != sourceModel()) {
 
45
        if (sourceModel() != NULL) {
 
46
            sourceModel()->disconnect(this);
 
47
        }
 
48
 
 
49
        setSourceModel(itemModel);
 
50
 
 
51
        connect(itemModel, SIGNAL(modelReset()), SIGNAL(totalCountChanged()));
 
52
        connect(itemModel, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(totalCountChanged()));
 
53
        connect(itemModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(totalCountChanged()));
 
54
        Q_EMIT totalCountChanged();
 
55
        Q_EMIT countChanged();
 
56
        Q_EMIT modelChanged();
 
57
    }
 
58
}
 
59
 
 
60
QVariant
 
61
QSortFilterProxyModelQML::data(int row, int role)
 
62
{
 
63
    if (sourceModel() == NULL) {
 
64
        return QVariant();
 
65
    }
 
66
 
 
67
    return index(row, 0).data(role);
 
68
}
 
69
 
 
70
int
 
71
QSortFilterProxyModelQML::totalCount() const
 
72
{
 
73
    if (sourceModel() != NULL) {
 
74
        return sourceModel()->rowCount();
 
75
    } else {
 
76
        return 0;
 
77
    }
 
78
}
 
79
 
 
80
int
 
81
QSortFilterProxyModelQML::count()
 
82
{
 
83
    return rowCount();
 
84
}
 
85
 
 
86
bool
 
87
QSortFilterProxyModelQML::invertMatch() const
 
88
{
 
89
    return m_invertMatch;
 
90
}
 
91
 
 
92
void
 
93
QSortFilterProxyModelQML::setInvertMatch(bool invertMatch)
 
94
{
 
95
    if (invertMatch != m_invertMatch) {
 
96
        m_invertMatch = invertMatch;
 
97
        Q_EMIT invertMatchChanged(invertMatch);
 
98
    }
 
99
}
 
100
 
 
101
bool
 
102
QSortFilterProxyModelQML::filterAcceptsRow(int sourceRow,
 
103
                                           const QModelIndex &sourceParent) const
 
104
{
 
105
    // If there's no regexp set, always accept all rows indepenently of the invertMatch setting
 
106
    if (filterRegExp().isEmpty()) {
 
107
        return true;
 
108
    }
 
109
 
 
110
    bool result = QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
 
111
    return (m_invertMatch) ? !result : result;
 
112
}
 
113
 
 
114
int
 
115
QSortFilterProxyModelQML::findFirst(int role, const QVariant& value) const
 
116
{
 
117
    QModelIndexList matches = match(index(0, 0), role, value, 1, Qt::MatchExactly);
 
118
    if (!matches.isEmpty()) {
 
119
        return matches.first().row();
 
120
    } else {
 
121
        return -1;
 
122
    }
 
123
}