Model.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Badge.h>
  28. #include <AK/Function.h>
  29. #include <AK/HashTable.h>
  30. #include <AK/RefCounted.h>
  31. #include <AK/String.h>
  32. #include <LibCore/MimeData.h>
  33. #include <LibGUI/ModelIndex.h>
  34. #include <LibGUI/ModelRole.h>
  35. #include <LibGUI/ModelSelection.h>
  36. #include <LibGUI/Variant.h>
  37. #include <LibGfx/Forward.h>
  38. #include <LibGfx/TextAlignment.h>
  39. namespace GUI {
  40. enum class SortOrder {
  41. None,
  42. Ascending,
  43. Descending
  44. };
  45. class ModelClient {
  46. public:
  47. virtual ~ModelClient() { }
  48. virtual void model_did_update(unsigned flags) = 0;
  49. };
  50. class Model : public RefCounted<Model> {
  51. public:
  52. enum UpdateFlag {
  53. DontInvalidateIndexes = 0,
  54. InvalidateAllIndexes = 1 << 0,
  55. };
  56. enum MatchesFlag {
  57. AllMatching = 0,
  58. FirstMatchOnly = 1 << 0,
  59. CaseInsensitive = 1 << 1,
  60. MatchAtStart = 1 << 2,
  61. };
  62. virtual ~Model();
  63. virtual int row_count(const ModelIndex& = ModelIndex()) const = 0;
  64. virtual int column_count(const ModelIndex& = ModelIndex()) const = 0;
  65. virtual String column_name(int) const { return {}; }
  66. virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const = 0;
  67. virtual TriState data_matches(const ModelIndex&, Variant) const { return TriState::Unknown; }
  68. virtual void update() = 0;
  69. virtual ModelIndex parent_index(const ModelIndex&) const { return {}; }
  70. virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const;
  71. virtual bool is_editable(const ModelIndex&) const { return false; }
  72. virtual bool is_searchable() const { return false; }
  73. virtual void set_data(const ModelIndex&, const Variant&) { }
  74. virtual int tree_column() const { return 0; }
  75. virtual bool accepts_drag(const ModelIndex&, const StringView& data_type);
  76. virtual Vector<ModelIndex, 1> matches(const StringView&, unsigned = MatchesFlag::AllMatching, const ModelIndex& = ModelIndex()) { return {}; }
  77. virtual bool is_column_sortable([[maybe_unused]] int column_index) const { return true; }
  78. virtual void sort([[maybe_unused]] int column, SortOrder) { }
  79. bool is_valid(const ModelIndex& index) const
  80. {
  81. auto parent_index = this->parent_index(index);
  82. return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index);
  83. }
  84. virtual StringView drag_data_type() const { return {}; }
  85. virtual RefPtr<Core::MimeData> mime_data(const ModelSelection&) const;
  86. void register_view(Badge<AbstractView>, AbstractView&);
  87. void unregister_view(Badge<AbstractView>, AbstractView&);
  88. void register_client(ModelClient&);
  89. void unregister_client(ModelClient&);
  90. protected:
  91. Model();
  92. void for_each_view(Function<void(AbstractView&)>);
  93. void did_update(unsigned flags = UpdateFlag::InvalidateAllIndexes);
  94. static bool string_matches(const StringView& str, const StringView& needle, unsigned flags)
  95. {
  96. auto case_sensitivity = (flags & CaseInsensitive) ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive;
  97. if (flags & MatchAtStart)
  98. return str.starts_with(needle, case_sensitivity);
  99. return str.contains(needle, case_sensitivity);
  100. }
  101. ModelIndex create_index(int row, int column, const void* data = nullptr) const;
  102. private:
  103. HashTable<AbstractView*> m_views;
  104. HashTable<ModelClient*> m_clients;
  105. };
  106. inline ModelIndex ModelIndex::parent() const
  107. {
  108. return m_model ? m_model->parent_index(*this) : ModelIndex();
  109. }
  110. }