0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtGui module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "qsortfilterproxymodel.h"
|
|
43 |
|
|
44 |
#ifndef QT_NO_SORTFILTERPROXYMODEL
|
|
45 |
|
|
46 |
#include "qitemselectionmodel.h"
|
|
47 |
#include <qsize.h>
|
|
48 |
#include <qdebug.h>
|
|
49 |
#include <qdatetime.h>
|
|
50 |
#include <qpair.h>
|
|
51 |
#include <qstringlist.h>
|
|
52 |
#include <private/qabstractitemmodel_p.h>
|
|
53 |
#include <private/qabstractproxymodel_p.h>
|
|
54 |
|
|
55 |
QT_BEGIN_NAMESPACE
|
|
56 |
|
|
57 |
typedef QList<QPair<QModelIndex, QPersistentModelIndex> > QModelIndexPairList;
|
|
58 |
|
|
59 |
static inline QSet<int> qVectorToSet(const QVector<int> &vector)
|
|
60 |
{
|
|
61 |
QSet<int> set;
|
|
62 |
set.reserve(vector.size());
|
|
63 |
for(int i=0; i < vector.size(); ++i)
|
|
64 |
set << vector.at(i);
|
|
65 |
return set;
|
|
66 |
}
|
|
67 |
|
|
68 |
class QSortFilterProxyModelLessThan
|
|
69 |
{
|
|
70 |
public:
|
|
71 |
inline QSortFilterProxyModelLessThan(int column, const QModelIndex &parent,
|
|
72 |
const QAbstractItemModel *source,
|
|
73 |
const QSortFilterProxyModel *proxy)
|
|
74 |
: sort_column(column), source_parent(parent), source_model(source), proxy_model(proxy) {}
|
|
75 |
|
|
76 |
inline bool operator()(int r1, int r2) const
|
|
77 |
{
|
|
78 |
QModelIndex i1 = source_model->index(r1, sort_column, source_parent);
|
|
79 |
QModelIndex i2 = source_model->index(r2, sort_column, source_parent);
|
|
80 |
return proxy_model->lessThan(i1, i2);
|
|
81 |
}
|
|
82 |
|
|
83 |
private:
|
|
84 |
int sort_column;
|
|
85 |
QModelIndex source_parent;
|
|
86 |
const QAbstractItemModel *source_model;
|
|
87 |
const QSortFilterProxyModel *proxy_model;
|
|
88 |
};
|
|
89 |
|
|
90 |
class QSortFilterProxyModelGreaterThan
|
|
91 |
{
|
|
92 |
public:
|
|
93 |
inline QSortFilterProxyModelGreaterThan(int column, const QModelIndex &parent,
|
|
94 |
const QAbstractItemModel *source,
|
|
95 |
const QSortFilterProxyModel *proxy)
|
|
96 |
: sort_column(column), source_parent(parent),
|
|
97 |
source_model(source), proxy_model(proxy) {}
|
|
98 |
|
|
99 |
inline bool operator()(int r1, int r2) const
|
|
100 |
{
|
|
101 |
QModelIndex i1 = source_model->index(r1, sort_column, source_parent);
|
|
102 |
QModelIndex i2 = source_model->index(r2, sort_column, source_parent);
|
|
103 |
return proxy_model->lessThan(i2, i1);
|
|
104 |
}
|
|
105 |
|
|
106 |
private:
|
|
107 |
int sort_column;
|
|
108 |
QModelIndex source_parent;
|
|
109 |
const QAbstractItemModel *source_model;
|
|
110 |
const QSortFilterProxyModel *proxy_model;
|
|
111 |
};
|
|
112 |
|
|
113 |
|
|
114 |
class QSortFilterProxyModelPrivate : public QAbstractProxyModelPrivate
|
|
115 |
{
|
|
116 |
Q_DECLARE_PUBLIC(QSortFilterProxyModel)
|
|
117 |
|
|
118 |
public:
|
|
119 |
struct Mapping {
|
|
120 |
QVector<int> source_rows;
|
|
121 |
QVector<int> source_columns;
|
|
122 |
QVector<int> proxy_rows;
|
|
123 |
QVector<int> proxy_columns;
|
|
124 |
QVector<QModelIndex> mapped_children;
|
|
125 |
QMap<QModelIndex, Mapping *>::const_iterator map_iter;
|
|
126 |
};
|
|
127 |
|
|
128 |
mutable QMap<QModelIndex, Mapping*> source_index_mapping;
|
|
129 |
|
|
130 |
int source_sort_column;
|
|
131 |
int proxy_sort_column;
|
|
132 |
Qt::SortOrder sort_order;
|
|
133 |
Qt::CaseSensitivity sort_casesensitivity;
|
|
134 |
int sort_role;
|
|
135 |
bool sort_localeaware;
|
|
136 |
|
|
137 |
int filter_column;
|
|
138 |
QRegExp filter_regexp;
|
|
139 |
int filter_role;
|
|
140 |
|
|
141 |
bool dynamic_sortfilter;
|
|
142 |
|
|
143 |
QModelIndexPairList saved_persistent_indexes;
|
|
144 |
|
|
145 |
QMap<QModelIndex, Mapping *>::const_iterator create_mapping(
|
|
146 |
const QModelIndex &source_parent) const;
|
|
147 |
QModelIndex proxy_to_source(const QModelIndex &proxyIndex) const;
|
|
148 |
QModelIndex source_to_proxy(const QModelIndex &sourceIndex) const;
|
|
149 |
bool can_create_mapping(const QModelIndex &source_parent) const;
|
|
150 |
|
|
151 |
void remove_from_mapping(const QModelIndex &source_parent);
|
|
152 |
|
|
153 |
inline QMap<QModelIndex, Mapping *>::const_iterator index_to_iterator(
|
|
154 |
const QModelIndex &proxy_index) const
|
|
155 |
{
|
|
156 |
Q_ASSERT(proxy_index.isValid());
|
|
157 |
Q_ASSERT(proxy_index.model() == q_func());
|
|
158 |
const void *p = proxy_index.internalPointer();
|
|
159 |
Q_ASSERT(p);
|
|
160 |
QMap<QModelIndex, Mapping *>::const_iterator it =
|
|
161 |
static_cast<const Mapping*>(p)->map_iter;
|
|
162 |
Q_ASSERT(it != source_index_mapping.constEnd());
|
|
163 |
Q_ASSERT(it.value());
|
|
164 |
return it;
|
|
165 |
}
|
|
166 |
|
|
167 |
inline QModelIndex create_index(int row, int column,
|
|
168 |
QMap<QModelIndex, Mapping*>::const_iterator it) const
|
|
169 |
{
|
|
170 |
return q_func()->createIndex(row, column, *it);
|
|
171 |
}
|
|
172 |
|
|
173 |
void _q_sourceDataChanged(const QModelIndex &source_top_left,
|
|
174 |
const QModelIndex &source_bottom_right);
|
|
175 |
void _q_sourceHeaderDataChanged(Qt::Orientation orientation, int start, int end);
|
|
176 |
|
|
177 |
void _q_sourceAboutToBeReset();
|
|
178 |
void _q_sourceReset();
|
|
179 |
|
|
180 |
void _q_sourceLayoutAboutToBeChanged();
|
|
181 |
void _q_sourceLayoutChanged();
|
|
182 |
|
|
183 |
void _q_sourceRowsAboutToBeInserted(const QModelIndex &source_parent,
|
|
184 |
int start, int end);
|
|
185 |
void _q_sourceRowsInserted(const QModelIndex &source_parent,
|
|
186 |
int start, int end);
|
|
187 |
void _q_sourceRowsAboutToBeRemoved(const QModelIndex &source_parent,
|
|
188 |
int start, int end);
|
|
189 |
void _q_sourceRowsRemoved(const QModelIndex &source_parent,
|
|
190 |
int start, int end);
|
|
191 |
void _q_sourceColumnsAboutToBeInserted(const QModelIndex &source_parent,
|
|
192 |
int start, int end);
|
|
193 |
void _q_sourceColumnsInserted(const QModelIndex &source_parent,
|
|
194 |
int start, int end);
|
|
195 |
void _q_sourceColumnsAboutToBeRemoved(const QModelIndex &source_parent,
|
|
196 |
int start, int end);
|
|
197 |
void _q_sourceColumnsRemoved(const QModelIndex &source_parent,
|
|
198 |
int start, int end);
|
|
199 |
|
|
200 |
void clear_mapping();
|
|
201 |
|
|
202 |
void sort();
|
|
203 |
bool update_source_sort_column();
|
|
204 |
void sort_source_rows(QVector<int> &source_rows,
|
|
205 |
const QModelIndex &source_parent) const;
|
|
206 |
QVector<QPair<int, QVector<int > > > proxy_intervals_for_source_items_to_add(
|
|
207 |
const QVector<int> &proxy_to_source, const QVector<int> &source_items,
|
|
208 |
const QModelIndex &source_parent, Qt::Orientation orient) const;
|
|
209 |
QVector<QPair<int, int > > proxy_intervals_for_source_items(
|
|
210 |
const QVector<int> &source_to_proxy, const QVector<int> &source_items) const;
|
|
211 |
void insert_source_items(
|
|
212 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source,
|
|
213 |
const QVector<int> &source_items, const QModelIndex &source_parent,
|
|
214 |
Qt::Orientation orient, bool emit_signal = true);
|
|
215 |
void remove_source_items(
|
|
216 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source,
|
|
217 |
const QVector<int> &source_items, const QModelIndex &source_parent,
|
|
218 |
Qt::Orientation orient, bool emit_signal = true);
|
|
219 |
void remove_proxy_interval(
|
|
220 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source,
|
|
221 |
int proxy_start, int proxy_end, const QModelIndex &proxy_parent,
|
|
222 |
Qt::Orientation orient, bool emit_signal = true);
|
|
223 |
void build_source_to_proxy_mapping(
|
|
224 |
const QVector<int> &proxy_to_source, QVector<int> &source_to_proxy) const;
|
|
225 |
void source_items_inserted(const QModelIndex &source_parent,
|
|
226 |
int start, int end, Qt::Orientation orient);
|
|
227 |
void source_items_about_to_be_removed(const QModelIndex &source_parent,
|
|
228 |
int start, int end, Qt::Orientation orient);
|
|
229 |
void source_items_removed(const QModelIndex &source_parent,
|
|
230 |
int start, int end, Qt::Orientation orient);
|
|
231 |
void proxy_item_range(
|
|
232 |
const QVector<int> &source_to_proxy, const QVector<int> &source_items,
|
|
233 |
int &proxy_low, int &proxy_high) const;
|
|
234 |
|
|
235 |
QModelIndexPairList store_persistent_indexes();
|
|
236 |
void update_persistent_indexes(const QModelIndexPairList &source_indexes);
|
|
237 |
|
|
238 |
void filter_changed(const QModelIndex &source_parent = QModelIndex());
|
|
239 |
QSet<int> handle_filter_changed(
|
|
240 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source,
|
|
241 |
const QModelIndex &source_parent, Qt::Orientation orient);
|
|
242 |
|
|
243 |
void updateChildrenMapping(const QModelIndex &source_parent, Mapping *parent_mapping,
|
|
244 |
Qt::Orientation orient, int start, int end, int delta_item_count, bool remove);
|
|
245 |
|
|
246 |
virtual void _q_sourceModelDestroyed();
|
|
247 |
};
|
|
248 |
|
|
249 |
typedef QMap<QModelIndex, QSortFilterProxyModelPrivate::Mapping *> IndexMap;
|
|
250 |
|
|
251 |
void QSortFilterProxyModelPrivate::_q_sourceModelDestroyed()
|
|
252 |
{
|
|
253 |
QAbstractProxyModelPrivate::_q_sourceModelDestroyed();
|
|
254 |
clear_mapping();
|
|
255 |
}
|
|
256 |
|
|
257 |
void QSortFilterProxyModelPrivate::remove_from_mapping(const QModelIndex &source_parent)
|
|
258 |
{
|
|
259 |
if (Mapping *m = source_index_mapping.take(source_parent)) {
|
|
260 |
for (int i = 0; i < m->mapped_children.size(); ++i)
|
|
261 |
remove_from_mapping(m->mapped_children.at(i));
|
|
262 |
delete m;
|
|
263 |
}
|
|
264 |
}
|
|
265 |
|
|
266 |
void QSortFilterProxyModelPrivate::clear_mapping()
|
|
267 |
{
|
|
268 |
// store the persistent indexes
|
|
269 |
QModelIndexPairList source_indexes = store_persistent_indexes();
|
|
270 |
|
|
271 |
qDeleteAll(source_index_mapping);
|
|
272 |
source_index_mapping.clear();
|
|
273 |
|
|
274 |
// update the persistent indexes
|
|
275 |
update_persistent_indexes(source_indexes);
|
|
276 |
}
|
|
277 |
|
|
278 |
IndexMap::const_iterator QSortFilterProxyModelPrivate::create_mapping(
|
|
279 |
const QModelIndex &source_parent) const
|
|
280 |
{
|
|
281 |
Q_Q(const QSortFilterProxyModel);
|
|
282 |
|
|
283 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_parent);
|
|
284 |
if (it != source_index_mapping.constEnd()) // was mapped already
|
|
285 |
return it;
|
|
286 |
|
|
287 |
Mapping *m = new Mapping;
|
|
288 |
|
|
289 |
int source_rows = model->rowCount(source_parent);
|
|
290 |
for (int i = 0; i < source_rows; ++i) {
|
|
291 |
if (q->filterAcceptsRow(i, source_parent))
|
|
292 |
m->source_rows.append(i);
|
|
293 |
}
|
|
294 |
int source_cols = model->columnCount(source_parent);
|
|
295 |
for (int i = 0; i < source_cols; ++i) {
|
|
296 |
if (q->filterAcceptsColumn(i, source_parent))
|
|
297 |
m->source_columns.append(i);
|
|
298 |
}
|
|
299 |
|
|
300 |
sort_source_rows(m->source_rows, source_parent);
|
|
301 |
m->proxy_rows.resize(source_rows);
|
|
302 |
build_source_to_proxy_mapping(m->source_rows, m->proxy_rows);
|
|
303 |
m->proxy_columns.resize(source_cols);
|
|
304 |
build_source_to_proxy_mapping(m->source_columns, m->proxy_columns);
|
|
305 |
|
|
306 |
it = IndexMap::const_iterator(source_index_mapping.insert(source_parent, m));
|
|
307 |
m->map_iter = it;
|
|
308 |
|
|
309 |
if (source_parent.isValid()) {
|
|
310 |
QModelIndex source_grand_parent = source_parent.parent();
|
|
311 |
IndexMap::const_iterator it2 = create_mapping(source_grand_parent);
|
|
312 |
Q_ASSERT(it2 != source_index_mapping.constEnd());
|
|
313 |
it2.value()->mapped_children.append(source_parent);
|
|
314 |
}
|
|
315 |
|
|
316 |
Q_ASSERT(it != source_index_mapping.constEnd());
|
|
317 |
Q_ASSERT(it.value());
|
|
318 |
|
|
319 |
return it;
|
|
320 |
}
|
|
321 |
|
|
322 |
QModelIndex QSortFilterProxyModelPrivate::proxy_to_source(const QModelIndex &proxy_index) const
|
|
323 |
{
|
|
324 |
if (!proxy_index.isValid())
|
|
325 |
return QModelIndex(); // for now; we may want to be able to set a root index later
|
|
326 |
if (proxy_index.model() != q_func()) {
|
|
327 |
qWarning() << "QSortFilterProxyModel: index from wrong model passed to mapToSource";
|
|
328 |
return QModelIndex();
|
|
329 |
}
|
|
330 |
IndexMap::const_iterator it = index_to_iterator(proxy_index);
|
|
331 |
Mapping *m = it.value();
|
|
332 |
if ((proxy_index.row() >= m->source_rows.size()) || (proxy_index.column() >= m->source_columns.size()))
|
|
333 |
return QModelIndex();
|
|
334 |
int source_row = m->source_rows.at(proxy_index.row());
|
|
335 |
int source_col = m->source_columns.at(proxy_index.column());
|
|
336 |
return model->index(source_row, source_col, it.key());
|
|
337 |
}
|
|
338 |
|
|
339 |
QModelIndex QSortFilterProxyModelPrivate::source_to_proxy(const QModelIndex &source_index) const
|
|
340 |
{
|
|
341 |
if (!source_index.isValid())
|
|
342 |
return QModelIndex(); // for now; we may want to be able to set a root index later
|
|
343 |
if (source_index.model() != model) {
|
|
344 |
qWarning() << "QSortFilterProxyModel: index from wrong model passed to mapFromSource";
|
|
345 |
return QModelIndex();
|
|
346 |
}
|
|
347 |
QModelIndex source_parent = source_index.parent();
|
|
348 |
IndexMap::const_iterator it = create_mapping(source_parent);
|
|
349 |
Mapping *m = it.value();
|
|
350 |
if ((source_index.row() >= m->proxy_rows.size()) || (source_index.column() >= m->proxy_columns.size()))
|
|
351 |
return QModelIndex();
|
|
352 |
int proxy_row = m->proxy_rows.at(source_index.row());
|
|
353 |
int proxy_column = m->proxy_columns.at(source_index.column());
|
|
354 |
if (proxy_row == -1 || proxy_column == -1)
|
|
355 |
return QModelIndex();
|
|
356 |
return create_index(proxy_row, proxy_column, it);
|
|
357 |
}
|
|
358 |
|
|
359 |
bool QSortFilterProxyModelPrivate::can_create_mapping(const QModelIndex &source_parent) const
|
|
360 |
{
|
|
361 |
if (source_parent.isValid()) {
|
|
362 |
QModelIndex source_grand_parent = source_parent.parent();
|
|
363 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_grand_parent);
|
|
364 |
if (it == source_index_mapping.constEnd()) {
|
|
365 |
// Don't care, since we don't have mapping for the grand parent
|
|
366 |
return false;
|
|
367 |
}
|
|
368 |
Mapping *gm = it.value();
|
|
369 |
if (gm->proxy_rows.at(source_parent.row()) == -1 ||
|
|
370 |
gm->proxy_columns.at(source_parent.column()) == -1) {
|
|
371 |
// Don't care, since parent is filtered
|
|
372 |
return false;
|
|
373 |
}
|
|
374 |
}
|
|
375 |
return true;
|
|
376 |
}
|
|
377 |
|
|
378 |
/*!
|
|
379 |
\internal
|
|
380 |
|
|
381 |
Sorts the existing mappings.
|
|
382 |
*/
|
|
383 |
void QSortFilterProxyModelPrivate::sort()
|
|
384 |
{
|
|
385 |
Q_Q(QSortFilterProxyModel);
|
|
386 |
emit q->layoutAboutToBeChanged();
|
|
387 |
QModelIndexPairList source_indexes = store_persistent_indexes();
|
|
388 |
IndexMap::const_iterator it = source_index_mapping.constBegin();
|
|
389 |
for (; it != source_index_mapping.constEnd(); ++it) {
|
|
390 |
QModelIndex source_parent = it.key();
|
|
391 |
Mapping *m = it.value();
|
|
392 |
sort_source_rows(m->source_rows, source_parent);
|
|
393 |
build_source_to_proxy_mapping(m->source_rows, m->proxy_rows);
|
|
394 |
}
|
|
395 |
update_persistent_indexes(source_indexes);
|
|
396 |
emit q->layoutChanged();
|
|
397 |
}
|
|
398 |
|
|
399 |
/*!
|
|
400 |
\internal
|
|
401 |
|
|
402 |
update the source_sort_column according to the proxy_sort_column
|
|
403 |
return true if the column was changed
|
|
404 |
*/
|
|
405 |
bool QSortFilterProxyModelPrivate::update_source_sort_column()
|
|
406 |
{
|
|
407 |
Q_Q(QSortFilterProxyModel);
|
|
408 |
QModelIndex proxy_index = q->index(0, proxy_sort_column, QModelIndex());
|
|
409 |
int old_source_sort_colum = source_sort_column;
|
|
410 |
source_sort_column = q->mapToSource(proxy_index).column();
|
|
411 |
return old_source_sort_colum != source_sort_column;
|
|
412 |
}
|
|
413 |
|
|
414 |
|
|
415 |
/*!
|
|
416 |
\internal
|
|
417 |
|
|
418 |
Sorts the given \a source_rows according to current sort column and order.
|
|
419 |
*/
|
|
420 |
void QSortFilterProxyModelPrivate::sort_source_rows(
|
|
421 |
QVector<int> &source_rows, const QModelIndex &source_parent) const
|
|
422 |
{
|
|
423 |
Q_Q(const QSortFilterProxyModel);
|
|
424 |
if (source_sort_column >= 0) {
|
|
425 |
if (sort_order == Qt::AscendingOrder) {
|
|
426 |
QSortFilterProxyModelLessThan lt(source_sort_column, source_parent, model, q);
|
|
427 |
qStableSort(source_rows.begin(), source_rows.end(), lt);
|
|
428 |
} else {
|
|
429 |
QSortFilterProxyModelGreaterThan gt(source_sort_column, source_parent, model, q);
|
|
430 |
qStableSort(source_rows.begin(), source_rows.end(), gt);
|
|
431 |
}
|
|
432 |
} else { // restore the source model order
|
|
433 |
qStableSort(source_rows.begin(), source_rows.end());
|
|
434 |
}
|
|
435 |
}
|
|
436 |
|
|
437 |
/*!
|
|
438 |
\internal
|
|
439 |
|
|
440 |
Given source-to-proxy mapping \a source_to_proxy and the set of
|
|
441 |
source items \a source_items (which are part of that mapping),
|
|
442 |
determines the corresponding proxy item intervals that should
|
|
443 |
be removed from the proxy model.
|
|
444 |
|
|
445 |
The result is a vector of pairs, where each pair represents a
|
|
446 |
(start, end) tuple, sorted in ascending order.
|
|
447 |
*/
|
|
448 |
QVector<QPair<int, int > > QSortFilterProxyModelPrivate::proxy_intervals_for_source_items(
|
|
449 |
const QVector<int> &source_to_proxy, const QVector<int> &source_items) const
|
|
450 |
{
|
|
451 |
QVector<QPair<int, int> > proxy_intervals;
|
|
452 |
if (source_items.isEmpty())
|
|
453 |
return proxy_intervals;
|
|
454 |
|
|
455 |
int source_items_index = 0;
|
|
456 |
while (source_items_index < source_items.size()) {
|
|
457 |
int first_proxy_item = source_to_proxy.at(source_items.at(source_items_index));
|
|
458 |
Q_ASSERT(first_proxy_item != -1);
|
|
459 |
int last_proxy_item = first_proxy_item;
|
|
460 |
++source_items_index;
|
|
461 |
// Find end of interval
|
|
462 |
while ((source_items_index < source_items.size())
|
|
463 |
&& (source_to_proxy.at(source_items.at(source_items_index)) == last_proxy_item + 1)) {
|
|
464 |
++last_proxy_item;
|
|
465 |
++source_items_index;
|
|
466 |
}
|
|
467 |
// Add interval to result
|
|
468 |
proxy_intervals.append(QPair<int, int>(first_proxy_item, last_proxy_item));
|
|
469 |
}
|
|
470 |
qStableSort(proxy_intervals.begin(), proxy_intervals.end());
|
|
471 |
return proxy_intervals;
|
|
472 |
}
|
|
473 |
|
|
474 |
/*!
|
|
475 |
\internal
|
|
476 |
|
|
477 |
Given source-to-proxy mapping \a src_to_proxy and proxy-to-source mapping
|
|
478 |
\a proxy_to_source, removes \a source_items from this proxy model.
|
|
479 |
The corresponding proxy items are removed in intervals, so that the proper
|
|
480 |
rows/columnsRemoved(start, end) signals will be generated.
|
|
481 |
*/
|
|
482 |
void QSortFilterProxyModelPrivate::remove_source_items(
|
|
483 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source,
|
|
484 |
const QVector<int> &source_items, const QModelIndex &source_parent,
|
|
485 |
Qt::Orientation orient, bool emit_signal)
|
|
486 |
{
|
|
487 |
Q_Q(QSortFilterProxyModel);
|
|
488 |
QModelIndex proxy_parent = q->mapFromSource(source_parent);
|
|
489 |
if (!proxy_parent.isValid() && source_parent.isValid())
|
|
490 |
return; // nothing to do (already removed)
|
|
491 |
|
|
492 |
QVector<QPair<int, int> > proxy_intervals;
|
|
493 |
proxy_intervals = proxy_intervals_for_source_items(source_to_proxy, source_items);
|
|
494 |
|
|
495 |
for (int i = proxy_intervals.size()-1; i >= 0; --i) {
|
|
496 |
QPair<int, int> interval = proxy_intervals.at(i);
|
|
497 |
int proxy_start = interval.first;
|
|
498 |
int proxy_end = interval.second;
|
|
499 |
remove_proxy_interval(source_to_proxy, proxy_to_source, proxy_start, proxy_end,
|
|
500 |
proxy_parent, orient, emit_signal);
|
|
501 |
}
|
|
502 |
}
|
|
503 |
|
|
504 |
/*!
|
|
505 |
\internal
|
|
506 |
|
|
507 |
Given source-to-proxy mapping \a source_to_proxy and proxy-to-source mapping
|
|
508 |
\a proxy_to_source, removes items from \a proxy_start to \a proxy_end
|
|
509 |
(inclusive) from this proxy model.
|
|
510 |
*/
|
|
511 |
void QSortFilterProxyModelPrivate::remove_proxy_interval(
|
|
512 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source, int proxy_start, int proxy_end,
|
|
513 |
const QModelIndex &proxy_parent, Qt::Orientation orient, bool emit_signal)
|
|
514 |
{
|
|
515 |
Q_Q(QSortFilterProxyModel);
|
|
516 |
if (emit_signal) {
|
|
517 |
if (orient == Qt::Vertical)
|
|
518 |
q->beginRemoveRows(proxy_parent, proxy_start, proxy_end);
|
|
519 |
else
|
|
520 |
q->beginRemoveColumns(proxy_parent, proxy_start, proxy_end);
|
|
521 |
}
|
|
522 |
|
|
523 |
// Remove items from proxy-to-source mapping
|
|
524 |
proxy_to_source.remove(proxy_start, proxy_end - proxy_start + 1);
|
|
525 |
|
|
526 |
build_source_to_proxy_mapping(proxy_to_source, source_to_proxy);
|
|
527 |
|
|
528 |
if (emit_signal) {
|
|
529 |
if (orient == Qt::Vertical)
|
|
530 |
q->endRemoveRows();
|
|
531 |
else
|
|
532 |
q->endRemoveColumns();
|
|
533 |
}
|
|
534 |
}
|
|
535 |
|
|
536 |
/*!
|
|
537 |
\internal
|
|
538 |
|
|
539 |
Given proxy-to-source mapping \a proxy_to_source and a set of
|
|
540 |
unmapped source items \a source_items, determines the proxy item
|
|
541 |
intervals at which the subsets of source items should be inserted
|
|
542 |
(but does not actually add them to the mapping).
|
|
543 |
|
|
544 |
The result is a vector of pairs, each pair representing a tuple (start,
|
|
545 |
items), where items is a vector containing the (sorted) source items that
|
|
546 |
should be inserted at that proxy model location.
|
|
547 |
*/
|
|
548 |
QVector<QPair<int, QVector<int > > > QSortFilterProxyModelPrivate::proxy_intervals_for_source_items_to_add(
|
|
549 |
const QVector<int> &proxy_to_source, const QVector<int> &source_items,
|
|
550 |
const QModelIndex &source_parent, Qt::Orientation orient) const
|
|
551 |
{
|
|
552 |
Q_Q(const QSortFilterProxyModel);
|
|
553 |
QVector<QPair<int, QVector<int> > > proxy_intervals;
|
|
554 |
if (source_items.isEmpty())
|
|
555 |
return proxy_intervals;
|
|
556 |
|
|
557 |
int proxy_low = 0;
|
|
558 |
int proxy_item = 0;
|
|
559 |
int source_items_index = 0;
|
|
560 |
QVector<int> source_items_in_interval;
|
|
561 |
bool compare = (orient == Qt::Vertical && source_sort_column >= 0);
|
|
562 |
while (source_items_index < source_items.size()) {
|
|
563 |
source_items_in_interval.clear();
|
|
564 |
int first_new_source_item = source_items.at(source_items_index);
|
|
565 |
source_items_in_interval.append(first_new_source_item);
|
|
566 |
++source_items_index;
|
|
567 |
|
|
568 |
// Find proxy item at which insertion should be started
|
|
569 |
int proxy_high = proxy_to_source.size() - 1;
|
|
570 |
QModelIndex i1 = compare ? model->index(first_new_source_item, source_sort_column, source_parent) : QModelIndex();
|
|
571 |
while (proxy_low <= proxy_high) {
|
|
572 |
proxy_item = (proxy_low + proxy_high) / 2;
|
|
573 |
if (compare) {
|
|
574 |
QModelIndex i2 = model->index(proxy_to_source.at(proxy_item), source_sort_column, source_parent);
|
|
575 |
if ((sort_order == Qt::AscendingOrder) ? q->lessThan(i1, i2) : q->lessThan(i2, i1))
|
|
576 |
proxy_high = proxy_item - 1;
|
|
577 |
else
|
|
578 |
proxy_low = proxy_item + 1;
|
|
579 |
} else {
|
|
580 |
if (first_new_source_item < proxy_to_source.at(proxy_item))
|
|
581 |
proxy_high = proxy_item - 1;
|
|
582 |
else
|
|
583 |
proxy_low = proxy_item + 1;
|
|
584 |
}
|
|
585 |
}
|
|
586 |
proxy_item = proxy_low;
|
|
587 |
|
|
588 |
// Find the sequence of new source items that should be inserted here
|
|
589 |
if (proxy_item >= proxy_to_source.size()) {
|
|
590 |
for ( ; source_items_index < source_items.size(); ++source_items_index)
|
|
591 |
source_items_in_interval.append(source_items.at(source_items_index));
|
|
592 |
} else {
|
|
593 |
i1 = compare ? model->index(proxy_to_source.at(proxy_item), source_sort_column, source_parent) : QModelIndex();
|
|
594 |
for ( ; source_items_index < source_items.size(); ++source_items_index) {
|
|
595 |
int new_source_item = source_items.at(source_items_index);
|
|
596 |
if (compare) {
|
|
597 |
QModelIndex i2 = model->index(new_source_item, source_sort_column, source_parent);
|
|
598 |
if ((sort_order == Qt::AscendingOrder) ? q->lessThan(i1, i2) : q->lessThan(i2, i1))
|
|
599 |
break;
|
|
600 |
} else {
|
|
601 |
if (proxy_to_source.at(proxy_item) < new_source_item)
|
|
602 |
break;
|
|
603 |
}
|
|
604 |
source_items_in_interval.append(new_source_item);
|
|
605 |
}
|
|
606 |
}
|
|
607 |
|
|
608 |
// Add interval to result
|
|
609 |
proxy_intervals.append(QPair<int, QVector<int> >(proxy_item, source_items_in_interval));
|
|
610 |
}
|
|
611 |
return proxy_intervals;
|
|
612 |
}
|
|
613 |
|
|
614 |
/*!
|
|
615 |
\internal
|
|
616 |
|
|
617 |
Given source-to-proxy mapping \a source_to_proxy and proxy-to-source mapping
|
|
618 |
\a proxy_to_source, inserts the given \a source_items into this proxy model.
|
|
619 |
The source items are inserted in intervals (based on some sorted order), so
|
|
620 |
that the proper rows/columnsInserted(start, end) signals will be generated.
|
|
621 |
*/
|
|
622 |
void QSortFilterProxyModelPrivate::insert_source_items(
|
|
623 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source,
|
|
624 |
const QVector<int> &source_items, const QModelIndex &source_parent,
|
|
625 |
Qt::Orientation orient, bool emit_signal)
|
|
626 |
{
|
|
627 |
Q_Q(QSortFilterProxyModel);
|
|
628 |
QModelIndex proxy_parent = q->mapFromSource(source_parent);
|
|
629 |
if (!proxy_parent.isValid() && source_parent.isValid())
|
|
630 |
return; // nothing to do (source_parent is not mapped)
|
|
631 |
|
|
632 |
QVector<QPair<int, QVector<int> > > proxy_intervals;
|
|
633 |
proxy_intervals = proxy_intervals_for_source_items_to_add(
|
|
634 |
proxy_to_source, source_items, source_parent, orient);
|
|
635 |
|
|
636 |
for (int i = proxy_intervals.size()-1; i >= 0; --i) {
|
|
637 |
QPair<int, QVector<int> > interval = proxy_intervals.at(i);
|
|
638 |
int proxy_start = interval.first;
|
|
639 |
QVector<int> source_items = interval.second;
|
|
640 |
int proxy_end = proxy_start + source_items.size() - 1;
|
|
641 |
|
|
642 |
if (emit_signal) {
|
|
643 |
if (orient == Qt::Vertical)
|
|
644 |
q->beginInsertRows(proxy_parent, proxy_start, proxy_end);
|
|
645 |
else
|
|
646 |
q->beginInsertColumns(proxy_parent, proxy_start, proxy_end);
|
|
647 |
}
|
|
648 |
|
|
649 |
for (int i = 0; i < source_items.size(); ++i)
|
|
650 |
proxy_to_source.insert(proxy_start + i, source_items.at(i));
|
|
651 |
|
|
652 |
build_source_to_proxy_mapping(proxy_to_source, source_to_proxy);
|
|
653 |
|
|
654 |
if (emit_signal) {
|
|
655 |
if (orient == Qt::Vertical)
|
|
656 |
q->endInsertRows();
|
|
657 |
else
|
|
658 |
q->endInsertColumns();
|
|
659 |
}
|
|
660 |
}
|
|
661 |
}
|
|
662 |
|
|
663 |
/*!
|
|
664 |
\internal
|
|
665 |
|
|
666 |
Handles source model items insertion (columnsInserted(), rowsInserted()).
|
|
667 |
Determines
|
|
668 |
1) which of the inserted items to also insert into proxy model (filtering),
|
|
669 |
2) where to insert the items into the proxy model (sorting),
|
|
670 |
then inserts those items.
|
|
671 |
The items are inserted into the proxy model in intervals (based on
|
|
672 |
sorted order), so that the proper rows/columnsInserted(start, end)
|
|
673 |
signals will be generated.
|
|
674 |
*/
|
|
675 |
void QSortFilterProxyModelPrivate::source_items_inserted(
|
|
676 |
const QModelIndex &source_parent, int start, int end, Qt::Orientation orient)
|
|
677 |
{
|
|
678 |
Q_Q(QSortFilterProxyModel);
|
|
679 |
if ((start < 0) || (end < 0))
|
|
680 |
return;
|
|
681 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_parent);
|
|
682 |
if (it == source_index_mapping.constEnd()) {
|
|
683 |
if (!can_create_mapping(source_parent))
|
|
684 |
return;
|
|
685 |
it = create_mapping(source_parent);
|
|
686 |
Mapping *m = it.value();
|
|
687 |
QModelIndex proxy_parent = q->mapFromSource(source_parent);
|
|
688 |
if (m->source_rows.count() > 0) {
|
|
689 |
q->beginInsertRows(proxy_parent, 0, m->source_rows.count() - 1);
|
|
690 |
q->endInsertRows();
|
|
691 |
}
|
|
692 |
if (m->source_columns.count() > 0) {
|
|
693 |
q->beginInsertColumns(proxy_parent, 0, m->source_columns.count() - 1);
|
|
694 |
q->endInsertColumns();
|
|
695 |
}
|
|
696 |
return;
|
|
697 |
}
|
|
698 |
|
|
699 |
Mapping *m = it.value();
|
|
700 |
QVector<int> &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns;
|
|
701 |
QVector<int> &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns;
|
|
702 |
|
|
703 |
int delta_item_count = end - start + 1;
|
|
704 |
int old_item_count = source_to_proxy.size();
|
|
705 |
|
|
706 |
updateChildrenMapping(source_parent, m, orient, start, end, delta_item_count, false);
|
|
707 |
|
|
708 |
// Expand source-to-proxy mapping to account for new items
|
|
709 |
if (start < 0 || start > source_to_proxy.size()) {
|
|
710 |
qWarning("QSortFilterProxyModel: invalid inserted rows reported by source model");
|
|
711 |
remove_from_mapping(source_parent);
|
|
712 |
return;
|
|
713 |
}
|
|
714 |
source_to_proxy.insert(start, delta_item_count, -1);
|
|
715 |
|
|
716 |
if (start < old_item_count) {
|
|
717 |
// Adjust existing "stale" indexes in proxy-to-source mapping
|
|
718 |
int proxy_count = proxy_to_source.size();
|
|
719 |
for (int proxy_item = 0; proxy_item < proxy_count; ++proxy_item) {
|
|
720 |
int source_item = proxy_to_source.at(proxy_item);
|
|
721 |
if (source_item >= start)
|
|
722 |
proxy_to_source.replace(proxy_item, source_item + delta_item_count);
|
|
723 |
}
|
|
724 |
build_source_to_proxy_mapping(proxy_to_source, source_to_proxy);
|
|
725 |
}
|
|
726 |
|
|
727 |
// Figure out which items to add to mapping based on filter
|
|
728 |
QVector<int> source_items;
|
|
729 |
for (int i = start; i <= end; ++i) {
|
|
730 |
if ((orient == Qt::Vertical)
|
|
731 |
? q->filterAcceptsRow(i, source_parent)
|
|
732 |
: q->filterAcceptsColumn(i, source_parent)) {
|
|
733 |
source_items.append(i);
|
|
734 |
}
|
|
735 |
}
|
|
736 |
|
|
737 |
// Sort and insert the items
|
|
738 |
if (orient == Qt::Vertical) // Only sort rows
|
|
739 |
sort_source_rows(source_items, source_parent);
|
|
740 |
insert_source_items(source_to_proxy, proxy_to_source, source_items, source_parent, orient);
|
|
741 |
}
|
|
742 |
|
|
743 |
/*!
|
|
744 |
\internal
|
|
745 |
|
|
746 |
Handles source model items removal
|
|
747 |
(columnsAboutToBeRemoved(), rowsAboutToBeRemoved()).
|
|
748 |
*/
|
|
749 |
void QSortFilterProxyModelPrivate::source_items_about_to_be_removed(
|
|
750 |
const QModelIndex &source_parent, int start, int end, Qt::Orientation orient)
|
|
751 |
{
|
|
752 |
if ((start < 0) || (end < 0))
|
|
753 |
return;
|
|
754 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_parent);
|
|
755 |
if (it == source_index_mapping.constEnd()) {
|
|
756 |
// Don't care, since we don't have mapping for this index
|
|
757 |
return;
|
|
758 |
}
|
|
759 |
|
|
760 |
Mapping *m = it.value();
|
|
761 |
QVector<int> &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns;
|
|
762 |
QVector<int> &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns;
|
|
763 |
|
|
764 |
// figure out which items to remove
|
|
765 |
QVector<int> source_items_to_remove;
|
|
766 |
int proxy_count = proxy_to_source.size();
|
|
767 |
for (int proxy_item = 0; proxy_item < proxy_count; ++proxy_item) {
|
|
768 |
int source_item = proxy_to_source.at(proxy_item);
|
|
769 |
if ((source_item >= start) && (source_item <= end))
|
|
770 |
source_items_to_remove.append(source_item);
|
|
771 |
}
|
|
772 |
|
|
773 |
remove_source_items(source_to_proxy, proxy_to_source, source_items_to_remove,
|
|
774 |
source_parent, orient);
|
|
775 |
}
|
|
776 |
|
|
777 |
/*!
|
|
778 |
\internal
|
|
779 |
|
|
780 |
Handles source model items removal (columnsRemoved(), rowsRemoved()).
|
|
781 |
*/
|
|
782 |
void QSortFilterProxyModelPrivate::source_items_removed(
|
|
783 |
const QModelIndex &source_parent, int start, int end, Qt::Orientation orient)
|
|
784 |
{
|
|
785 |
if ((start < 0) || (end < 0))
|
|
786 |
return;
|
|
787 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_parent);
|
|
788 |
if (it == source_index_mapping.constEnd()) {
|
|
789 |
// Don't care, since we don't have mapping for this index
|
|
790 |
return;
|
|
791 |
}
|
|
792 |
|
|
793 |
Mapping *m = it.value();
|
|
794 |
QVector<int> &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns;
|
|
795 |
QVector<int> &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns;
|
|
796 |
|
|
797 |
if (end >= source_to_proxy.size())
|
|
798 |
end = source_to_proxy.size() - 1;
|
|
799 |
|
|
800 |
// Shrink the source-to-proxy mapping to reflect the new item count
|
|
801 |
int delta_item_count = end - start + 1;
|
|
802 |
source_to_proxy.remove(start, delta_item_count);
|
|
803 |
|
|
804 |
int proxy_count = proxy_to_source.size();
|
|
805 |
if (proxy_count > source_to_proxy.size()) {
|
|
806 |
// mapping is in an inconsistent state -- redo the whole mapping
|
|
807 |
qWarning("QSortFilterProxyModel: inconsistent changes reported by source model");
|
|
808 |
remove_from_mapping(source_parent);
|
|
809 |
Q_Q(QSortFilterProxyModel);
|
|
810 |
q->reset();
|
|
811 |
return;
|
|
812 |
}
|
|
813 |
|
|
814 |
// Adjust "stale" indexes in proxy-to-source mapping
|
|
815 |
for (int proxy_item = 0; proxy_item < proxy_count; ++proxy_item) {
|
|
816 |
int source_item = proxy_to_source.at(proxy_item);
|
|
817 |
if (source_item >= start) {
|
|
818 |
Q_ASSERT(source_item - delta_item_count >= 0);
|
|
819 |
proxy_to_source.replace(proxy_item, source_item - delta_item_count);
|
|
820 |
}
|
|
821 |
}
|
|
822 |
build_source_to_proxy_mapping(proxy_to_source, source_to_proxy);
|
|
823 |
|
|
824 |
updateChildrenMapping(source_parent, m, orient, start, end, delta_item_count, true);
|
|
825 |
|
|
826 |
}
|
|
827 |
|
|
828 |
|
|
829 |
/*!
|
|
830 |
\internal
|
|
831 |
updates the mapping of the children when inserting or removing items
|
|
832 |
*/
|
|
833 |
void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &source_parent, Mapping *parent_mapping,
|
|
834 |
Qt::Orientation orient, int start, int end, int delta_item_count, bool remove)
|
|
835 |
{
|
|
836 |
// see if any mapped children should be (re)moved
|
|
837 |
QVector<QPair<QModelIndex, Mapping*> > moved_source_index_mappings;
|
|
838 |
QVector<QModelIndex>::iterator it2 = parent_mapping->mapped_children.begin();
|
|
839 |
for ( ; it2 != parent_mapping->mapped_children.end();) {
|
|
840 |
const QModelIndex source_child_index = *it2;
|
|
841 |
const int pos = (orient == Qt::Vertical)
|
|
842 |
? source_child_index.row()
|
|
843 |
: source_child_index.column();
|
|
844 |
if (pos < start) {
|
|
845 |
// not affected
|
|
846 |
++it2;
|
|
847 |
} else if (remove && pos <= end) {
|
|
848 |
// in the removed interval
|
|
849 |
it2 = parent_mapping->mapped_children.erase(it2);
|
|
850 |
remove_from_mapping(source_child_index);
|
|
851 |
} else {
|
|
852 |
// below the removed items -- recompute the index
|
|
853 |
QModelIndex new_index;
|
|
854 |
const int newpos = remove ? pos - delta_item_count : pos + delta_item_count;
|
|
855 |
if (orient == Qt::Vertical) {
|
|
856 |
new_index = model->index(newpos,
|
|
857 |
source_child_index.column(),
|
|
858 |
source_parent);
|
|
859 |
} else {
|
|
860 |
new_index = model->index(source_child_index.row(),
|
|
861 |
newpos,
|
|
862 |
source_parent);
|
|
863 |
}
|
|
864 |
*it2 = new_index;
|
|
865 |
++it2;
|
|
866 |
|
|
867 |
// update mapping
|
|
868 |
Mapping *cm = source_index_mapping.take(source_child_index);
|
|
869 |
Q_ASSERT(cm);
|
|
870 |
// we do not reinsert right away, because the new index might be identical with another, old index
|
|
871 |
moved_source_index_mappings.append(QPair<QModelIndex, Mapping*>(new_index, cm));
|
|
872 |
}
|
|
873 |
}
|
|
874 |
|
|
875 |
// reinsert moved, mapped indexes
|
|
876 |
QVector<QPair<QModelIndex, Mapping*> >::iterator it = moved_source_index_mappings.begin();
|
|
877 |
for (; it != moved_source_index_mappings.end(); ++it) {
|
|
878 |
#ifdef QT_STRICT_ITERATORS
|
|
879 |
source_index_mapping.insert((*it).first, (*it).second);
|
|
880 |
(*it).second->map_iter = source_index_mapping.constFind((*it).first);
|
|
881 |
#else
|
|
882 |
(*it).second->map_iter = source_index_mapping.insert((*it).first, (*it).second);
|
|
883 |
#endif
|
|
884 |
}
|
|
885 |
}
|
|
886 |
|
|
887 |
/*!
|
|
888 |
\internal
|
|
889 |
*/
|
|
890 |
void QSortFilterProxyModelPrivate::proxy_item_range(
|
|
891 |
const QVector<int> &source_to_proxy, const QVector<int> &source_items,
|
|
892 |
int &proxy_low, int &proxy_high) const
|
|
893 |
{
|
|
894 |
proxy_low = INT_MAX;
|
|
895 |
proxy_high = INT_MIN;
|
|
896 |
for (int i = 0; i < source_items.count(); ++i) {
|
|
897 |
int proxy_item = source_to_proxy.at(source_items.at(i));
|
|
898 |
Q_ASSERT(proxy_item != -1);
|
|
899 |
if (proxy_item < proxy_low)
|
|
900 |
proxy_low = proxy_item;
|
|
901 |
if (proxy_item > proxy_high)
|
|
902 |
proxy_high = proxy_item;
|
|
903 |
}
|
|
904 |
}
|
|
905 |
|
|
906 |
/*!
|
|
907 |
\internal
|
|
908 |
*/
|
|
909 |
void QSortFilterProxyModelPrivate::build_source_to_proxy_mapping(
|
|
910 |
const QVector<int> &proxy_to_source, QVector<int> &source_to_proxy) const
|
|
911 |
{
|
|
912 |
source_to_proxy.fill(-1);
|
|
913 |
int proxy_count = proxy_to_source.size();
|
|
914 |
for (int i = 0; i < proxy_count; ++i)
|
|
915 |
source_to_proxy[proxy_to_source.at(i)] = i;
|
|
916 |
}
|
|
917 |
|
|
918 |
/*!
|
|
919 |
\internal
|
|
920 |
|
|
921 |
Maps the persistent proxy indexes to source indexes and
|
|
922 |
returns the list of source indexes.
|
|
923 |
*/
|
|
924 |
QModelIndexPairList QSortFilterProxyModelPrivate::store_persistent_indexes()
|
|
925 |
{
|
|
926 |
Q_Q(QSortFilterProxyModel);
|
|
927 |
QModelIndexPairList source_indexes;
|
|
928 |
foreach (QPersistentModelIndexData *data, persistent.indexes) {
|
|
929 |
QModelIndex proxy_index = data->index;
|
|
930 |
QModelIndex source_index = q->mapToSource(proxy_index);
|
|
931 |
source_indexes.append(qMakePair(proxy_index, QPersistentModelIndex(source_index)));
|
|
932 |
}
|
|
933 |
return source_indexes;
|
|
934 |
}
|
|
935 |
|
|
936 |
/*!
|
|
937 |
\internal
|
|
938 |
|
|
939 |
Maps \a source_indexes to proxy indexes and stores those
|
|
940 |
as persistent indexes.
|
|
941 |
*/
|
|
942 |
void QSortFilterProxyModelPrivate::update_persistent_indexes(
|
|
943 |
const QModelIndexPairList &source_indexes)
|
|
944 |
{
|
|
945 |
Q_Q(QSortFilterProxyModel);
|
|
946 |
QModelIndexList from, to;
|
|
947 |
for (int i = 0; i < source_indexes.count(); ++i) {
|
|
948 |
QModelIndex source_index = source_indexes.at(i).second;
|
|
949 |
QModelIndex old_proxy_index = source_indexes.at(i).first;
|
|
950 |
create_mapping(source_index.parent());
|
|
951 |
QModelIndex proxy_index = q->mapFromSource(source_index);
|
|
952 |
from << old_proxy_index;
|
|
953 |
to << proxy_index;
|
|
954 |
}
|
|
955 |
q->changePersistentIndexList(from, to);
|
|
956 |
}
|
|
957 |
|
|
958 |
|
|
959 |
/*!
|
|
960 |
\internal
|
|
961 |
|
|
962 |
Updates the proxy model (adds/removes rows) based on the
|
|
963 |
new filter.
|
|
964 |
*/
|
|
965 |
void QSortFilterProxyModelPrivate::filter_changed(const QModelIndex &source_parent)
|
|
966 |
{
|
|
967 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_parent);
|
|
968 |
if (it == source_index_mapping.constEnd())
|
|
969 |
return;
|
|
970 |
Mapping *m = it.value();
|
|
971 |
QSet<int> rows_removed = handle_filter_changed(m->proxy_rows, m->source_rows, source_parent, Qt::Vertical);
|
|
972 |
QSet<int> columns_removed = handle_filter_changed(m->proxy_columns, m->source_columns, source_parent, Qt::Horizontal);
|
|
973 |
QVector<QModelIndex>::iterator it2 = m->mapped_children.end();
|
|
974 |
while (it2 != m->mapped_children.begin()) {
|
|
975 |
--it2;
|
|
976 |
const QModelIndex source_child_index = *it2;
|
|
977 |
if (rows_removed.contains(source_child_index.row()) || columns_removed.contains(source_child_index.column())) {
|
|
978 |
it2 = m->mapped_children.erase(it2);
|
|
979 |
remove_from_mapping(source_child_index);
|
|
980 |
} else {
|
|
981 |
filter_changed(source_child_index);
|
|
982 |
}
|
|
983 |
}
|
|
984 |
}
|
|
985 |
|
|
986 |
/*!
|
|
987 |
\internal
|
|
988 |
returns the removed items indexes
|
|
989 |
*/
|
|
990 |
QSet<int> QSortFilterProxyModelPrivate::handle_filter_changed(
|
|
991 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source,
|
|
992 |
const QModelIndex &source_parent, Qt::Orientation orient)
|
|
993 |
{
|
|
994 |
Q_Q(QSortFilterProxyModel);
|
|
995 |
// Figure out which mapped items to remove
|
|
996 |
QVector<int> source_items_remove;
|
|
997 |
for (int i = 0; i < proxy_to_source.count(); ++i) {
|
|
998 |
const int source_item = proxy_to_source.at(i);
|
|
999 |
if ((orient == Qt::Vertical)
|
|
1000 |
? !q->filterAcceptsRow(source_item, source_parent)
|
|
1001 |
: !q->filterAcceptsColumn(source_item, source_parent)) {
|
|
1002 |
// This source item does not satisfy the filter, so it must be removed
|
|
1003 |
source_items_remove.append(source_item);
|
|
1004 |
}
|
|
1005 |
}
|
|
1006 |
// Figure out which non-mapped items to insert
|
|
1007 |
QVector<int> source_items_insert;
|
|
1008 |
int source_count = source_to_proxy.size();
|
|
1009 |
for (int source_item = 0; source_item < source_count; ++source_item) {
|
|
1010 |
if (source_to_proxy.at(source_item) == -1) {
|
|
1011 |
if ((orient == Qt::Vertical)
|
|
1012 |
? q->filterAcceptsRow(source_item, source_parent)
|
|
1013 |
: q->filterAcceptsColumn(source_item, source_parent)) {
|
|
1014 |
// This source item satisfies the filter, so it must be added
|
|
1015 |
source_items_insert.append(source_item);
|
|
1016 |
}
|
|
1017 |
}
|
|
1018 |
}
|
|
1019 |
if (!source_items_remove.isEmpty() || !source_items_insert.isEmpty()) {
|
|
1020 |
// Do item removal and insertion
|
|
1021 |
remove_source_items(source_to_proxy, proxy_to_source,
|
|
1022 |
source_items_remove, source_parent, orient);
|
|
1023 |
if (orient == Qt::Vertical)
|
|
1024 |
sort_source_rows(source_items_insert, source_parent);
|
|
1025 |
insert_source_items(source_to_proxy, proxy_to_source,
|
|
1026 |
source_items_insert, source_parent, orient);
|
|
1027 |
}
|
|
1028 |
return qVectorToSet(source_items_remove);
|
|
1029 |
}
|
|
1030 |
|
|
1031 |
void QSortFilterProxyModelPrivate::_q_sourceDataChanged(const QModelIndex &source_top_left,
|
|
1032 |
const QModelIndex &source_bottom_right)
|
|
1033 |
{
|
|
1034 |
Q_Q(QSortFilterProxyModel);
|
|
1035 |
if (!source_top_left.isValid() || !source_bottom_right.isValid())
|
|
1036 |
return;
|
|
1037 |
QModelIndex source_parent = source_top_left.parent();
|
|
1038 |
IndexMap::const_iterator it = create_mapping(source_parent);
|
|
1039 |
if (it == source_index_mapping.constEnd()) {
|
|
1040 |
// Don't care, since we don't have mapping for this index
|
|
1041 |
return;
|
|
1042 |
}
|
|
1043 |
Mapping *m = it.value();
|
|
1044 |
|
|
1045 |
// Figure out how the source changes affect us
|
|
1046 |
QVector<int> source_rows_remove;
|
|
1047 |
QVector<int> source_rows_insert;
|
|
1048 |
QVector<int> source_rows_change;
|
|
1049 |
QVector<int> source_rows_resort;
|
|
1050 |
int end = qMin(source_bottom_right.row(), m->proxy_rows.count() - 1);
|
|
1051 |
for (int source_row = source_top_left.row(); source_row <= end; ++source_row) {
|
|
1052 |
if (dynamic_sortfilter) {
|
|
1053 |
if (m->proxy_rows.at(source_row) != -1) {
|
|
1054 |
if (!q->filterAcceptsRow(source_row, source_parent)) {
|
|
1055 |
// This source row no longer satisfies the filter, so it must be removed
|
|
1056 |
source_rows_remove.append(source_row);
|
|
1057 |
} else if (source_sort_column >= source_top_left.column() && source_sort_column <= source_bottom_right.column()) {
|
|
1058 |
// This source row has changed in a way that may affect sorted order
|
|
1059 |
source_rows_resort.append(source_row);
|
|
1060 |
} else {
|
|
1061 |
// This row has simply changed, without affecting filtering nor sorting
|
|
1062 |
source_rows_change.append(source_row);
|
|
1063 |
}
|
|
1064 |
} else {
|
|
1065 |
if (q->filterAcceptsRow(source_row, source_parent)) {
|
|
1066 |
// This source row now satisfies the filter, so it must be added
|
|
1067 |
source_rows_insert.append(source_row);
|
|
1068 |
}
|
|
1069 |
}
|
|
1070 |
} else {
|
|
1071 |
if (m->proxy_rows.at(source_row) != -1)
|
|
1072 |
source_rows_change.append(source_row);
|
|
1073 |
}
|
|
1074 |
}
|
|
1075 |
|
|
1076 |
if (!source_rows_remove.isEmpty()) {
|
|
1077 |
remove_source_items(m->proxy_rows, m->source_rows,
|
|
1078 |
source_rows_remove, source_parent, Qt::Vertical);
|
|
1079 |
QSet<int> source_rows_remove_set = qVectorToSet(source_rows_remove);
|
|
1080 |
QVector<QModelIndex>::iterator it = m->mapped_children.end();
|
|
1081 |
while (it != m->mapped_children.begin()) {
|
|
1082 |
--it;
|
|
1083 |
const QModelIndex source_child_index = *it;
|
|
1084 |
if (source_rows_remove_set.contains(source_child_index.row())) {
|
|
1085 |
it = m->mapped_children.erase(it);
|
|
1086 |
remove_from_mapping(source_child_index);
|
|
1087 |
}
|
|
1088 |
}
|
|
1089 |
}
|
|
1090 |
|
|
1091 |
if (!source_rows_resort.isEmpty()) {
|
|
1092 |
// Re-sort the rows
|
|
1093 |
emit q->layoutAboutToBeChanged();
|
|
1094 |
QModelIndexPairList source_indexes = store_persistent_indexes();
|
|
1095 |
remove_source_items(m->proxy_rows, m->source_rows, source_rows_resort,
|
|
1096 |
source_parent, Qt::Vertical, false);
|
|
1097 |
sort_source_rows(source_rows_resort, source_parent);
|
|
1098 |
insert_source_items(m->proxy_rows, m->source_rows, source_rows_resort,
|
|
1099 |
source_parent, Qt::Vertical, false);
|
|
1100 |
update_persistent_indexes(source_indexes);
|
|
1101 |
emit q->layoutChanged();
|
|
1102 |
// Make sure we also emit dataChanged for the rows
|
|
1103 |
source_rows_change += source_rows_resort;
|
|
1104 |
}
|
|
1105 |
|
|
1106 |
if (!source_rows_change.isEmpty()) {
|
|
1107 |
// Find the proxy row range
|
|
1108 |
int proxy_start_row;
|
|
1109 |
int proxy_end_row;
|
|
1110 |
proxy_item_range(m->proxy_rows, source_rows_change,
|
|
1111 |
proxy_start_row, proxy_end_row);
|
|
1112 |
// ### Find the proxy column range also
|
|
1113 |
if (proxy_end_row >= 0) {
|
|
1114 |
// the row was accepted, but some columns might still be filtered out
|
|
1115 |
int source_left_column = source_top_left.column();
|
|
1116 |
while (source_left_column < source_bottom_right.column()
|
|
1117 |
&& m->proxy_columns.at(source_left_column) == -1)
|
|
1118 |
++source_left_column;
|
|
1119 |
const QModelIndex proxy_top_left = create_index(
|
|
1120 |
proxy_start_row, m->proxy_columns.at(source_left_column), it);
|
|
1121 |
int source_right_column = source_bottom_right.column();
|
|
1122 |
while (source_right_column > source_top_left.column()
|
|
1123 |
&& m->proxy_columns.at(source_right_column) == -1)
|
|
1124 |
--source_right_column;
|
|
1125 |
const QModelIndex proxy_bottom_right = create_index(
|
|
1126 |
proxy_end_row, m->proxy_columns.at(source_right_column), it);
|
|
1127 |
emit q->dataChanged(proxy_top_left, proxy_bottom_right);
|
|
1128 |
}
|
|
1129 |
}
|
|
1130 |
|
|
1131 |
if (!source_rows_insert.isEmpty()) {
|
|
1132 |
sort_source_rows(source_rows_insert, source_parent);
|
|
1133 |
insert_source_items(m->proxy_rows, m->source_rows,
|
|
1134 |
source_rows_insert, source_parent, Qt::Vertical);
|
|
1135 |
}
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
void QSortFilterProxyModelPrivate::_q_sourceHeaderDataChanged(Qt::Orientation orientation,
|
|
1139 |
int start, int end)
|
|
1140 |
{
|
|
1141 |
Q_Q(QSortFilterProxyModel);
|
|
1142 |
Mapping *m = create_mapping(QModelIndex()).value();
|
|
1143 |
int proxy_start = (orientation == Qt::Vertical
|
|
1144 |
? m->proxy_rows.at(start)
|
|
1145 |
: m->proxy_columns.at(start));
|
|
1146 |
int proxy_end = (orientation == Qt::Vertical
|
|
1147 |
? m->proxy_rows.at(end)
|
|
1148 |
: m->proxy_columns.at(end));
|
|
1149 |
emit q->headerDataChanged(orientation, proxy_start, proxy_end);
|
|
1150 |
}
|
|
1151 |
|
|
1152 |
void QSortFilterProxyModelPrivate::_q_sourceAboutToBeReset()
|
|
1153 |
{
|
|
1154 |
Q_Q(QSortFilterProxyModel);
|
|
1155 |
q->beginResetModel();
|
|
1156 |
}
|
|
1157 |
|
|
1158 |
void QSortFilterProxyModelPrivate::_q_sourceReset()
|
|
1159 |
{
|
|
1160 |
Q_Q(QSortFilterProxyModel);
|
|
1161 |
// All internal structures are deleted in clear()
|
|
1162 |
q->endResetModel();
|
|
1163 |
update_source_sort_column();
|
|
1164 |
if (dynamic_sortfilter)
|
|
1165 |
sort();
|
|
1166 |
}
|
|
1167 |
|
|
1168 |
void QSortFilterProxyModelPrivate::_q_sourceLayoutAboutToBeChanged()
|
|
1169 |
{
|
|
1170 |
Q_Q(QSortFilterProxyModel);
|
|
1171 |
saved_persistent_indexes.clear();
|
|
1172 |
if (persistent.indexes.isEmpty())
|
|
1173 |
return;
|
|
1174 |
emit q->layoutAboutToBeChanged();
|
|
1175 |
saved_persistent_indexes = store_persistent_indexes();
|
|
1176 |
}
|
|
1177 |
|
|
1178 |
void QSortFilterProxyModelPrivate::_q_sourceLayoutChanged()
|
|
1179 |
{
|
|
1180 |
Q_Q(QSortFilterProxyModel);
|
|
1181 |
if (saved_persistent_indexes.isEmpty()) {
|
|
1182 |
q->invalidate();
|
|
1183 |
return;
|
|
1184 |
}
|
|
1185 |
|
|
1186 |
qDeleteAll(source_index_mapping);
|
|
1187 |
source_index_mapping.clear();
|
|
1188 |
|
|
1189 |
update_persistent_indexes(saved_persistent_indexes);
|
|
1190 |
saved_persistent_indexes.clear();
|
|
1191 |
|
|
1192 |
update_source_sort_column();
|
|
1193 |
|
|
1194 |
emit q->layoutChanged();
|
|
1195 |
}
|
|
1196 |
|
|
1197 |
void QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeInserted(
|
|
1198 |
const QModelIndex &source_parent, int start, int end)
|
|
1199 |
{
|
|
1200 |
Q_UNUSED(start);
|
|
1201 |
Q_UNUSED(end);
|
|
1202 |
//Force the creation of a mapping now, even if its empty.
|
|
1203 |
//We need it because the proxy can be acessed at the moment it emits rowsAboutToBeInserted in insert_source_items
|
|
1204 |
if (can_create_mapping(source_parent))
|
|
1205 |
create_mapping(source_parent);
|
|
1206 |
}
|
|
1207 |
|
|
1208 |
void QSortFilterProxyModelPrivate::_q_sourceRowsInserted(
|
|
1209 |
const QModelIndex &source_parent, int start, int end)
|
|
1210 |
{
|
|
1211 |
source_items_inserted(source_parent, start, end, Qt::Vertical);
|
|
1212 |
if (update_source_sort_column()) //previous call to update_source_sort_column may fail if the model has no column.
|
|
1213 |
sort(); // now it should succeed so we need to make sure to sort again
|
|
1214 |
}
|
|
1215 |
|
|
1216 |
void QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeRemoved(
|
|
1217 |
const QModelIndex &source_parent, int start, int end)
|
|
1218 |
{
|
|
1219 |
source_items_about_to_be_removed(source_parent, start, end,
|
|
1220 |
Qt::Vertical);
|
|
1221 |
}
|
|
1222 |
|
|
1223 |
void QSortFilterProxyModelPrivate::_q_sourceRowsRemoved(
|
|
1224 |
const QModelIndex &source_parent, int start, int end)
|
|
1225 |
{
|
|
1226 |
source_items_removed(source_parent, start, end, Qt::Vertical);
|
|
1227 |
}
|
|
1228 |
|
|
1229 |
void QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeInserted(
|
|
1230 |
const QModelIndex &source_parent, int start, int end)
|
|
1231 |
{
|
|
1232 |
Q_UNUSED(start);
|
|
1233 |
Q_UNUSED(end);
|
|
1234 |
//Force the creation of a mapping now, even if its empty.
|
|
1235 |
//We need it because the proxy can be acessed at the moment it emits columnsAboutToBeInserted in insert_source_items
|
|
1236 |
if (can_create_mapping(source_parent))
|
|
1237 |
create_mapping(source_parent);
|
|
1238 |
}
|
|
1239 |
|
|
1240 |
void QSortFilterProxyModelPrivate::_q_sourceColumnsInserted(
|
|
1241 |
const QModelIndex &source_parent, int start, int end)
|
|
1242 |
{
|
|
1243 |
Q_Q(const QSortFilterProxyModel);
|
|
1244 |
source_items_inserted(source_parent, start, end, Qt::Horizontal);
|
|
1245 |
|
|
1246 |
if (source_parent.isValid())
|
|
1247 |
return; //we sort according to the root column only
|
|
1248 |
if (source_sort_column == -1) {
|
|
1249 |
//we update the source_sort_column depending on the prox_sort_column
|
|
1250 |
if (update_source_sort_column())
|
|
1251 |
sort();
|
|
1252 |
} else {
|
|
1253 |
if (start <= source_sort_column)
|
|
1254 |
source_sort_column += end - start + 1;
|
|
1255 |
|
|
1256 |
proxy_sort_column = q->mapFromSource(model->index(0,source_sort_column, source_parent)).column();
|
|
1257 |
}
|
|
1258 |
}
|
|
1259 |
|
|
1260 |
void QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeRemoved(
|
|
1261 |
const QModelIndex &source_parent, int start, int end)
|
|
1262 |
{
|
|
1263 |
source_items_about_to_be_removed(source_parent, start, end,
|
|
1264 |
Qt::Horizontal);
|
|
1265 |
}
|
|
1266 |
|
|
1267 |
void QSortFilterProxyModelPrivate::_q_sourceColumnsRemoved(
|
|
1268 |
const QModelIndex &source_parent, int start, int end)
|
|
1269 |
{
|
|
1270 |
Q_Q(const QSortFilterProxyModel);
|
|
1271 |
source_items_removed(source_parent, start, end, Qt::Horizontal);
|
|
1272 |
|
|
1273 |
if (source_parent.isValid())
|
|
1274 |
return; //we sort according to the root column only
|
|
1275 |
if (start <= source_sort_column) {
|
|
1276 |
if (end < source_sort_column)
|
|
1277 |
source_sort_column -= end - start + 1;
|
|
1278 |
else
|
|
1279 |
source_sort_column = -1;
|
|
1280 |
}
|
|
1281 |
|
|
1282 |
proxy_sort_column = q->mapFromSource(model->index(0,source_sort_column, source_parent)).column();
|
|
1283 |
}
|
|
1284 |
|
|
1285 |
/*!
|
|
1286 |
\since 4.1
|
|
1287 |
\class QSortFilterProxyModel
|
|
1288 |
\brief The QSortFilterProxyModel class provides support for sorting and
|
|
1289 |
filtering data passed between another model and a view.
|
|
1290 |
|
|
1291 |
\ingroup model-view
|
|
1292 |
|
|
1293 |
QSortFilterProxyModel can be used for sorting items, filtering out items,
|
|
1294 |
or both. The model transforms the structure of a source model by mapping
|
|
1295 |
the model indexes it supplies to new indexes, corresponding to different
|
|
1296 |
locations, for views to use. This approach allows a given source model to
|
|
1297 |
be restructured as far as views are concerned without requiring any
|
|
1298 |
transformations on the underlying data, and without duplicating the data in
|
|
1299 |
memory.
|
|
1300 |
|
|
1301 |
Let's assume that we want to sort and filter the items provided by a custom
|
|
1302 |
model. The code to set up the model and the view, \e without sorting and
|
|
1303 |
filtering, would look like this:
|
|
1304 |
|
|
1305 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 1
|
|
1306 |
|
|
1307 |
To add sorting and filtering support to \c MyItemModel, we need to create
|
|
1308 |
a QSortFilterProxyModel, call setSourceModel() with the \c MyItemModel as
|
|
1309 |
argument, and install the QSortFilterProxyModel on the view:
|
|
1310 |
|
|
1311 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 0
|
|
1312 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 2
|
|
1313 |
|
|
1314 |
At this point, neither sorting nor filtering is enabled; the original data
|
|
1315 |
is displayed in the view. Any changes made through the
|
|
1316 |
QSortFilterProxyModel are applied to the original model.
|
|
1317 |
|
|
1318 |
The QSortFilterProxyModel acts as a wrapper for the original model. If you
|
|
1319 |
need to convert source \l{QModelIndex}es to sorted/filtered model indexes
|
|
1320 |
or vice versa, use mapToSource(), mapFromSource(), mapSelectionToSource(),
|
|
1321 |
and mapSelectionFromSource().
|
|
1322 |
|
|
1323 |
\note By default, the model does not dynamically re-sort and re-filter data
|
|
1324 |
whenever the original model changes. This behavior can be changed by
|
|
1325 |
setting the \l{QSortFilterProxyModel::dynamicSortFilter}{dynamicSortFilter}
|
|
1326 |
property.
|
|
1327 |
|
|
1328 |
The \l{itemviews/basicsortfiltermodel}{Basic Sort/Filter Model} and
|
|
1329 |
\l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model} examples
|
|
1330 |
illustrate how to use QSortFilterProxyModel to perform basic sorting and
|
|
1331 |
filtering and how to subclass it to implement custom behavior.
|
|
1332 |
|
|
1333 |
\section1 Sorting
|
|
1334 |
|
|
1335 |
QTableView and QTreeView have a
|
|
1336 |
\l{QTreeView::sortingEnabled}{sortingEnabled} property that controls
|
|
1337 |
whether the user can sort the view by clicking the view's horizontal
|
|
1338 |
header. For example:
|
|
1339 |
|
|
1340 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 3
|
|
1341 |
|
|
1342 |
When this feature is on (the default is off), clicking on a header section
|
|
1343 |
sorts the items according to that column. By clicking repeatedly, the user
|
|
1344 |
can alternate between ascending and descending order.
|
|
1345 |
|
|
1346 |
\image qsortfilterproxymodel-sorting.png A sorted QTreeView
|
|
1347 |
|
|
1348 |
Behind the scene, the view calls the sort() virtual function on the model
|
|
1349 |
to reorder the data in the model. To make your data sortable, you can
|
|
1350 |
either implement sort() in your model, or use a QSortFilterProxyModel to
|
|
1351 |
wrap your model -- QSortFilterProxyModel provides a generic sort()
|
|
1352 |
reimplementation that operates on the sortRole() (Qt::DisplayRole by
|
|
1353 |
default) of the items and that understands several data types, including
|
|
1354 |
\c int, QString, and QDateTime. For hierarchical models, sorting is applied
|
|
1355 |
recursively to all child items. String comparisons are case sensitive by
|
|
1356 |
default; this can be changed by setting the \l{QSortFilterProxyModel::}
|
|
1357 |
{sortCaseSensitivity} property.
|
|
1358 |
|
|
1359 |
Custom sorting behavior is achieved by subclassing
|
|
1360 |
QSortFilterProxyModel and reimplementing lessThan(), which is
|
|
1361 |
used to compare items. For example:
|
|
1362 |
|
|
1363 |
\snippet examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp 5
|
|
1364 |
|
|
1365 |
(This code snippet comes from the
|
|
1366 |
\l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model}
|
|
1367 |
example.)
|
|
1368 |
|
|
1369 |
An alternative approach to sorting is to disable sorting on the view and to
|
|
1370 |
impose a certain order to the user. This is done by explicitly calling
|
|
1371 |
sort() with the desired column and order as arguments on the
|
|
1372 |
QSortFilterProxyModel (or on the original model if it implements sort()).
|
|
1373 |
For example:
|
|
1374 |
|
|
1375 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 4
|
|
1376 |
|
|
1377 |
QSortFilterProxyModel can be sorted by column -1, in which case it returns
|
|
1378 |
to the sort order of the underlying source model.
|
|
1379 |
|
|
1380 |
\section1 Filtering
|
|
1381 |
|
|
1382 |
In addition to sorting, QSortFilterProxyModel can be used to hide items
|
|
1383 |
that do not match a certain filter. The filter is specified using a QRegExp
|
|
1384 |
object and is applied to the filterRole() (Qt::DisplayRole by default) of
|
|
1385 |
each item, for a given column. The QRegExp object can be used to match a
|
|
1386 |
regular expression, a wildcard pattern, or a fixed string. For example:
|
|
1387 |
|
|
1388 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 5
|
|
1389 |
|
|
1390 |
For hierarchical models, the filter is applied recursively to all children.
|
|
1391 |
If a parent item doesn't match the filter, none of its children will be
|
|
1392 |
shown.
|
|
1393 |
|
|
1394 |
A common use case is to let the user specify the filter regexp, wildcard
|
|
1395 |
pattern, or fixed string in a QLineEdit and to connect the
|
|
1396 |
\l{QLineEdit::textChanged()}{textChanged()} signal to setFilterRegExp(),
|
|
1397 |
setFilterWildcard(), or setFilterFixedString() to reapply the filter.
|
|
1398 |
|
|
1399 |
Custom filtering behavior can be achieved by reimplementing the
|
|
1400 |
filterAcceptsRow() and filterAcceptsColumn() functions. For
|
|
1401 |
example (from the \l{itemviews/customsortfiltermodel}
|
|
1402 |
{Custom Sort/Filter Model} example), the following implementation ignores
|
|
1403 |
the \l{QSortFilterProxyModel::filterKeyColumn}{filterKeyColumn} property
|
|
1404 |
and performs filtering on columns 0, 1, and 2:
|
|
1405 |
|
|
1406 |
\snippet examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp 3
|
|
1407 |
|
|
1408 |
(This code snippet comes from the
|
|
1409 |
\l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model}
|
|
1410 |
example.)
|
|
1411 |
|
|
1412 |
If you are working with large amounts of filtering and have to invoke
|
|
1413 |
invalidateFilter() repeatedly, using reset() may be more efficient,
|
|
1414 |
depending on the implementation of your model. However, reset() returns the
|
|
1415 |
proxy model to its original state, losing selection information, and will
|
|
1416 |
cause the proxy model to be repopulated.
|
|
1417 |
|
|
1418 |
\section1 Subclassing
|
|
1419 |
|
|
1420 |
Since QAbstractProxyModel and its subclasses are derived from
|
|
1421 |
QAbstractItemModel, much of the same advice about subclassing normal models
|
|
1422 |
also applies to proxy models. In addition, it is worth noting that many of
|
|
1423 |
the default implementations of functions in this class are written so that
|
|
1424 |
they call the equivalent functions in the relevant source model. This
|
|
1425 |
simple proxying mechanism may need to be overridden for source models with
|
|
1426 |
more complex behavior; for example, if the source model provides a custom
|
|
1427 |
hasChildren() implementation, you should also provide one in the proxy
|
|
1428 |
model.
|
|
1429 |
|
|
1430 |
\note Some general guidelines for subclassing models are available in the
|
|
1431 |
\l{Model Subclassing Reference}.
|
|
1432 |
|
|
1433 |
\sa QAbstractProxyModel, QAbstractItemModel, {Model/View Programming},
|
|
1434 |
{Basic Sort/Filter Model Example}, {Custom Sort/Filter Model Example}
|
|
1435 |
*/
|
|
1436 |
|
|
1437 |
/*!
|
|
1438 |
Constructs a sorting filter model with the given \a parent.
|
|
1439 |
*/
|
|
1440 |
|
|
1441 |
QSortFilterProxyModel::QSortFilterProxyModel(QObject *parent)
|
|
1442 |
: QAbstractProxyModel(*new QSortFilterProxyModelPrivate, parent)
|
|
1443 |
{
|
|
1444 |
Q_D(QSortFilterProxyModel);
|
|
1445 |
d->proxy_sort_column = d->source_sort_column = -1;
|
|
1446 |
d->sort_order = Qt::AscendingOrder;
|
|
1447 |
d->sort_casesensitivity = Qt::CaseSensitive;
|
|
1448 |
d->sort_role = Qt::DisplayRole;
|
|
1449 |
d->sort_localeaware = false;
|
|
1450 |
d->filter_column = 0;
|
|
1451 |
d->filter_role = Qt::DisplayRole;
|
|
1452 |
d->dynamic_sortfilter = false;
|
|
1453 |
connect(this, SIGNAL(modelReset()), this, SLOT(invalidate()));
|
|
1454 |
}
|
|
1455 |
|
|
1456 |
/*!
|
|
1457 |
Destroys this sorting filter model.
|
|
1458 |
*/
|
|
1459 |
QSortFilterProxyModel::~QSortFilterProxyModel()
|
|
1460 |
{
|
|
1461 |
Q_D(QSortFilterProxyModel);
|
|
1462 |
qDeleteAll(d->source_index_mapping);
|
|
1463 |
d->source_index_mapping.clear();
|
|
1464 |
}
|
|
1465 |
|
|
1466 |
/*!
|
|
1467 |
\reimp
|
|
1468 |
*/
|
|
1469 |
void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
|
|
1470 |
{
|
|
1471 |
Q_D(QSortFilterProxyModel);
|
|
1472 |
|
|
1473 |
disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
|
1474 |
this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
|
|
1475 |
|
|
1476 |
disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
|
|
1477 |
this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int)));
|
|
1478 |
|
|
1479 |
disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
|
|
1480 |
this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int)));
|
|
1481 |
|
|
1482 |
disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
|
1483 |
this, SLOT(_q_sourceRowsInserted(QModelIndex,int,int)));
|
|
1484 |
|
|
1485 |
disconnect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
|
|
1486 |
this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int)));
|
|
1487 |
|
|
1488 |
disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),
|
|
1489 |
this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int)));
|
|
1490 |
|
|
1491 |
disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
|
|
1492 |
this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int)));
|
|
1493 |
|
|
1494 |
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
|
1495 |
this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int)));
|
|
1496 |
|
|
1497 |
disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
|
|
1498 |
this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int)));
|
|
1499 |
|
|
1500 |
disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
|
|
1501 |
this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int)));
|
|
1502 |
|
|
1503 |
disconnect(d->model, SIGNAL(layoutAboutToBeChanged()),
|
|
1504 |
this, SLOT(_q_sourceLayoutAboutToBeChanged()));
|
|
1505 |
|
|
1506 |
disconnect(d->model, SIGNAL(layoutChanged()),
|
|
1507 |
this, SLOT(_q_sourceLayoutChanged()));
|
|
1508 |
|
|
1509 |
disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_sourceAboutToBeReset()));
|
|
1510 |
disconnect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset()));
|
|
1511 |
|
|
1512 |
QAbstractProxyModel::setSourceModel(sourceModel);
|
|
1513 |
|
|
1514 |
connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
|
1515 |
this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
|
|
1516 |
|
|
1517 |
connect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
|
|
1518 |
this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int)));
|
|
1519 |
|
|
1520 |
connect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
|
|
1521 |
this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int)));
|
|
1522 |
|
|
1523 |
connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
|
1524 |
this, SLOT(_q_sourceRowsInserted(QModelIndex,int,int)));
|
|
1525 |
|
|
1526 |
connect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
|
|
1527 |
this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int)));
|
|
1528 |
|
|
1529 |
connect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),
|
|
1530 |
this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int)));
|
|
1531 |
|
|
1532 |
connect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
|
|
1533 |
this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int)));
|
|
1534 |
|
|
1535 |
connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
|
1536 |
this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int)));
|
|
1537 |
|
|
1538 |
connect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
|
|
1539 |
this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int)));
|
|
1540 |
|
|
1541 |
connect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
|
|
1542 |
this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int)));
|
|
1543 |
|
|
1544 |
connect(d->model, SIGNAL(layoutAboutToBeChanged()),
|
|
1545 |
this, SLOT(_q_sourceLayoutAboutToBeChanged()));
|
|
1546 |
|
|
1547 |
connect(d->model, SIGNAL(layoutChanged()),
|
|
1548 |
this, SLOT(_q_sourceLayoutChanged()));
|
|
1549 |
|
|
1550 |
connect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_sourceAboutToBeReset()));
|
|
1551 |
connect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset()));
|
|
1552 |
|
|
1553 |
d->clear_mapping();
|
|
1554 |
reset();
|
|
1555 |
if (d->update_source_sort_column() && d->dynamic_sortfilter)
|
|
1556 |
d->sort();
|
|
1557 |
}
|
|
1558 |
|
|
1559 |
/*!
|
|
1560 |
\reimp
|
|
1561 |
*/
|
|
1562 |
QModelIndex QSortFilterProxyModel::index(int row, int column, const QModelIndex &parent) const
|
|
1563 |
{
|
|
1564 |
Q_D(const QSortFilterProxyModel);
|
|
1565 |
if (row < 0 || column < 0)
|
|
1566 |
return QModelIndex();
|
|
1567 |
|
|
1568 |
QModelIndex source_parent = mapToSource(parent); // parent is already mapped at this point
|
|
1569 |
IndexMap::const_iterator it = d->create_mapping(source_parent); // but make sure that the children are mapped
|
|
1570 |
if (it.value()->source_rows.count() <= row || it.value()->source_columns.count() <= column)
|
|
1571 |
return QModelIndex();
|
|
1572 |
|
|
1573 |
return d->create_index(row, column, it);
|
|
1574 |
}
|
|
1575 |
|
|
1576 |
/*!
|
|
1577 |
\reimp
|
|
1578 |
*/
|
|
1579 |
QModelIndex QSortFilterProxyModel::parent(const QModelIndex &child) const
|
|
1580 |
{
|
|
1581 |
Q_D(const QSortFilterProxyModel);
|
|
1582 |
if (!d->indexValid(child))
|
|
1583 |
return QModelIndex();
|
|
1584 |
IndexMap::const_iterator it = d->index_to_iterator(child);
|
|
1585 |
Q_ASSERT(it != d->source_index_mapping.constEnd());
|
|
1586 |
QModelIndex source_parent = it.key();
|
|
1587 |
QModelIndex proxy_parent = mapFromSource(source_parent);
|
|
1588 |
return proxy_parent;
|
|
1589 |
}
|
|
1590 |
|
|
1591 |
/*!
|
|
1592 |
\reimp
|
|
1593 |
*/
|
|
1594 |
int QSortFilterProxyModel::rowCount(const QModelIndex &parent) const
|
|
1595 |
{
|
|
1596 |
Q_D(const QSortFilterProxyModel);
|
|
1597 |
QModelIndex source_parent = mapToSource(parent);
|
|
1598 |
if (parent.isValid() && !source_parent.isValid())
|
|
1599 |
return 0;
|
|
1600 |
IndexMap::const_iterator it = d->create_mapping(source_parent);
|
|
1601 |
return it.value()->source_rows.count();
|
|
1602 |
}
|
|
1603 |
|
|
1604 |
/*!
|
|
1605 |
\reimp
|
|
1606 |
*/
|
|
1607 |
int QSortFilterProxyModel::columnCount(const QModelIndex &parent) const
|
|
1608 |
{
|
|
1609 |
Q_D(const QSortFilterProxyModel);
|
|
1610 |
QModelIndex source_parent = mapToSource(parent);
|
|
1611 |
if (parent.isValid() && !source_parent.isValid())
|
|
1612 |
return 0;
|
|
1613 |
IndexMap::const_iterator it = d->create_mapping(source_parent);
|
|
1614 |
return it.value()->source_columns.count();
|
|
1615 |
}
|
|
1616 |
|
|
1617 |
/*!
|
|
1618 |
\reimp
|
|
1619 |
*/
|
|
1620 |
bool QSortFilterProxyModel::hasChildren(const QModelIndex &parent) const
|
|
1621 |
{
|
|
1622 |
Q_D(const QSortFilterProxyModel);
|
|
1623 |
QModelIndex source_parent = mapToSource(parent);
|
|
1624 |
if (parent.isValid() && !source_parent.isValid())
|
|
1625 |
return false;
|
|
1626 |
if (!d->model->hasChildren(source_parent))
|
|
1627 |
return false;
|
|
1628 |
|
|
1629 |
if (d->model->canFetchMore(source_parent))
|
|
1630 |
return true; //we assume we might have children that can be fetched
|
|
1631 |
|
|
1632 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value();
|
|
1633 |
return m->source_rows.count() != 0 && m->source_columns.count() != 0;
|
|
1634 |
}
|
|
1635 |
|
|
1636 |
/*!
|
|
1637 |
\reimp
|
|
1638 |
*/
|
|
1639 |
QVariant QSortFilterProxyModel::data(const QModelIndex &index, int role) const
|
|
1640 |
{
|
|
1641 |
Q_D(const QSortFilterProxyModel);
|
|
1642 |
QModelIndex source_index = mapToSource(index);
|
|
1643 |
if (index.isValid() && !source_index.isValid())
|
|
1644 |
return QVariant();
|
|
1645 |
return d->model->data(source_index, role);
|
|
1646 |
}
|
|
1647 |
|
|
1648 |
/*!
|
|
1649 |
\reimp
|
|
1650 |
*/
|
|
1651 |
bool QSortFilterProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
1652 |
{
|
|
1653 |
Q_D(QSortFilterProxyModel);
|
|
1654 |
QModelIndex source_index = mapToSource(index);
|
|
1655 |
if (index.isValid() && !source_index.isValid())
|
|
1656 |
return false;
|
|
1657 |
return d->model->setData(source_index, value, role);
|
|
1658 |
}
|
|
1659 |
|
|
1660 |
/*!
|
|
1661 |
\reimp
|
|
1662 |
*/
|
|
1663 |
QVariant QSortFilterProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
1664 |
{
|
|
1665 |
Q_D(const QSortFilterProxyModel);
|
|
1666 |
IndexMap::const_iterator it = d->create_mapping(QModelIndex());
|
|
1667 |
if (it.value()->source_rows.count() * it.value()->source_columns.count() > 0)
|
|
1668 |
return QAbstractProxyModel::headerData(section, orientation, role);
|
|
1669 |
int source_section;
|
|
1670 |
if (orientation == Qt::Vertical) {
|
|
1671 |
if (section < 0 || section >= it.value()->source_rows.count())
|
|
1672 |
return QVariant();
|
|
1673 |
source_section = it.value()->source_rows.at(section);
|
|
1674 |
} else {
|
|
1675 |
if (section < 0 || section >= it.value()->source_columns.count())
|
|
1676 |
return QVariant();
|
|
1677 |
source_section = it.value()->source_columns.at(section);
|
|
1678 |
}
|
|
1679 |
return d->model->headerData(source_section, orientation, role);
|
|
1680 |
}
|
|
1681 |
|
|
1682 |
/*!
|
|
1683 |
\reimp
|
|
1684 |
*/
|
|
1685 |
bool QSortFilterProxyModel::setHeaderData(int section, Qt::Orientation orientation,
|
|
1686 |
const QVariant &value, int role)
|
|
1687 |
{
|
|
1688 |
Q_D(QSortFilterProxyModel);
|
|
1689 |
IndexMap::const_iterator it = d->create_mapping(QModelIndex());
|
|
1690 |
if (it.value()->source_rows.count() * it.value()->source_columns.count() > 0)
|
|
1691 |
return QAbstractProxyModel::setHeaderData(section, orientation, value, role);
|
|
1692 |
int source_section;
|
|
1693 |
if (orientation == Qt::Vertical) {
|
|
1694 |
if (section < 0 || section >= it.value()->source_rows.count())
|
|
1695 |
return false;
|
|
1696 |
source_section = it.value()->source_rows.at(section);
|
|
1697 |
} else {
|
|
1698 |
if (section < 0 || section >= it.value()->source_columns.count())
|
|
1699 |
return false;
|
|
1700 |
source_section = it.value()->source_columns.at(section);
|
|
1701 |
}
|
|
1702 |
return d->model->setHeaderData(source_section, orientation, value, role);
|
|
1703 |
}
|
|
1704 |
|
|
1705 |
/*!
|
|
1706 |
\reimp
|
|
1707 |
*/
|
|
1708 |
QMimeData *QSortFilterProxyModel::mimeData(const QModelIndexList &indexes) const
|
|
1709 |
{
|
|
1710 |
Q_D(const QSortFilterProxyModel);
|
|
1711 |
QModelIndexList source_indexes;
|
|
1712 |
for (int i = 0; i < indexes.count(); ++i)
|
|
1713 |
source_indexes << mapToSource(indexes.at(i));
|
|
1714 |
return d->model->mimeData(source_indexes);
|
|
1715 |
}
|
|
1716 |
|
|
1717 |
/*!
|
|
1718 |
\reimp
|
|
1719 |
*/
|
|
1720 |
QStringList QSortFilterProxyModel::mimeTypes() const
|
|
1721 |
{
|
|
1722 |
Q_D(const QSortFilterProxyModel);
|
|
1723 |
return d->model->mimeTypes();
|
|
1724 |
}
|
|
1725 |
|
|
1726 |
/*!
|
|
1727 |
\reimp
|
|
1728 |
*/
|
|
1729 |
Qt::DropActions QSortFilterProxyModel::supportedDropActions() const
|
|
1730 |
{
|
|
1731 |
Q_D(const QSortFilterProxyModel);
|
|
1732 |
return d->model->supportedDropActions();
|
|
1733 |
}
|
|
1734 |
|
|
1735 |
/*!
|
|
1736 |
\reimp
|
|
1737 |
*/
|
|
1738 |
bool QSortFilterProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
|
|
1739 |
int row, int column, const QModelIndex &parent)
|
|
1740 |
{
|
|
1741 |
Q_D(QSortFilterProxyModel);
|
|
1742 |
if ((row == -1) && (column == -1))
|
|
1743 |
return d->model->dropMimeData(data, action, -1, -1, mapToSource(parent));
|
|
1744 |
int source_destination_row = -1;
|
|
1745 |
int source_destination_column = -1;
|
|
1746 |
QModelIndex source_parent;
|
|
1747 |
if (row == rowCount(parent)) {
|
|
1748 |
source_parent = mapToSource(parent);
|
|
1749 |
source_destination_row = d->model->rowCount(source_parent);
|
|
1750 |
} else {
|
|
1751 |
QModelIndex proxy_index = index(row, column, parent);
|
|
1752 |
QModelIndex source_index = mapToSource(proxy_index);
|
|
1753 |
source_destination_row = source_index.row();
|
|
1754 |
source_destination_column = source_index.column();
|
|
1755 |
source_parent = source_index.parent();
|
|
1756 |
}
|
|
1757 |
return d->model->dropMimeData(data, action, source_destination_row,
|
|
1758 |
source_destination_column, source_parent);
|
|
1759 |
}
|
|
1760 |
|
|
1761 |
/*!
|
|
1762 |
\reimp
|
|
1763 |
*/
|
|
1764 |
bool QSortFilterProxyModel::insertRows(int row, int count, const QModelIndex &parent)
|
|
1765 |
{
|
|
1766 |
Q_D(QSortFilterProxyModel);
|
|
1767 |
if (row < 0 || count <= 0)
|
|
1768 |
return false;
|
|
1769 |
QModelIndex source_parent = mapToSource(parent);
|
|
1770 |
if (parent.isValid() && !source_parent.isValid())
|
|
1771 |
return false;
|
|
1772 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value();
|
|
1773 |
if (row > m->source_rows.count())
|
|
1774 |
return false;
|
|
1775 |
int source_row = (row >= m->source_rows.count()
|
|
1776 |
? m->source_rows.count()
|
|
1777 |
: m->source_rows.at(row));
|
|
1778 |
return d->model->insertRows(source_row, count, source_parent);
|
|
1779 |
}
|
|
1780 |
|
|
1781 |
/*!
|
|
1782 |
\reimp
|
|
1783 |
*/
|
|
1784 |
bool QSortFilterProxyModel::insertColumns(int column, int count, const QModelIndex &parent)
|
|
1785 |
{
|
|
1786 |
Q_D(QSortFilterProxyModel);
|
|
1787 |
if (column < 0|| count <= 0)
|
|
1788 |
return false;
|
|
1789 |
QModelIndex source_parent = mapToSource(parent);
|
|
1790 |
if (parent.isValid() && !source_parent.isValid())
|
|
1791 |
return false;
|
|
1792 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value();
|
|
1793 |
if (column > m->source_columns.count())
|
|
1794 |
return false;
|
|
1795 |
int source_column = (column >= m->source_columns.count()
|
|
1796 |
? m->source_columns.count()
|
|
1797 |
: m->source_columns.at(column));
|
|
1798 |
return d->model->insertColumns(source_column, count, source_parent);
|
|
1799 |
}
|
|
1800 |
|
|
1801 |
/*!
|
|
1802 |
\reimp
|
|
1803 |
*/
|
|
1804 |
bool QSortFilterProxyModel::removeRows(int row, int count, const QModelIndex &parent)
|
|
1805 |
{
|
|
1806 |
Q_D(QSortFilterProxyModel);
|
|
1807 |
if (row < 0 || count <= 0)
|
|
1808 |
return false;
|
|
1809 |
QModelIndex source_parent = mapToSource(parent);
|
|
1810 |
if (parent.isValid() && !source_parent.isValid())
|
|
1811 |
return false;
|
|
1812 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value();
|
|
1813 |
if (row + count > m->source_rows.count())
|
|
1814 |
return false;
|
|
1815 |
if ((count == 1)
|
|
1816 |
|| ((d->source_sort_column < 0) && (m->proxy_rows.count() == m->source_rows.count()))) {
|
|
1817 |
int source_row = m->source_rows.at(row);
|
|
1818 |
return d->model->removeRows(source_row, count, source_parent);
|
|
1819 |
}
|
|
1820 |
// remove corresponding source intervals
|
|
1821 |
// ### if this proves to be slow, we can switch to single-row removal
|
|
1822 |
QVector<int> rows;
|
|
1823 |
for (int i = row; i < row + count; ++i)
|
|
1824 |
rows.append(m->source_rows.at(i));
|
|
1825 |
qSort(rows.begin(), rows.end());
|
|
1826 |
|
|
1827 |
int pos = rows.count() - 1;
|
|
1828 |
bool ok = true;
|
|
1829 |
while (pos >= 0) {
|
|
1830 |
const int source_end = rows.at(pos--);
|
|
1831 |
int source_start = source_end;
|
|
1832 |
while ((pos >= 0) && (rows.at(pos) == (source_start - 1))) {
|
|
1833 |
--source_start;
|
|
1834 |
--pos;
|
|
1835 |
}
|
|
1836 |
ok = ok && d->model->removeRows(source_start, source_end - source_start + 1,
|
|
1837 |
source_parent);
|
|
1838 |
}
|
|
1839 |
return ok;
|
|
1840 |
}
|
|
1841 |
|
|
1842 |
/*!
|
|
1843 |
\reimp
|
|
1844 |
*/
|
|
1845 |
bool QSortFilterProxyModel::removeColumns(int column, int count, const QModelIndex &parent)
|
|
1846 |
{
|
|
1847 |
Q_D(QSortFilterProxyModel);
|
|
1848 |
if (column < 0 || count <= 0)
|
|
1849 |
return false;
|
|
1850 |
QModelIndex source_parent = mapToSource(parent);
|
|
1851 |
if (parent.isValid() && !source_parent.isValid())
|
|
1852 |
return false;
|
|
1853 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value();
|
|
1854 |
if (column + count > m->source_columns.count())
|
|
1855 |
return false;
|
|
1856 |
if ((count == 1) || (m->proxy_columns.count() == m->source_columns.count())) {
|
|
1857 |
int source_column = m->source_columns.at(column);
|
|
1858 |
return d->model->removeColumns(source_column, count, source_parent);
|
|
1859 |
}
|
|
1860 |
// remove corresponding source intervals
|
|
1861 |
QVector<int> columns;
|
|
1862 |
for (int i = column; i < column + count; ++i)
|
|
1863 |
columns.append(m->source_columns.at(i));
|
|
1864 |
|
|
1865 |
int pos = columns.count() - 1;
|
|
1866 |
bool ok = true;
|
|
1867 |
while (pos >= 0) {
|
|
1868 |
const int source_end = columns.at(pos--);
|
|
1869 |
int source_start = source_end;
|
|
1870 |
while ((pos >= 0) && (columns.at(pos) == (source_start - 1))) {
|
|
1871 |
--source_start;
|
|
1872 |
--pos;
|
|
1873 |
}
|
|
1874 |
ok = ok && d->model->removeColumns(source_start, source_end - source_start + 1,
|
|
1875 |
source_parent);
|
|
1876 |
}
|
|
1877 |
return ok;
|
|
1878 |
}
|
|
1879 |
|
|
1880 |
/*!
|
|
1881 |
\reimp
|
|
1882 |
*/
|
|
1883 |
void QSortFilterProxyModel::fetchMore(const QModelIndex &parent)
|
|
1884 |
{
|
|
1885 |
Q_D(QSortFilterProxyModel);
|
|
1886 |
QModelIndex source_parent;
|
|
1887 |
if (d->indexValid(parent))
|
|
1888 |
source_parent = mapToSource(parent);
|
|
1889 |
d->model->fetchMore(source_parent);
|
|
1890 |
}
|
|
1891 |
|
|
1892 |
/*!
|
|
1893 |
\reimp
|
|
1894 |
*/
|
|
1895 |
bool QSortFilterProxyModel::canFetchMore(const QModelIndex &parent) const
|
|
1896 |
{
|
|
1897 |
Q_D(const QSortFilterProxyModel);
|
|
1898 |
QModelIndex source_parent;
|
|
1899 |
if (d->indexValid(parent))
|
|
1900 |
source_parent = mapToSource(parent);
|
|
1901 |
return d->model->canFetchMore(source_parent);
|
|
1902 |
}
|
|
1903 |
|
|
1904 |
/*!
|
|
1905 |
\reimp
|
|
1906 |
*/
|
|
1907 |
Qt::ItemFlags QSortFilterProxyModel::flags(const QModelIndex &index) const
|
|
1908 |
{
|
|
1909 |
Q_D(const QSortFilterProxyModel);
|
|
1910 |
QModelIndex source_index;
|
|
1911 |
if (d->indexValid(index))
|
|
1912 |
source_index = mapToSource(index);
|
|
1913 |
return d->model->flags(source_index);
|
|
1914 |
}
|
|
1915 |
|
|
1916 |
/*!
|
|
1917 |
\reimp
|
|
1918 |
*/
|
|
1919 |
QModelIndex QSortFilterProxyModel::buddy(const QModelIndex &index) const
|
|
1920 |
{
|
|
1921 |
Q_D(const QSortFilterProxyModel);
|
|
1922 |
if (!d->indexValid(index))
|
|
1923 |
return QModelIndex();
|
|
1924 |
QModelIndex source_index = mapToSource(index);
|
|
1925 |
QModelIndex source_buddy = d->model->buddy(source_index);
|
|
1926 |
if (source_index == source_buddy)
|
|
1927 |
return index;
|
|
1928 |
return mapFromSource(source_buddy);
|
|
1929 |
}
|
|
1930 |
|
|
1931 |
/*!
|
|
1932 |
\reimp
|
|
1933 |
*/
|
|
1934 |
QModelIndexList QSortFilterProxyModel::match(const QModelIndex &start, int role,
|
|
1935 |
const QVariant &value, int hits,
|
|
1936 |
Qt::MatchFlags flags) const
|
|
1937 |
{
|
|
1938 |
return QAbstractProxyModel::match(start, role, value, hits, flags);
|
|
1939 |
}
|
|
1940 |
|
|
1941 |
/*!
|
|
1942 |
\reimp
|
|
1943 |
*/
|
|
1944 |
QSize QSortFilterProxyModel::span(const QModelIndex &index) const
|
|
1945 |
{
|
|
1946 |
Q_D(const QSortFilterProxyModel);
|
|
1947 |
QModelIndex source_index = mapToSource(index);
|
|
1948 |
if (index.isValid() && !source_index.isValid())
|
|
1949 |
return QSize();
|
|
1950 |
return d->model->span(source_index);
|
|
1951 |
}
|
|
1952 |
|
|
1953 |
/*!
|
|
1954 |
\reimp
|
|
1955 |
*/
|
|
1956 |
void QSortFilterProxyModel::sort(int column, Qt::SortOrder order)
|
|
1957 |
{
|
|
1958 |
Q_D(QSortFilterProxyModel);
|
|
1959 |
if (d->dynamic_sortfilter && d->proxy_sort_column == column && d->sort_order == order)
|
|
1960 |
return;
|
|
1961 |
d->sort_order = order;
|
|
1962 |
d->proxy_sort_column = column;
|
|
1963 |
d->update_source_sort_column();
|
|
1964 |
d->sort();
|
|
1965 |
}
|
|
1966 |
|
|
1967 |
/*!
|
|
1968 |
\since 4.5
|
|
1969 |
\brief the column currently used for sorting
|
|
1970 |
|
|
1971 |
This returns the most recently used sort column.
|
|
1972 |
*/
|
|
1973 |
int QSortFilterProxyModel::sortColumn() const
|
|
1974 |
{
|
|
1975 |
Q_D(const QSortFilterProxyModel);
|
|
1976 |
return d->proxy_sort_column;
|
|
1977 |
}
|
|
1978 |
|
|
1979 |
/*!
|
|
1980 |
\since 4.5
|
|
1981 |
\brief the order currently used for sorting
|
|
1982 |
|
|
1983 |
This returns the most recently used sort order.
|
|
1984 |
*/
|
|
1985 |
Qt::SortOrder QSortFilterProxyModel::sortOrder() const
|
|
1986 |
{
|
|
1987 |
Q_D(const QSortFilterProxyModel);
|
|
1988 |
return d->sort_order;
|
|
1989 |
}
|
|
1990 |
|
|
1991 |
/*!
|
|
1992 |
\property QSortFilterProxyModel::filterRegExp
|
|
1993 |
\brief the QRegExp used to filter the contents of the source model
|
|
1994 |
|
|
1995 |
Setting this property overwrites the current
|
|
1996 |
\l{QSortFilterProxyModel::filterCaseSensitivity}{filterCaseSensitivity}.
|
|
1997 |
By default, the QRegExp is an empty string matching all contents.
|
|
1998 |
|
|
1999 |
If no QRegExp or an empty string is set, everything in the source model
|
|
2000 |
will be accepted.
|
|
2001 |
|
|
2002 |
\sa filterCaseSensitivity, setFilterWildcard(), setFilterFixedString()
|
|
2003 |
*/
|
|
2004 |
QRegExp QSortFilterProxyModel::filterRegExp() const
|
|
2005 |
{
|
|
2006 |
Q_D(const QSortFilterProxyModel);
|
|
2007 |
return d->filter_regexp;
|
|
2008 |
}
|
|
2009 |
|
|
2010 |
void QSortFilterProxyModel::setFilterRegExp(const QRegExp ®Exp)
|
|
2011 |
{
|
|
2012 |
Q_D(QSortFilterProxyModel);
|
|
2013 |
d->filter_regexp = regExp;
|
|
2014 |
d->filter_changed();
|
|
2015 |
}
|
|
2016 |
|
|
2017 |
/*!
|
|
2018 |
\property QSortFilterProxyModel::filterKeyColumn
|
|
2019 |
\brief the column where the key used to filter the contents of the
|
|
2020 |
source model is read from.
|
|
2021 |
|
|
2022 |
The default value is 0. If the value is -1, the keys will be read
|
|
2023 |
from all columns.
|
|
2024 |
*/
|
|
2025 |
int QSortFilterProxyModel::filterKeyColumn() const
|
|
2026 |
{
|
|
2027 |
Q_D(const QSortFilterProxyModel);
|
|
2028 |
return d->filter_column;
|
|
2029 |
}
|
|
2030 |
|
|
2031 |
void QSortFilterProxyModel::setFilterKeyColumn(int column)
|
|
2032 |
{
|
|
2033 |
Q_D(QSortFilterProxyModel);
|
|
2034 |
d->filter_column = column;
|
|
2035 |
d->filter_changed();
|
|
2036 |
}
|
|
2037 |
|
|
2038 |
/*!
|
|
2039 |
\property QSortFilterProxyModel::filterCaseSensitivity
|
|
2040 |
|
|
2041 |
\brief the case sensitivity of the QRegExp pattern used to filter the
|
|
2042 |
contents of the source model
|
|
2043 |
|
|
2044 |
By default, the filter is case sensitive.
|
|
2045 |
|
|
2046 |
\sa filterRegExp, sortCaseSensitivity
|
|
2047 |
*/
|
|
2048 |
Qt::CaseSensitivity QSortFilterProxyModel::filterCaseSensitivity() const
|
|
2049 |
{
|
|
2050 |
Q_D(const QSortFilterProxyModel);
|
|
2051 |
return d->filter_regexp.caseSensitivity();
|
|
2052 |
}
|
|
2053 |
|
|
2054 |
void QSortFilterProxyModel::setFilterCaseSensitivity(Qt::CaseSensitivity cs)
|
|
2055 |
{
|
|
2056 |
Q_D(QSortFilterProxyModel);
|
|
2057 |
if (cs == d->filter_regexp.caseSensitivity())
|
|
2058 |
return;
|
|
2059 |
d->filter_regexp.setCaseSensitivity(cs);
|
|
2060 |
d->filter_changed();
|
|
2061 |
}
|
|
2062 |
|
|
2063 |
/*!
|
|
2064 |
\since 4.2
|
|
2065 |
\property QSortFilterProxyModel::sortCaseSensitivity
|
|
2066 |
\brief the case sensitivity setting used for comparing strings when sorting
|
|
2067 |
|
|
2068 |
By default, sorting is case sensitive.
|
|
2069 |
|
|
2070 |
\sa filterCaseSensitivity, lessThan()
|
|
2071 |
*/
|
|
2072 |
Qt::CaseSensitivity QSortFilterProxyModel::sortCaseSensitivity() const
|
|
2073 |
{
|
|
2074 |
Q_D(const QSortFilterProxyModel);
|
|
2075 |
return d->sort_casesensitivity;
|
|
2076 |
}
|
|
2077 |
|
|
2078 |
void QSortFilterProxyModel::setSortCaseSensitivity(Qt::CaseSensitivity cs)
|
|
2079 |
{
|
|
2080 |
Q_D(QSortFilterProxyModel);
|
|
2081 |
if (d->sort_casesensitivity == cs)
|
|
2082 |
return;
|
|
2083 |
|
|
2084 |
d->sort_casesensitivity = cs;
|
|
2085 |
d->sort();
|
|
2086 |
}
|
|
2087 |
|
|
2088 |
/*!
|
|
2089 |
\since 4.3
|
|
2090 |
\property QSortFilterProxyModel::isSortLocaleAware
|
|
2091 |
\brief the local aware setting used for comparing strings when sorting
|
|
2092 |
|
|
2093 |
By default, sorting is not local aware.
|
|
2094 |
|
|
2095 |
\sa sortCaseSensitivity, lessThan()
|
|
2096 |
*/
|
|
2097 |
bool QSortFilterProxyModel::isSortLocaleAware() const
|
|
2098 |
{
|
|
2099 |
Q_D(const QSortFilterProxyModel);
|
|
2100 |
return d->sort_localeaware;
|
|
2101 |
}
|
|
2102 |
|
|
2103 |
void QSortFilterProxyModel::setSortLocaleAware(bool on)
|
|
2104 |
{
|
|
2105 |
Q_D(QSortFilterProxyModel);
|
|
2106 |
if (d->sort_localeaware == on)
|
|
2107 |
return;
|
|
2108 |
|
|
2109 |
d->sort_localeaware = on;
|
|
2110 |
d->sort();
|
|
2111 |
}
|
|
2112 |
|
|
2113 |
/*!
|
|
2114 |
\overload
|
|
2115 |
|
|
2116 |
Sets the regular expression used to filter the contents
|
|
2117 |
of the source model to \a pattern.
|
|
2118 |
|
|
2119 |
\sa setFilterCaseSensitivity(), setFilterWildcard(), setFilterFixedString(), filterRegExp()
|
|
2120 |
*/
|
|
2121 |
void QSortFilterProxyModel::setFilterRegExp(const QString &pattern)
|
|
2122 |
{
|
|
2123 |
Q_D(QSortFilterProxyModel);
|
|
2124 |
d->filter_regexp.setPatternSyntax(QRegExp::RegExp);
|
|
2125 |
d->filter_regexp.setPattern(pattern);
|
|
2126 |
d->filter_changed();
|
|
2127 |
}
|
|
2128 |
|
|
2129 |
/*!
|
|
2130 |
Sets the wildcard expression used to filter the contents
|
|
2131 |
of the source model to the given \a pattern.
|
|
2132 |
|
|
2133 |
\sa setFilterCaseSensitivity(), setFilterRegExp(), setFilterFixedString(), filterRegExp()
|
|
2134 |
*/
|
|
2135 |
void QSortFilterProxyModel::setFilterWildcard(const QString &pattern)
|
|
2136 |
{
|
|
2137 |
Q_D(QSortFilterProxyModel);
|
|
2138 |
d->filter_regexp.setPatternSyntax(QRegExp::Wildcard);
|
|
2139 |
d->filter_regexp.setPattern(pattern);
|
|
2140 |
d->filter_changed();
|
|
2141 |
}
|
|
2142 |
|
|
2143 |
/*!
|
|
2144 |
Sets the fixed string used to filter the contents
|
|
2145 |
of the source model to the given \a pattern.
|
|
2146 |
|
|
2147 |
\sa setFilterCaseSensitivity(), setFilterRegExp(), setFilterWildcard(), filterRegExp()
|
|
2148 |
*/
|
|
2149 |
void QSortFilterProxyModel::setFilterFixedString(const QString &pattern)
|
|
2150 |
{
|
|
2151 |
Q_D(QSortFilterProxyModel);
|
|
2152 |
d->filter_regexp.setPatternSyntax(QRegExp::FixedString);
|
|
2153 |
d->filter_regexp.setPattern(pattern);
|
|
2154 |
d->filter_changed();
|
|
2155 |
}
|
|
2156 |
|
|
2157 |
/*!
|
|
2158 |
\since 4.2
|
|
2159 |
\property QSortFilterProxyModel::dynamicSortFilter
|
|
2160 |
\brief whether the proxy model is dynamically sorted and filtered
|
|
2161 |
whenever the contents of the source model change
|
|
2162 |
|
|
2163 |
The default value is false.
|
|
2164 |
*/
|
|
2165 |
bool QSortFilterProxyModel::dynamicSortFilter() const
|
|
2166 |
{
|
|
2167 |
Q_D(const QSortFilterProxyModel);
|
|
2168 |
return d->dynamic_sortfilter;
|
|
2169 |
}
|
|
2170 |
|
|
2171 |
void QSortFilterProxyModel::setDynamicSortFilter(bool enable)
|
|
2172 |
{
|
|
2173 |
Q_D(QSortFilterProxyModel);
|
|
2174 |
d->dynamic_sortfilter = enable;
|
|
2175 |
if (enable)
|
|
2176 |
d->sort();
|
|
2177 |
}
|
|
2178 |
|
|
2179 |
/*!
|
|
2180 |
\since 4.2
|
|
2181 |
\property QSortFilterProxyModel::sortRole
|
|
2182 |
\brief the item role that is used to query the source model's data when sorting items
|
|
2183 |
|
|
2184 |
The default value is Qt::DisplayRole.
|
|
2185 |
|
|
2186 |
\sa lessThan()
|
|
2187 |
*/
|
|
2188 |
int QSortFilterProxyModel::sortRole() const
|
|
2189 |
{
|
|
2190 |
Q_D(const QSortFilterProxyModel);
|
|
2191 |
return d->sort_role;
|
|
2192 |
}
|
|
2193 |
|
|
2194 |
void QSortFilterProxyModel::setSortRole(int role)
|
|
2195 |
{
|
|
2196 |
Q_D(QSortFilterProxyModel);
|
|
2197 |
if (d->sort_role == role)
|
|
2198 |
return;
|
|
2199 |
d->sort_role = role;
|
|
2200 |
d->sort();
|
|
2201 |
}
|
|
2202 |
|
|
2203 |
/*!
|
|
2204 |
\since 4.2
|
|
2205 |
\property QSortFilterProxyModel::filterRole
|
|
2206 |
\brief the item role that is used to query the source model's data when filtering items
|
|
2207 |
|
|
2208 |
The default value is Qt::DisplayRole.
|
|
2209 |
|
|
2210 |
\sa filterAcceptsRow()
|
|
2211 |
*/
|
|
2212 |
int QSortFilterProxyModel::filterRole() const
|
|
2213 |
{
|
|
2214 |
Q_D(const QSortFilterProxyModel);
|
|
2215 |
return d->filter_role;
|
|
2216 |
}
|
|
2217 |
|
|
2218 |
void QSortFilterProxyModel::setFilterRole(int role)
|
|
2219 |
{
|
|
2220 |
Q_D(QSortFilterProxyModel);
|
|
2221 |
if (d->filter_role == role)
|
|
2222 |
return;
|
|
2223 |
d->filter_role = role;
|
|
2224 |
d->filter_changed();
|
|
2225 |
}
|
|
2226 |
|
|
2227 |
/*!
|
|
2228 |
\obsolete
|
|
2229 |
|
|
2230 |
This function is obsolete. Use invalidate() instead.
|
|
2231 |
*/
|
|
2232 |
void QSortFilterProxyModel::clear()
|
|
2233 |
{
|
|
2234 |
Q_D(QSortFilterProxyModel);
|
|
2235 |
emit layoutAboutToBeChanged();
|
|
2236 |
d->clear_mapping();
|
|
2237 |
emit layoutChanged();
|
|
2238 |
}
|
|
2239 |
|
|
2240 |
/*!
|
|
2241 |
\since 4.3
|
|
2242 |
|
|
2243 |
Invalidates the current sorting and filtering.
|
|
2244 |
|
|
2245 |
\sa invalidateFilter()
|
|
2246 |
*/
|
|
2247 |
void QSortFilterProxyModel::invalidate()
|
|
2248 |
{
|
|
2249 |
Q_D(QSortFilterProxyModel);
|
|
2250 |
emit layoutAboutToBeChanged();
|
|
2251 |
d->clear_mapping();
|
|
2252 |
emit layoutChanged();
|
|
2253 |
}
|
|
2254 |
|
|
2255 |
/*!
|
|
2256 |
\obsolete
|
|
2257 |
|
|
2258 |
This function is obsolete. Use invalidateFilter() instead.
|
|
2259 |
*/
|
|
2260 |
void QSortFilterProxyModel::filterChanged()
|
|
2261 |
{
|
|
2262 |
Q_D(QSortFilterProxyModel);
|
|
2263 |
d->filter_changed();
|
|
2264 |
}
|
|
2265 |
|
|
2266 |
/*!
|
|
2267 |
\since 4.3
|
|
2268 |
|
|
2269 |
Invalidates the current filtering.
|
|
2270 |
|
|
2271 |
This function should be called if you are implementing custom filtering
|
|
2272 |
(e.g. filterAcceptsRow()), and your filter parameters have changed.
|
|
2273 |
|
|
2274 |
\sa invalidate()
|
|
2275 |
*/
|
|
2276 |
void QSortFilterProxyModel::invalidateFilter()
|
|
2277 |
{
|
|
2278 |
Q_D(QSortFilterProxyModel);
|
|
2279 |
d->filter_changed();
|
|
2280 |
}
|
|
2281 |
|
|
2282 |
/*!
|
|
2283 |
Returns true if the value of the item referred to by the given
|
|
2284 |
index \a left is less than the value of the item referred to by
|
|
2285 |
the given index \a right, otherwise returns false.
|
|
2286 |
|
|
2287 |
This function is used as the < operator when sorting, and handles
|
|
2288 |
the following QVariant types:
|
|
2289 |
|
|
2290 |
\list
|
|
2291 |
\o QVariant::Int
|
|
2292 |
\o QVariant::UInt
|
|
2293 |
\o QVariant::LongLong
|
|
2294 |
\o QVariant::ULongLong
|
|
2295 |
\o QVariant::Double
|
|
2296 |
\o QVariant::Char
|
|
2297 |
\o QVariant::Date
|
|
2298 |
\o QVariant::Time
|
|
2299 |
\o QVariant::DateTime
|
|
2300 |
\o QVariant::String
|
|
2301 |
\endlist
|
|
2302 |
|
|
2303 |
Any other type will be converted to a QString using
|
|
2304 |
QVariant::toString().
|
|
2305 |
|
|
2306 |
Comparison of \l{QString}s is case sensitive by default; this can
|
|
2307 |
be changed using the \l {QSortFilterProxyModel::sortCaseSensitivity}
|
|
2308 |
{sortCaseSensitivity} property.
|
|
2309 |
|
|
2310 |
By default, the Qt::DisplayRole associated with the
|
|
2311 |
\l{QModelIndex}es is used for comparisons. This can be changed by
|
|
2312 |
setting the \l {QSortFilterProxyModel::sortRole} {sortRole} property.
|
|
2313 |
|
|
2314 |
\note The indices passed in correspond to the source model.
|
|
2315 |
|
|
2316 |
\sa sortRole, sortCaseSensitivity, dynamicSortFilter
|
|
2317 |
*/
|
|
2318 |
bool QSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
|
2319 |
{
|
|
2320 |
Q_D(const QSortFilterProxyModel);
|
|
2321 |
QVariant l = (left.model() ? left.model()->data(left, d->sort_role) : QVariant());
|
|
2322 |
QVariant r = (right.model() ? right.model()->data(right, d->sort_role) : QVariant());
|
|
2323 |
switch (l.userType()) {
|
|
2324 |
case QVariant::Invalid:
|
|
2325 |
return (r.type() == QVariant::Invalid);
|
|
2326 |
case QVariant::Int:
|
|
2327 |
return l.toInt() < r.toInt();
|
|
2328 |
case QVariant::UInt:
|
|
2329 |
return l.toUInt() < r.toUInt();
|
|
2330 |
case QVariant::LongLong:
|
|
2331 |
return l.toLongLong() < r.toLongLong();
|
|
2332 |
case QVariant::ULongLong:
|
|
2333 |
return l.toULongLong() < r.toULongLong();
|
|
2334 |
case QMetaType::Float:
|
|
2335 |
return l.toFloat() < r.toFloat();
|
|
2336 |
case QVariant::Double:
|
|
2337 |
return l.toDouble() < r.toDouble();
|
|
2338 |
case QVariant::Char:
|
|
2339 |
return l.toChar() < r.toChar();
|
|
2340 |
case QVariant::Date:
|
|
2341 |
return l.toDate() < r.toDate();
|
|
2342 |
case QVariant::Time:
|
|
2343 |
return l.toTime() < r.toTime();
|
|
2344 |
case QVariant::DateTime:
|
|
2345 |
return l.toDateTime() < r.toDateTime();
|
|
2346 |
case QVariant::String:
|
|
2347 |
default:
|
|
2348 |
if (d->sort_localeaware)
|
|
2349 |
return l.toString().localeAwareCompare(r.toString()) < 0;
|
|
2350 |
else
|
|
2351 |
return l.toString().compare(r.toString(), d->sort_casesensitivity) < 0;
|
|
2352 |
}
|
|
2353 |
return false;
|
|
2354 |
}
|
|
2355 |
|
|
2356 |
/*!
|
|
2357 |
Returns true if the item in the row indicated by the given \a source_row
|
|
2358 |
and \a source_parent should be included in the model; otherwise returns
|
|
2359 |
false.
|
|
2360 |
|
|
2361 |
The default implementation returns true if the value held by the relevant item
|
|
2362 |
matches the filter string, wildcard string or regular expression.
|
|
2363 |
|
|
2364 |
\note By default, the Qt::DisplayRole is used to determine if the row
|
|
2365 |
should be accepted or not. This can be changed by setting the
|
|
2366 |
\l{QSortFilterProxyModel::filterRole}{filterRole} property.
|
|
2367 |
|
|
2368 |
\sa filterAcceptsColumn(), setFilterFixedString(), setFilterRegExp(), setFilterWildcard()
|
|
2369 |
*/
|
|
2370 |
bool QSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
|
2371 |
{
|
|
2372 |
Q_D(const QSortFilterProxyModel);
|
|
2373 |
if (d->filter_regexp.isEmpty())
|
|
2374 |
return true;
|
|
2375 |
if (d->filter_column == -1) {
|
|
2376 |
int column_count = d->model->columnCount(source_parent);
|
|
2377 |
for (int column = 0; column < column_count; ++column) {
|
|
2378 |
QModelIndex source_index = d->model->index(source_row, column, source_parent);
|
|
2379 |
QString key = d->model->data(source_index, d->filter_role).toString();
|
|
2380 |
if (key.contains(d->filter_regexp))
|
|
2381 |
return true;
|
|
2382 |
}
|
|
2383 |
return false;
|
|
2384 |
}
|
|
2385 |
QModelIndex source_index = d->model->index(source_row, d->filter_column, source_parent);
|
|
2386 |
if (!source_index.isValid()) // the column may not exist
|
|
2387 |
return true;
|
|
2388 |
QString key = d->model->data(source_index, d->filter_role).toString();
|
|
2389 |
return key.contains(d->filter_regexp);
|
|
2390 |
}
|
|
2391 |
|
|
2392 |
/*!
|
|
2393 |
Returns true if the item in the column indicated by the given \a source_column
|
|
2394 |
and \a source_parent should be included in the model; otherwise returns false.
|
|
2395 |
|
|
2396 |
The default implementation returns true if the value held by the relevant item
|
|
2397 |
matches the filter string, wildcard string or regular expression.
|
|
2398 |
|
|
2399 |
\note By default, the Qt::DisplayRole is used to determine if the row
|
|
2400 |
should be accepted or not. This can be changed by setting the \l
|
|
2401 |
filterRole property.
|
|
2402 |
|
|
2403 |
\sa filterAcceptsRow(), setFilterFixedString(), setFilterRegExp(), setFilterWildcard()
|
|
2404 |
*/
|
|
2405 |
bool QSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
|
|
2406 |
{
|
|
2407 |
Q_UNUSED(source_column);
|
|
2408 |
Q_UNUSED(source_parent);
|
|
2409 |
return true;
|
|
2410 |
}
|
|
2411 |
|
|
2412 |
/*!
|
|
2413 |
Returns the source model index corresponding to the given \a
|
|
2414 |
proxyIndex from the sorting filter model.
|
|
2415 |
|
|
2416 |
\sa mapFromSource()
|
|
2417 |
*/
|
|
2418 |
QModelIndex QSortFilterProxyModel::mapToSource(const QModelIndex &proxyIndex) const
|
|
2419 |
{
|
|
2420 |
Q_D(const QSortFilterProxyModel);
|
|
2421 |
return d->proxy_to_source(proxyIndex);
|
|
2422 |
}
|
|
2423 |
|
|
2424 |
/*!
|
|
2425 |
Returns the model index in the QSortFilterProxyModel given the \a
|
|
2426 |
sourceIndex from the source model.
|
|
2427 |
|
|
2428 |
\sa mapToSource()
|
|
2429 |
*/
|
|
2430 |
QModelIndex QSortFilterProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
|
|
2431 |
{
|
|
2432 |
Q_D(const QSortFilterProxyModel);
|
|
2433 |
return d->source_to_proxy(sourceIndex);
|
|
2434 |
}
|
|
2435 |
|
|
2436 |
/*!
|
|
2437 |
\reimp
|
|
2438 |
*/
|
|
2439 |
QItemSelection QSortFilterProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const
|
|
2440 |
{
|
|
2441 |
return QAbstractProxyModel::mapSelectionToSource(proxySelection);
|
|
2442 |
}
|
|
2443 |
|
|
2444 |
/*!
|
|
2445 |
\reimp
|
|
2446 |
*/
|
|
2447 |
QItemSelection QSortFilterProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const
|
|
2448 |
{
|
|
2449 |
return QAbstractProxyModel::mapSelectionFromSource(sourceSelection);
|
|
2450 |
}
|
|
2451 |
|
|
2452 |
/*!
|
|
2453 |
\fn QObject *QSortFilterProxyModel::parent() const
|
|
2454 |
\internal
|
|
2455 |
*/
|
|
2456 |
|
|
2457 |
QT_END_NAMESPACE
|
|
2458 |
|
|
2459 |
#include "moc_qsortfilterproxymodel.cpp"
|
|
2460 |
|
|
2461 |
#endif // QT_NO_SORTFILTERPROXYMODEL
|