IconView.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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&) 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. private:
  36. IconView();
  37. virtual void model_did_update(unsigned flags) override;
  38. virtual void paint_event(PaintEvent&) override;
  39. virtual void second_paint_event(PaintEvent&) override;
  40. virtual void resize_event(ResizeEvent&) override;
  41. virtual void mousedown_event(MouseEvent&) override;
  42. virtual void mousemove_event(MouseEvent&) override;
  43. virtual void mouseup_event(MouseEvent&) override;
  44. virtual void did_change_hovered_index(ModelIndex const& old_index, ModelIndex const& new_index) override;
  45. virtual void did_change_cursor_index(ModelIndex const& old_index, ModelIndex const& new_index) override;
  46. virtual void editing_widget_did_change(ModelIndex const& index) override;
  47. virtual void move_cursor(CursorMovement, SelectionUpdate) override;
  48. virtual void on_automatic_scrolling_timer_fired() override;
  49. struct ItemData {
  50. Gfx::IntRect text_rect;
  51. Optional<Gfx::IntRect> text_rect_wrapped;
  52. Gfx::IntRect icon_rect;
  53. int icon_offset_y;
  54. int text_offset_y;
  55. String text;
  56. Vector<StringView> wrapped_text_lines;
  57. ModelIndex index;
  58. bool valid { false };
  59. bool selected { false }; // always valid
  60. bool selection_toggled; // only used as a temporary marker
  61. bool is_valid() const { return valid; }
  62. void invalidate()
  63. {
  64. valid = false;
  65. text = {};
  66. }
  67. Gfx::IntRect hot_icon_rect() const { return icon_rect.inflated(10, 10); }
  68. Gfx::IntRect hot_text_rect() const { return text_rect.inflated(2, 2); }
  69. bool is_intersecting(Gfx::IntRect const& rect) const
  70. {
  71. VERIFY(valid);
  72. return hot_icon_rect().intersects(rect) || hot_text_rect().intersects(rect);
  73. }
  74. bool is_containing(Gfx::IntPoint const& point) const
  75. {
  76. VERIFY(valid);
  77. return hot_icon_rect().contains(point) || hot_text_rect().contains(point);
  78. }
  79. Gfx::IntRect rect(bool wrapped = false) const
  80. {
  81. if (wrapped && text_rect_wrapped.has_value())
  82. return text_rect_wrapped->united(icon_rect);
  83. return text_rect.united(icon_rect);
  84. }
  85. };
  86. template<typename Function>
  87. IterationDecision for_each_item_intersecting_rect(Gfx::IntRect const&, Function) const;
  88. template<typename Function>
  89. IterationDecision for_each_item_intersecting_rects(Vector<Gfx::IntRect> const&, Function) const;
  90. void column_row_from_content_position(Gfx::IntPoint const& content_position, int& row, int& column) const
  91. {
  92. row = max(0, min(m_visual_row_count - 1, content_position.y() / effective_item_size().height()));
  93. column = max(0, min(m_visual_column_count - 1, content_position.x() / effective_item_size().width()));
  94. }
  95. int item_count() const;
  96. Gfx::IntRect item_rect(int item_index) const;
  97. void update_content_size();
  98. void update_item_rects(int item_index, ItemData& item_data) const;
  99. void get_item_rects(int item_index, ItemData& item_data, Gfx::Font const&) const;
  100. bool update_rubber_banding(Gfx::IntPoint const&);
  101. int items_per_page() const;
  102. void rebuild_item_cache() const;
  103. int model_index_to_item_index(ModelIndex const& model_index) const
  104. {
  105. VERIFY(model_index.row() < item_count());
  106. return model_index.row();
  107. }
  108. virtual void did_update_selection() override;
  109. virtual void clear_selection() override;
  110. virtual void add_selection(ModelIndex const& new_index) override;
  111. virtual void set_selection(ModelIndex const& new_index) override;
  112. virtual void toggle_selection(ModelIndex const& new_index) override;
  113. ItemData& get_item_data(int) const;
  114. ItemData* item_data_from_content_position(Gfx::IntPoint const&) const;
  115. void do_clear_selection();
  116. bool do_add_selection(ItemData&);
  117. void add_selection(ItemData&);
  118. void remove_item_selection(ItemData&);
  119. void toggle_selection(ItemData&);
  120. int m_horizontal_padding { 5 };
  121. int m_model_column { 0 };
  122. int m_visual_column_count { 0 };
  123. int m_visual_row_count { 0 };
  124. Gfx::IntSize m_effective_item_size { 80, 80 };
  125. bool m_always_wrap_item_labels { false };
  126. bool m_rubber_banding { false };
  127. Gfx::IntPoint m_out_of_view_position;
  128. Gfx::IntPoint m_rubber_band_origin;
  129. Gfx::IntPoint m_rubber_band_current;
  130. Gfx::IntPoint m_rubber_band_scroll_delta;
  131. FlowDirection m_flow_direction { FlowDirection::LeftToRight };
  132. mutable Vector<ItemData> m_item_data_cache;
  133. mutable int m_selected_count_cache { 0 };
  134. mutable int m_first_selected_hint { 0 };
  135. mutable bool m_item_data_cache_valid { false };
  136. bool m_changing_selection { false };
  137. bool m_had_valid_size { false };
  138. };
  139. }