ListView.cpp 10 KB

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