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