TableView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Glenford Williams <gw_dev@outlook.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/HeaderView.h>
  10. #include <LibGUI/Menu.h>
  11. #include <LibGUI/Model.h>
  12. #include <LibGUI/ModelEditingDelegate.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGUI/TableView.h>
  15. #include <LibGUI/Window.h>
  16. #include <LibGfx/Palette.h>
  17. REGISTER_WIDGET(GUI, TableView)
  18. namespace GUI {
  19. TableView::TableView()
  20. {
  21. set_fill_with_background_color(true);
  22. set_background_role(ColorRole::Base);
  23. set_foreground_role(ColorRole::BaseText);
  24. }
  25. TableView::~TableView()
  26. {
  27. }
  28. void TableView::paint_event(PaintEvent& event)
  29. {
  30. Color widget_background_color = palette().color(background_role());
  31. Frame::paint_event(event);
  32. Painter painter(*this);
  33. painter.add_clip_rect(frame_inner_rect());
  34. painter.add_clip_rect(event.rect());
  35. if (fill_with_background_color())
  36. painter.fill_rect(event.rect(), widget_background_color);
  37. painter.translate(frame_thickness(), frame_thickness());
  38. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  39. if (!model())
  40. return;
  41. auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
  42. int exposed_width = max(content_size().width(), width());
  43. int x_offset = row_header().is_visible() ? row_header().width() : 0;
  44. int y_offset = column_header().is_visible() ? column_header().height() : 0;
  45. bool dummy;
  46. int first_visible_row = index_at_event_position(frame_inner_rect().top_left().translated(x_offset, y_offset), dummy).row();
  47. int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right().translated(x_offset, y_offset), dummy).row();
  48. if (first_visible_row == -1)
  49. first_visible_row = 0;
  50. if (last_visible_row == -1)
  51. last_visible_row = model()->row_count() - 1;
  52. int painted_item_index = first_visible_row;
  53. for (int row_index = first_visible_row; row_index <= last_visible_row; ++row_index) {
  54. bool is_selected_row = selection().contains_row(row_index);
  55. int y = y_offset + painted_item_index * row_height();
  56. Color background_color;
  57. Color key_column_background_color;
  58. if (is_selected_row && highlight_selected_rows()) {
  59. background_color = selection_color;
  60. key_column_background_color = selection_color;
  61. } else {
  62. if (alternating_row_colors() && (painted_item_index % 2)) {
  63. background_color = widget_background_color.darkened(0.8f);
  64. key_column_background_color = widget_background_color.darkened(0.7f);
  65. } else {
  66. background_color = widget_background_color;
  67. key_column_background_color = widget_background_color.darkened(0.9f);
  68. }
  69. }
  70. auto row_rect = this->row_rect(painted_item_index);
  71. painter.fill_rect(row_rect, background_color);
  72. int x = x_offset;
  73. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  74. if (!column_header().is_section_visible(column_index))
  75. continue;
  76. int column_width = this->column_width(column_index);
  77. bool is_key_column = m_key_column == column_index;
  78. Gfx::IntRect cell_rect(horizontal_padding() + x, y, column_width, row_height());
  79. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  80. if (is_key_column && is_key_column_highlighted())
  81. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  82. auto cell_index = model()->index(row_index, column_index);
  83. auto* delegate = column_painting_delegate(column_index);
  84. if (delegate && delegate->should_paint(cell_index)) {
  85. delegate->paint(painter, cell_rect, palette(), cell_index);
  86. } else {
  87. auto data = cell_index.data();
  88. if (data.is_bitmap()) {
  89. auto cell_constrained_bitmap_rect = data.as_bitmap().rect();
  90. if (data.as_bitmap().rect().width() > column_width)
  91. cell_constrained_bitmap_rect.set_width(column_width);
  92. if (data.as_bitmap().rect().height() > row_height())
  93. cell_constrained_bitmap_rect.set_height(row_height());
  94. cell_rect.set_y(cell_rect.y() + (row_height() - cell_constrained_bitmap_rect.height()) / 2);
  95. cell_rect.set_x(cell_rect.x() + (column_width - cell_constrained_bitmap_rect.width()) / 2);
  96. painter.blit(cell_rect.location(), data.as_bitmap(), cell_constrained_bitmap_rect);
  97. } else if (data.is_icon()) {
  98. if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
  99. cell_rect.set_y(cell_rect.y() + (row_height() - bitmap->height()) / 2);
  100. if (is_selected_row) {
  101. auto tint = selection_color.with_alpha(100);
  102. painter.blit_filtered(cell_rect.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
  103. } else if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row()) {
  104. painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect());
  105. } else {
  106. auto opacity = cell_index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  107. painter.blit(cell_rect.location(), *bitmap, bitmap->rect(), opacity);
  108. }
  109. }
  110. } else {
  111. if (!is_selected_row) {
  112. auto cell_background_color = cell_index.data(ModelRole::BackgroundColor);
  113. if (cell_background_color.is_valid())
  114. painter.fill_rect(cell_rect_for_fill, cell_background_color.to_color(background_color));
  115. }
  116. auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
  117. draw_item_text(painter, cell_index, is_selected_row, cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, Gfx::TextElision::Right);
  118. }
  119. }
  120. if (m_grid_style == GridStyle::Horizontal || m_grid_style == GridStyle::Both)
  121. painter.draw_line(cell_rect_for_fill.bottom_left(), cell_rect_for_fill.bottom_right(), palette().ruler());
  122. if (m_grid_style == GridStyle::Vertical || m_grid_style == GridStyle::Both)
  123. painter.draw_line(cell_rect_for_fill.top_right(), cell_rect_for_fill.bottom_right(), palette().ruler());
  124. if (selection_behavior() == SelectionBehavior::SelectItems && cell_index == cursor_index())
  125. painter.draw_rect(cell_rect_for_fill, palette().text_cursor());
  126. x += column_width + horizontal_padding() * 2;
  127. }
  128. if (is_focused() && selection_behavior() == SelectionBehavior::SelectRows && row_index == cursor_index().row()) {
  129. painter.draw_rect(row_rect, widget_background_color);
  130. painter.draw_focus_rect(row_rect, palette().focus_outline());
  131. }
  132. if (has_pending_drop() && selection_behavior() == SelectionBehavior::SelectRows && row_index == drop_candidate_index().row()) {
  133. painter.draw_rect(row_rect, palette().selection(), true);
  134. }
  135. ++painted_item_index;
  136. };
  137. Gfx::IntRect unpainted_rect(0, column_header().height() + painted_item_index * row_height(), exposed_width, height());
  138. if (fill_with_background_color())
  139. painter.fill_rect(unpainted_rect, widget_background_color);
  140. }
  141. void TableView::keydown_event(KeyEvent& event)
  142. {
  143. if (!model())
  144. return AbstractTableView::keydown_event(event);
  145. AbstractTableView::keydown_event(event);
  146. if (event.is_accepted())
  147. return;
  148. auto is_delete = event.key() == Key_Delete;
  149. auto is_backspace = event.key() == Key_Backspace;
  150. auto is_clear = is_delete || is_backspace;
  151. if (is_editable() && edit_triggers() & EditTrigger::AnyKeyPressed && (event.code_point() != 0 || is_clear)) {
  152. begin_editing(cursor_index());
  153. if (m_editing_delegate) {
  154. if (is_delete) {
  155. if (selection().size() > 1) {
  156. selection().for_each_index([&](GUI::ModelIndex& index) {
  157. begin_editing(index);
  158. m_editing_delegate->set_value(String {});
  159. });
  160. } else {
  161. m_editing_delegate->set_value(String {});
  162. }
  163. } else if (is_backspace) {
  164. m_editing_delegate->set_value(String::empty());
  165. } else {
  166. m_editing_delegate->set_value(event.text(), ModelEditingDelegate::SelectionBehavior::DoNotSelect);
  167. }
  168. }
  169. }
  170. }
  171. void TableView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  172. {
  173. if (!model())
  174. return;
  175. auto& model = *this->model();
  176. switch (movement) {
  177. case CursorMovement::Left:
  178. move_cursor_relative(0, -1, selection_update);
  179. break;
  180. case CursorMovement::Right:
  181. move_cursor_relative(0, 1, selection_update);
  182. break;
  183. case CursorMovement::Up:
  184. move_cursor_relative(-1, 0, selection_update);
  185. break;
  186. case CursorMovement::Down:
  187. move_cursor_relative(1, 0, selection_update);
  188. break;
  189. case CursorMovement::Home: {
  190. auto index = model.index(0, 0);
  191. set_cursor(index, selection_update);
  192. break;
  193. }
  194. case CursorMovement::End: {
  195. auto index = model.index(model.row_count() - 1, 0);
  196. set_cursor(index, selection_update);
  197. break;
  198. }
  199. case CursorMovement::PageUp: {
  200. int items_per_page = visible_content_rect().height() / row_height();
  201. auto old_index = selection().first();
  202. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  203. if (model.is_within_range(new_index))
  204. set_cursor(new_index, selection_update);
  205. break;
  206. }
  207. case CursorMovement::PageDown: {
  208. int items_per_page = visible_content_rect().height() / row_height();
  209. auto old_index = selection().first();
  210. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  211. if (model.is_within_range(new_index))
  212. set_cursor(new_index, selection_update);
  213. break;
  214. }
  215. }
  216. }
  217. void TableView::set_grid_style(GridStyle style)
  218. {
  219. if (m_grid_style == style)
  220. return;
  221. m_grid_style = style;
  222. update();
  223. }
  224. }