Serenity Operating System
at master 216 lines 8.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com> 4 * Copyright (c) 2022, the SerenityOS developers. 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#pragma once 10 11#include <AK/Badge.h> 12#include <AK/DeprecatedString.h> 13#include <AK/Function.h> 14#include <AK/HashMap.h> 15#include <AK/HashTable.h> 16#include <AK/RefCounted.h> 17#include <AK/WeakPtr.h> 18#include <LibCore/MimeData.h> 19#include <LibGUI/Forward.h> 20#include <LibGUI/ModelIndex.h> 21#include <LibGUI/ModelRole.h> 22#include <LibGUI/ModelSelection.h> 23#include <LibGUI/Variant.h> 24#include <LibGfx/Forward.h> 25#include <LibGfx/TextAlignment.h> 26 27namespace GUI { 28 29enum class SortOrder { 30 None, 31 Ascending, 32 Descending 33}; 34 35class ModelClient { 36public: 37 virtual ~ModelClient() = default; 38 39 virtual void model_did_update(unsigned flags) = 0; 40 41 virtual void model_did_insert_rows([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { } 42 virtual void model_did_insert_columns([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { } 43 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) { } 44 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) { } 45 virtual void model_did_delete_rows([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { } 46 virtual void model_did_delete_columns([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { } 47}; 48 49class Model : public RefCounted<Model> { 50public: 51 enum UpdateFlag { 52 DontInvalidateIndices = 0, 53 InvalidateAllIndices = 1 << 0, 54 DontResizeColumns = 1 << 1, 55 }; 56 57 enum MatchesFlag { 58 AllMatching = 0, 59 FirstMatchOnly = 1 << 0, 60 CaseInsensitive = 1 << 1, 61 MatchAtStart = 1 << 2, 62 MatchFull = 1 << 3, 63 }; 64 65 virtual ~Model(); 66 67 virtual int row_count(ModelIndex const& = ModelIndex()) const = 0; 68 virtual int column_count(ModelIndex const& = ModelIndex()) const = 0; 69 virtual DeprecatedString column_name(int) const { return {}; } 70 virtual Variant data(ModelIndex const&, ModelRole = ModelRole::Display) const = 0; 71 virtual TriState data_matches(ModelIndex const&, Variant const&) const { return TriState::Unknown; } 72 virtual void invalidate(); 73 virtual ModelIndex parent_index(ModelIndex const&) const { return {}; } 74 virtual ModelIndex index(int row, int column = 0, ModelIndex const& parent = ModelIndex()) const; 75 virtual bool is_editable(ModelIndex const&) const { return false; } 76 virtual bool is_searchable() const { return false; } 77 virtual void set_data(ModelIndex const&, Variant const&) { } 78 virtual int tree_column() const { return 0; } 79 virtual bool accepts_drag(ModelIndex const&, Vector<DeprecatedString> const& mime_types) const; 80 virtual Vector<ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, ModelIndex const& = ModelIndex()) { return {}; } 81 82 virtual bool is_column_sortable([[maybe_unused]] int column_index) const { return true; } 83 virtual void sort([[maybe_unused]] int column, SortOrder) { } 84 85 bool is_within_range(ModelIndex const& index) const 86 { 87 auto parent_index = this->parent_index(index); 88 return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index); 89 } 90 91 virtual StringView drag_data_type() const { return {}; } 92 virtual RefPtr<Core::MimeData> mime_data(ModelSelection const&) const; 93 94 void register_view(Badge<AbstractView>, AbstractView&); 95 void unregister_view(Badge<AbstractView>, AbstractView&); 96 97 void register_client(ModelClient&); 98 void unregister_client(ModelClient&); 99 100 WeakPtr<PersistentHandle> register_persistent_index(Badge<PersistentModelIndex>, ModelIndex const&); 101 102 // NOTE: This is a public version of create_index() which is normally protected, 103 // but this can be used when creating a model translator like in Ladybird. 104 ModelIndex unsafe_create_index(int row, int column, void const* data = nullptr) const 105 { 106 return create_index(row, column, data); 107 } 108 109protected: 110 Model(); 111 112 void for_each_view(Function<void(AbstractView&)>); 113 void for_each_client(Function<void(ModelClient&)>); 114 void did_update(unsigned flags = UpdateFlag::InvalidateAllIndices); 115 116 static bool string_matches(StringView str, StringView needle, unsigned flags) 117 { 118 auto case_sensitivity = (flags & CaseInsensitive) ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive; 119 if (flags & MatchFull) 120 return str.length() == needle.length() && str.starts_with(needle, case_sensitivity); 121 if (flags & MatchAtStart) 122 return str.starts_with(needle, case_sensitivity); 123 return str.contains(needle, case_sensitivity); 124 } 125 126 ModelIndex create_index(int row, int column, void const* data = nullptr) const; 127 128 void begin_insert_rows(ModelIndex const& parent, int first, int last); 129 void begin_insert_columns(ModelIndex const& parent, int first, int last); 130 void begin_move_rows(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index); 131 void begin_move_columns(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index); 132 void begin_delete_rows(ModelIndex const& parent, int first, int last); 133 void begin_delete_columns(ModelIndex const& parent, int first, int last); 134 135 void end_insert_rows(); 136 void end_insert_columns(); 137 void end_move_rows(); 138 void end_move_columns(); 139 void end_delete_rows(); 140 void end_delete_columns(); 141 142 void change_persistent_index_list(Vector<ModelIndex> const& old_indices, Vector<ModelIndex> const& new_indices); 143 144private: 145 enum class OperationType { 146 Invalid = 0, 147 Insert, 148 Move, 149 Delete, 150 Reset 151 }; 152 enum class Direction { 153 Row, 154 Column 155 }; 156 157 struct Operation { 158 OperationType type { OperationType::Invalid }; 159 Direction direction { Direction::Row }; 160 ModelIndex source_parent; 161 int first { 0 }; 162 int last { 0 }; 163 ModelIndex target_parent; 164 int target { 0 }; 165 166 Operation(OperationType type) 167 : type(type) 168 { 169 } 170 171 Operation(OperationType type, Direction direction, ModelIndex const& parent, int first, int last) 172 : type(type) 173 , direction(direction) 174 , source_parent(parent) 175 , first(first) 176 , last(last) 177 { 178 } 179 180 Operation(OperationType type, Direction direction, ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target) 181 : type(type) 182 , direction(direction) 183 , source_parent(source_parent) 184 , first(first) 185 , last(last) 186 , target_parent(target_parent) 187 , target(target) 188 { 189 } 190 }; 191 192 void handle_insert(Operation const&); 193 void handle_move(Operation const&); 194 void handle_delete(Operation const&); 195 196 template<bool IsRow> 197 void save_deleted_indices(ModelIndex const& parent, int first, int last); 198 199 HashMap<ModelIndex, OwnPtr<PersistentHandle>> m_persistent_handles; 200 Vector<Operation> m_operation_stack; 201 // NOTE: We need to save which indices have been deleted before the delete 202 // actually happens, because we can't figure out which persistent handles 203 // belong to us in end_delete_rows/columns (because accessing the parents of 204 // the indices might be impossible). 205 Vector<Vector<ModelIndex>> m_deleted_indices_stack; 206 207 HashTable<AbstractView*> m_views; 208 HashTable<ModelClient*> m_clients; 209}; 210 211inline ModelIndex ModelIndex::parent() const 212{ 213 return m_model ? m_model->parent_index(*this) : ModelIndex(); 214} 215 216}