ListView.cpp 9.3 KB

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