IconView.h 6.6 KB

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