~fboucault/unity-2d/dash_default_resuts_non_empty

« back to all changes in this revision

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

  • Committer: Tarmac
  • Author(s): Florian Boucault
  • Date: 2011-09-08 14:12:10 UTC
  • mfrom: (705.1.1 more_robust_qsortfilter)
  • Revision ID: tarmac-20110908141210-rotaekemojv45pna
Simplify implementation of QSortFilterProxyModel's count property by overriding the filterAcceptRow method as recommended by the documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
112
112
}
113
113
 
114
114
int
115
 
QSortFilterProxyModelQML::rowCount(const QModelIndex &parent) const
116
 
{
117
 
    int count = QSortFilterProxyModel::rowCount(parent);
118
 
    if (m_limit >= 0) {
119
 
        return qMin(count, m_limit);
120
 
    } else {
121
 
        return count;
122
 
    }
123
 
}
124
 
 
125
 
int
126
115
QSortFilterProxyModelQML::limit() const
127
116
{
128
117
    return m_limit;
131
120
void
132
121
QSortFilterProxyModelQML::setLimit(int limit)
133
122
{
 
123
    if (limit != -1 && !filterRegExp().isEmpty()) {
 
124
        qFatal("QSortFilterProxyModel: filterRegExp and limit are both set which is not supported");
 
125
    }
 
126
 
134
127
    if (limit != m_limit) {
135
 
        int count = QSortFilterProxyModel::rowCount();
136
 
        int start;
137
 
        int end;
138
 
        bool inserted = false;
139
 
        bool removed = false;
140
 
 
141
 
        if (m_limit == -1) {
142
 
            if (limit < count) {
143
 
                start = qMin(count, limit);
144
 
                end = count-1;
145
 
                removed = true;
146
 
            }
147
 
        } else if (limit == -1) {
148
 
            if (m_limit < count) {
149
 
                start = qMin(count, m_limit);
150
 
                end = count-1;
151
 
                inserted = true;
152
 
            }
153
 
        } else if (m_limit >= count && limit >= count) {
154
 
            // Nothing
155
 
        } else if (m_limit >= count && limit < count) {
156
 
            start = qMin(count, limit);
157
 
            end = count-1;
158
 
            removed = true;
159
 
        } else if (m_limit < count && limit >= count) {
160
 
            start = qMin(count, m_limit);
161
 
            end = count-1;
162
 
            inserted = true;
163
 
        } else if (m_limit < count && limit < count) {
164
 
            if (m_limit < limit) {
165
 
                start = qMin(count, m_limit);
166
 
                end = qMin(count, limit)-1;
167
 
                inserted = true;
168
 
            } else {
169
 
                start = qMin(count, limit);
170
 
                end = qMin(count, m_limit)-1;
171
 
                removed = true;
172
 
            }
173
 
        }
174
 
 
175
 
        if (inserted) {
176
 
            beginInsertRows(QModelIndex(), start, end);
177
 
        }
178
 
        if (removed) {
179
 
            beginRemoveRows(QModelIndex(), start, end);
180
 
        }
181
128
        m_limit = limit;
182
 
 
183
 
        if (inserted) {
184
 
            endInsertRows();
185
 
        }
186
 
        if (removed) {
187
 
            endRemoveRows();
188
 
        }
189
 
 
 
129
        invalidateFilter();
190
130
        Q_EMIT limitChanged();
191
131
    }
192
132
}
210
150
QSortFilterProxyModelQML::filterAcceptsRow(int sourceRow,
211
151
                                           const QModelIndex &sourceParent) const
212
152
{
 
153
    if (m_limit != -1 && sourceRow >= m_limit) {
 
154
        return false;
 
155
    }
213
156
    // If there's no regexp set, always accept all rows indepenently of the invertMatch setting
214
157
    if (filterRegExp().isEmpty()) {
215
158
        return true;