AbstractView.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. const Model* model() const { return m_model.ptr(); }
  48. ModelSelection& selection() { return m_selection; }
  49. const ModelSelection& selection() const { return m_selection; }
  50. virtual void select_all() { }
  51. bool is_editable() const { return m_editable; }
  52. void set_editable(bool editable) { m_editable = editable; }
  53. bool is_searchable() const;
  54. void set_searchable(bool);
  55. enum EditTrigger {
  56. None = 0,
  57. DoubleClicked = 1 << 0,
  58. EditKeyPressed = 1 << 1,
  59. AnyKeyPressed = 1 << 2,
  60. };
  61. unsigned edit_triggers() const { return m_edit_triggers; }
  62. void set_edit_triggers(unsigned);
  63. SelectionBehavior selection_behavior() const { return m_selection_behavior; }
  64. void set_selection_behavior(SelectionBehavior behavior) { m_selection_behavior = behavior; }
  65. SelectionMode selection_mode() const { return m_selection_mode; }
  66. void set_selection_mode(SelectionMode);
  67. virtual void model_did_update(unsigned flags) override;
  68. virtual void did_update_selection();
  69. virtual Gfx::IntRect content_rect(const ModelIndex&) const { return {}; }
  70. virtual Gfx::IntRect editing_rect(ModelIndex const& index) const { return content_rect(index); }
  71. virtual Gfx::IntRect paint_invalidation_rect(ModelIndex const& index) const { return content_rect(index); }
  72. virtual ModelIndex index_at_event_position(const Gfx::IntPoint&) const { return {}; }
  73. void begin_editing(const ModelIndex&);
  74. void stop_editing();
  75. void set_activates_on_selection(bool b) { m_activates_on_selection = b; }
  76. bool activates_on_selection() const { return m_activates_on_selection; }
  77. Function<void()> on_selection_change;
  78. Function<void(const ModelIndex&)> on_activation;
  79. Function<void(const ModelIndex&, const ContextMenuEvent&)> on_context_menu_request;
  80. Function<void(const ModelIndex&, const DropEvent&)> on_drop;
  81. Function<OwnPtr<ModelEditingDelegate>(const ModelIndex&)> aid_create_editing_delegate;
  82. void notify_selection_changed(Badge<ModelSelection>);
  83. NonnullRefPtr<Gfx::Font> font_for_index(const ModelIndex&) const;
  84. void set_key_column_and_sort_order(int column, SortOrder);
  85. int key_column() const { return m_key_column; }
  86. SortOrder sort_order() const { return m_sort_order; }
  87. virtual void scroll_into_view(const ModelIndex&, [[maybe_unused]] bool scroll_horizontally = true, [[maybe_unused]] bool scroll_vertically = true) { }
  88. const ModelIndex& cursor_index() const { return m_cursor_index; }
  89. const ModelIndex& selection_start_index() const { return m_selection_start_index; }
  90. void set_cursor(ModelIndex, SelectionUpdate, bool scroll_cursor_into_view = true);
  91. bool is_tab_key_navigation_enabled() const { return m_tab_key_navigation_enabled; }
  92. void set_tab_key_navigation_enabled(bool enabled) { m_tab_key_navigation_enabled = enabled; }
  93. void set_draw_item_text_with_shadow(bool b) { m_draw_item_text_with_shadow = b; }
  94. protected:
  95. AbstractView();
  96. virtual ~AbstractView() override;
  97. virtual void keydown_event(KeyEvent&) override;
  98. virtual void mousedown_event(MouseEvent&) override;
  99. virtual void mousemove_event(MouseEvent&) override;
  100. virtual void mouseup_event(MouseEvent&) override;
  101. virtual void doubleclick_event(MouseEvent&) override;
  102. virtual void context_menu_event(ContextMenuEvent&) override;
  103. virtual void drag_enter_event(DragEvent&) override;
  104. virtual void drag_move_event(DragEvent&) override;
  105. virtual void drag_leave_event(Event&) override;
  106. virtual void drop_event(DropEvent&) override;
  107. virtual void leave_event(Core::Event&) override;
  108. virtual void hide_event(HideEvent&) override;
  109. virtual void focusin_event(FocusEvent&) override;
  110. virtual void clear_selection();
  111. virtual void set_selection(const ModelIndex&);
  112. virtual void set_selection_start_index(const ModelIndex&);
  113. virtual void add_selection(const ModelIndex&);
  114. virtual void remove_selection(const ModelIndex&);
  115. virtual void toggle_selection(const ModelIndex&);
  116. virtual void did_change_hovered_index([[maybe_unused]] const ModelIndex& old_index, [[maybe_unused]] const ModelIndex& new_index) { }
  117. virtual void did_change_cursor_index([[maybe_unused]] const ModelIndex& old_index, [[maybe_unused]] const ModelIndex& new_index) { }
  118. virtual void editing_widget_did_change([[maybe_unused]] const ModelIndex& index) { }
  119. void draw_item_text(Gfx::Painter&, const ModelIndex&, bool, const Gfx::IntRect&, const StringView&, const Gfx::Font&, Gfx::TextAlignment, Gfx::TextElision, size_t search_highlighting_offset = 0);
  120. void set_suppress_update_on_selection_change(bool value) { m_suppress_update_on_selection_change = value; }
  121. virtual void did_scroll() override;
  122. void set_hovered_index(const ModelIndex&);
  123. void activate(const ModelIndex&);
  124. void activate_selected();
  125. void update_edit_widget_position();
  126. bool is_searching() const { return !m_searching.is_null(); }
  127. void cancel_searching();
  128. void start_searching_timer();
  129. void do_search(String&&);
  130. ModelIndex drop_candidate_index() const { return m_drop_candidate_index; }
  131. bool m_editable { false };
  132. bool m_searchable { true };
  133. RefPtr<Widget> m_edit_widget;
  134. Gfx::IntRect m_edit_widget_content_rect;
  135. OwnPtr<ModelEditingDelegate> m_editing_delegate;
  136. Gfx::IntPoint m_left_mousedown_position;
  137. bool m_might_drag { false };
  138. int m_key_column { -1 };
  139. SortOrder m_sort_order;
  140. ModelIndex m_edit_index;
  141. ModelIndex m_hovered_index;
  142. ModelIndex m_highlighted_search_index;
  143. private:
  144. ModelIndex m_selection_start_index;
  145. ModelIndex m_cursor_index;
  146. ModelIndex m_drop_candidate_index;
  147. RefPtr<Model> m_model;
  148. ModelSelection m_selection;
  149. String m_searching;
  150. RefPtr<Core::Timer> m_searching_timer;
  151. SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems };
  152. SelectionMode m_selection_mode { SelectionMode::SingleSelection };
  153. unsigned m_edit_triggers { EditTrigger::DoubleClicked | EditTrigger::EditKeyPressed };
  154. bool m_activates_on_selection { false };
  155. bool m_tab_key_navigation_enabled { false };
  156. bool m_is_dragging { false };
  157. bool m_draw_item_text_with_shadow { false };
  158. bool m_suppress_update_on_selection_change { false };
  159. };
  160. }