LibGUI: Virtualize SortingProxyModel comparator

This patch adds SortingProxyModel::less_than(ModelIndex, ModelIndex)
which is now used to implement the sort order. This allows you to
subclass SortingProxyModel if you want to tweak how sorting works.

Get rid of the awkward case sensitivity logic in SortingProxyModel
since that can now be done outside. Turns out nobody was actually
using it anyway, but it seems like something we will want to do
in the future someday.
This commit is contained in:
Andreas Kling 2020-08-15 13:59:47 +02:00
parent b503f74d25
commit 05dd9e5bfb
Notes: sideshowbarker 2024-07-19 03:33:41 +09:00
2 changed files with 13 additions and 14 deletions

View file

@ -109,6 +109,15 @@ void SortingProxyModel::set_key_column_and_sort_order(int column, SortOrder sort
resort();
}
bool SortingProxyModel::less_than(const ModelIndex& index1, const ModelIndex& index2) const
{
auto data1 = data(index1, Role::Sort);
auto data2 = data(index2, Role::Sort);
if (data1.is_string() && data2.is_string())
return data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
return data1 < data2;
}
void SortingProxyModel::resort(unsigned flags)
{
auto old_row_mappings = m_row_mappings;
@ -121,15 +130,7 @@ void SortingProxyModel::resort(unsigned flags)
return;
}
quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
auto data1 = source().data(source().index(row1, m_key_column), m_sort_role);
auto data2 = source().data(source().index(row2, m_key_column), m_sort_role);
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;
bool is_less_than = less_than(source().index(row1, m_key_column), source().index(row2, m_key_column));
return m_sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
});
for_each_view([&](AbstractView& view) {

View file

@ -30,7 +30,7 @@
namespace GUI {
class SortingProxyModel final
class SortingProxyModel
: public Model
, private ModelClient {
public:
@ -49,6 +49,8 @@ public:
virtual void set_key_column_and_sort_order(int, SortOrder) override;
virtual bool is_column_sortable(int column_index) const override;
virtual bool less_than(const ModelIndex&, const ModelIndex&) const;
ModelIndex map_to_source(const ModelIndex&) const;
Role sort_role() const { return m_sort_role; }
@ -65,15 +67,11 @@ private:
void resort(unsigned flags = Model::UpdateFlag::DontInvalidateIndexes);
void set_sorting_case_sensitive(bool b) { m_sorting_case_sensitive = b; }
bool is_sorting_case_sensitive() { return m_sorting_case_sensitive; }
NonnullRefPtr<Model> m_source;
Vector<int> m_row_mappings;
int m_key_column { -1 };
SortOrder m_sort_order { SortOrder::Ascending };
Role m_sort_role { Role::Sort };
bool m_sorting_case_sensitive { false };
};
}