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