ListView.cpp 8.8 KB

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