~fboucault/unity-2d/dash_category_underline_color

« back to all changes in this revision

Viewing changes to libunity-2d-private/Unity2d/qsortfilterproxymodelqml.cpp

  • Committer: Tarmac
  • Author(s): Florian Boucault
  • Date: 2011-07-27 12:31:33 UTC
  • mfrom: (615.5.9 proxy_model_count)
  • Revision ID: ubuntu.builder@gmail.com-20110727123133-2qfgqog3fx5630x8
[backend] New APIs for QSortFilterProxyModelQML and unit tests.

QSortFilterProxyModel: added 'count' and 'totalCount' properties that give respectively the number of rows of the proxy model and the total number of rows of the source model before filtering.
QSortFilterProxyModel: added 'limit' property to limit the number of rows exposed from the source model. Implemented by limiting the number returned by rowCount instead of overriding filterAcceptsRow.
Added unit tests for QSortFilterProxyModelQML. One of the test fails and has been deactivated. It is unsure if that broken case causes an actual user visible problem and should be revisited in the near future.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include <debug_p.h>
19
19
 
20
20
QSortFilterProxyModelQML::QSortFilterProxyModelQML(QObject *parent) :
21
 
    QSortFilterProxyModel(parent)
 
21
    QSortFilterProxyModel(parent), m_limit(-1)
22
22
{
 
23
    connect(this, SIGNAL(modelReset()), SIGNAL(countChanged()));
 
24
    connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(countChanged()));
 
25
    connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(countChanged()));
23
26
}
24
27
 
25
28
void QSortFilterProxyModelQML::setRoleNames(const QHash<int,QByteArray> &roleNames)
64
67
    setRoleNames(itemModel->roleNames());
65
68
 
66
69
    setSourceModel(itemModel);
 
70
 
 
71
    connect(itemModel, SIGNAL(modelReset()), SIGNAL(totalCountChanged()));
 
72
    connect(itemModel, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(totalCountChanged()));
 
73
    connect(itemModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(totalCountChanged()));
 
74
    Q_EMIT totalCountChanged();
 
75
    Q_EMIT countChanged();
67
76
}
68
77
 
69
78
QVariantMap
85
94
}
86
95
 
87
96
int
 
97
QSortFilterProxyModelQML::totalCount() const
 
98
{
 
99
    if (sourceModel() != NULL) {
 
100
        return sourceModel()->rowCount();
 
101
    } else {
 
102
        return 0;
 
103
    }
 
104
}
 
105
 
 
106
int
88
107
QSortFilterProxyModelQML::count()
89
108
{
90
109
    return rowCount();
91
110
}
 
111
 
 
112
int
 
113
QSortFilterProxyModelQML::rowCount(const QModelIndex &parent) const
 
114
{
 
115
    int count = QSortFilterProxyModel::rowCount(parent);
 
116
    if (m_limit >= 0) {
 
117
        return qMin(count, m_limit);
 
118
    } else {
 
119
        return count;
 
120
    }
 
121
}
 
122
 
 
123
int
 
124
QSortFilterProxyModelQML::limit() const
 
125
{
 
126
    return m_limit;
 
127
}
 
128
 
 
129
void
 
130
QSortFilterProxyModelQML::setLimit(int limit)
 
131
{
 
132
    if (limit != m_limit) {
 
133
        int count = QSortFilterProxyModel::rowCount();
 
134
        int start;
 
135
        int end;
 
136
        bool inserted = false;
 
137
        bool removed = false;
 
138
 
 
139
        if (m_limit == -1) {
 
140
            if (limit < count) {
 
141
                start = qMin(count, limit);
 
142
                end = count-1;
 
143
                removed = true;
 
144
            }
 
145
        } else if (limit == -1) {
 
146
            if (m_limit < count) {
 
147
                start = qMin(count, m_limit);
 
148
                end = count-1;
 
149
                inserted = true;
 
150
            }
 
151
        } else if (m_limit >= count && limit >= count) {
 
152
            // Nothing
 
153
        } else if (m_limit >= count && limit < count) {
 
154
            start = qMin(count, limit);
 
155
            end = count-1;
 
156
            removed = true;
 
157
        } else if (m_limit < count && limit >= count) {
 
158
            start = qMin(count, m_limit);
 
159
            end = count-1;
 
160
            inserted = true;
 
161
        } else if (m_limit < count && limit < count) {
 
162
            if (m_limit < limit) {
 
163
                start = qMin(count, m_limit);
 
164
                end = qMin(count, limit)-1;
 
165
                inserted = true;
 
166
            } else {
 
167
                start = qMin(count, limit);
 
168
                end = qMin(count, m_limit)-1;
 
169
                removed = true;
 
170
            }
 
171
        }
 
172
 
 
173
        if (inserted) {
 
174
            beginInsertRows(QModelIndex(), start, end);
 
175
        }
 
176
        if (removed) {
 
177
            beginRemoveRows(QModelIndex(), start, end);
 
178
        }
 
179
        m_limit = limit;
 
180
 
 
181
        if (inserted) {
 
182
            endInsertRows();
 
183
        }
 
184
        if (removed) {
 
185
            endRemoveRows();
 
186
        }
 
187
 
 
188
        Q_EMIT limitChanged();
 
189
    }
 
190
}