ListView.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include <LibGUI/ListView.h>
  8. #include <LibGUI/Model.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGUI/Scrollbar.h>
  11. #include <LibGfx/Palette.h>
  12. REGISTER_WIDGET(GUI, ListView)
  13. namespace GUI {
  14. ListView::ListView()
  15. {
  16. set_fill_with_background_color(true);
  17. set_background_role(ColorRole::Base);
  18. set_foreground_role(ColorRole::BaseText);
  19. set_searchable(true);
  20. }
  21. ListView::~ListView() = default;
  22. void ListView::select_all()
  23. {
  24. selection().clear();
  25. for (int item_index = 0; item_index < item_count(); ++item_index) {
  26. auto index = model()->index(item_index, m_model_column);
  27. selection().add(index);
  28. }
  29. }
  30. void ListView::update_content_size()
  31. {
  32. if (!model())
  33. return set_content_size({});
  34. int content_width = 0;
  35. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  36. auto text = model()->index(row, m_model_column).data();
  37. content_width = max(content_width, font().width(text.to_string()) + horizontal_padding() * 2);
  38. }
  39. m_max_item_width = content_width;
  40. content_width = max(content_width, widget_inner_rect().width());
  41. int content_height = item_count() * item_height();
  42. set_content_size({ content_width, content_height });
  43. }
  44. void ListView::resize_event(ResizeEvent& event)
  45. {
  46. update_content_size();
  47. AbstractView::resize_event(event);
  48. }
  49. void ListView::layout_relevant_change_occured()
  50. {
  51. update_content_size();
  52. AbstractView::layout_relevant_change_occured();
  53. }
  54. void ListView::model_did_update(unsigned flags)
  55. {
  56. AbstractView::model_did_update(flags);
  57. update_content_size();
  58. update();
  59. }
  60. Gfx::IntRect ListView::content_rect(int row) const
  61. {
  62. return { 0, row * item_height(), content_width(), item_height() };
  63. }
  64. Gfx::IntRect ListView::content_rect(ModelIndex const& index) const
  65. {
  66. return content_rect(index.row());
  67. }
  68. ModelIndex ListView::index_at_event_position(Gfx::IntPoint const& point) const
  69. {
  70. VERIFY(model());
  71. auto adjusted_position = this->adjusted_position(point);
  72. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  73. if (!content_rect(row).contains(adjusted_position))
  74. continue;
  75. return model()->index(row, m_model_column);
  76. }
  77. return {};
  78. }
  79. Gfx::IntPoint ListView::adjusted_position(Gfx::IntPoint const& position) const
  80. {
  81. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  82. }
  83. void ListView::paint_list_item(Painter& painter, int row_index, int painted_item_index)
  84. {
  85. bool is_selected_row = selection().contains_row(row_index);
  86. int y = painted_item_index * item_height();
  87. Color background_color;
  88. if (is_selected_row) {
  89. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  90. } else {
  91. Color row_fill_color = palette().color(background_role());
  92. if (alternating_row_colors() && (painted_item_index % 2)) {
  93. background_color = row_fill_color.darkened(0.8f);
  94. } else {
  95. background_color = row_fill_color;
  96. }
  97. }
  98. Gfx::IntRect row_rect(0, y, content_width(), item_height());
  99. painter.fill_rect(row_rect, background_color);
  100. auto index = model()->index(row_index, m_model_column);
  101. auto data = index.data();
  102. auto font = font_for_index(index);
  103. if (data.is_bitmap()) {
  104. painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  105. } else if (data.is_icon()) {
  106. if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
  107. auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  108. painter.blit(row_rect.location(), *bitmap, bitmap->rect(), opacity);
  109. }
  110. } else {
  111. auto text_rect = row_rect;
  112. text_rect.translate_by(horizontal_padding(), 0);
  113. text_rect.set_width(text_rect.width() - horizontal_padding() * 2);
  114. auto text_alignment = index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
  115. draw_item_text(painter, index, is_selected_row, text_rect, data.to_string(), font, text_alignment, Gfx::TextElision::None);
  116. }
  117. }
  118. void ListView::paint_event(PaintEvent& event)
  119. {
  120. Frame::paint_event(event);
  121. if (!model())
  122. return;
  123. Painter painter(*this);
  124. painter.add_clip_rect(frame_inner_rect());
  125. painter.add_clip_rect(event.rect());
  126. painter.translate(frame_thickness(), frame_thickness());
  127. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  128. int exposed_width = max(content_size().width(), width());
  129. int painted_item_index = 0;
  130. for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
  131. paint_list_item(painter, row_index, painted_item_index);
  132. ++painted_item_index;
  133. };
  134. Gfx::IntRect unpainted_rect(0, painted_item_index * item_height(), exposed_width, height());
  135. if (fill_with_background_color())
  136. painter.fill_rect(unpainted_rect, palette().color(background_role()));
  137. }
  138. int ListView::item_count() const
  139. {
  140. if (!model())
  141. return 0;
  142. return model()->row_count();
  143. }
  144. void ListView::mousemove_event(MouseEvent& event)
  145. {
  146. auto previous_hovered_index = m_hovered_index;
  147. AbstractView::mousemove_event(event);
  148. if (hover_highlighting() && previous_hovered_index != m_hovered_index)
  149. set_cursor(m_hovered_index, SelectionUpdate::Set);
  150. }
  151. void ListView::keydown_event(KeyEvent& event)
  152. {
  153. if (!model())
  154. return AbstractView::keydown_event(event);
  155. if (event.key() == KeyCode::Key_Escape) {
  156. if (on_escape_pressed)
  157. on_escape_pressed();
  158. return;
  159. }
  160. AbstractView::keydown_event(event);
  161. }
  162. void ListView::move_cursor_relative(int steps, SelectionUpdate selection_update)
  163. {
  164. if (!model())
  165. return;
  166. auto& model = *this->model();
  167. ModelIndex new_index;
  168. if (cursor_index().is_valid()) {
  169. auto row = cursor_index().row();
  170. if (steps > 0) {
  171. if (row + steps >= model.row_count())
  172. row = model.row_count() - 1;
  173. else
  174. row += steps;
  175. } else if (steps < 0) {
  176. if (row < -steps)
  177. row = 0;
  178. else
  179. row += steps;
  180. }
  181. new_index = model.index(row, cursor_index().column());
  182. } else {
  183. new_index = model.index(0, 0);
  184. }
  185. set_cursor(new_index, selection_update);
  186. }
  187. void ListView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  188. {
  189. if (!model())
  190. return;
  191. auto& model = *this->model();
  192. if (!cursor_index().is_valid()) {
  193. set_cursor(model.index(0, 0), SelectionUpdate::Set);
  194. return;
  195. }
  196. ModelIndex new_index;
  197. switch (movement) {
  198. case CursorMovement::Up:
  199. new_index = model.index(cursor_index().row() - 1, cursor_index().column());
  200. break;
  201. case CursorMovement::Down:
  202. new_index = model.index(cursor_index().row() + 1, cursor_index().column());
  203. break;
  204. case CursorMovement::Home:
  205. new_index = model.index(0, 0);
  206. break;
  207. case CursorMovement::End:
  208. new_index = model.index(model.row_count() - 1, 0);
  209. break;
  210. case CursorMovement::PageUp: {
  211. int items_per_page = visible_content_rect().height() / item_height();
  212. new_index = model.index(max(0, cursor_index().row() - items_per_page), cursor_index().column());
  213. break;
  214. }
  215. case CursorMovement::PageDown: {
  216. int items_per_page = visible_content_rect().height() / item_height();
  217. new_index = model.index(min(model.row_count() - 1, cursor_index().row() + items_per_page), cursor_index().column());
  218. break;
  219. }
  220. default:
  221. break;
  222. }
  223. if (new_index.is_valid())
  224. set_cursor(new_index, selection_update);
  225. }
  226. void ListView::scroll_into_view(ModelIndex const& index, bool scroll_horizontally, bool scroll_vertically)
  227. {
  228. if (!model())
  229. return;
  230. AbstractScrollableWidget::scroll_into_view(content_rect(index.row()), scroll_horizontally, scroll_vertically);
  231. }
  232. Optional<UISize> ListView::calculated_min_size() const
  233. {
  234. auto min_width = horizontal_scrollbar().effective_min_size().width().as_int() + vertical_scrollbar().width() + frame_thickness() * 2;
  235. auto min_height = vertical_scrollbar().effective_min_size().height().as_int() + horizontal_scrollbar().height() + frame_thickness() * 2;
  236. auto content_exceeds_minimums = content_width() > min_width && content_height() > min_height;
  237. auto scrollbars_are_visible = horizontal_scrollbar().is_visible() && vertical_scrollbar().is_visible();
  238. if (content_exceeds_minimums || scrollbars_are_visible)
  239. return AbstractScrollableWidget::calculated_min_size();
  240. return { { m_max_item_width + frame_thickness() * 2, content_height() + frame_thickness() * 2 } };
  241. }
  242. }