Model.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/Function.h>
  9. #include <AK/HashTable.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/String.h>
  12. #include <LibCore/MimeData.h>
  13. #include <LibGUI/ModelIndex.h>
  14. #include <LibGUI/ModelRole.h>
  15. #include <LibGUI/ModelSelection.h>
  16. #include <LibGUI/Variant.h>
  17. #include <LibGfx/Forward.h>
  18. #include <LibGfx/TextAlignment.h>
  19. namespace GUI {
  20. enum class SortOrder {
  21. None,
  22. Ascending,
  23. Descending
  24. };
  25. class ModelClient {
  26. public:
  27. virtual ~ModelClient() { }
  28. virtual void model_did_update(unsigned flags) = 0;
  29. };
  30. class Model : public RefCounted<Model> {
  31. public:
  32. enum UpdateFlag {
  33. DontInvalidateIndices = 0,
  34. InvalidateAllIndices = 1 << 0,
  35. };
  36. enum MatchesFlag {
  37. AllMatching = 0,
  38. FirstMatchOnly = 1 << 0,
  39. CaseInsensitive = 1 << 1,
  40. MatchAtStart = 1 << 2,
  41. MatchFull = 1 << 3,
  42. };
  43. virtual ~Model();
  44. virtual int row_count(const ModelIndex& = ModelIndex()) const = 0;
  45. virtual int column_count(const ModelIndex& = ModelIndex()) const = 0;
  46. virtual String column_name(int) const { return {}; }
  47. virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const = 0;
  48. virtual TriState data_matches(const ModelIndex&, const Variant&) const { return TriState::Unknown; }
  49. virtual void invalidate();
  50. virtual ModelIndex parent_index(const ModelIndex&) const { return {}; }
  51. virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const;
  52. virtual bool is_editable(const ModelIndex&) const { return false; }
  53. virtual bool is_searchable() const { return false; }
  54. virtual void set_data(const ModelIndex&, const Variant&) { }
  55. virtual int tree_column() const { return 0; }
  56. virtual bool accepts_drag(const ModelIndex&, const Vector<String>& mime_types) const;
  57. virtual Vector<ModelIndex, 1> matches(const StringView&, unsigned = MatchesFlag::AllMatching, const ModelIndex& = ModelIndex()) { return {}; }
  58. virtual bool is_column_sortable([[maybe_unused]] int column_index) const { return true; }
  59. virtual void sort([[maybe_unused]] int column, SortOrder) { }
  60. bool is_valid(const ModelIndex& index) const
  61. {
  62. auto parent_index = this->parent_index(index);
  63. return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index);
  64. }
  65. virtual StringView drag_data_type() const { return {}; }
  66. virtual RefPtr<Core::MimeData> mime_data(const ModelSelection&) const;
  67. void register_view(Badge<AbstractView>, AbstractView&);
  68. void unregister_view(Badge<AbstractView>, AbstractView&);
  69. void register_client(ModelClient&);
  70. void unregister_client(ModelClient&);
  71. protected:
  72. Model();
  73. void for_each_view(Function<void(AbstractView&)>);
  74. void did_update(unsigned flags = UpdateFlag::InvalidateAllIndices);
  75. static bool string_matches(const StringView& str, const StringView& needle, unsigned flags)
  76. {
  77. auto case_sensitivity = (flags & CaseInsensitive) ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive;
  78. if (flags & MatchFull)
  79. return str.length() == needle.length() && str.starts_with(needle, case_sensitivity);
  80. if (flags & MatchAtStart)
  81. return str.starts_with(needle, case_sensitivity);
  82. return str.contains(needle, case_sensitivity);
  83. }
  84. ModelIndex create_index(int row, int column, const void* data = nullptr) const;
  85. private:
  86. HashTable<AbstractView*> m_views;
  87. HashTable<ModelClient*> m_clients;
  88. };
  89. inline ModelIndex ModelIndex::parent() const
  90. {
  91. return m_model ? m_model->parent_index(*this) : ModelIndex();
  92. }
  93. }