mirror of
https://github.com/RGBCube/serenity
synced 2025-05-19 19:45:08 +00:00
LibGUI: Preserve selection when resorting a GSortingProxyTableModel.
This commit is contained in:
parent
0b32ab12f1
commit
48e4b3e751
1 changed files with 10 additions and 1 deletions
|
@ -28,7 +28,6 @@ int GSortingProxyTableModel::column_count() const
|
||||||
|
|
||||||
GModelIndex GSortingProxyTableModel::map_to_target(const GModelIndex& index) const
|
GModelIndex GSortingProxyTableModel::map_to_target(const GModelIndex& index) const
|
||||||
{
|
{
|
||||||
ASSERT(!m_row_mappings.is_empty());
|
|
||||||
if (!index.is_valid())
|
if (!index.is_valid())
|
||||||
return { };
|
return { };
|
||||||
if (index.row() >= row_count() || index.column() >= column_count())
|
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()
|
void GSortingProxyTableModel::resort()
|
||||||
{
|
{
|
||||||
|
int previously_selected_target_row = map_to_target(selected_index()).row();
|
||||||
int row_count = target().row_count();
|
int row_count = target().row_count();
|
||||||
m_row_mappings.resize(row_count);
|
m_row_mappings.resize(row_count);
|
||||||
for (int i = 0; i < row_count; ++i)
|
for (int i = 0; i < row_count; ++i)
|
||||||
|
@ -93,6 +93,15 @@ void GSortingProxyTableModel::resort()
|
||||||
bool is_less_than = data1 < data2;
|
bool is_less_than = data1 < data2;
|
||||||
return m_sort_order == GSortOrder::Ascending ? is_less_than : !is_less_than;
|
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();
|
did_update();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue