From 6715b696fa898f03da5ad921e3d65167828eb50e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 16 Aug 2020 11:05:09 +0200 Subject: [PATCH] LibGUI: Maintain selections across SortingProxyModel resorts Now that we sort models imperatively, it's easy to preserve any view selection across a local (same-parent) resort. --- Libraries/LibGUI/SortingProxyModel.cpp | 32 +++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Libraries/LibGUI/SortingProxyModel.cpp b/Libraries/LibGUI/SortingProxyModel.cpp index c2dad94168..e25f179b8d 100644 --- a/Libraries/LibGUI/SortingProxyModel.cpp +++ b/Libraries/LibGUI/SortingProxyModel.cpp @@ -176,6 +176,8 @@ void SortingProxyModel::sort_mapping(Mapping& mapping, int column, SortOrder sor return; } + auto old_source_rows = mapping.source_rows; + int row_count = source().row_count(mapping.source_parent); for (int i = 0; i < row_count; ++i) mapping.source_rows[i] = i; @@ -187,6 +189,34 @@ void SortingProxyModel::sort_mapping(Mapping& mapping, int column, SortOrder sor for (int i = 0; i < row_count; ++i) mapping.proxy_rows[mapping.source_rows[i]] = i; + + // FIXME: I really feel like this should be done at the view layer somehow. + for_each_view([&](AbstractView& view) { + view.selection().change_from_model({}, [&](ModelSelection& selection) { + Vector selected_indexes_in_source; + Vector stale_indexes_in_selection; + selection.for_each_index([&](const ModelIndex& index) { + if (index.parent() == mapping.source_parent) { + stale_indexes_in_selection.append(index); + selected_indexes_in_source.append(source().index(old_source_rows[index.row()], index.column(), mapping.source_parent)); + } + }); + + for (auto& index : stale_indexes_in_selection) { + selection.remove(index); + } + + for (auto& index : selected_indexes_in_source) { + for (size_t i = 0; i < mapping.source_rows.size(); ++i) { + if (mapping.source_rows[i] == index.row()) { + auto new_source_index = this->index(i, index.column(), mapping.source_parent); + selection.add(new_source_index); + break; + } + } + } + }); + }); } void SortingProxyModel::sort(int column, SortOrder sort_order) @@ -199,7 +229,7 @@ void SortingProxyModel::sort(int column, SortOrder sort_order) m_last_key_column = column; m_last_sort_order = sort_order; - did_update(); + did_update(UpdateFlag::DontInvalidateIndexes); } SortingProxyModel::InternalMapIterator SortingProxyModel::build_mapping(const ModelIndex& source_parent)