diff options
author | Gabriel de Dietrich <gabriel.dietrich-de@nokia.com> | 2009-08-10 18:46:16 (GMT) |
---|---|---|
committer | Gabriel de Dietrich <gabriel.dietrich-de@nokia.com> | 2009-08-10 18:52:11 (GMT) |
commit | 36c93eca351218ce43a0c6346209d364737b6b84 (patch) | |
tree | e07eaca509e13e6128036dc38af6c82029f61754 /src | |
parent | dd9d869300d34725bc480d827f91b3103c84f045 (diff) | |
download | Qt-36c93eca351218ce43a0c6346209d364737b6b84.zip Qt-36c93eca351218ce43a0c6346209d364737b6b84.tar.gz Qt-36c93eca351218ce43a0c6346209d364737b6b84.tar.bz2 |
Better implementation for commit d13418effc5f00474541ae513a30c9a42c2a1cb3. The
previous version could run in an endless loop with infinite models.
Reviewed-by: olivier
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/itemviews/qitemselectionmodel.cpp | 60 | ||||
-rw-r--r-- | src/gui/itemviews/qitemselectionmodel_p.h | 2 |
2 files changed, 36 insertions, 26 deletions
diff --git a/src/gui/itemviews/qitemselectionmodel.cpp b/src/gui/itemviews/qitemselectionmodel.cpp index 0f35ac1..06c345f 100644 --- a/src/gui/itemviews/qitemselectionmodel.cpp +++ b/src/gui/itemviews/qitemselectionmodel.cpp @@ -590,31 +590,43 @@ void QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved(const QModelIndex &pare emit q->currentColumnChanged(currentIndex, old); } - // update selectionsx - QModelIndex tl = model->index(start, 0, parent); - QModelIndex br = model->index(end, model->columnCount(parent) - 1, parent); - recursiveDeselect(QItemSelectionRange(tl, br)); - finalize(); -} - -void QItemSelectionModelPrivate::recursiveDeselect(const QItemSelectionRange &range) -{ - Q_Q(QItemSelectionModel); - - QItemSelection sel(range.topLeft(), range.bottomRight()); - q->select(sel, QItemSelectionModel::Deselect); - - QModelIndexList idxList = range.indexes(); - QModelIndexList::const_iterator it = idxList.begin(); - for (; it != idxList.end(); ++it) - { - if (!model->hasChildren(*it)) - continue; - - const QModelIndex &firstChild = it->child(0,0); - const QModelIndex &lastChild = it->child(model->rowCount(*it) - 1, model->columnCount(*it) - 1); - recursiveDeselect(QItemSelectionRange(firstChild, lastChild)); + QItemSelection deselected; + QItemSelection::iterator it = currentSelection.begin(); + while (it != currentSelection.end()) { + if (it->topLeft().parent() != parent) { // Check parents until reaching root or contained in range + QModelIndex itParent = it->topLeft().parent(); + while (itParent.isValid() && itParent.parent() != parent) + itParent = itParent.parent(); + + if (parent.isValid() && start <= itParent.row() && itParent.row() <= end) { + deselected.append(*it); + it = currentSelection.erase(it); + } else { + ++it; + } + } else if (start <= it->bottom() && it->bottom() <= end // Full inclusion + && start <= it->top() && it->top() <= end) { + deselected.append(*it); + it = currentSelection.erase(it); + } else if (start <= it->top() && it->top() <= end) { // Top intersection + deselected.append(QItemSelectionRange(it->topLeft(), model->index(end, it->left(), it->parent()))); + it = currentSelection.insert(it, QItemSelectionRange(model->index(end + 1, it->left(), it->parent()), + it->bottomRight())); + it = currentSelection.erase(++it); + } else if (start <= it->bottom() && it->bottom() <= end) { // Bottom intersection + deselected.append(QItemSelectionRange(model->index(start, it->right(), it->parent()), it->bottomRight())); + it = currentSelection.insert(it, QItemSelectionRange(it->topLeft(), + model->index(start - 1, it->right(), it->parent()))); + it = currentSelection.erase(++it); + } else { + if (it->top() < start && end < it->bottom()) // Middle intersection (do nothing) + deselected.append(QItemSelectionRange(model->index(start, it->right(), it->parent()), + model->index(end, it->left(), it->parent()))); + ++it; + } } + + emit q->selectionChanged(QItemSelection(), deselected); } /*! diff --git a/src/gui/itemviews/qitemselectionmodel_p.h b/src/gui/itemviews/qitemselectionmodel_p.h index 8176d4c..18ad506 100644 --- a/src/gui/itemviews/qitemselectionmodel_p.h +++ b/src/gui/itemviews/qitemselectionmodel_p.h @@ -77,8 +77,6 @@ public: void _q_layoutAboutToBeChanged(); void _q_layoutChanged(); - void recursiveDeselect(const QItemSelectionRange &range); - inline void remove(QList<QItemSelectionRange> &r) { QList<QItemSelectionRange>::const_iterator it = r.constBegin(); |