IconView.h 6.1 KB

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