IconView.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. int content_width() const;
  37. int horizontal_padding() const { return m_horizontal_padding; }
  38. virtual void scroll_into_view(const ModelIndex&, bool scroll_horizontally = true, bool scroll_vertically = true) override;
  39. Gfx::IntSize effective_item_size() const { return m_effective_item_size; }
  40. int model_column() const { return m_model_column; }
  41. void set_model_column(int column) { m_model_column = column; }
  42. virtual ModelIndex index_at_event_position(const Gfx::IntPoint&) const override;
  43. virtual void select_all() override;
  44. private:
  45. IconView();
  46. virtual void did_update_model(unsigned flags) override;
  47. virtual void paint_event(PaintEvent&) override;
  48. virtual void second_paint_event(PaintEvent&) override;
  49. virtual void resize_event(ResizeEvent&) override;
  50. virtual void mousedown_event(MouseEvent&) override;
  51. virtual void mousemove_event(MouseEvent&) override;
  52. virtual void mouseup_event(MouseEvent&) override;
  53. virtual void keydown_event(KeyEvent&) override;
  54. virtual void drag_move_event(DragEvent&) override;
  55. virtual void move_cursor(CursorMovement, SelectionUpdate) override;
  56. struct ItemData {
  57. Gfx::IntRect text_rect;
  58. Gfx::IntRect icon_rect;
  59. int icon_offset_y;
  60. int text_offset_y;
  61. Variant data;
  62. ModelIndex index;
  63. bool valid { false };
  64. bool selected { false }; // always valid
  65. bool selection_toggled; // only used as a temporary marker
  66. bool is_valid() const { return valid; }
  67. void invalidate()
  68. {
  69. valid = false;
  70. data.clear();
  71. }
  72. bool is_intersecting(const Gfx::IntRect& rect) const
  73. {
  74. ASSERT(valid);
  75. return icon_rect.intersects(rect) || text_rect.intersects(rect);
  76. }
  77. bool is_containing(const Gfx::IntPoint& point) const
  78. {
  79. ASSERT(valid);
  80. return icon_rect.contains(point) || text_rect.contains(point);
  81. }
  82. };
  83. template<typename Function>
  84. IterationDecision for_each_item_intersecting_rect(const Gfx::IntRect& rect, Function f) const
  85. {
  86. ASSERT(model());
  87. if (rect.is_empty())
  88. return IterationDecision::Continue;
  89. int begin_row, begin_column;
  90. column_row_from_content_position(rect.top_left(), begin_row, begin_column);
  91. int end_row, end_column;
  92. column_row_from_content_position(rect.bottom_right(), end_row, end_column);
  93. int items_per_column = end_column - begin_column + 1;
  94. int item_index = max(0, begin_row * m_visual_column_count + begin_column);
  95. int last_index = min(item_count(), end_row * m_visual_column_count + end_column + 1);
  96. while (item_index < last_index) {
  97. for (int i = item_index; i < min(item_index + items_per_column, last_index); i++) {
  98. auto& item_data = get_item_data(i);
  99. if (item_data.is_intersecting(rect)) {
  100. auto decision = f(item_data);
  101. if (decision != IterationDecision::Continue)
  102. return decision;
  103. }
  104. }
  105. item_index += m_visual_column_count;
  106. };
  107. return IterationDecision::Continue;
  108. }
  109. template<typename Function>
  110. IterationDecision for_each_item_intersecting_rects(const Vector<Gfx::IntRect>& rects, Function f) const
  111. {
  112. for (auto& rect : rects) {
  113. auto decision = for_each_item_intersecting_rect(rect, f);
  114. if (decision != IterationDecision::Continue)
  115. return decision;
  116. }
  117. return IterationDecision::Continue;
  118. }
  119. void column_row_from_content_position(const Gfx::IntPoint& content_position, int& row, int& column) const
  120. {
  121. row = max(0, min(m_visual_row_count - 1, content_position.y() / effective_item_size().height()));
  122. column = max(0, min(m_visual_column_count - 1, content_position.x() / effective_item_size().width()));
  123. }
  124. int item_count() const;
  125. Gfx::IntRect item_rect(int item_index) const;
  126. void update_content_size();
  127. void update_item_rects(int item_index, ItemData& item_data) const;
  128. void get_item_rects(int item_index, ItemData& item_data, const Gfx::Font&) const;
  129. bool update_rubber_banding(const Gfx::IntPoint&);
  130. void scroll_out_of_view_timer_fired();
  131. void reinit_item_cache() const;
  132. int model_index_to_item_index(const ModelIndex& model_index) const
  133. {
  134. ASSERT(model_index.row() < item_count());
  135. return model_index.row();
  136. }
  137. virtual void did_update_selection() override;
  138. virtual void clear_selection() override;
  139. virtual void add_selection(const ModelIndex& new_index) override;
  140. virtual void set_selection(const ModelIndex& new_index) override;
  141. virtual void toggle_selection(const ModelIndex& new_index) override;
  142. ItemData& get_item_data(int) const;
  143. ItemData* item_data_from_content_position(const Gfx::IntPoint&) const;
  144. void do_clear_selection();
  145. bool do_add_selection(ItemData&);
  146. void add_selection(ItemData&);
  147. void remove_selection(ItemData&);
  148. void toggle_selection(ItemData&);
  149. int m_horizontal_padding { 5 };
  150. int m_model_column { 0 };
  151. int m_visual_column_count { 0 };
  152. int m_visual_row_count { 0 };
  153. Gfx::IntSize m_effective_item_size { 80, 80 };
  154. bool m_rubber_banding { false };
  155. bool m_rubber_banding_store_selection { false };
  156. RefPtr<Core::Timer> m_out_of_view_timer;
  157. Gfx::IntPoint m_out_of_view_position;
  158. Gfx::IntPoint m_rubber_band_origin;
  159. Gfx::IntPoint m_rubber_band_current;
  160. ModelIndex m_drop_candidate_index;
  161. mutable Vector<ItemData> m_item_data_cache;
  162. mutable int m_selected_count_cache { 0 };
  163. mutable int m_first_selected_hint { 0 };
  164. mutable bool m_item_data_cache_valid { false };
  165. bool m_changing_selection { false };
  166. };
  167. }