2
* Copyright (C) 2012, 2013 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 "qlimitproxymodelqml.h"
23
QLimitProxyModelQML::QLimitProxyModelQML(QObject *parent)
24
: QIdentityProxyModel(parent)
26
, m_sourceInserting(false)
27
, m_sourceRemoving(false)
29
connect(this, SIGNAL(modelReset()), SIGNAL(countChanged()));
30
connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(countChanged()));
31
connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(countChanged()));
34
QHash<int, QByteArray> QLimitProxyModelQML::roleNames() const
36
return sourceModel() ? sourceModel()->roleNames() : QHash<int, QByteArray>();
40
QLimitProxyModelQML::setModel(QAbstractItemModel *itemModel)
42
if (sourceModel() != NULL) {
43
sourceModel()->disconnect(this);
46
if (itemModel != sourceModel()) {
47
setSourceModel(itemModel);
49
if (sourceModel() != NULL) {
50
// Disconnect the QIdentityProxyModel handling for rows removed/added...
51
disconnect(sourceModel(), SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, NULL);
52
disconnect(sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, NULL);
53
disconnect(sourceModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, NULL);
54
disconnect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, NULL);
56
// ... and use our own
57
connect(sourceModel(), SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
58
this, SLOT(sourceRowsAboutToBeInserted(QModelIndex,int,int)));
59
connect(sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
60
this, SLOT(sourceRowsInserted(QModelIndex,int,int)));
61
connect(sourceModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
62
this, SLOT(sourceRowsAboutToBeRemoved(QModelIndex,int,int)));
63
connect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
64
this, SLOT(sourceRowsRemoved(QModelIndex,int,int)));
66
Q_EMIT modelChanged();
71
QLimitProxyModelQML::rowCount(const QModelIndex &parent) const
73
if (parent.isValid()) // We are not a tree
76
const int unlimitedCount = QIdentityProxyModel::rowCount(parent);
77
return m_limit < 0 ? unlimitedCount : qMin(m_limit, unlimitedCount);
81
QLimitProxyModelQML::limit() const
87
QLimitProxyModelQML::setLimit(int limit)
89
if (limit != m_limit) {
90
bool inserting = false;
91
bool removing = false;
92
const int oldCount = rowCount();
93
const int unlimitedCount = QIdentityProxyModel::rowCount();
95
if (limit < oldCount) {
97
beginRemoveRows(QModelIndex(), limit, oldCount - 1);
99
} else if (limit < 0) {
100
if (m_limit < unlimitedCount) {
102
beginInsertRows(QModelIndex(), m_limit, unlimitedCount - 1);
105
if (limit > m_limit && unlimitedCount > m_limit) {
107
beginInsertRows(QModelIndex(), m_limit, qMin(limit, unlimitedCount) - 1);
108
} else if (limit < m_limit && limit < oldCount) {
110
beginRemoveRows(QModelIndex(), limit, oldCount - 1);
118
} else if (removing) {
122
Q_EMIT limitChanged();
127
QLimitProxyModelQML::sourceRowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
130
beginInsertRows(mapFromSource(parent), start, end);
131
m_sourceInserting = true;
132
} else if (start < m_limit) {
133
beginInsertRows(mapFromSource(parent), start, qMin(m_limit - 1, end));
134
m_sourceInserting = true;
139
QLimitProxyModelQML::sourceRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
142
beginRemoveRows(mapFromSource(parent), start, end);
143
m_sourceRemoving = true;
144
} else if (start < m_limit) {
145
beginRemoveRows(mapFromSource(parent), start, qMin(m_limit - 1, end));
146
m_sourceRemoving = true;
151
QLimitProxyModelQML::sourceRowsInserted(const QModelIndex & /*parent*/, int /*start*/, int /*end*/)
153
if (m_sourceInserting) {
155
m_sourceInserting = false;
160
QLimitProxyModelQML::sourceRowsRemoved(const QModelIndex & /*parent*/, int /*start*/, int /*end*/)
162
if (m_sourceRemoving) {
164
m_sourceRemoving = false;