2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-02 10:08:31 +02:00
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-02-28 01:43:50 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
#include <AK/Badge.h>
|
|
|
|
#include <AK/Function.h>
|
2021-05-13 09:07:43 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-02-28 10:57:09 +01:00
|
|
|
#include <AK/HashTable.h>
|
2019-06-21 18:58:45 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2020-01-07 22:43:31 +11:00
|
|
|
#include <AK/String.h>
|
2021-05-13 09:07:43 +00:00
|
|
|
#include <AK/WeakPtr.h>
|
2020-11-07 23:14:21 +03:30
|
|
|
#include <LibCore/MimeData.h>
|
2021-05-13 09:07:43 +00:00
|
|
|
#include <LibGUI/Forward.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/ModelIndex.h>
|
2020-08-16 16:00:07 +02:00
|
|
|
#include <LibGUI/ModelRole.h>
|
2020-11-07 23:14:21 +03:30
|
|
|
#include <LibGUI/ModelSelection.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Variant.h>
|
2020-02-15 00:10:34 +01:00
|
|
|
#include <LibGfx/Forward.h>
|
|
|
|
#include <LibGfx/TextAlignment.h>
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
enum class SortOrder {
|
2019-05-28 11:53:16 +02:00
|
|
|
None,
|
|
|
|
Ascending,
|
|
|
|
Descending
|
|
|
|
};
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2020-07-11 06:47:26 -06:00
|
|
|
class ModelClient {
|
|
|
|
public:
|
2022-02-26 10:50:04 -07:00
|
|
|
virtual ~ModelClient() = default;
|
2020-07-11 06:47:26 -06:00
|
|
|
|
2020-08-13 20:06:14 +02:00
|
|
|
virtual void model_did_update(unsigned flags) = 0;
|
2021-06-02 10:08:31 +02:00
|
|
|
|
|
|
|
virtual void model_did_insert_rows([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
|
|
|
|
virtual void model_did_insert_columns([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
|
|
|
|
virtual void model_did_move_rows([[maybe_unused]] ModelIndex const& source_parent, [[maybe_unused]] int first, [[maybe_unused]] int last, [[maybe_unused]] ModelIndex const& target_parent, [[maybe_unused]] int target_index) { }
|
|
|
|
virtual void model_did_move_columns([[maybe_unused]] ModelIndex const& source_parent, [[maybe_unused]] int first, [[maybe_unused]] int last, [[maybe_unused]] ModelIndex const& target_parent, [[maybe_unused]] int target_index) { }
|
|
|
|
virtual void model_did_delete_rows([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
|
|
|
|
virtual void model_did_delete_columns([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
|
2020-07-11 06:47:26 -06:00
|
|
|
};
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
class Model : public RefCounted<Model> {
|
2019-02-28 01:43:50 +01:00
|
|
|
public:
|
2020-04-12 12:03:33 +02:00
|
|
|
enum UpdateFlag {
|
2021-04-29 22:23:52 +02:00
|
|
|
DontInvalidateIndices = 0,
|
|
|
|
InvalidateAllIndices = 1 << 0,
|
2020-04-12 12:03:33 +02:00
|
|
|
};
|
|
|
|
|
2020-10-20 15:13:28 -06:00
|
|
|
enum MatchesFlag {
|
|
|
|
AllMatching = 0,
|
|
|
|
FirstMatchOnly = 1 << 0,
|
|
|
|
CaseInsensitive = 1 << 1,
|
|
|
|
MatchAtStart = 1 << 2,
|
2021-02-28 20:50:52 +02:00
|
|
|
MatchFull = 1 << 3,
|
2020-10-20 15:13:28 -06:00
|
|
|
};
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual ~Model();
|
2019-02-28 01:43:50 +01:00
|
|
|
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual int row_count(ModelIndex const& = ModelIndex()) const = 0;
|
|
|
|
virtual int column_count(ModelIndex const& = ModelIndex()) const = 0;
|
2019-05-28 11:53:16 +02:00
|
|
|
virtual String column_name(int) const { return {}; }
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual Variant data(ModelIndex const&, ModelRole = ModelRole::Display) const = 0;
|
|
|
|
virtual TriState data_matches(ModelIndex const&, Variant const&) const { return TriState::Unknown; }
|
2021-05-25 14:13:19 +00:00
|
|
|
virtual void invalidate();
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual ModelIndex parent_index(ModelIndex const&) const { return {}; }
|
|
|
|
virtual ModelIndex index(int row, int column = 0, ModelIndex const& parent = ModelIndex()) const;
|
|
|
|
virtual bool is_editable(ModelIndex const&) const { return false; }
|
2020-10-20 15:13:28 -06:00
|
|
|
virtual bool is_searchable() const { return false; }
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual void set_data(ModelIndex const&, Variant const&) { }
|
2019-12-13 23:36:36 +01:00
|
|
|
virtual int tree_column() const { return 0; }
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual bool accepts_drag(ModelIndex const&, Vector<String> const& mime_types) const;
|
2021-11-11 00:55:02 +01:00
|
|
|
virtual Vector<ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, ModelIndex const& = ModelIndex()) { return {}; }
|
2019-02-28 10:20:04 +01:00
|
|
|
|
2020-05-21 19:45:15 +02:00
|
|
|
virtual bool is_column_sortable([[maybe_unused]] int column_index) const { return true; }
|
2020-08-16 10:44:10 +02:00
|
|
|
virtual void sort([[maybe_unused]] int column, SortOrder) { }
|
2020-05-21 19:45:15 +02:00
|
|
|
|
2021-06-27 12:08:16 +00:00
|
|
|
bool is_within_range(ModelIndex const& index) const
|
2019-02-28 10:20:04 +01:00
|
|
|
{
|
2020-01-10 18:48:27 +03:00
|
|
|
auto parent_index = this->parent_index(index);
|
|
|
|
return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index);
|
2019-02-28 10:20:04 +01:00
|
|
|
}
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2020-01-22 21:15:46 +03:00
|
|
|
virtual StringView drag_data_type() const { return {}; }
|
2021-09-07 18:14:34 +02:00
|
|
|
virtual RefPtr<Core::MimeData> mime_data(ModelSelection const&) const;
|
2020-01-22 21:15:46 +03:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void register_view(Badge<AbstractView>, AbstractView&);
|
|
|
|
void unregister_view(Badge<AbstractView>, AbstractView&);
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2020-07-11 06:47:26 -06:00
|
|
|
void register_client(ModelClient&);
|
|
|
|
void unregister_client(ModelClient&);
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2021-05-13 09:07:43 +00:00
|
|
|
WeakPtr<PersistentHandle> register_persistent_index(Badge<PersistentModelIndex>, ModelIndex const&);
|
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
protected:
|
2020-02-02 15:07:41 +01:00
|
|
|
Model();
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void for_each_view(Function<void(AbstractView&)>);
|
2021-06-02 10:08:31 +02:00
|
|
|
void for_each_client(Function<void(ModelClient&)>);
|
2021-04-29 22:23:52 +02:00
|
|
|
void did_update(unsigned flags = UpdateFlag::InvalidateAllIndices);
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
static bool string_matches(StringView str, StringView needle, unsigned flags)
|
2020-10-20 15:13:28 -06:00
|
|
|
{
|
|
|
|
auto case_sensitivity = (flags & CaseInsensitive) ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive;
|
2021-02-28 20:50:52 +02:00
|
|
|
if (flags & MatchFull)
|
|
|
|
return str.length() == needle.length() && str.starts_with(needle, case_sensitivity);
|
2020-10-20 15:13:28 -06:00
|
|
|
if (flags & MatchAtStart)
|
|
|
|
return str.starts_with(needle, case_sensitivity);
|
|
|
|
return str.contains(needle, case_sensitivity);
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:14:34 +02:00
|
|
|
ModelIndex create_index(int row, int column, void const* data = nullptr) const;
|
2019-03-29 04:58:15 +01:00
|
|
|
|
2021-06-02 10:08:31 +02:00
|
|
|
void begin_insert_rows(ModelIndex const& parent, int first, int last);
|
|
|
|
void begin_insert_columns(ModelIndex const& parent, int first, int last);
|
|
|
|
void begin_move_rows(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
|
|
|
|
void begin_move_columns(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
|
|
|
|
void begin_delete_rows(ModelIndex const& parent, int first, int last);
|
|
|
|
void begin_delete_columns(ModelIndex const& parent, int first, int last);
|
|
|
|
|
|
|
|
void end_insert_rows();
|
|
|
|
void end_insert_columns();
|
|
|
|
void end_move_rows();
|
|
|
|
void end_move_columns();
|
|
|
|
void end_delete_rows();
|
|
|
|
void end_delete_columns();
|
|
|
|
|
|
|
|
void change_persistent_index_list(Vector<ModelIndex> const& old_indices, Vector<ModelIndex> const& new_indices);
|
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
private:
|
2021-06-02 10:08:31 +02:00
|
|
|
enum class OperationType {
|
|
|
|
Invalid = 0,
|
|
|
|
Insert,
|
|
|
|
Move,
|
|
|
|
Delete,
|
|
|
|
Reset
|
|
|
|
};
|
|
|
|
enum class Direction {
|
|
|
|
Row,
|
|
|
|
Column
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Operation {
|
|
|
|
OperationType type { OperationType::Invalid };
|
|
|
|
Direction direction { Direction::Row };
|
|
|
|
ModelIndex source_parent;
|
|
|
|
int first { 0 };
|
|
|
|
int last { 0 };
|
|
|
|
ModelIndex target_parent;
|
|
|
|
int target { 0 };
|
|
|
|
|
|
|
|
Operation(OperationType type)
|
|
|
|
: type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation(OperationType type, Direction direction, ModelIndex const& parent, int first, int last)
|
|
|
|
: type(type)
|
|
|
|
, direction(direction)
|
|
|
|
, source_parent(parent)
|
|
|
|
, first(first)
|
|
|
|
, last(last)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation(OperationType type, Direction direction, ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target)
|
|
|
|
: type(type)
|
|
|
|
, direction(direction)
|
|
|
|
, source_parent(source_parent)
|
|
|
|
, first(first)
|
|
|
|
, last(last)
|
|
|
|
, target_parent(target_parent)
|
|
|
|
, target(target)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void handle_insert(Operation const&);
|
|
|
|
void handle_move(Operation const&);
|
|
|
|
void handle_delete(Operation const&);
|
|
|
|
|
|
|
|
template<bool IsRow>
|
|
|
|
void save_deleted_indices(ModelIndex const& parent, int first, int last);
|
|
|
|
|
|
|
|
HashMap<ModelIndex, OwnPtr<PersistentHandle>> m_persistent_handles;
|
|
|
|
Vector<Operation> m_operation_stack;
|
|
|
|
// NOTE: We need to save which indices have been deleted before the delete
|
|
|
|
// actually happens, because we can't figure out which persistent handles
|
|
|
|
// belong to us in end_delete_rows/columns (because accessing the parents of
|
|
|
|
// the indices might be impossible).
|
|
|
|
Vector<Vector<ModelIndex>> m_deleted_indices_stack;
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
HashTable<AbstractView*> m_views;
|
2020-07-11 06:47:26 -06:00
|
|
|
HashTable<ModelClient*> m_clients;
|
2019-02-28 01:43:50 +01:00
|
|
|
};
|
2019-03-29 14:46:53 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
inline ModelIndex ModelIndex::parent() const
|
2019-03-29 14:46:53 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
return m_model ? m_model->parent_index(*this) : ModelIndex();
|
|
|
|
}
|
|
|
|
|
2019-03-29 14:46:53 +01:00
|
|
|
}
|