2
* Copyright (C) 2012 Canonical, Ltd.
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.
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.
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/>.
18
#include "qsortfilterproxymodelqml.h"
23
QSortFilterProxyModelQML::QSortFilterProxyModelQML(QObject *parent)
24
: QSortFilterProxyModel(parent)
25
, m_invertMatch(false)
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()));
32
QHash<int, QByteArray> QSortFilterProxyModelQML::roleNames() const
34
return sourceModel() ? sourceModel()->roleNames() : QHash<int, QByteArray>();
38
QSortFilterProxyModelQML::setModel(QAbstractItemModel *itemModel)
40
if (itemModel == NULL) {
44
if (itemModel != sourceModel()) {
45
if (sourceModel() != NULL) {
46
sourceModel()->disconnect(this);
49
setSourceModel(itemModel);
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();
61
QSortFilterProxyModelQML::data(int row, int role)
63
if (sourceModel() == NULL) {
67
return index(row, 0).data(role);
71
QSortFilterProxyModelQML::totalCount() const
73
if (sourceModel() != NULL) {
74
return sourceModel()->rowCount();
81
QSortFilterProxyModelQML::count()
87
QSortFilterProxyModelQML::invertMatch() const
93
QSortFilterProxyModelQML::setInvertMatch(bool invertMatch)
95
if (invertMatch != m_invertMatch) {
96
m_invertMatch = invertMatch;
97
Q_EMIT invertMatchChanged(invertMatch);
102
QSortFilterProxyModelQML::filterAcceptsRow(int sourceRow,
103
const QModelIndex &sourceParent) const
105
// If there's no regexp set, always accept all rows indepenently of the invertMatch setting
106
if (filterRegExp().isEmpty()) {
110
bool result = QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
111
return (m_invertMatch) ? !result : result;
115
QSortFilterProxyModelQML::findFirst(int role, const QVariant& value) const
117
QModelIndexList matches = match(index(0, 0), role, value, 1, Qt::MatchExactly);
118
if (!matches.isEmpty()) {
119
return matches.first().row();