LibGUI: Preserve selection when resorting a GSortingProxyTableModel.

This commit is contained in:
Andreas Kling 2019-03-10 02:13:42 +01:00
parent 0b32ab12f1
commit 48e4b3e751
Notes: sideshowbarker 2024-07-19 15:05:49 +09:00

View file

@ -28,7 +28,6 @@ int GSortingProxyTableModel::column_count() const
GModelIndex GSortingProxyTableModel::map_to_target(const GModelIndex& index) const
{
ASSERT(!m_row_mappings.is_empty());
if (!index.is_valid())
return { };
if (index.row() >= row_count() || index.column() >= column_count())
@ -79,6 +78,7 @@ void GSortingProxyTableModel::set_key_column_and_sort_order(int column, GSortOrd
void GSortingProxyTableModel::resort()
{
int previously_selected_target_row = map_to_target(selected_index()).row();
int row_count = target().row_count();
m_row_mappings.resize(row_count);
for (int i = 0; i < row_count; ++i)
@ -93,6 +93,15 @@ void GSortingProxyTableModel::resort()
bool is_less_than = data1 < data2;
return m_sort_order == GSortOrder::Ascending ? is_less_than : !is_less_than;
});
if (previously_selected_target_row != -1) {
// Preserve selection.
for (int i = 0; i < row_count; ++i) {
if (m_row_mappings[i] == previously_selected_target_row) {
set_selected_index({ i, 0 });
break;
}
}
}
did_update();
}