author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
737 |
if (model->rowCount(source_parent) == delta_item_count) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
738 |
// Items were inserted where there were none before. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
739 |
// If it was new rows make sure to create mappings for columns so that a |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
740 |
// valid mapping can be retreived later and vice-versa. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
741 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
742 |
QVector<int> &orthogonal_proxy_to_source = (orient == Qt::Horizontal) ? m->source_rows : m->source_columns; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
743 |
QVector<int> &orthogonal_source_to_proxy = (orient == Qt::Horizontal) ? m->proxy_rows : m->proxy_columns; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
744 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
745 |
if (orthogonal_source_to_proxy.isEmpty()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
746 |
const int ortho_end = (orient == Qt::Horizontal) ? model->rowCount(source_parent) : model->columnCount(source_parent); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
747 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
748 |
for (int ortho_item = 0; ortho_item < ortho_end; ++ortho_item) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
749 |
if ((orient == Qt::Horizontal) ? q->filterAcceptsRow(ortho_item, source_parent) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
750 |
: q->filterAcceptsColumn(ortho_item, source_parent)) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
751 |
orthogonal_proxy_to_source.append(ortho_item); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
752 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
753 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
754 |
orthogonal_source_to_proxy.resize(orthogonal_proxy_to_source.size()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
755 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
756 |
if (orient == Qt::Horizontal) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
757 |
// We're reacting to columnsInserted, but we've just inserted new rows. Sort them. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
758 |
sort_source_rows(orthogonal_proxy_to_source, source_parent); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
759 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
760 |
build_source_to_proxy_mapping(orthogonal_proxy_to_source, orthogonal_source_to_proxy); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
761 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
762 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
763 |
|
0 | 764 |
// Sort and insert the items |
765 |
if (orient == Qt::Vertical) // Only sort rows |
|
766 |
sort_source_rows(source_items, source_parent); |
|
767 |
insert_source_items(source_to_proxy, proxy_to_source, source_items, source_parent, orient); |
|
768 |
} |
|
769 |
||
770 |
/*! |
|
771 |
\internal |
|
772 |
||
773 |
Handles source model items removal |
|
774 |
(columnsAboutToBeRemoved(), rowsAboutToBeRemoved()). |
|
775 |
*/ |
|
776 |
void QSortFilterProxyModelPrivate::source_items_about_to_be_removed( |
|
777 |
const QModelIndex &source_parent, int start, int end, Qt::Orientation orient) |
|
778 |
{ |
|
779 |
if ((start < 0) || (end < 0)) |
|
780 |
return; |
|
781 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_parent); |
|
782 |
if (it == source_index_mapping.constEnd()) { |
|
783 |
// Don't care, since we don't have mapping for this index |
|
784 |
return; |
|
785 |
} |
|
786 |
||
787 |
Mapping *m = it.value(); |
|
788 |
QVector<int> &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; |
|
789 |
QVector<int> &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns; |
|
790 |
||
791 |
// figure out which items to remove |
|
792 |
QVector<int> source_items_to_remove; |
|
793 |
int proxy_count = proxy_to_source.size(); |
|
794 |
for (int proxy_item = 0; proxy_item < proxy_count; ++proxy_item) { |
|
795 |
int source_item = proxy_to_source.at(proxy_item); |
|
796 |
if ((source_item >= start) && (source_item <= end)) |
|
797 |
source_items_to_remove.append(source_item); |
|
798 |
} |
|
799 |
||
800 |
remove_source_items(source_to_proxy, proxy_to_source, source_items_to_remove, |
|
801 |
source_parent, orient); |
|
802 |
} |
|
803 |
||
804 |
/*! |
|
805 |
\internal |
|
806 |
||
807 |
Handles source model items removal (columnsRemoved(), rowsRemoved()). |
|
808 |
*/ |
|
809 |
void QSortFilterProxyModelPrivate::source_items_removed( |
|
810 |
const QModelIndex &source_parent, int start, int end, Qt::Orientation orient) |
|
811 |
{ |
|
812 |
if ((start < 0) || (end < 0)) |
|
813 |
return; |
|
814 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_parent); |
|
815 |
if (it == source_index_mapping.constEnd()) { |
|
816 |
// Don't care, since we don't have mapping for this index |
|
817 |
return; |
|
818 |
} |
|
819 |
||
820 |
Mapping *m = it.value(); |
|
821 |
QVector<int> &source_to_proxy = (orient == Qt::Vertical) ? m->proxy_rows : m->proxy_columns; |
|
822 |
QVector<int> &proxy_to_source = (orient == Qt::Vertical) ? m->source_rows : m->source_columns; |
|
823 |
||
824 |
if (end >= source_to_proxy.size()) |
|
825 |
end = source_to_proxy.size() - 1; |
|
826 |
||
827 |
// Shrink the source-to-proxy mapping to reflect the new item count |
|
828 |
int delta_item_count = end - start + 1; |
|
829 |
source_to_proxy.remove(start, delta_item_count); |
|
830 |
||
831 |
int proxy_count = proxy_to_source.size(); |
|
832 |
if (proxy_count > source_to_proxy.size()) { |
|
833 |
// mapping is in an inconsistent state -- redo the whole mapping |
|
834 |
qWarning("QSortFilterProxyModel: inconsistent changes reported by source model"); |
|
835 |
remove_from_mapping(source_parent); |
|
836 |
Q_Q(QSortFilterProxyModel); |
|
837 |
q->reset(); |
|
838 |
return; |
|
839 |
} |
|
840 |
||
841 |
// Adjust "stale" indexes in proxy-to-source mapping |
|
842 |
for (int proxy_item = 0; proxy_item < proxy_count; ++proxy_item) { |
|
843 |
int source_item = proxy_to_source.at(proxy_item); |
|
844 |
if (source_item >= start) { |
|
845 |
Q_ASSERT(source_item - delta_item_count >= 0); |
|
846 |
proxy_to_source.replace(proxy_item, source_item - delta_item_count); |
|
847 |
} |
|
848 |
} |
|
849 |
build_source_to_proxy_mapping(proxy_to_source, source_to_proxy); |
|
850 |
||
851 |
updateChildrenMapping(source_parent, m, orient, start, end, delta_item_count, true); |
|
852 |
||
853 |
} |
|
854 |
||
855 |
||
856 |
/*! |
|
857 |
\internal |
|
858 |
updates the mapping of the children when inserting or removing items |
|
859 |
*/ |
|
860 |
void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &source_parent, Mapping *parent_mapping, |
|
861 |
Qt::Orientation orient, int start, int end, int delta_item_count, bool remove) |
|
862 |
{ |
|
863 |
// see if any mapped children should be (re)moved |
|
864 |
QVector<QPair<QModelIndex, Mapping*> > moved_source_index_mappings; |
|
865 |
QVector<QModelIndex>::iterator it2 = parent_mapping->mapped_children.begin(); |
|
866 |
for ( ; it2 != parent_mapping->mapped_children.end();) { |
|
867 |
const QModelIndex source_child_index = *it2; |
|
868 |
const int pos = (orient == Qt::Vertical) |
|
869 |
? source_child_index.row() |
|
870 |
: source_child_index.column(); |
|
871 |
if (pos < start) { |
|
872 |
// not affected |
|
873 |
++it2; |
|
874 |
} else if (remove && pos <= end) { |
|
875 |
// in the removed interval |
|
876 |
it2 = parent_mapping->mapped_children.erase(it2); |
|
877 |
remove_from_mapping(source_child_index); |
|
878 |
} else { |
|
879 |
// below the removed items -- recompute the index |
|
880 |
QModelIndex new_index; |
|
881 |
const int newpos = remove ? pos - delta_item_count : pos + delta_item_count; |
|
882 |
if (orient == Qt::Vertical) { |
|
883 |
new_index = model->index(newpos, |
|
884 |
source_child_index.column(), |
|
885 |
source_parent); |
|
886 |
} else { |
|
887 |
new_index = model->index(source_child_index.row(), |
|
888 |
newpos, |
|
889 |
source_parent); |
|
890 |
} |
|
891 |
*it2 = new_index; |
|
892 |
++it2; |
|
893 |
||
894 |
// update mapping |
|
895 |
Mapping *cm = source_index_mapping.take(source_child_index); |
|
896 |
Q_ASSERT(cm); |
|
897 |
// we do not reinsert right away, because the new index might be identical with another, old index |
|
898 |
moved_source_index_mappings.append(QPair<QModelIndex, Mapping*>(new_index, cm)); |
|
899 |
} |
|
900 |
} |
|
901 |
||
902 |
// reinsert moved, mapped indexes |
|
903 |
QVector<QPair<QModelIndex, Mapping*> >::iterator it = moved_source_index_mappings.begin(); |
|
904 |
for (; it != moved_source_index_mappings.end(); ++it) { |
|
905 |
#ifdef QT_STRICT_ITERATORS |
|
906 |
source_index_mapping.insert((*it).first, (*it).second); |
|
907 |
(*it).second->map_iter = source_index_mapping.constFind((*it).first); |
|
908 |
#else |
|
909 |
(*it).second->map_iter = source_index_mapping.insert((*it).first, (*it).second); |
|
910 |
#endif |
|
911 |
} |
|
912 |
} |
|
913 |
||
914 |
/*! |
|
915 |
\internal |
|
916 |
*/ |
|
917 |
void QSortFilterProxyModelPrivate::proxy_item_range( |
|
918 |
const QVector<int> &source_to_proxy, const QVector<int> &source_items, |
|
919 |
int &proxy_low, int &proxy_high) const |
|
920 |
{ |
|
921 |
proxy_low = INT_MAX; |
|
922 |
proxy_high = INT_MIN; |
|
923 |
for (int i = 0; i < source_items.count(); ++i) { |
|
924 |
int proxy_item = source_to_proxy.at(source_items.at(i)); |
|
925 |
Q_ASSERT(proxy_item != -1); |
|
926 |
if (proxy_item < proxy_low) |
|
927 |
proxy_low = proxy_item; |
|
928 |
if (proxy_item > proxy_high) |
|
929 |
proxy_high = proxy_item; |
|
930 |
} |
|
931 |
} |
|
932 |
||
933 |
/*! |
|
934 |
\internal |
|
935 |
*/ |
|
936 |
void QSortFilterProxyModelPrivate::build_source_to_proxy_mapping( |
|
937 |
const QVector<int> &proxy_to_source, QVector<int> &source_to_proxy) const |
|
938 |
{ |
|
939 |
source_to_proxy.fill(-1); |
|
940 |
int proxy_count = proxy_to_source.size(); |
|
941 |
for (int i = 0; i < proxy_count; ++i) |
|
942 |
source_to_proxy[proxy_to_source.at(i)] = i; |
|
943 |
} |
|
944 |
||
945 |
/*! |
|
946 |
\internal |
|
947 |
||
948 |
Maps the persistent proxy indexes to source indexes and |
|
949 |
returns the list of source indexes. |
|
950 |
*/ |
|
951 |
QModelIndexPairList QSortFilterProxyModelPrivate::store_persistent_indexes() |
|
952 |
{ |
|
953 |
Q_Q(QSortFilterProxyModel); |
|
954 |
QModelIndexPairList source_indexes; |
|
955 |
foreach (QPersistentModelIndexData *data, persistent.indexes) { |
|
956 |
QModelIndex proxy_index = data->index; |
|
957 |
QModelIndex source_index = q->mapToSource(proxy_index); |
|
958 |
source_indexes.append(qMakePair(proxy_index, QPersistentModelIndex(source_index))); |
|
959 |
} |
|
960 |
return source_indexes; |
|
961 |
} |
|
962 |
||
963 |
/*! |
|
964 |
\internal |
|
965 |
||
966 |
Maps \a source_indexes to proxy indexes and stores those |
|
967 |
as persistent indexes. |
|
968 |
*/ |
|
969 |
void QSortFilterProxyModelPrivate::update_persistent_indexes( |
|
970 |
const QModelIndexPairList &source_indexes) |
|
971 |
{ |
|
972 |
Q_Q(QSortFilterProxyModel); |
|
973 |
QModelIndexList from, to; |
|
974 |
for (int i = 0; i < source_indexes.count(); ++i) { |
|
975 |
QModelIndex source_index = source_indexes.at(i).second; |
|
976 |
QModelIndex old_proxy_index = source_indexes.at(i).first; |
|
977 |
create_mapping(source_index.parent()); |
|
978 |
QModelIndex proxy_index = q->mapFromSource(source_index); |
|
979 |
from << old_proxy_index; |
|
980 |
to << proxy_index; |
|
981 |
} |
|
982 |
q->changePersistentIndexList(from, to); |
|
983 |
} |
|
984 |
||
985 |
||
986 |
/*! |
|
987 |
\internal |
|
988 |
||
989 |
Updates the proxy model (adds/removes rows) based on the |
|
990 |
new filter. |
|
991 |
*/ |
|
992 |
void QSortFilterProxyModelPrivate::filter_changed(const QModelIndex &source_parent) |
|
993 |
{ |
|
994 |
IndexMap::const_iterator it = source_index_mapping.constFind(source_parent); |
|
995 |
if (it == source_index_mapping.constEnd()) |
|
996 |
return; |
|
997 |
Mapping *m = it.value(); |
|
998 |
QSet<int> rows_removed = handle_filter_changed(m->proxy_rows, m->source_rows, source_parent, Qt::Vertical); |
|
999 |
QSet<int> columns_removed = handle_filter_changed(m->proxy_columns, m->source_columns, source_parent, Qt::Horizontal); |
|
1000 |
QVector<QModelIndex>::iterator it2 = m->mapped_children.end(); |
|
1001 |
while (it2 != m->mapped_children.begin()) { |
|
1002 |
--it2; |
|
1003 |
const QModelIndex source_child_index = *it2; |
|
1004 |
if (rows_removed.contains(source_child_index.row()) || columns_removed.contains(source_child_index.column())) { |
|
1005 |
it2 = m->mapped_children.erase(it2); |
|
1006 |
remove_from_mapping(source_child_index); |
|
1007 |
} else { |
|
1008 |
filter_changed(source_child_index); |
|
1009 |
} |
|
1010 |
} |
|
1011 |
} |
|
1012 |
||
1013 |
/*! |
|
1014 |
\internal |
|
1015 |
returns the removed items indexes |
|
1016 |
*/ |
|
1017 |
QSet<int> QSortFilterProxyModelPrivate::handle_filter_changed( |
|
1018 |
QVector<int> &source_to_proxy, QVector<int> &proxy_to_source, |
|
1019 |
const QModelIndex &source_parent, Qt::Orientation orient) |
|
1020 |
{ |
|
1021 |
Q_Q(QSortFilterProxyModel); |
|
1022 |
// Figure out which mapped items to remove |
|
1023 |
QVector<int> source_items_remove; |
|
1024 |
for (int i = 0; i < proxy_to_source.count(); ++i) { |
|
1025 |
const int source_item = proxy_to_source.at(i); |
|
1026 |
if ((orient == Qt::Vertical) |
|
1027 |
? !q->filterAcceptsRow(source_item, source_parent) |
|
1028 |
: !q->filterAcceptsColumn(source_item, source_parent)) { |
|
1029 |
// This source item does not satisfy the filter, so it must be removed |
|
1030 |
source_items_remove.append(source_item); |
|
1031 |
} |
|
1032 |
} |
|
1033 |
// Figure out which non-mapped items to insert |
|
1034 |
QVector<int> source_items_insert; |
|
1035 |
int source_count = source_to_proxy.size(); |
|
1036 |
for (int source_item = 0; source_item < source_count; ++source_item) { |
|
1037 |
if (source_to_proxy.at(source_item) == -1) { |
|
1038 |
if ((orient == Qt::Vertical) |
|
1039 |
? q->filterAcceptsRow(source_item, source_parent) |
|
1040 |
: q->filterAcceptsColumn(source_item, source_parent)) { |
|
1041 |
// This source item satisfies the filter, so it must be added |
|
1042 |
source_items_insert.append(source_item); |
|
1043 |
} |
|
1044 |
} |
|
1045 |
} |
|
1046 |
if (!source_items_remove.isEmpty() || !source_items_insert.isEmpty()) { |
|
1047 |
// Do item removal and insertion |
|
1048 |
remove_source_items(source_to_proxy, proxy_to_source, |
|
1049 |
source_items_remove, source_parent, orient); |
|
1050 |
if (orient == Qt::Vertical) |
|
1051 |
sort_source_rows(source_items_insert, source_parent); |
|
1052 |
insert_source_items(source_to_proxy, proxy_to_source, |
|
1053 |
source_items_insert, source_parent, orient); |
|
1054 |
} |
|
1055 |
return qVectorToSet(source_items_remove); |
|
1056 |
} |
|
1057 |
||
1058 |
void QSortFilterProxyModelPrivate::_q_sourceDataChanged(const QModelIndex &source_top_left, |
|
1059 |
const QModelIndex &source_bottom_right) |
|
1060 |
{ |
|
1061 |
Q_Q(QSortFilterProxyModel); |
|
1062 |
if (!source_top_left.isValid() || !source_bottom_right.isValid()) |
|
1063 |
return; |
|
1064 |
QModelIndex source_parent = source_top_left.parent(); |
|
1065 |
IndexMap::const_iterator it = create_mapping(source_parent); |
|
1066 |
if (it == source_index_mapping.constEnd()) { |
|
1067 |
// Don't care, since we don't have mapping for this index |
|
1068 |
return; |
|
1069 |
} |
|
1070 |
Mapping *m = it.value(); |
|
1071 |
||
1072 |
// Figure out how the source changes affect us |
|
1073 |
QVector<int> source_rows_remove; |
|
1074 |
QVector<int> source_rows_insert; |
|
1075 |
QVector<int> source_rows_change; |
|
1076 |
QVector<int> source_rows_resort; |
|
1077 |
int end = qMin(source_bottom_right.row(), m->proxy_rows.count() - 1); |
|
1078 |
for (int source_row = source_top_left.row(); source_row <= end; ++source_row) { |
|
1079 |
if (dynamic_sortfilter) { |
|
1080 |
if (m->proxy_rows.at(source_row) != -1) { |
|
1081 |
if (!q->filterAcceptsRow(source_row, source_parent)) { |
|
1082 |
// This source row no longer satisfies the filter, so it must be removed |
|
1083 |
source_rows_remove.append(source_row); |
|
1084 |
} else if (source_sort_column >= source_top_left.column() && source_sort_column <= source_bottom_right.column()) { |
|
1085 |
// This source row has changed in a way that may affect sorted order |
|
1086 |
source_rows_resort.append(source_row); |
|
1087 |
} else { |
|
1088 |
// This row has simply changed, without affecting filtering nor sorting |
|
1089 |
source_rows_change.append(source_row); |
|
1090 |
} |
|
1091 |
} else { |
|
1092 |
if (q->filterAcceptsRow(source_row, source_parent)) { |
|
1093 |
// This source row now satisfies the filter, so it must be added |
|
1094 |
source_rows_insert.append(source_row); |
|
1095 |
} |
|
1096 |
} |
|
1097 |
} else { |
|
1098 |
if (m->proxy_rows.at(source_row) != -1) |
|
1099 |
source_rows_change.append(source_row); |
|
1100 |
} |
|
1101 |
} |
|
1102 |
||
1103 |
if (!source_rows_remove.isEmpty()) { |
|
1104 |
remove_source_items(m->proxy_rows, m->source_rows, |
|
1105 |
source_rows_remove, source_parent, Qt::Vertical); |
|
1106 |
QSet<int> source_rows_remove_set = qVectorToSet(source_rows_remove); |
|
1107 |
QVector<QModelIndex>::iterator it = m->mapped_children.end(); |
|
1108 |
while (it != m->mapped_children.begin()) { |
|
1109 |
--it; |
|
1110 |
const QModelIndex source_child_index = *it; |
|
1111 |
if (source_rows_remove_set.contains(source_child_index.row())) { |
|
1112 |
it = m->mapped_children.erase(it); |
|
1113 |
remove_from_mapping(source_child_index); |
|
1114 |
} |
|
1115 |
} |
|
1116 |
} |
|
1117 |
||
1118 |
if (!source_rows_resort.isEmpty()) { |
|
1119 |
// Re-sort the rows |
|
1120 |
emit q->layoutAboutToBeChanged(); |
|
1121 |
QModelIndexPairList source_indexes = store_persistent_indexes(); |
|
1122 |
remove_source_items(m->proxy_rows, m->source_rows, source_rows_resort, |
|
1123 |
source_parent, Qt::Vertical, false); |
|
1124 |
sort_source_rows(source_rows_resort, source_parent); |
|
1125 |
insert_source_items(m->proxy_rows, m->source_rows, source_rows_resort, |
|
1126 |
source_parent, Qt::Vertical, false); |
|
1127 |
update_persistent_indexes(source_indexes); |
|
1128 |
emit q->layoutChanged(); |
|
1129 |
// Make sure we also emit dataChanged for the rows |
|
1130 |
source_rows_change += source_rows_resort; |
|
1131 |
} |
|
1132 |
||
1133 |
if (!source_rows_change.isEmpty()) { |
|
1134 |
// Find the proxy row range |
|
1135 |
int proxy_start_row; |
|
1136 |
int proxy_end_row; |
|
1137 |
proxy_item_range(m->proxy_rows, source_rows_change, |
|
1138 |
proxy_start_row, proxy_end_row); |
|
1139 |
// ### Find the proxy column range also |
|
1140 |
if (proxy_end_row >= 0) { |
|
1141 |
// the row was accepted, but some columns might still be filtered out |
|
1142 |
int source_left_column = source_top_left.column(); |
|
1143 |
while (source_left_column < source_bottom_right.column() |
|
1144 |
&& m->proxy_columns.at(source_left_column) == -1) |
|
1145 |
++source_left_column; |
|
1146 |
const QModelIndex proxy_top_left = create_index( |
|
1147 |
proxy_start_row, m->proxy_columns.at(source_left_column), it); |
|
1148 |
int source_right_column = source_bottom_right.column(); |
|
1149 |
while (source_right_column > source_top_left.column() |
|
1150 |
&& m->proxy_columns.at(source_right_column) == -1) |
|
1151 |
--source_right_column; |
|
1152 |
const QModelIndex proxy_bottom_right = create_index( |
|
1153 |
proxy_end_row, m->proxy_columns.at(source_right_column), it); |
|
1154 |
emit q->dataChanged(proxy_top_left, proxy_bottom_right); |
|
1155 |
} |
|
1156 |
} |
|
1157 |
||
1158 |
if (!source_rows_insert.isEmpty()) { |
|
1159 |
sort_source_rows(source_rows_insert, source_parent); |
|
1160 |
insert_source_items(m->proxy_rows, m->source_rows, |
|
1161 |
source_rows_insert, source_parent, Qt::Vertical); |
|
1162 |
} |
|
1163 |
} |
|
1164 |
||
1165 |
void QSortFilterProxyModelPrivate::_q_sourceHeaderDataChanged(Qt::Orientation orientation, |
|
1166 |
int start, int end) |
|
1167 |
{ |
|
1168 |
Q_Q(QSortFilterProxyModel); |
|
1169 |
Mapping *m = create_mapping(QModelIndex()).value(); |
|
1170 |
int proxy_start = (orientation == Qt::Vertical |
|
1171 |
? m->proxy_rows.at(start) |
|
1172 |
: m->proxy_columns.at(start)); |
|
1173 |
int proxy_end = (orientation == Qt::Vertical |
|
1174 |
? m->proxy_rows.at(end) |
|
1175 |
: m->proxy_columns.at(end)); |
|
1176 |
emit q->headerDataChanged(orientation, proxy_start, proxy_end); |
|
1177 |
} |
|
1178 |
||
1179 |
void QSortFilterProxyModelPrivate::_q_sourceAboutToBeReset() |
|
1180 |
{ |
|
1181 |
Q_Q(QSortFilterProxyModel); |
|
1182 |
q->beginResetModel(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1183 |
invalidatePersistentIndexes(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1184 |
clear_mapping(); |
0 | 1185 |
} |
1186 |
||
1187 |
void QSortFilterProxyModelPrivate::_q_sourceReset() |
|
1188 |
{ |
|
1189 |
Q_Q(QSortFilterProxyModel); |
|
1190 |
// All internal structures are deleted in clear() |
|
1191 |
q->endResetModel(); |
|
1192 |
update_source_sort_column(); |
|
1193 |
if (dynamic_sortfilter) |
|
1194 |
sort(); |
|
1195 |
} |
|
1196 |
||
1197 |
void QSortFilterProxyModelPrivate::_q_sourceLayoutAboutToBeChanged() |
|
1198 |
{ |
|
1199 |
Q_Q(QSortFilterProxyModel); |
|
1200 |
saved_persistent_indexes.clear(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1201 |
emit q->layoutAboutToBeChanged(); |
0 | 1202 |
if (persistent.indexes.isEmpty()) |
1203 |
return; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1204 |
|
0 | 1205 |
saved_persistent_indexes = store_persistent_indexes(); |
1206 |
} |
|
1207 |
||
1208 |
void QSortFilterProxyModelPrivate::_q_sourceLayoutChanged() |
|
1209 |
{ |
|
1210 |
Q_Q(QSortFilterProxyModel); |
|
1211 |
if (saved_persistent_indexes.isEmpty()) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1212 |
clear_mapping(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1213 |
emit q->layoutChanged(); |
0 | 1214 |
return; |
1215 |
} |
|
1216 |
||
1217 |
qDeleteAll(source_index_mapping); |
|
1218 |
source_index_mapping.clear(); |
|
1219 |
||
1220 |
update_persistent_indexes(saved_persistent_indexes); |
|
1221 |
saved_persistent_indexes.clear(); |
|
1222 |
||
1223 |
update_source_sort_column(); |
|
1224 |
||
1225 |
emit q->layoutChanged(); |
|
1226 |
} |
|
1227 |
||
1228 |
void QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeInserted( |
|
1229 |
const QModelIndex &source_parent, int start, int end) |
|
1230 |
{ |
|
1231 |
Q_UNUSED(start); |
|
1232 |
Q_UNUSED(end); |
|
1233 |
//Force the creation of a mapping now, even if its empty. |
|
1234 |
//We need it because the proxy can be acessed at the moment it emits rowsAboutToBeInserted in insert_source_items |
|
1235 |
if (can_create_mapping(source_parent)) |
|
1236 |
create_mapping(source_parent); |
|
1237 |
} |
|
1238 |
||
1239 |
void QSortFilterProxyModelPrivate::_q_sourceRowsInserted( |
|
1240 |
const QModelIndex &source_parent, int start, int end) |
|
1241 |
{ |
|
1242 |
source_items_inserted(source_parent, start, end, Qt::Vertical); |
|
1243 |
if (update_source_sort_column()) //previous call to update_source_sort_column may fail if the model has no column. |
|
1244 |
sort(); // now it should succeed so we need to make sure to sort again |
|
1245 |
} |
|
1246 |
||
1247 |
void QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeRemoved( |
|
1248 |
const QModelIndex &source_parent, int start, int end) |
|
1249 |
{ |
|
1250 |
source_items_about_to_be_removed(source_parent, start, end, |
|
1251 |
Qt::Vertical); |
|
1252 |
} |
|
1253 |
||
1254 |
void QSortFilterProxyModelPrivate::_q_sourceRowsRemoved( |
|
1255 |
const QModelIndex &source_parent, int start, int end) |
|
1256 |
{ |
|
1257 |
source_items_removed(source_parent, start, end, Qt::Vertical); |
|
1258 |
} |
|
1259 |
||
1260 |
void QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeInserted( |
|
1261 |
const QModelIndex &source_parent, int start, int end) |
|
1262 |
{ |
|
1263 |
Q_UNUSED(start); |
|
1264 |
Q_UNUSED(end); |
|
1265 |
//Force the creation of a mapping now, even if its empty. |
|
1266 |
//We need it because the proxy can be acessed at the moment it emits columnsAboutToBeInserted in insert_source_items |
|
1267 |
if (can_create_mapping(source_parent)) |
|
1268 |
create_mapping(source_parent); |
|
1269 |
} |
|
1270 |
||
1271 |
void QSortFilterProxyModelPrivate::_q_sourceColumnsInserted( |
|
1272 |
const QModelIndex &source_parent, int start, int end) |
|
1273 |
{ |
|
1274 |
Q_Q(const QSortFilterProxyModel); |
|
1275 |
source_items_inserted(source_parent, start, end, Qt::Horizontal); |
|
1276 |
||
1277 |
if (source_parent.isValid()) |
|
1278 |
return; //we sort according to the root column only |
|
1279 |
if (source_sort_column == -1) { |
|
1280 |
//we update the source_sort_column depending on the prox_sort_column |
|
1281 |
if (update_source_sort_column()) |
|
1282 |
sort(); |
|
1283 |
} else { |
|
1284 |
if (start <= source_sort_column) |
|
1285 |
source_sort_column += end - start + 1; |
|
1286 |
||
1287 |
proxy_sort_column = q->mapFromSource(model->index(0,source_sort_column, source_parent)).column(); |
|
1288 |
} |
|
1289 |
} |
|
1290 |
||
1291 |
void QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeRemoved( |
|
1292 |
const QModelIndex &source_parent, int start, int end) |
|
1293 |
{ |
|
1294 |
source_items_about_to_be_removed(source_parent, start, end, |
|
1295 |
Qt::Horizontal); |
|
1296 |
} |
|
1297 |
||
1298 |
void QSortFilterProxyModelPrivate::_q_sourceColumnsRemoved( |
|
1299 |
const QModelIndex &source_parent, int start, int end) |
|
1300 |
{ |
|
1301 |
Q_Q(const QSortFilterProxyModel); |
|
1302 |
source_items_removed(source_parent, start, end, Qt::Horizontal); |
|
1303 |
||
1304 |
if (source_parent.isValid()) |
|
1305 |
return; //we sort according to the root column only |
|
1306 |
if (start <= source_sort_column) { |
|
1307 |
if (end < source_sort_column) |
|
1308 |
source_sort_column -= end - start + 1; |
|
1309 |
else |
|
1310 |
source_sort_column = -1; |
|
1311 |
} |
|
1312 |
||
1313 |
proxy_sort_column = q->mapFromSource(model->index(0,source_sort_column, source_parent)).column(); |
|
1314 |
} |
|
1315 |
||
1316 |
/*! |
|
1317 |
\since 4.1 |
|
1318 |
\class QSortFilterProxyModel |
|
1319 |
\brief The QSortFilterProxyModel class provides support for sorting and |
|
1320 |
filtering data passed between another model and a view. |
|
1321 |
||
1322 |
\ingroup model-view |
|
1323 |
||
1324 |
QSortFilterProxyModel can be used for sorting items, filtering out items, |
|
1325 |
or both. The model transforms the structure of a source model by mapping |
|
1326 |
the model indexes it supplies to new indexes, corresponding to different |
|
1327 |
locations, for views to use. This approach allows a given source model to |
|
1328 |
be restructured as far as views are concerned without requiring any |
|
1329 |
transformations on the underlying data, and without duplicating the data in |
|
1330 |
memory. |
|
1331 |
||
1332 |
Let's assume that we want to sort and filter the items provided by a custom |
|
1333 |
model. The code to set up the model and the view, \e without sorting and |
|
1334 |
filtering, would look like this: |
|
1335 |
||
1336 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 1 |
|
1337 |
||
1338 |
To add sorting and filtering support to \c MyItemModel, we need to create |
|
1339 |
a QSortFilterProxyModel, call setSourceModel() with the \c MyItemModel as |
|
1340 |
argument, and install the QSortFilterProxyModel on the view: |
|
1341 |
||
1342 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 0 |
|
1343 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 2 |
|
1344 |
||
1345 |
At this point, neither sorting nor filtering is enabled; the original data |
|
1346 |
is displayed in the view. Any changes made through the |
|
1347 |
QSortFilterProxyModel are applied to the original model. |
|
1348 |
||
1349 |
The QSortFilterProxyModel acts as a wrapper for the original model. If you |
|
1350 |
need to convert source \l{QModelIndex}es to sorted/filtered model indexes |
|
1351 |
or vice versa, use mapToSource(), mapFromSource(), mapSelectionToSource(), |
|
1352 |
and mapSelectionFromSource(). |
|
1353 |
||
1354 |
\note By default, the model does not dynamically re-sort and re-filter data |
|
1355 |
whenever the original model changes. This behavior can be changed by |
|
1356 |
setting the \l{QSortFilterProxyModel::dynamicSortFilter}{dynamicSortFilter} |
|
1357 |
property. |
|
1358 |
||
1359 |
The \l{itemviews/basicsortfiltermodel}{Basic Sort/Filter Model} and |
|
1360 |
\l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model} examples |
|
1361 |
illustrate how to use QSortFilterProxyModel to perform basic sorting and |
|
1362 |
filtering and how to subclass it to implement custom behavior. |
|
1363 |
||
1364 |
\section1 Sorting |
|
1365 |
||
1366 |
QTableView and QTreeView have a |
|
1367 |
\l{QTreeView::sortingEnabled}{sortingEnabled} property that controls |
|
1368 |
whether the user can sort the view by clicking the view's horizontal |
|
1369 |
header. For example: |
|
1370 |
||
1371 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 3 |
|
1372 |
||
1373 |
When this feature is on (the default is off), clicking on a header section |
|
1374 |
sorts the items according to that column. By clicking repeatedly, the user |
|
1375 |
can alternate between ascending and descending order. |
|
1376 |
||
1377 |
\image qsortfilterproxymodel-sorting.png A sorted QTreeView |
|
1378 |
||
1379 |
Behind the scene, the view calls the sort() virtual function on the model |
|
1380 |
to reorder the data in the model. To make your data sortable, you can |
|
1381 |
either implement sort() in your model, or use a QSortFilterProxyModel to |
|
1382 |
wrap your model -- QSortFilterProxyModel provides a generic sort() |
|
1383 |
reimplementation that operates on the sortRole() (Qt::DisplayRole by |
|
1384 |
default) of the items and that understands several data types, including |
|
1385 |
\c int, QString, and QDateTime. For hierarchical models, sorting is applied |
|
1386 |
recursively to all child items. String comparisons are case sensitive by |
|
1387 |
default; this can be changed by setting the \l{QSortFilterProxyModel::} |
|
1388 |
{sortCaseSensitivity} property. |
|
1389 |
||
1390 |
Custom sorting behavior is achieved by subclassing |
|
1391 |
QSortFilterProxyModel and reimplementing lessThan(), which is |
|
1392 |
used to compare items. For example: |
|
1393 |
||
1394 |
\snippet examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp 5 |
|
1395 |
||
1396 |
(This code snippet comes from the |
|
1397 |
\l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model} |
|
1398 |
example.) |
|
1399 |
||
1400 |
An alternative approach to sorting is to disable sorting on the view and to |
|
1401 |
impose a certain order to the user. This is done by explicitly calling |
|
1402 |
sort() with the desired column and order as arguments on the |
|
1403 |
QSortFilterProxyModel (or on the original model if it implements sort()). |
|
1404 |
For example: |
|
1405 |
||
1406 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 4 |
|
1407 |
||
1408 |
QSortFilterProxyModel can be sorted by column -1, in which case it returns |
|
1409 |
to the sort order of the underlying source model. |
|
1410 |
||
1411 |
\section1 Filtering |
|
1412 |
||
1413 |
In addition to sorting, QSortFilterProxyModel can be used to hide items |
|
1414 |
that do not match a certain filter. The filter is specified using a QRegExp |
|
1415 |
object and is applied to the filterRole() (Qt::DisplayRole by default) of |
|
1416 |
each item, for a given column. The QRegExp object can be used to match a |
|
1417 |
regular expression, a wildcard pattern, or a fixed string. For example: |
|
1418 |
||
1419 |
\snippet doc/src/snippets/qsortfilterproxymodel-details/main.cpp 5 |
|
1420 |
||
1421 |
For hierarchical models, the filter is applied recursively to all children. |
|
1422 |
If a parent item doesn't match the filter, none of its children will be |
|
1423 |
shown. |
|
1424 |
||
1425 |
A common use case is to let the user specify the filter regexp, wildcard |
|
1426 |
pattern, or fixed string in a QLineEdit and to connect the |
|
1427 |
\l{QLineEdit::textChanged()}{textChanged()} signal to setFilterRegExp(), |
|
1428 |
setFilterWildcard(), or setFilterFixedString() to reapply the filter. |
|
1429 |
||
1430 |
Custom filtering behavior can be achieved by reimplementing the |
|
1431 |
filterAcceptsRow() and filterAcceptsColumn() functions. For |
|
1432 |
example (from the \l{itemviews/customsortfiltermodel} |
|
1433 |
{Custom Sort/Filter Model} example), the following implementation ignores |
|
1434 |
the \l{QSortFilterProxyModel::filterKeyColumn}{filterKeyColumn} property |
|
1435 |
and performs filtering on columns 0, 1, and 2: |
|
1436 |
||
1437 |
\snippet examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp 3 |
|
1438 |
||
1439 |
(This code snippet comes from the |
|
1440 |
\l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model} |
|
1441 |
example.) |
|
1442 |
||
1443 |
If you are working with large amounts of filtering and have to invoke |
|
1444 |
invalidateFilter() repeatedly, using reset() may be more efficient, |
|
1445 |
depending on the implementation of your model. However, reset() returns the |
|
1446 |
proxy model to its original state, losing selection information, and will |
|
1447 |
cause the proxy model to be repopulated. |
|
1448 |
||
1449 |
\section1 Subclassing |
|
1450 |
||
1451 |
Since QAbstractProxyModel and its subclasses are derived from |
|
1452 |
QAbstractItemModel, much of the same advice about subclassing normal models |
|
1453 |
also applies to proxy models. In addition, it is worth noting that many of |
|
1454 |
the default implementations of functions in this class are written so that |
|
1455 |
they call the equivalent functions in the relevant source model. This |
|
1456 |
simple proxying mechanism may need to be overridden for source models with |
|
1457 |
more complex behavior; for example, if the source model provides a custom |
|
1458 |
hasChildren() implementation, you should also provide one in the proxy |
|
1459 |
model. |
|
1460 |
||
1461 |
\note Some general guidelines for subclassing models are available in the |
|
1462 |
\l{Model Subclassing Reference}. |
|
1463 |
||
1464 |
\sa QAbstractProxyModel, QAbstractItemModel, {Model/View Programming}, |
|
1465 |
{Basic Sort/Filter Model Example}, {Custom Sort/Filter Model Example} |
|
1466 |
*/ |
|
1467 |
||
1468 |
/*! |
|
1469 |
Constructs a sorting filter model with the given \a parent. |
|
1470 |
*/ |
|
1471 |
||
1472 |
QSortFilterProxyModel::QSortFilterProxyModel(QObject *parent) |
|
1473 |
: QAbstractProxyModel(*new QSortFilterProxyModelPrivate, parent) |
|
1474 |
{ |
|
1475 |
Q_D(QSortFilterProxyModel); |
|
1476 |
d->proxy_sort_column = d->source_sort_column = -1; |
|
1477 |
d->sort_order = Qt::AscendingOrder; |
|
1478 |
d->sort_casesensitivity = Qt::CaseSensitive; |
|
1479 |
d->sort_role = Qt::DisplayRole; |
|
1480 |
d->sort_localeaware = false; |
|
1481 |
d->filter_column = 0; |
|
1482 |
d->filter_role = Qt::DisplayRole; |
|
1483 |
d->dynamic_sortfilter = false; |
|
1484 |
connect(this, SIGNAL(modelReset()), this, SLOT(invalidate())); |
|
1485 |
} |
|
1486 |
||
1487 |
/*! |
|
1488 |
Destroys this sorting filter model. |
|
1489 |
*/ |
|
1490 |
QSortFilterProxyModel::~QSortFilterProxyModel() |
|
1491 |
{ |
|
1492 |
Q_D(QSortFilterProxyModel); |
|
1493 |
qDeleteAll(d->source_index_mapping); |
|
1494 |
d->source_index_mapping.clear(); |
|
1495 |
} |
|
1496 |
||
1497 |
/*! |
|
1498 |
\reimp |
|
1499 |
*/ |
|
1500 |
void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) |
|
1501 |
{ |
|
1502 |
Q_D(QSortFilterProxyModel); |
|
1503 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1504 |
beginResetModel(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1505 |
|
0 | 1506 |
disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), |
1507 |
this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex))); |
|
1508 |
||
1509 |
disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), |
|
1510 |
this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); |
|
1511 |
||
1512 |
disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), |
|
1513 |
this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int))); |
|
1514 |
||
1515 |
disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), |
|
1516 |
this, SLOT(_q_sourceRowsInserted(QModelIndex,int,int))); |
|
1517 |
||
1518 |
disconnect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), |
|
1519 |
this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int))); |
|
1520 |
||
1521 |
disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)), |
|
1522 |
this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int))); |
|
1523 |
||
1524 |
disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), |
|
1525 |
this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int))); |
|
1526 |
||
1527 |
disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), |
|
1528 |
this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int))); |
|
1529 |
||
1530 |
disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), |
|
1531 |
this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int))); |
|
1532 |
||
1533 |
disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)), |
|
1534 |
this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int))); |
|
1535 |
||
1536 |
disconnect(d->model, SIGNAL(layoutAboutToBeChanged()), |
|
1537 |
this, SLOT(_q_sourceLayoutAboutToBeChanged())); |
|
1538 |
||
1539 |
disconnect(d->model, SIGNAL(layoutChanged()), |
|
1540 |
this, SLOT(_q_sourceLayoutChanged())); |
|
1541 |
||
1542 |
disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_sourceAboutToBeReset())); |
|
1543 |
disconnect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset())); |
|
1544 |
||
1545 |
QAbstractProxyModel::setSourceModel(sourceModel); |
|
1546 |
||
1547 |
connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), |
|
1548 |
this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex))); |
|
1549 |
||
1550 |
connect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), |
|
1551 |
this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); |
|
1552 |
||
1553 |
connect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), |
|
1554 |
this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int))); |
|
1555 |
||
1556 |
connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), |
|
1557 |
this, SLOT(_q_sourceRowsInserted(QModelIndex,int,int))); |
|
1558 |
||
1559 |
connect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), |
|
1560 |
this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int))); |
|
1561 |
||
1562 |
connect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)), |
|
1563 |
this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int))); |
|
1564 |
||
1565 |
connect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), |
|
1566 |
this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int))); |
|
1567 |
||
1568 |
connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), |
|
1569 |
this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int))); |
|
1570 |
||
1571 |
connect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), |
|
1572 |
this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int))); |
|
1573 |
||
1574 |
connect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)), |
|
1575 |
this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int))); |
|
1576 |
||
1577 |
connect(d->model, SIGNAL(layoutAboutToBeChanged()), |
|
1578 |
this, SLOT(_q_sourceLayoutAboutToBeChanged())); |
|
1579 |
||
1580 |
connect(d->model, SIGNAL(layoutChanged()), |
|
1581 |
this, SLOT(_q_sourceLayoutChanged())); |
|
1582 |
||
1583 |
connect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_sourceAboutToBeReset())); |
|
1584 |
connect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset())); |
|
1585 |
||
1586 |
d->clear_mapping(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1587 |
endResetModel(); |
0 | 1588 |
if (d->update_source_sort_column() && d->dynamic_sortfilter) |
1589 |
d->sort(); |
|
1590 |
} |
|
1591 |
||
1592 |
/*! |
|
1593 |
\reimp |
|
1594 |
*/ |
|
1595 |
QModelIndex QSortFilterProxyModel::index(int row, int column, const QModelIndex &parent) const |
|
1596 |
{ |
|
1597 |
Q_D(const QSortFilterProxyModel); |
|
1598 |
if (row < 0 || column < 0) |
|
1599 |
return QModelIndex(); |
|
1600 |
||
1601 |
QModelIndex source_parent = mapToSource(parent); // parent is already mapped at this point |
|
1602 |
IndexMap::const_iterator it = d->create_mapping(source_parent); // but make sure that the children are mapped |
|
1603 |
if (it.value()->source_rows.count() <= row || it.value()->source_columns.count() <= column) |
|
1604 |
return QModelIndex(); |
|
1605 |
||
1606 |
return d->create_index(row, column, it); |
|
1607 |
} |
|
1608 |
||
1609 |
/*! |
|
1610 |
\reimp |
|
1611 |
*/ |
|
1612 |
QModelIndex QSortFilterProxyModel::parent(const QModelIndex &child) const |
|
1613 |
{ |
|
1614 |
Q_D(const QSortFilterProxyModel); |
|
1615 |
if (!d->indexValid(child)) |
|
1616 |
return QModelIndex(); |
|
1617 |
IndexMap::const_iterator it = d->index_to_iterator(child); |
|
1618 |
Q_ASSERT(it != d->source_index_mapping.constEnd()); |
|
1619 |
QModelIndex source_parent = it.key(); |
|
1620 |
QModelIndex proxy_parent = mapFromSource(source_parent); |
|
1621 |
return proxy_parent; |
|
1622 |
} |
|
1623 |
||
1624 |
/*! |
|
1625 |
\reimp |
|
1626 |
*/ |
|
1627 |
int QSortFilterProxyModel::rowCount(const QModelIndex &parent) const |
|
1628 |
{ |
|
1629 |
Q_D(const QSortFilterProxyModel); |
|
1630 |
QModelIndex source_parent = mapToSource(parent); |
|
1631 |
if (parent.isValid() && !source_parent.isValid()) |
|
1632 |
return 0; |
|
1633 |
IndexMap::const_iterator it = d->create_mapping(source_parent); |
|
1634 |
return it.value()->source_rows.count(); |
|
1635 |
} |
|
1636 |
||
1637 |
/*! |
|
1638 |
\reimp |
|
1639 |
*/ |
|
1640 |
int QSortFilterProxyModel::columnCount(const QModelIndex &parent) const |
|
1641 |
{ |
|
1642 |
Q_D(const QSortFilterProxyModel); |
|
1643 |
QModelIndex source_parent = mapToSource(parent); |
|
1644 |
if (parent.isValid() && !source_parent.isValid()) |
|
1645 |
return 0; |
|
1646 |
IndexMap::const_iterator it = d->create_mapping(source_parent); |
|
1647 |
return it.value()->source_columns.count(); |
|
1648 |
} |
|
1649 |
||
1650 |
/*! |
|
1651 |
\reimp |
|
1652 |
*/ |
|
1653 |
bool QSortFilterProxyModel::hasChildren(const QModelIndex &parent) const |
|
1654 |
{ |
|
1655 |
Q_D(const QSortFilterProxyModel); |
|
1656 |
QModelIndex source_parent = mapToSource(parent); |
|
1657 |
if (parent.isValid() && !source_parent.isValid()) |
|
1658 |
return false; |
|
1659 |
if (!d->model->hasChildren(source_parent)) |
|
1660 |
return false; |
|
1661 |
||
1662 |
if (d->model->canFetchMore(source_parent)) |
|
1663 |
return true; //we assume we might have children that can be fetched |
|
1664 |
||
1665 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value(); |
|
1666 |
return m->source_rows.count() != 0 && m->source_columns.count() != 0; |
|
1667 |
} |
|
1668 |
||
1669 |
/*! |
|
1670 |
\reimp |
|
1671 |
*/ |
|
1672 |
QVariant QSortFilterProxyModel::data(const QModelIndex &index, int role) const |
|
1673 |
{ |
|
1674 |
Q_D(const QSortFilterProxyModel); |
|
1675 |
QModelIndex source_index = mapToSource(index); |
|
1676 |
if (index.isValid() && !source_index.isValid()) |
|
1677 |
return QVariant(); |
|
1678 |
return d->model->data(source_index, role); |
|
1679 |
} |
|
1680 |
||
1681 |
/*! |
|
1682 |
\reimp |
|
1683 |
*/ |
|
1684 |
bool QSortFilterProxyModel::setData(const QModelIndex &index, const QVariant &value, int role) |
|
1685 |
{ |
|
1686 |
Q_D(QSortFilterProxyModel); |
|
1687 |
QModelIndex source_index = mapToSource(index); |
|
1688 |
if (index.isValid() && !source_index.isValid()) |
|
1689 |
return false; |
|
1690 |
return d->model->setData(source_index, value, role); |
|
1691 |
} |
|
1692 |
||
1693 |
/*! |
|
1694 |
\reimp |
|
1695 |
*/ |
|
1696 |
QVariant QSortFilterProxyModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
1697 |
{ |
|
1698 |
Q_D(const QSortFilterProxyModel); |
|
1699 |
IndexMap::const_iterator it = d->create_mapping(QModelIndex()); |
|
1700 |
if (it.value()->source_rows.count() * it.value()->source_columns.count() > 0) |
|
1701 |
return QAbstractProxyModel::headerData(section, orientation, role); |
|
1702 |
int source_section; |
|
1703 |
if (orientation == Qt::Vertical) { |
|
1704 |
if (section < 0 || section >= it.value()->source_rows.count()) |
|
1705 |
return QVariant(); |
|
1706 |
source_section = it.value()->source_rows.at(section); |
|
1707 |
} else { |
|
1708 |
if (section < 0 || section >= it.value()->source_columns.count()) |
|
1709 |
return QVariant(); |
|
1710 |
source_section = it.value()->source_columns.at(section); |
|
1711 |
} |
|
1712 |
return d->model->headerData(source_section, orientation, role); |
|
1713 |
} |
|
1714 |
||
1715 |
/*! |
|
1716 |
\reimp |
|
1717 |
*/ |
|
1718 |
bool QSortFilterProxyModel::setHeaderData(int section, Qt::Orientation orientation, |
|
1719 |
const QVariant &value, int role) |
|
1720 |
{ |
|
1721 |
Q_D(QSortFilterProxyModel); |
|
1722 |
IndexMap::const_iterator it = d->create_mapping(QModelIndex()); |
|
1723 |
if (it.value()->source_rows.count() * it.value()->source_columns.count() > 0) |
|
1724 |
return QAbstractProxyModel::setHeaderData(section, orientation, value, role); |
|
1725 |
int source_section; |
|
1726 |
if (orientation == Qt::Vertical) { |
|
1727 |
if (section < 0 || section >= it.value()->source_rows.count()) |
|
1728 |
return false; |
|
1729 |
source_section = it.value()->source_rows.at(section); |
|
1730 |
} else { |
|
1731 |
if (section < 0 || section >= it.value()->source_columns.count()) |
|
1732 |
return false; |
|
1733 |
source_section = it.value()->source_columns.at(section); |
|
1734 |
} |
|
1735 |
return d->model->setHeaderData(source_section, orientation, value, role); |
|
1736 |
} |
|
1737 |
||
1738 |
/*! |
|
1739 |
\reimp |
|
1740 |
*/ |
|
1741 |
QMimeData *QSortFilterProxyModel::mimeData(const QModelIndexList &indexes) const |
|
1742 |
{ |
|
1743 |
Q_D(const QSortFilterProxyModel); |
|
1744 |
QModelIndexList source_indexes; |
|
1745 |
for (int i = 0; i < indexes.count(); ++i) |
|
1746 |
source_indexes << mapToSource(indexes.at(i)); |
|
1747 |
return d->model->mimeData(source_indexes); |
|
1748 |
} |
|
1749 |
||
1750 |
/*! |
|
1751 |
\reimp |
|
1752 |
*/ |
|
1753 |
QStringList QSortFilterProxyModel::mimeTypes() const |
|
1754 |
{ |
|
1755 |
Q_D(const QSortFilterProxyModel); |
|
1756 |
return d->model->mimeTypes(); |
|
1757 |
} |
|
1758 |
||
1759 |
/*! |
|
1760 |
\reimp |
|
1761 |
*/ |
|
1762 |
Qt::DropActions QSortFilterProxyModel::supportedDropActions() const |
|
1763 |
{ |
|
1764 |
Q_D(const QSortFilterProxyModel); |
|
1765 |
return d->model->supportedDropActions(); |
|
1766 |
} |
|
1767 |
||
1768 |
/*! |
|
1769 |
\reimp |
|
1770 |
*/ |
|
1771 |
bool QSortFilterProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, |
|
1772 |
int row, int column, const QModelIndex &parent) |
|
1773 |
{ |
|
1774 |
Q_D(QSortFilterProxyModel); |
|
1775 |
if ((row == -1) && (column == -1)) |
|
1776 |
return d->model->dropMimeData(data, action, -1, -1, mapToSource(parent)); |
|
1777 |
int source_destination_row = -1; |
|
1778 |
int source_destination_column = -1; |
|
1779 |
QModelIndex source_parent; |
|
1780 |
if (row == rowCount(parent)) { |
|
1781 |
source_parent = mapToSource(parent); |
|
1782 |
source_destination_row = d->model->rowCount(source_parent); |
|
1783 |
} else { |
|
1784 |
QModelIndex proxy_index = index(row, column, parent); |
|
1785 |
QModelIndex source_index = mapToSource(proxy_index); |
|
1786 |
source_destination_row = source_index.row(); |
|
1787 |
source_destination_column = source_index.column(); |
|
1788 |
source_parent = source_index.parent(); |
|
1789 |
} |
|
1790 |
return d->model->dropMimeData(data, action, source_destination_row, |
|
1791 |
source_destination_column, source_parent); |
|
1792 |
} |
|
1793 |
||
1794 |
/*! |
|
1795 |
\reimp |
|
1796 |
*/ |
|
1797 |
bool QSortFilterProxyModel::insertRows(int row, int count, const QModelIndex &parent) |
|
1798 |
{ |
|
1799 |
Q_D(QSortFilterProxyModel); |
|
1800 |
if (row < 0 || count <= 0) |
|
1801 |
return false; |
|
1802 |
QModelIndex source_parent = mapToSource(parent); |
|
1803 |
if (parent.isValid() && !source_parent.isValid()) |
|
1804 |
return false; |
|
1805 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value(); |
|
1806 |
if (row > m->source_rows.count()) |
|
1807 |
return false; |
|
1808 |
int source_row = (row >= m->source_rows.count() |
|
1809 |
? m->source_rows.count() |
|
1810 |
: m->source_rows.at(row)); |
|
1811 |
return d->model->insertRows(source_row, count, source_parent); |
|
1812 |
} |
|
1813 |
||
1814 |
/*! |
|
1815 |
\reimp |
|
1816 |
*/ |
|
1817 |
bool QSortFilterProxyModel::insertColumns(int column, int count, const QModelIndex &parent) |
|
1818 |
{ |
|
1819 |
Q_D(QSortFilterProxyModel); |
|
1820 |
if (column < 0|| count <= 0) |
|
1821 |
return false; |
|
1822 |
QModelIndex source_parent = mapToSource(parent); |
|
1823 |
if (parent.isValid() && !source_parent.isValid()) |
|
1824 |
return false; |
|
1825 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value(); |
|
1826 |
if (column > m->source_columns.count()) |
|
1827 |
return false; |
|
1828 |
int source_column = (column >= m->source_columns.count() |
|
1829 |
? m->source_columns.count() |
|
1830 |
: m->source_columns.at(column)); |
|
1831 |
return d->model->insertColumns(source_column, count, source_parent); |
|
1832 |
} |
|
1833 |
||
1834 |
/*! |
|
1835 |
\reimp |
|
1836 |
*/ |
|
1837 |
bool QSortFilterProxyModel::removeRows(int row, int count, const QModelIndex &parent) |
|
1838 |
{ |
|
1839 |
Q_D(QSortFilterProxyModel); |
|
1840 |
if (row < 0 || count <= 0) |
|
1841 |
return false; |
|
1842 |
QModelIndex source_parent = mapToSource(parent); |
|
1843 |
if (parent.isValid() && !source_parent.isValid()) |
|
1844 |
return false; |
|
1845 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value(); |
|
1846 |
if (row + count > m->source_rows.count()) |
|
1847 |
return false; |
|
1848 |
if ((count == 1) |
|
1849 |
|| ((d->source_sort_column < 0) && (m->proxy_rows.count() == m->source_rows.count()))) { |
|
1850 |
int source_row = m->source_rows.at(row); |
|
1851 |
return d->model->removeRows(source_row, count, source_parent); |
|
1852 |
} |
|
1853 |
// remove corresponding source intervals |
|
1854 |
// ### if this proves to be slow, we can switch to single-row removal |
|
1855 |
QVector<int> rows; |
|
1856 |
for (int i = row; i < row + count; ++i) |
|
1857 |
rows.append(m->source_rows.at(i)); |
|
1858 |
qSort(rows.begin(), rows.end()); |
|
1859 |
||
1860 |
int pos = rows.count() - 1; |
|
1861 |
bool ok = true; |
|
1862 |
while (pos >= 0) { |
|
1863 |
const int source_end = rows.at(pos--); |
|
1864 |
int source_start = source_end; |
|
1865 |
while ((pos >= 0) && (rows.at(pos) == (source_start - 1))) { |
|
1866 |
--source_start; |
|
1867 |
--pos; |
|
1868 |
} |
|
1869 |
ok = ok && d->model->removeRows(source_start, source_end - source_start + 1, |
|
1870 |
source_parent); |
|
1871 |
} |
|
1872 |
return ok; |
|
1873 |
} |
|
1874 |
||
1875 |
/*! |
|
1876 |
\reimp |
|
1877 |
*/ |
|
1878 |
bool QSortFilterProxyModel::removeColumns(int column, int count, const QModelIndex &parent) |
|
1879 |
{ |
|
1880 |
Q_D(QSortFilterProxyModel); |
|
1881 |
if (column < 0 || count <= 0) |
|
1882 |
return false; |
|
1883 |
QModelIndex source_parent = mapToSource(parent); |
|
1884 |
if (parent.isValid() && !source_parent.isValid()) |
|
1885 |
return false; |
|
1886 |
QSortFilterProxyModelPrivate::Mapping *m = d->create_mapping(source_parent).value(); |
|
1887 |
if (column + count > m->source_columns.count()) |
|
1888 |
return false; |
|
1889 |
if ((count == 1) || (m->proxy_columns.count() == m->source_columns.count())) { |
|
1890 |
int source_column = m->source_columns.at(column); |
|
1891 |
return d->model->removeColumns(source_column, count, source_parent); |
|
1892 |
} |
|
1893 |
// remove corresponding source intervals |
|
1894 |
QVector<int> columns; |
|
1895 |
for (int i = column; i < column + count; ++i) |
|
1896 |
columns.append(m->source_columns.at(i)); |
|
1897 |
||
1898 |
int pos = columns.count() - 1; |
|
1899 |
bool ok = true; |
|
1900 |
while (pos >= 0) { |
|
1901 |
const int source_end = columns.at(pos--); |
|
1902 |
int source_start = source_end; |
|
1903 |
while ((pos >= 0) && (columns.at(pos) == (source_start - 1))) { |
|
1904 |
--source_start; |
|
1905 |
--pos; |
|
1906 |
} |
|
1907 |
ok = ok && d->model->removeColumns(source_start, source_end - source_start + 1, |
|
1908 |
source_parent); |
|
1909 |
} |
|
1910 |
return ok; |
|
1911 |
} |
|
1912 |
||
1913 |
/*! |
|
1914 |
\reimp |
|
1915 |
*/ |
|
1916 |
void QSortFilterProxyModel::fetchMore(const QModelIndex &parent) |
|
1917 |
{ |
|
1918 |
Q_D(QSortFilterProxyModel); |
|
1919 |
QModelIndex source_parent; |
|
1920 |
if (d->indexValid(parent)) |
|
1921 |
source_parent = mapToSource(parent); |
|
1922 |
d->model->fetchMore(source_parent); |
|
1923 |
} |
|
1924 |
||
1925 |
/*! |
|
1926 |
\reimp |
|
1927 |
*/ |
|
1928 |
bool QSortFilterProxyModel::canFetchMore(const QModelIndex &parent) const |
|
1929 |
{ |
|
1930 |
Q_D(const QSortFilterProxyModel); |
|
1931 |
QModelIndex source_parent; |
|
1932 |
if (d->indexValid(parent)) |
|
1933 |
source_parent = mapToSource(parent); |
|
1934 |
return d->model->canFetchMore(source_parent); |
|
1935 |
} |
|
1936 |
||
1937 |
/*! |
|
1938 |
\reimp |
|
1939 |
*/ |
|
1940 |
Qt::ItemFlags QSortFilterProxyModel::flags(const QModelIndex &index) const |
|
1941 |
{ |
|
1942 |
Q_D(const QSortFilterProxyModel); |
|
1943 |
QModelIndex source_index; |
|
1944 |
if (d->indexValid(index)) |
|
1945 |
source_index = mapToSource(index); |
|
1946 |
return d->model->flags(source_index); |
|
1947 |
} |
|
1948 |
||
1949 |
/*! |
|
1950 |
\reimp |
|
1951 |
*/ |
|
1952 |
QModelIndex QSortFilterProxyModel::buddy(const QModelIndex &index) const |
|
1953 |
{ |
|
1954 |
Q_D(const QSortFilterProxyModel); |
|
1955 |
if (!d->indexValid(index)) |
|
1956 |
return QModelIndex(); |
|
1957 |
QModelIndex source_index = mapToSource(index); |
|
1958 |
QModelIndex source_buddy = d->model->buddy(source_index); |
|
1959 |
if (source_index == source_buddy) |
|
1960 |
return index; |
|
1961 |
return mapFromSource(source_buddy); |
|
1962 |
} |
|
1963 |
||
1964 |
/*! |
|
1965 |
\reimp |
|
1966 |
*/ |
|
1967 |
QModelIndexList QSortFilterProxyModel::match(const QModelIndex &start, int role, |
|
1968 |
const QVariant &value, int hits, |
|
1969 |
Qt::MatchFlags flags) const |
|
1970 |
{ |
|
1971 |
return QAbstractProxyModel::match(start, role, value, hits, flags); |
|
1972 |
} |
|
1973 |
||
1974 |
/*! |
|
1975 |
\reimp |
|
1976 |
*/ |
|
1977 |
QSize QSortFilterProxyModel::span(const QModelIndex &index) const |
|
1978 |
{ |
|
1979 |
Q_D(const QSortFilterProxyModel); |
|
1980 |
QModelIndex source_index = mapToSource(index); |
|
1981 |
if (index.isValid() && !source_index.isValid()) |
|
1982 |
return QSize(); |
|
1983 |
return d->model->span(source_index); |
|
1984 |
} |
|
1985 |
||
1986 |
/*! |
|
1987 |
\reimp |
|
1988 |
*/ |
|
1989 |
void QSortFilterProxyModel::sort(int column, Qt::SortOrder order) |
|
1990 |
{ |
|
1991 |
Q_D(QSortFilterProxyModel); |
|
1992 |
if (d->dynamic_sortfilter && d->proxy_sort_column == column && d->sort_order == order) |
|
1993 |
return; |
|
1994 |
d->sort_order = order; |
|
1995 |
d->proxy_sort_column = column; |
|
1996 |
d->update_source_sort_column(); |
|
1997 |
d->sort(); |
|
1998 |
} |
|
1999 |
||
2000 |
/*! |
|
2001 |
\since 4.5 |
|
2002 |
\brief the column currently used for sorting |
|
2003 |
||
2004 |
This returns the most recently used sort column. |
|
2005 |
*/ |
|
2006 |
int QSortFilterProxyModel::sortColumn() const |
|
2007 |
{ |
|
2008 |
Q_D(const QSortFilterProxyModel); |
|
2009 |
return d->proxy_sort_column; |
|
2010 |
} |
|
2011 |
||
2012 |
/*! |
|
2013 |
\since 4.5 |
|
2014 |
\brief the order currently used for sorting |
|
2015 |
||
2016 |
This returns the most recently used sort order. |
|
2017 |
*/ |
|
2018 |
Qt::SortOrder QSortFilterProxyModel::sortOrder() const |
|
2019 |
{ |
|
2020 |
Q_D(const QSortFilterProxyModel); |
|
2021 |
return d->sort_order; |
|
2022 |
} |
|
2023 |
||
2024 |
/*! |
|
2025 |
\property QSortFilterProxyModel::filterRegExp |
|
2026 |
\brief the QRegExp used to filter the contents of the source model |
|
2027 |
||
2028 |
Setting this property overwrites the current |
|
2029 |
\l{QSortFilterProxyModel::filterCaseSensitivity}{filterCaseSensitivity}. |
|
2030 |
By default, the QRegExp is an empty string matching all contents. |
|
2031 |
||
2032 |
If no QRegExp or an empty string is set, everything in the source model |
|
2033 |
will be accepted. |
|
2034 |
||
2035 |
\sa filterCaseSensitivity, setFilterWildcard(), setFilterFixedString() |
|
2036 |
*/ |
|
2037 |
QRegExp QSortFilterProxyModel::filterRegExp() const |
|
2038 |
{ |
|
2039 |
Q_D(const QSortFilterProxyModel); |
|
2040 |
return d->filter_regexp; |
|
2041 |
} |
|
2042 |
||
2043 |
void QSortFilterProxyModel::setFilterRegExp(const QRegExp ®Exp) |
|
2044 |
{ |
|
2045 |
Q_D(QSortFilterProxyModel); |
|
2046 |
d->filter_regexp = regExp; |
|
2047 |
d->filter_changed(); |
|
2048 |
} |
|
2049 |
||
2050 |
/*! |
|
2051 |
\property QSortFilterProxyModel::filterKeyColumn |
|
2052 |
\brief the column where the key used to filter the contents of the |
|
2053 |
source model is read from. |
|
2054 |
||
2055 |
The default value is 0. If the value is -1, the keys will be read |
|
2056 |
from all columns. |
|
2057 |
*/ |
|
2058 |
int QSortFilterProxyModel::filterKeyColumn() const |
|
2059 |
{ |
|
2060 |
Q_D(const QSortFilterProxyModel); |
|
2061 |
return d->filter_column; |
|
2062 |
} |
|
2063 |
||
2064 |
void QSortFilterProxyModel::setFilterKeyColumn(int column) |
|
2065 |
{ |
|
2066 |
Q_D(QSortFilterProxyModel); |
|
2067 |
d->filter_column = column; |
|
2068 |
d->filter_changed(); |
|
2069 |
} |
|
2070 |
||
2071 |
/*! |
|
2072 |
\property QSortFilterProxyModel::filterCaseSensitivity |
|
2073 |
||
2074 |
\brief the case sensitivity of the QRegExp pattern used to filter the |
|
2075 |
contents of the source model |
|
2076 |
||
2077 |
By default, the filter is case sensitive. |
|
2078 |
||
2079 |
\sa filterRegExp, sortCaseSensitivity |
|
2080 |
*/ |
|
2081 |
Qt::CaseSensitivity QSortFilterProxyModel::filterCaseSensitivity() const |
|
2082 |
{ |
|
2083 |
Q_D(const QSortFilterProxyModel); |
|
2084 |
return d->filter_regexp.caseSensitivity(); |
|
2085 |
} |
|
2086 |
||
2087 |
void QSortFilterProxyModel::setFilterCaseSensitivity(Qt::CaseSensitivity cs) |
|
2088 |
{ |
|
2089 |
Q_D(QSortFilterProxyModel); |
|
2090 |
if (cs == d->filter_regexp.caseSensitivity()) |
|
2091 |
return; |
|
2092 |
d->filter_regexp.setCaseSensitivity(cs); |
|
2093 |
d->filter_changed(); |
|
2094 |
} |
|
2095 |
||
2096 |
/*! |
|
2097 |
\since 4.2 |
|
2098 |
\property QSortFilterProxyModel::sortCaseSensitivity |
|
2099 |
\brief the case sensitivity setting used for comparing strings when sorting |
|
2100 |
||
2101 |
By default, sorting is case sensitive. |
|
2102 |
||
2103 |
\sa filterCaseSensitivity, lessThan() |
|
2104 |
*/ |
|
2105 |
Qt::CaseSensitivity QSortFilterProxyModel::sortCaseSensitivity() const |
|
2106 |
{ |
|
2107 |
Q_D(const QSortFilterProxyModel); |
|
2108 |
return d->sort_casesensitivity; |
|
2109 |
} |
|
2110 |
||
2111 |
void QSortFilterProxyModel::setSortCaseSensitivity(Qt::CaseSensitivity cs) |
|
2112 |
{ |
|
2113 |
Q_D(QSortFilterProxyModel); |
|
2114 |
if (d->sort_casesensitivity == cs) |
|
2115 |
return; |
|
2116 |
||
2117 |
d->sort_casesensitivity = cs; |
|
2118 |
d->sort(); |
|
2119 |
} |
|
2120 |
||
2121 |
/*! |
|
2122 |
\since 4.3 |
|
2123 |
\property QSortFilterProxyModel::isSortLocaleAware |
|
2124 |
\brief the local aware setting used for comparing strings when sorting |
|
2125 |
||
2126 |
By default, sorting is not local aware. |
|
2127 |
||
2128 |
\sa sortCaseSensitivity, lessThan() |
|
2129 |
*/ |
|
2130 |
bool QSortFilterProxyModel::isSortLocaleAware() const |
|
2131 |
{ |
|
2132 |
Q_D(const QSortFilterProxyModel); |
|
2133 |
return d->sort_localeaware; |
|
2134 |
} |
|
2135 |
||
2136 |
void QSortFilterProxyModel::setSortLocaleAware(bool on) |
|
2137 |
{ |
|
2138 |
Q_D(QSortFilterProxyModel); |
|
2139 |
if (d->sort_localeaware == on) |
|
2140 |
return; |
|
2141 |
||
2142 |
d->sort_localeaware = on; |
|
2143 |
d->sort(); |
|
2144 |
} |
|
2145 |
||
2146 |
/*! |
|
2147 |
\overload |
|
2148 |
||
2149 |
Sets the regular expression used to filter the contents |
|
2150 |
of the source model to \a pattern. |
|
2151 |
||
2152 |
\sa setFilterCaseSensitivity(), setFilterWildcard(), setFilterFixedString(), filterRegExp() |
|
2153 |
*/ |
|
2154 |
void QSortFilterProxyModel::setFilterRegExp(const QString &pattern) |
|
2155 |
{ |
|
2156 |
Q_D(QSortFilterProxyModel); |
|
2157 |
d->filter_regexp.setPatternSyntax(QRegExp::RegExp); |
|
2158 |
d->filter_regexp.setPattern(pattern); |
|
2159 |
d->filter_changed(); |
|
2160 |
} |
|
2161 |
||
2162 |
/*! |
|
2163 |
Sets the wildcard expression used to filter the contents |
|
2164 |
of the source model to the given \a pattern. |
|
2165 |
||
2166 |
\sa setFilterCaseSensitivity(), setFilterRegExp(), setFilterFixedString(), filterRegExp() |
|
2167 |
*/ |
|
2168 |
void QSortFilterProxyModel::setFilterWildcard(const QString &pattern) |
|
2169 |
{ |
|
2170 |
Q_D(QSortFilterProxyModel); |
|
2171 |
d->filter_regexp.setPatternSyntax(QRegExp::Wildcard); |
|
2172 |
d->filter_regexp.setPattern(pattern); |
|
2173 |
d->filter_changed(); |
|
2174 |
} |
|
2175 |
||
2176 |
/*! |
|
2177 |
Sets the fixed string used to filter the contents |
|
2178 |
of the source model to the given \a pattern. |
|
2179 |
||
2180 |
\sa setFilterCaseSensitivity(), setFilterRegExp(), setFilterWildcard(), filterRegExp() |
|
2181 |
*/ |
|
2182 |
void QSortFilterProxyModel::setFilterFixedString(const QString &pattern) |
|
2183 |
{ |
|
2184 |
Q_D(QSortFilterProxyModel); |
|
2185 |
d->filter_regexp.setPatternSyntax(QRegExp::FixedString); |
|
2186 |
d->filter_regexp.setPattern(pattern); |
|
2187 |
d->filter_changed(); |
|
2188 |
} |
|
2189 |
||
2190 |
/*! |
|
2191 |
\since 4.2 |
|
2192 |
\property QSortFilterProxyModel::dynamicSortFilter |
|
2193 |
\brief whether the proxy model is dynamically sorted and filtered |
|
2194 |
whenever the contents of the source model change |
|
2195 |
||
2196 |
The default value is false. |
|
2197 |
*/ |
|
2198 |
bool QSortFilterProxyModel::dynamicSortFilter() const |
|
2199 |
{ |
|
2200 |
Q_D(const QSortFilterProxyModel); |
|
2201 |
return d->dynamic_sortfilter; |
|
2202 |
} |
|
2203 |
||
2204 |
void QSortFilterProxyModel::setDynamicSortFilter(bool enable) |
|
2205 |
{ |
|
2206 |
Q_D(QSortFilterProxyModel); |
|
2207 |
d->dynamic_sortfilter = enable; |
|
2208 |
if (enable) |
|
2209 |
d->sort(); |
|
2210 |
} |
|
2211 |
||
2212 |
/*! |
|
2213 |
\since 4.2 |
|
2214 |
\property QSortFilterProxyModel::sortRole |
|
2215 |
\brief the item role that is used to query the source model's data when sorting items |
|
2216 |
||
2217 |
The default value is Qt::DisplayRole. |
|
2218 |
||
2219 |
\sa lessThan() |
|
2220 |
*/ |
|
2221 |
int QSortFilterProxyModel::sortRole() const |
|
2222 |
{ |
|
2223 |
Q_D(const QSortFilterProxyModel); |
|
2224 |
return d->sort_role; |
|
2225 |
} |
|
2226 |
||
2227 |
void QSortFilterProxyModel::setSortRole(int role) |
|
2228 |
{ |
|
2229 |
Q_D(QSortFilterProxyModel); |
|
2230 |
if (d->sort_role == role) |
|
2231 |
return; |
|
2232 |
d->sort_role = role; |
|
2233 |
d->sort(); |
|
2234 |
} |
|
2235 |
||
2236 |
/*! |
|
2237 |
\since 4.2 |
|
2238 |
\property QSortFilterProxyModel::filterRole |
|
2239 |
\brief the item role that is used to query the source model's data when filtering items |
|
2240 |
||
2241 |
The default value is Qt::DisplayRole. |
|
2242 |
||
2243 |
\sa filterAcceptsRow() |
|
2244 |
*/ |
|
2245 |
int QSortFilterProxyModel::filterRole() const |
|
2246 |
{ |
|
2247 |
Q_D(const QSortFilterProxyModel); |
|
2248 |
return d->filter_role; |
|
2249 |
} |
|
2250 |
||
2251 |
void QSortFilterProxyModel::setFilterRole(int role) |
|
2252 |
{ |
|
2253 |
Q_D(QSortFilterProxyModel); |
|
2254 |
if (d->filter_role == role) |
|
2255 |
return; |
|
2256 |
d->filter_role = role; |
|
2257 |
d->filter_changed(); |
|
2258 |
} |
|
2259 |
||
2260 |
/*! |
|
2261 |
\obsolete |
|
2262 |
||
2263 |
This function is obsolete. Use invalidate() instead. |
|
2264 |
*/ |
|
2265 |
void QSortFilterProxyModel::clear() |
|
2266 |
{ |
|
2267 |
Q_D(QSortFilterProxyModel); |
|
2268 |
emit layoutAboutToBeChanged(); |
|
2269 |
d->clear_mapping(); |
|
2270 |
emit layoutChanged(); |
|
2271 |
} |
|
2272 |
||
2273 |
/*! |
|
2274 |
\since 4.3 |
|
2275 |
||
2276 |
Invalidates the current sorting and filtering. |
|
2277 |
||
2278 |
\sa invalidateFilter() |
|
2279 |
*/ |
|
2280 |
void QSortFilterProxyModel::invalidate() |
|
2281 |
{ |
|
2282 |
Q_D(QSortFilterProxyModel); |
|
2283 |
emit layoutAboutToBeChanged(); |
|
2284 |
d->clear_mapping(); |
|
2285 |
emit layoutChanged(); |
|
2286 |
} |
|
2287 |
||
2288 |
/*! |
|
2289 |
\obsolete |
|
2290 |
||
2291 |
This function is obsolete. Use invalidateFilter() instead. |
|
2292 |
*/ |
|
2293 |
void QSortFilterProxyModel::filterChanged() |
|
2294 |
{ |
|
2295 |
Q_D(QSortFilterProxyModel); |
|
2296 |
d->filter_changed(); |
|
2297 |
} |
|
2298 |
||
2299 |
/*! |
|
2300 |
\since 4.3 |
|
2301 |
||
2302 |
Invalidates the current filtering. |
|
2303 |
||
2304 |
This function should be called if you are implementing custom filtering |
|
2305 |
(e.g. filterAcceptsRow()), and your filter parameters have changed. |
|
2306 |
||
2307 |
\sa invalidate() |
|
2308 |
*/ |
|
2309 |
void QSortFilterProxyModel::invalidateFilter() |
|
2310 |
{ |
|
2311 |
Q_D(QSortFilterProxyModel); |
|
2312 |
d->filter_changed(); |
|
2313 |
} |
|
2314 |
||
2315 |
/*! |
|
2316 |
Returns true if the value of the item referred to by the given |
|
2317 |
index \a left is less than the value of the item referred to by |
|
2318 |
the given index \a right, otherwise returns false. |
|
2319 |
||
2320 |
This function is used as the < operator when sorting, and handles |
|
2321 |
the following QVariant types: |
|
2322 |
||
2323 |
\list |
|
2324 |
\o QVariant::Int |
|
2325 |
\o QVariant::UInt |
|
2326 |
\o QVariant::LongLong |
|
2327 |
\o QVariant::ULongLong |
|
2328 |
\o QVariant::Double |
|
2329 |
\o QVariant::Char |
|
2330 |
\o QVariant::Date |
|
2331 |
\o QVariant::Time |
|
2332 |
\o QVariant::DateTime |
|
2333 |
\o QVariant::String |
|
2334 |
\endlist |
|
2335 |
||
2336 |
Any other type will be converted to a QString using |
|
2337 |
QVariant::toString(). |
|
2338 |
||
2339 |
Comparison of \l{QString}s is case sensitive by default; this can |
|
2340 |
be changed using the \l {QSortFilterProxyModel::sortCaseSensitivity} |
|
2341 |
{sortCaseSensitivity} property. |
|
2342 |
||
2343 |
By default, the Qt::DisplayRole associated with the |
|
2344 |
\l{QModelIndex}es is used for comparisons. This can be changed by |
|
2345 |
setting the \l {QSortFilterProxyModel::sortRole} {sortRole} property. |
|
2346 |
||
2347 |
\note The indices passed in correspond to the source model. |
|
2348 |
||
2349 |
\sa sortRole, sortCaseSensitivity, dynamicSortFilter |
|
2350 |
*/ |
|
2351 |
bool QSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const |
|
2352 |
{ |
|
2353 |
Q_D(const QSortFilterProxyModel); |
|
2354 |
QVariant l = (left.model() ? left.model()->data(left, d->sort_role) : QVariant()); |
|
2355 |
QVariant r = (right.model() ? right.model()->data(right, d->sort_role) : QVariant()); |
|
2356 |
switch (l.userType()) { |
|
2357 |
case QVariant::Invalid: |
|
2358 |
return (r.type() == QVariant::Invalid); |
|
2359 |
case QVariant::Int: |
|
2360 |
return l.toInt() < r.toInt(); |
|
2361 |
case QVariant::UInt: |
|
2362 |
return l.toUInt() < r.toUInt(); |
|
2363 |
case QVariant::LongLong: |
|
2364 |
return l.toLongLong() < r.toLongLong(); |
|
2365 |
case QVariant::ULongLong: |
|
2366 |
return l.toULongLong() < r.toULongLong(); |
|
2367 |
case QMetaType::Float: |
|
2368 |
return l.toFloat() < r.toFloat(); |
|
2369 |
case QVariant::Double: |
|
2370 |
return l.toDouble() < r.toDouble(); |
|
2371 |
case QVariant::Char: |
|
2372 |
return l.toChar() < r.toChar(); |
|
2373 |
case QVariant::Date: |
|
2374 |
return l.toDate() < r.toDate(); |
|
2375 |
case QVariant::Time: |
|
2376 |
return l.toTime() < r.toTime(); |
|
2377 |
case QVariant::DateTime: |
|
2378 |
return l.toDateTime() < r.toDateTime(); |
|
2379 |
case QVariant::String: |
|
2380 |
default: |
|
2381 |
if (d->sort_localeaware) |
|
2382 |
return l.toString().localeAwareCompare(r.toString()) < 0; |
|
2383 |
else |
|
2384 |
return l.toString().compare(r.toString(), d->sort_casesensitivity) < 0; |
|
2385 |
} |
|
2386 |
return false; |
|
2387 |
} |
|
2388 |
||
2389 |
/*! |
|
2390 |
Returns true if the item in the row indicated by the given \a source_row |
|
2391 |
and \a source_parent should be included in the model; otherwise returns |
|
2392 |
false. |
|
2393 |
||
2394 |
The default implementation returns true if the value held by the relevant item |
|
2395 |
matches the filter string, wildcard string or regular expression. |
|
2396 |
||
2397 |
\note By default, the Qt::DisplayRole is used to determine if the row |
|
2398 |
should be accepted or not. This can be changed by setting the |
|
2399 |
\l{QSortFilterProxyModel::filterRole}{filterRole} property. |
|
2400 |
||
2401 |
\sa filterAcceptsColumn(), setFilterFixedString(), setFilterRegExp(), setFilterWildcard() |
|
2402 |
*/ |
|
2403 |
bool QSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const |
|
2404 |
{ |
|
2405 |
Q_D(const QSortFilterProxyModel); |
|
2406 |
if (d->filter_regexp.isEmpty()) |
|
2407 |
return true; |
|
2408 |
if (d->filter_column == -1) { |
|
2409 |
int column_count = d->model->columnCount(source_parent); |
|
2410 |
for (int column = 0; column < column_count; ++column) { |
|
2411 |
QModelIndex source_index = d->model->index(source_row, column, source_parent); |
|
2412 |
QString key = d->model->data(source_index, d->filter_role).toString(); |
|
2413 |
if (key.contains(d->filter_regexp)) |
|
2414 |
return true; |
|
2415 |
} |
|
2416 |
return false; |
|
2417 |
} |
|
2418 |
QModelIndex source_index = d->model->index(source_row, d->filter_column, source_parent); |
|
2419 |
if (!source_index.isValid()) // the column may not exist |
|
2420 |
return true; |
|
2421 |
QString key = d->model->data(source_index, d->filter_role).toString(); |
|
2422 |
return key.contains(d->filter_regexp); |
|
2423 |
} |
|
2424 |
||
2425 |
/*! |
|
2426 |
Returns true if the item in the column indicated by the given \a source_column |
|
2427 |
and \a source_parent should be included in the model; otherwise returns false. |
|
2428 |
||
2429 |
The default implementation returns true if the value held by the relevant item |
|
2430 |
matches the filter string, wildcard string or regular expression. |
|
2431 |
||
2432 |
\note By default, the Qt::DisplayRole is used to determine if the row |
|
2433 |
should be accepted or not. This can be changed by setting the \l |
|
2434 |
filterRole property. |
|
2435 |
||
2436 |
\sa filterAcceptsRow(), setFilterFixedString(), setFilterRegExp(), setFilterWildcard() |
|
2437 |
*/ |
|
2438 |
bool QSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const |
|
2439 |
{ |
|
2440 |
Q_UNUSED(source_column); |
|
2441 |
Q_UNUSED(source_parent); |
|
2442 |
return true; |
|
2443 |
} |
|
2444 |
||
2445 |
/*! |
|
2446 |
Returns the source model index corresponding to the given \a |
|
2447 |
proxyIndex from the sorting filter model. |
|
2448 |
||
2449 |
\sa mapFromSource() |
|
2450 |
*/ |
|
2451 |
QModelIndex QSortFilterProxyModel::mapToSource(const QModelIndex &proxyIndex) const |
|
2452 |
{ |
|
2453 |
Q_D(const QSortFilterProxyModel); |
|
2454 |
return d->proxy_to_source(proxyIndex); |
|
2455 |
} |
|
2456 |
||
2457 |
/*! |
|
2458 |
Returns the model index in the QSortFilterProxyModel given the \a |
|
2459 |
sourceIndex from the source model. |
|
2460 |
||
2461 |
\sa mapToSource() |
|
2462 |
*/ |
|
2463 |
QModelIndex QSortFilterProxyModel::mapFromSource(const QModelIndex &sourceIndex) const |
|
2464 |
{ |
|
2465 |
Q_D(const QSortFilterProxyModel); |
|
2466 |
return d->source_to_proxy(sourceIndex); |
|
2467 |
} |
|
2468 |
||
2469 |
/*! |
|
2470 |
\reimp |
|
2471 |
*/ |
|
2472 |
QItemSelection QSortFilterProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const |
|
2473 |
{ |
|
2474 |
return QAbstractProxyModel::mapSelectionToSource(proxySelection); |
|
2475 |
} |
|
2476 |
||
2477 |
/*! |
|
2478 |
\reimp |
|
2479 |
*/ |
|
2480 |
QItemSelection QSortFilterProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const |
|
2481 |
{ |
|
2482 |
return QAbstractProxyModel::mapSelectionFromSource(sourceSelection); |
|
2483 |
} |
|
2484 |
||
2485 |
/*! |
|
2486 |
\fn QObject *QSortFilterProxyModel::parent() const |
|
2487 |
\internal |
|
2488 |
*/ |
|
2489 |
||
2490 |
QT_END_NAMESPACE |
|
2491 |
||
2492 |
#include "moc_qsortfilterproxymodel.cpp" |
|
2493 |
||
2494 |
#endif // QT_NO_SORTFILTERPROXYMODEL |