AbstractView.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/Function.h>
  28. #include <LibGUI/ModelSelection.h>
  29. #include <LibGUI/ScrollableWidget.h>
  30. #include <LibGfx/TextElision.h>
  31. namespace GUI {
  32. class AbstractView : public ScrollableWidget {
  33. C_OBJECT_ABSTRACT(AbstractView);
  34. public:
  35. enum class CursorMovement {
  36. Up,
  37. Down,
  38. Left,
  39. Right,
  40. Home,
  41. End,
  42. PageUp,
  43. PageDown,
  44. };
  45. enum class SelectionUpdate {
  46. None,
  47. Set,
  48. Shift,
  49. Ctrl,
  50. ClearIfNotSelected
  51. };
  52. virtual void move_cursor(CursorMovement, SelectionUpdate) { }
  53. void set_model(RefPtr<Model>);
  54. Model* model() { return m_model.ptr(); }
  55. const Model* model() const { return m_model.ptr(); }
  56. ModelSelection& selection() { return m_selection; }
  57. const ModelSelection& selection() const { return m_selection; }
  58. virtual void select_all() { }
  59. bool is_editable() const { return m_editable; }
  60. void set_editable(bool editable) { m_editable = editable; }
  61. bool is_searching() const { return !m_searching.is_null(); }
  62. bool is_searchable() const;
  63. void set_searchable(bool);
  64. enum EditTrigger {
  65. None = 0,
  66. DoubleClicked = 1 << 0,
  67. EditKeyPressed = 1 << 1,
  68. AnyKeyPressed = 1 << 2,
  69. };
  70. unsigned edit_triggers() const { return m_edit_triggers; }
  71. void set_edit_triggers(unsigned);
  72. bool is_multi_select() const { return m_multi_select; }
  73. void set_multi_select(bool);
  74. virtual bool accepts_focus() const override { return true; }
  75. virtual void did_update_model(unsigned flags);
  76. virtual void did_update_selection();
  77. virtual Gfx::IntRect content_rect(const ModelIndex&) const { return {}; }
  78. virtual ModelIndex index_at_event_position(const Gfx::IntPoint&) const { return {}; }
  79. void begin_editing(const ModelIndex&);
  80. void stop_editing();
  81. void set_activates_on_selection(bool b) { m_activates_on_selection = b; }
  82. bool activates_on_selection() const { return m_activates_on_selection; }
  83. Function<void()> on_selection_change;
  84. Function<void(const ModelIndex&)> on_activation;
  85. Function<void(const ModelIndex&)> on_selection;
  86. Function<void(const ModelIndex&, const ContextMenuEvent&)> on_context_menu_request;
  87. Function<void(const ModelIndex&, const DropEvent&)> on_drop;
  88. Function<OwnPtr<ModelEditingDelegate>(const ModelIndex&)> aid_create_editing_delegate;
  89. void notify_selection_changed(Badge<ModelSelection>);
  90. NonnullRefPtr<Gfx::Font> font_for_index(const ModelIndex&) const;
  91. void set_key_column_and_sort_order(int column, SortOrder);
  92. int key_column() const { return m_key_column; }
  93. SortOrder sort_order() const { return m_sort_order; }
  94. virtual void scroll_into_view(const ModelIndex&, [[maybe_unused]] bool scroll_horizontally = true, [[maybe_unused]] bool scroll_vertically = true) { }
  95. const ModelIndex& cursor_index() const { return m_cursor_index; }
  96. void set_cursor(ModelIndex, SelectionUpdate, bool scroll_cursor_into_view = true);
  97. bool is_tab_key_navigation_enabled() const { return m_tab_key_navigation_enabled; }
  98. void set_tab_key_navigation_enabled(bool enabled) { m_tab_key_navigation_enabled = enabled; }
  99. protected:
  100. AbstractView();
  101. virtual ~AbstractView() override;
  102. virtual void keydown_event(KeyEvent&) override;
  103. virtual void mousedown_event(MouseEvent&) override;
  104. virtual void mousemove_event(MouseEvent&) override;
  105. virtual void mouseup_event(MouseEvent&) override;
  106. virtual void doubleclick_event(MouseEvent&) override;
  107. virtual void context_menu_event(ContextMenuEvent&) override;
  108. virtual void drop_event(DropEvent&) override;
  109. virtual void leave_event(Core::Event&) override;
  110. virtual void hide_event(HideEvent&) override;
  111. virtual void clear_selection();
  112. virtual void set_selection(const ModelIndex&);
  113. virtual void add_selection(const ModelIndex&);
  114. virtual void remove_selection(const ModelIndex&);
  115. virtual void toggle_selection(const ModelIndex&);
  116. void draw_item_text(Gfx::Painter&, const ModelIndex&, bool, const Gfx::IntRect&, const StringView&, const Gfx::Font&, Gfx::TextAlignment, Gfx::TextElision);
  117. virtual void did_scroll() override;
  118. void set_hovered_index(const ModelIndex&);
  119. void activate(const ModelIndex&);
  120. void activate_selected();
  121. void update_edit_widget_position();
  122. StringView searching() const { return m_searching; }
  123. void cancel_searching();
  124. void start_searching_timer();
  125. void do_search(String&&);
  126. bool is_highlighting_searching(const ModelIndex&) const;
  127. bool m_editable { false };
  128. bool m_searchable { true };
  129. ModelIndex m_edit_index;
  130. RefPtr<Widget> m_edit_widget;
  131. Gfx::IntRect m_edit_widget_content_rect;
  132. OwnPtr<ModelEditingDelegate> m_editing_delegate;
  133. Gfx::IntPoint m_left_mousedown_position;
  134. bool m_might_drag { false };
  135. ModelIndex m_hovered_index;
  136. ModelIndex m_highlighted_search_index;
  137. int m_key_column { -1 };
  138. SortOrder m_sort_order;
  139. private:
  140. RefPtr<Model> m_model;
  141. ModelSelection m_selection;
  142. String m_searching;
  143. RefPtr<Core::Timer> m_searching_timer;
  144. ModelIndex m_cursor_index;
  145. unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };
  146. bool m_activates_on_selection { false };
  147. bool m_multi_select { true };
  148. bool m_tab_key_navigation_enabled { false };
  149. };
  150. }