AbstractView.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/Function.h>
  8. #include <LibGUI/AbstractScrollableWidget.h>
  9. #include <LibGUI/Model.h>
  10. #include <LibGUI/ModelSelection.h>
  11. #include <LibGfx/TextElision.h>
  12. namespace GUI {
  13. class AbstractView
  14. : public AbstractScrollableWidget
  15. , public ModelClient {
  16. C_OBJECT_ABSTRACT(AbstractView);
  17. public:
  18. enum class CursorMovement {
  19. Up,
  20. Down,
  21. Left,
  22. Right,
  23. Home,
  24. End,
  25. PageUp,
  26. PageDown,
  27. };
  28. enum class SelectionUpdate {
  29. None,
  30. Set,
  31. Shift,
  32. Ctrl,
  33. ClearIfNotSelected
  34. };
  35. enum class SelectionBehavior {
  36. SelectItems,
  37. SelectRows,
  38. };
  39. enum class SelectionMode {
  40. SingleSelection,
  41. MultiSelection,
  42. NoSelection,
  43. };
  44. virtual void move_cursor(CursorMovement, SelectionUpdate) { }
  45. void set_model(RefPtr<Model>);
  46. Model* model() { return m_model.ptr(); }
  47. Model const* model() const { return m_model.ptr(); }
  48. ModelSelection& selection() { return m_selection; }
  49. ModelSelection const& selection() const { return m_selection; }
  50. virtual void select_all() { }
  51. void activate(ModelIndex const&);
  52. void activate_selected();
  53. bool is_editable() const { return m_editable; }
  54. void set_editable(bool editable) { m_editable = editable; }
  55. bool is_searchable() const;
  56. void set_searchable(bool);
  57. enum EditTrigger {
  58. None = 0,
  59. DoubleClicked = 1 << 0,
  60. EditKeyPressed = 1 << 1,
  61. AnyKeyPressed = 1 << 2,
  62. };
  63. unsigned edit_triggers() const { return m_edit_triggers; }
  64. void set_edit_triggers(unsigned);
  65. SelectionBehavior selection_behavior() const { return m_selection_behavior; }
  66. void set_selection_behavior(SelectionBehavior behavior) { m_selection_behavior = behavior; }
  67. SelectionMode selection_mode() const { return m_selection_mode; }
  68. void set_selection_mode(SelectionMode);
  69. virtual void model_did_update(unsigned flags) override;
  70. virtual void did_update_selection();
  71. virtual Gfx::IntRect content_rect(ModelIndex const&) const { return {}; }
  72. virtual Gfx::IntRect editing_rect(ModelIndex const& index) const { return content_rect(index); }
  73. virtual Gfx::IntRect paint_invalidation_rect(ModelIndex const& index) const { return content_rect(index); }
  74. virtual ModelIndex index_at_event_position(Gfx::IntPoint) const { return {}; }
  75. void begin_editing(ModelIndex const&);
  76. void stop_editing();
  77. void set_activates_on_selection(bool b) { m_activates_on_selection = b; }
  78. bool activates_on_selection() const { return m_activates_on_selection; }
  79. Function<void()> on_selection_change;
  80. Function<void(ModelIndex const&)> on_activation;
  81. Function<void(ModelIndex const&, ContextMenuEvent const&)> on_context_menu_request;
  82. Function<void(ModelIndex const&, DropEvent const&)> on_drop;
  83. Function<OwnPtr<ModelEditingDelegate>(ModelIndex const&)> aid_create_editing_delegate;
  84. void notify_selection_changed(Badge<ModelSelection>);
  85. NonnullRefPtr<Gfx::Font const> font_for_index(ModelIndex const&) const;
  86. void set_key_column_and_sort_order(int column, SortOrder);
  87. int key_column() const { return m_key_column; }
  88. void set_key_column(int column) { set_key_column_and_sort_order(column, sort_order()); }
  89. SortOrder sort_order() const { return m_sort_order; }
  90. void set_sort_order(SortOrder order) { set_key_column_and_sort_order(key_column(), order); }
  91. virtual void scroll_into_view(ModelIndex const&, [[maybe_unused]] bool scroll_horizontally = true, [[maybe_unused]] bool scroll_vertically = true) { }
  92. ModelIndex const& cursor_index() const { return m_cursor_index; }
  93. ModelIndex const& selection_start_index() const { return m_selection_start_index; }
  94. void set_cursor(ModelIndex, SelectionUpdate, bool scroll_cursor_into_view = true);
  95. bool is_tab_key_navigation_enabled() const { return m_tab_key_navigation_enabled; }
  96. void set_tab_key_navigation_enabled(bool enabled) { m_tab_key_navigation_enabled = enabled; }
  97. void set_draw_item_text_with_shadow(bool b) { m_draw_item_text_with_shadow = b; }
  98. bool does_draw_item_text_with_shadow() const { return m_draw_item_text_with_shadow; }
  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 drag_enter_event(DragEvent&) override;
  109. virtual void drag_move_event(DragEvent&) override;
  110. virtual void drag_leave_event(Event&) override;
  111. virtual void drop_event(DropEvent&) override;
  112. virtual void leave_event(Core::Event&) override;
  113. virtual void hide_event(HideEvent&) override;
  114. virtual void focusin_event(FocusEvent&) override;
  115. virtual void automatic_scrolling_timer_did_fire() override;
  116. virtual void clear_selection();
  117. virtual void set_selection(ModelIndex const&);
  118. virtual void set_selection_start_index(ModelIndex const&);
  119. virtual void add_selection(ModelIndex const&);
  120. virtual void remove_selection(ModelIndex const&);
  121. virtual void toggle_selection(ModelIndex const&);
  122. virtual void select_range(ModelIndex const&);
  123. virtual void did_change_hovered_index([[maybe_unused]] ModelIndex const& old_index, [[maybe_unused]] ModelIndex const& new_index) { }
  124. virtual void did_change_cursor_index([[maybe_unused]] ModelIndex const& old_index, [[maybe_unused]] ModelIndex const& new_index) { }
  125. virtual void editing_widget_did_change([[maybe_unused]] ModelIndex const& index) { }
  126. void draw_item_text(Gfx::Painter&, ModelIndex const&, bool, Gfx::IntRect const&, StringView, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextElision, size_t search_highlighting_offset = 0);
  127. void set_suppress_update_on_selection_change(bool value) { m_suppress_update_on_selection_change = value; }
  128. virtual void did_scroll() override;
  129. void set_hovered_index(ModelIndex const&);
  130. void update_edit_widget_position();
  131. void stop_highlighted_search_timer();
  132. void start_highlighted_search_timer();
  133. ModelIndex find_next_search_match(StringView const);
  134. void highlight_search(ModelIndex const index);
  135. ModelIndex drop_candidate_index() const { return m_drop_candidate_index; }
  136. bool m_editable { false };
  137. bool m_searchable { true };
  138. RefPtr<Widget> m_edit_widget;
  139. Gfx::IntRect m_edit_widget_content_rect;
  140. OwnPtr<ModelEditingDelegate> m_editing_delegate;
  141. Gfx::IntPoint m_left_mousedown_position;
  142. bool m_might_drag { false };
  143. int m_key_column { -1 };
  144. SortOrder m_sort_order;
  145. ModelIndex m_edit_index;
  146. ModelIndex m_hovered_index;
  147. ModelIndex m_highlighted_search_index;
  148. private:
  149. ModelIndex m_selection_start_index;
  150. ModelIndex m_cursor_index;
  151. ModelIndex m_drop_candidate_index;
  152. RefPtr<Model> m_model;
  153. ModelSelection m_selection;
  154. Optional<DeprecatedString> m_highlighted_search;
  155. RefPtr<Core::Timer> m_highlighted_search_timer;
  156. SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems };
  157. SelectionMode m_selection_mode { SelectionMode::SingleSelection };
  158. unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };
  159. bool m_activates_on_selection { false };
  160. bool m_tab_key_navigation_enabled { false };
  161. bool m_is_dragging { false };
  162. bool m_draw_item_text_with_shadow { false };
  163. bool m_suppress_update_on_selection_change { false };
  164. Gfx::IntPoint m_automatic_scroll_delta {};
  165. };
  166. }