AbstractView.h 7.5 KB

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