IconView.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/IterationDecision.h>
  9. #include <LibGUI/AbstractView.h>
  10. #include <LibGUI/Forward.h>
  11. #include <LibGUI/Variant.h>
  12. namespace GUI {
  13. class IconView : public AbstractView {
  14. C_OBJECT(IconView)
  15. public:
  16. virtual ~IconView() override = default;
  17. enum class FlowDirection {
  18. LeftToRight,
  19. TopToBottom,
  20. };
  21. FlowDirection flow_direction() const { return m_flow_direction; }
  22. void set_flow_direction(FlowDirection);
  23. int horizontal_padding() const { return m_horizontal_padding; }
  24. virtual void scroll_into_view(ModelIndex const&, bool scroll_horizontally = true, bool scroll_vertically = true) override;
  25. Gfx::IntSize effective_item_size() const { return m_effective_item_size; }
  26. bool always_wrap_item_labels() const { return m_always_wrap_item_labels; }
  27. void set_always_wrap_item_labels(bool value) { m_always_wrap_item_labels = value; }
  28. int model_column() const { return m_model_column; }
  29. void set_model_column(int column) { m_model_column = column; }
  30. virtual ModelIndex index_at_event_position(Gfx::IntPoint) const override;
  31. virtual Gfx::IntRect content_rect(ModelIndex const&) const override;
  32. virtual Gfx::IntRect editing_rect(ModelIndex const&) const override;
  33. virtual Gfx::IntRect paint_invalidation_rect(ModelIndex const&) const override;
  34. virtual void select_all() override;
  35. protected:
  36. virtual void did_change_font() override;
  37. private:
  38. IconView();
  39. virtual void model_did_update(unsigned flags) override;
  40. virtual void paint_event(PaintEvent&) override;
  41. virtual void second_paint_event(PaintEvent&) override;
  42. virtual void resize_event(ResizeEvent&) override;
  43. virtual void mousedown_event(MouseEvent&) override;
  44. virtual void mousemove_event(MouseEvent&) override;
  45. virtual void mouseup_event(MouseEvent&) override;
  46. virtual void did_change_hovered_index(ModelIndex const& old_index, ModelIndex const& new_index) override;
  47. virtual void did_change_cursor_index(ModelIndex const& old_index, ModelIndex const& new_index) override;
  48. virtual void editing_widget_did_change(ModelIndex const& index) override;
  49. virtual void move_cursor(CursorMovement, SelectionUpdate) override;
  50. virtual void automatic_scrolling_timer_did_fire() override;
  51. struct ItemData {
  52. Gfx::IntRect text_rect;
  53. Optional<Gfx::IntRect> text_rect_wrapped;
  54. Gfx::IntRect icon_rect;
  55. int icon_offset_y;
  56. int text_offset_y;
  57. DeprecatedString text;
  58. Vector<StringView> wrapped_text_lines;
  59. ModelIndex index;
  60. bool valid { false };
  61. bool selected { false }; // always valid
  62. bool selection_toggled; // only used as a temporary marker
  63. bool is_valid() const { return valid; }
  64. void invalidate()
  65. {
  66. valid = false;
  67. text = {};
  68. }
  69. Gfx::IntRect hot_icon_rect() const { return icon_rect.inflated(10, 10); }
  70. Gfx::IntRect hot_text_rect() const { return text_rect.inflated(2, 2); }
  71. bool is_intersecting(Gfx::IntRect const& rect) const
  72. {
  73. VERIFY(valid);
  74. return hot_icon_rect().intersects(rect) || hot_text_rect().intersects(rect);
  75. }
  76. bool is_containing(Gfx::IntPoint point) const
  77. {
  78. VERIFY(valid);
  79. return hot_icon_rect().contains(point) || hot_text_rect().contains(point);
  80. }
  81. Gfx::IntRect rect(bool wrapped = false) const
  82. {
  83. if (wrapped && text_rect_wrapped.has_value())
  84. return text_rect_wrapped->united(icon_rect);
  85. return text_rect.united(icon_rect);
  86. }
  87. };
  88. template<typename Function>
  89. IterationDecision for_each_item_intersecting_rect(Gfx::IntRect const&, Function) const;
  90. template<typename Function>
  91. IterationDecision for_each_item_intersecting_rects(Vector<Gfx::IntRect> const&, Function) const;
  92. void column_row_from_content_position(Gfx::IntPoint content_position, int& row, int& column) const
  93. {
  94. row = max(0, min(m_visual_row_count - 1, content_position.y() / effective_item_size().height()));
  95. column = max(0, min(m_visual_column_count - 1, content_position.x() / effective_item_size().width()));
  96. }
  97. int item_count() const;
  98. Gfx::IntRect item_rect(int item_index) const;
  99. void update_content_size();
  100. void update_item_rects(int item_index, ItemData& item_data) const;
  101. void get_item_rects(int item_index, ItemData& item_data, Gfx::Font const&) const;
  102. bool update_rubber_banding(Gfx::IntPoint);
  103. int items_per_page() const;
  104. void rebuild_item_cache() const;
  105. int model_index_to_item_index(ModelIndex const& model_index) const
  106. {
  107. VERIFY(model_index.row() < item_count());
  108. return model_index.row();
  109. }
  110. virtual void did_update_selection() override;
  111. virtual void clear_selection() override;
  112. virtual void add_selection(ModelIndex const& new_index) override;
  113. virtual void set_selection(ModelIndex const& new_index) override;
  114. virtual void toggle_selection(ModelIndex const& new_index) override;
  115. ItemData& get_item_data(int) const;
  116. ItemData* item_data_from_content_position(Gfx::IntPoint) const;
  117. void do_clear_selection();
  118. bool do_add_selection(ItemData&);
  119. void add_selection(ItemData&);
  120. void remove_item_selection(ItemData&);
  121. void toggle_selection(ItemData&);
  122. int m_horizontal_padding { 5 };
  123. int m_model_column { 0 };
  124. int m_visual_column_count { 0 };
  125. int m_visual_row_count { 0 };
  126. Gfx::IntSize m_effective_item_size { 80, 80 };
  127. bool m_always_wrap_item_labels { false };
  128. bool m_rubber_banding { false };
  129. Gfx::IntPoint m_out_of_view_position;
  130. Gfx::IntPoint m_rubber_band_origin;
  131. Gfx::IntPoint m_rubber_band_current;
  132. Gfx::IntPoint m_rubber_band_scroll_delta;
  133. FlowDirection m_flow_direction { FlowDirection::LeftToRight };
  134. mutable Vector<ItemData> m_item_data_cache;
  135. mutable int m_selected_count_cache { 0 };
  136. mutable int m_first_selected_hint { 0 };
  137. mutable bool m_item_data_cache_valid { false };
  138. bool m_changing_selection { false };
  139. bool m_had_valid_size { false };
  140. };
  141. }