2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-09 13:33:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Model.h>
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
2020-08-15 13:59:47 +02:00
|
|
|
class SortingProxyModel
|
2020-08-13 15:58:47 +02:00
|
|
|
: public Model
|
2020-07-11 06:47:26 -06:00
|
|
|
, private ModelClient {
|
2019-03-09 13:33:52 +01:00
|
|
|
public:
|
2021-12-24 12:12:04 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<SortingProxyModel>> create(NonnullRefPtr<Model> source)
|
|
|
|
{
|
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) SortingProxyModel(move(source)));
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual ~SortingProxyModel() override;
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2022-04-04 22:42:42 +02:00
|
|
|
virtual int tree_column() const override { return m_source->tree_column(); }
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual int row_count(ModelIndex const& = ModelIndex()) const override;
|
|
|
|
virtual int column_count(ModelIndex const& = ModelIndex()) const override;
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString column_name(int) const override;
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual Variant data(ModelIndex const&, ModelRole = ModelRole::Display) const override;
|
2021-05-25 14:13:19 +00:00
|
|
|
virtual void invalidate() override;
|
2020-01-22 21:15:46 +03:00
|
|
|
virtual StringView drag_data_type() const override;
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual ModelIndex parent_index(ModelIndex const&) const override;
|
|
|
|
virtual ModelIndex index(int row, int column, ModelIndex const& parent) const override;
|
|
|
|
virtual bool is_editable(ModelIndex const&) const override;
|
2020-10-20 15:13:28 -06:00
|
|
|
virtual bool is_searchable() const override;
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual void set_data(ModelIndex const&, Variant const&) override;
|
2021-11-11 00:55:02 +01:00
|
|
|
virtual Vector<ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, ModelIndex const& = ModelIndex()) override;
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual bool accepts_drag(ModelIndex const&, Vector<DeprecatedString> const& mime_types) const override;
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2020-05-21 19:45:15 +02:00
|
|
|
virtual bool is_column_sortable(int column_index) const override;
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual bool less_than(ModelIndex const&, ModelIndex const&) const;
|
2020-08-15 13:59:47 +02:00
|
|
|
|
2021-09-07 18:14:34 +02:00
|
|
|
ModelIndex map_to_source(ModelIndex const&) const;
|
|
|
|
ModelIndex map_to_proxy(ModelIndex const&) const;
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2020-08-16 16:00:07 +02:00
|
|
|
ModelRole sort_role() const { return m_sort_role; }
|
|
|
|
void set_sort_role(ModelRole role) { m_sort_role = role; }
|
2020-07-04 18:40:21 +02:00
|
|
|
|
2020-08-16 10:44:10 +02:00
|
|
|
virtual void sort(int column, SortOrder) override;
|
|
|
|
|
2019-03-09 13:33:52 +01:00
|
|
|
private:
|
2020-08-13 15:58:47 +02:00
|
|
|
explicit SortingProxyModel(NonnullRefPtr<Model> source);
|
2019-03-20 03:27:07 +01:00
|
|
|
|
2021-04-29 22:23:52 +02:00
|
|
|
// NOTE: The internal_data() of indices points to the corresponding Mapping object for that index.
|
2020-08-15 19:22:32 +02:00
|
|
|
struct Mapping {
|
|
|
|
Vector<int> source_rows;
|
|
|
|
Vector<int> proxy_rows;
|
|
|
|
ModelIndex source_parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
using InternalMapIterator = HashMap<ModelIndex, NonnullOwnPtr<Mapping>>::IteratorType;
|
|
|
|
|
2020-08-16 10:44:10 +02:00
|
|
|
void sort_mapping(Mapping&, int column, SortOrder);
|
|
|
|
|
2020-08-13 20:06:14 +02:00
|
|
|
// ^ModelClient
|
|
|
|
virtual void model_did_update(unsigned) override;
|
2020-07-11 06:47:26 -06:00
|
|
|
|
2020-08-13 15:58:47 +02:00
|
|
|
Model& source() { return *m_source; }
|
2021-09-07 18:14:34 +02:00
|
|
|
Model const& source() const { return *m_source; }
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2021-05-25 14:13:19 +00:00
|
|
|
void update_sort(unsigned = UpdateFlag::DontInvalidateIndices);
|
2021-09-07 18:14:34 +02:00
|
|
|
InternalMapIterator build_mapping(ModelIndex const& proxy_index);
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2020-08-13 15:58:47 +02:00
|
|
|
NonnullRefPtr<Model> m_source;
|
2020-08-15 19:22:32 +02:00
|
|
|
|
|
|
|
HashMap<ModelIndex, NonnullOwnPtr<Mapping>> m_mappings;
|
2020-08-16 16:00:07 +02:00
|
|
|
ModelRole m_sort_role { ModelRole::Sort };
|
2020-08-16 10:44:10 +02:00
|
|
|
int m_last_key_column { -1 };
|
|
|
|
SortOrder m_last_sort_order { SortOrder::Ascending };
|
2019-03-09 13:33:52 +01:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
}
|