ListView.cpp 7.9 KB

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