1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 20:25:07 +00:00

LibGUI: Make GSortingProxyModel update the selection on resort again

After resorting, we now re-map every selected index so it matches the
new row mappings. This makes the process table view in SystemMonitor
behave normally again :^)
This commit is contained in:
Andreas Kling 2019-11-27 19:04:35 +01:00
parent 23a2e84873
commit 449ebbddb6

View file

@ -1,4 +1,5 @@
#include <AK/QuickSort.h>
#include <LibGUI/GAbstractView.h>
#include <LibGUI/GSortingProxyModel.h>
#include <stdio.h>
#include <stdlib.h>
@ -73,6 +74,7 @@ void GSortingProxyModel::set_key_column_and_sort_order(int column, GSortOrder so
void GSortingProxyModel::resort()
{
auto old_row_mappings = m_row_mappings;
int row_count = target().row_count();
m_row_mappings.resize(row_count);
for (int i = 0; i < row_count; ++i)
@ -87,11 +89,28 @@ void GSortingProxyModel::resort()
if (data1 == data2)
return 0;
bool is_less_than;
if (data1.is_string() && data2.is_string() && !m_sorting_case_sensitive)
is_less_than = data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
else
is_less_than = data1 < data2;
if (data1.is_string() && data2.is_string() && !m_sorting_case_sensitive)
is_less_than = data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
else
is_less_than = data1 < data2;
return m_sort_order == GSortOrder::Ascending ? is_less_than : !is_less_than;
});
did_update();
for_each_view([&](GAbstractView& view) {
auto& selection = view.selection();
Vector<GModelIndex> selected_indexes_in_target;
selection.for_each_index([&](const GModelIndex& index) {
selected_indexes_in_target.append(target().index(old_row_mappings[index.row()], index.column()));
});
selection.clear();
for (auto& index : selected_indexes_in_target) {
for (int i = 0; i < m_row_mappings.size(); ++i) {
if (m_row_mappings[i] == index.row()) {
selection.add(this->index(i, index.column()));
continue;
}
}
}
});
}