TableView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Glenford Williams <gw_dev@outlook.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/StringBuilder.h>
  10. #include <LibGUI/Action.h>
  11. #include <LibGUI/HeaderView.h>
  12. #include <LibGUI/Menu.h>
  13. #include <LibGUI/Model.h>
  14. #include <LibGUI/ModelEditingDelegate.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGUI/TableView.h>
  17. #include <LibGUI/Window.h>
  18. #include <LibGfx/Palette.h>
  19. REGISTER_WIDGET(GUI, TableView)
  20. namespace GUI {
  21. TableView::TableView()
  22. {
  23. set_fill_with_background_color(true);
  24. set_background_role(ColorRole::Base);
  25. set_foreground_role(ColorRole::BaseText);
  26. }
  27. void TableView::paint_event(PaintEvent& event)
  28. {
  29. Color widget_background_color = palette().color(background_role());
  30. Frame::paint_event(event);
  31. Painter painter(*this);
  32. painter.add_clip_rect(frame_inner_rect());
  33. painter.add_clip_rect(event.rect());
  34. if (fill_with_background_color())
  35. painter.fill_rect(event.rect(), widget_background_color);
  36. painter.translate(frame_thickness(), frame_thickness());
  37. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  38. if (!model())
  39. return;
  40. auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
  41. int exposed_width = max(content_size().width(), width());
  42. int x_offset = row_header().is_visible() ? row_header().width() : 0;
  43. int y_offset = column_header().is_visible() ? column_header().height() : 0;
  44. bool dummy;
  45. int first_visible_row = index_at_event_position(frame_inner_rect().top_left().translated(x_offset, y_offset), dummy).row();
  46. int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right().translated(-1).translated(x_offset, y_offset), dummy).row();
  47. if (first_visible_row == -1)
  48. first_visible_row = 0;
  49. if (last_visible_row == -1)
  50. last_visible_row = model()->row_count() - 1;
  51. int painted_item_index = first_visible_row;
  52. for (int row_index = first_visible_row; row_index <= last_visible_row; ++row_index) {
  53. bool is_selected_row = selection().contains_row(row_index);
  54. int y = y_offset + painted_item_index * row_height();
  55. Color background_color;
  56. Color key_column_background_color;
  57. if (is_selected_row && highlight_selected_rows()) {
  58. background_color = selection_color;
  59. key_column_background_color = selection_color;
  60. } else {
  61. if (alternating_row_colors() && (painted_item_index % 2)) {
  62. background_color = widget_background_color.darkened(0.8f);
  63. key_column_background_color = widget_background_color.darkened(0.7f);
  64. } else {
  65. background_color = widget_background_color;
  66. key_column_background_color = widget_background_color.darkened(0.9f);
  67. }
  68. }
  69. auto row_rect = this->row_rect(painted_item_index);
  70. painter.fill_rect(row_rect, background_color);
  71. int x = x_offset;
  72. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  73. if (!column_header().is_section_visible(column_index))
  74. continue;
  75. int column_width = this->column_width(column_index);
  76. bool is_key_column = m_key_column == column_index;
  77. Gfx::IntRect cell_rect(horizontal_padding() + x, y, column_width, row_height());
  78. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  79. if (is_key_column && is_key_column_highlighted())
  80. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  81. auto cell_index = model()->index(row_index, column_index);
  82. auto* delegate = column_painting_delegate(column_index);
  83. if (delegate && delegate->should_paint(cell_index)) {
  84. delegate->paint(painter, cell_rect, palette(), cell_index);
  85. } else {
  86. auto data = cell_index.data();
  87. if (data.is_bitmap()) {
  88. auto cell_constrained_bitmap_rect = data.as_bitmap().rect();
  89. if (data.as_bitmap().rect().width() > column_width)
  90. cell_constrained_bitmap_rect.set_width(column_width);
  91. if (data.as_bitmap().rect().height() > row_height())
  92. cell_constrained_bitmap_rect.set_height(row_height());
  93. cell_rect.set_y(cell_rect.y() + (row_height() - cell_constrained_bitmap_rect.height()) / 2);
  94. cell_rect.set_x(cell_rect.x() + (column_width - cell_constrained_bitmap_rect.width()) / 2);
  95. painter.blit(cell_rect.location(), data.as_bitmap(), cell_constrained_bitmap_rect);
  96. } else if (data.is_icon()) {
  97. if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
  98. cell_rect.set_y(cell_rect.y() + (row_height() - bitmap->height()) / 2);
  99. if (is_selected_row) {
  100. auto tint = selection_color.with_alpha(100);
  101. painter.blit_filtered(cell_rect.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
  102. } else if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row()) {
  103. painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect());
  104. } else {
  105. auto opacity = cell_index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  106. painter.blit(cell_rect.location(), *bitmap, bitmap->rect(), opacity);
  107. }
  108. }
  109. } else {
  110. if (!is_selected_row) {
  111. auto cell_background_color = cell_index.data(ModelRole::BackgroundColor);
  112. if (cell_background_color.is_valid())
  113. painter.fill_rect(cell_rect_for_fill, cell_background_color.to_color(background_color));
  114. }
  115. auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
  116. draw_item_text(painter, cell_index, is_selected_row, cell_rect, data.to_deprecated_string(), font_for_index(cell_index), text_alignment, Gfx::TextElision::Right);
  117. }
  118. }
  119. if (m_grid_style == GridStyle::Horizontal || m_grid_style == GridStyle::Both)
  120. painter.draw_line(cell_rect_for_fill.bottom_left().moved_up(1), cell_rect_for_fill.bottom_right().translated(-1), palette().ruler());
  121. if (m_grid_style == GridStyle::Vertical || m_grid_style == GridStyle::Both)
  122. painter.draw_line(cell_rect_for_fill.top_right().moved_left(1), cell_rect_for_fill.bottom_right().translated(-1), palette().ruler());
  123. if (selection_behavior() == SelectionBehavior::SelectItems && cell_index == cursor_index())
  124. painter.draw_rect(cell_rect_for_fill, palette().text_cursor());
  125. x += column_width + horizontal_padding() * 2;
  126. }
  127. if (is_focused() && selection_behavior() == SelectionBehavior::SelectRows && row_index == cursor_index().row()) {
  128. painter.draw_rect(row_rect, widget_background_color);
  129. painter.draw_focus_rect(row_rect, palette().focus_outline());
  130. }
  131. if (has_pending_drop() && selection_behavior() == SelectionBehavior::SelectRows && row_index == drop_candidate_index().row()) {
  132. painter.draw_rect(row_rect, palette().selection(), true);
  133. }
  134. ++painted_item_index;
  135. };
  136. Gfx::IntRect unpainted_rect(0, column_header().height() + painted_item_index * row_height(), exposed_width, height());
  137. if (fill_with_background_color())
  138. painter.fill_rect(unpainted_rect, widget_background_color);
  139. }
  140. void TableView::second_paint_event(PaintEvent& event)
  141. {
  142. if (!m_rubber_banding)
  143. return;
  144. Painter painter(*this);
  145. painter.add_clip_rect(event.rect());
  146. painter.add_clip_rect(widget_inner_rect());
  147. // The rubber band rect always borders the widget inner to the left and right
  148. auto rubber_band_left = widget_inner_rect().left();
  149. auto rubber_band_right = widget_inner_rect().right();
  150. auto rubber_band_rect = Gfx::IntRect::from_two_points({ rubber_band_left, m_rubber_band_origin }, { rubber_band_right, m_rubber_band_current });
  151. painter.fill_rect(rubber_band_rect, palette().rubber_band_fill());
  152. painter.draw_rect(rubber_band_rect, palette().rubber_band_border());
  153. }
  154. void TableView::keydown_event(KeyEvent& event)
  155. {
  156. if (!model())
  157. return AbstractTableView::keydown_event(event);
  158. AbstractTableView::keydown_event(event);
  159. if (event.is_accepted())
  160. return;
  161. auto is_delete = event.key() == Key_Delete;
  162. auto is_backspace = event.key() == Key_Backspace;
  163. auto is_clear = is_delete || is_backspace;
  164. auto has_ctrl = event.modifiers() & KeyModifier::Mod_Ctrl;
  165. if (is_editable() && edit_triggers() & EditTrigger::AnyKeyPressed && (event.code_point() != 0 || is_clear) && !has_ctrl) {
  166. begin_editing(cursor_index());
  167. if (m_editing_delegate) {
  168. if (is_delete) {
  169. if (selection().size() > 1) {
  170. selection().for_each_index([&](GUI::ModelIndex& index) {
  171. begin_editing(index);
  172. m_editing_delegate->set_value(DeprecatedString {});
  173. });
  174. } else {
  175. m_editing_delegate->set_value(DeprecatedString {});
  176. }
  177. } else if (is_backspace) {
  178. m_editing_delegate->set_value(DeprecatedString::empty());
  179. } else {
  180. m_editing_delegate->set_value(event.text(), ModelEditingDelegate::SelectionBehavior::DoNotSelect);
  181. }
  182. }
  183. }
  184. }
  185. void TableView::mousedown_event(MouseEvent& event)
  186. {
  187. AbstractTableView::mousedown_event(event);
  188. if (!model())
  189. return;
  190. if (event.button() != MouseButton::Primary)
  191. return;
  192. if (m_might_drag)
  193. return;
  194. if (selection_mode() == SelectionMode::MultiSelection) {
  195. m_rubber_banding = true;
  196. m_rubber_band_origin = event.position().y();
  197. m_rubber_band_current = event.position().y();
  198. }
  199. }
  200. void TableView::mouseup_event(MouseEvent& event)
  201. {
  202. AbstractTableView::mouseup_event(event);
  203. if (m_rubber_banding && event.button() == MouseButton::Primary) {
  204. m_rubber_banding = false;
  205. update();
  206. }
  207. }
  208. void TableView::mousemove_event(MouseEvent& event)
  209. {
  210. if (m_rubber_banding) {
  211. // The rubber band rect cannot go outside the bounds of the rect enclosing all rows
  212. m_rubber_band_current = clamp(event.position().y(), widget_inner_rect().top() + column_header().height(), widget_inner_rect().bottom());
  213. int row_count = model()->row_count();
  214. clear_selection();
  215. set_suppress_update_on_selection_change(true);
  216. for (int row = 0; row < row_count; ++row) {
  217. auto index = model()->index(row);
  218. VERIFY(index.is_valid());
  219. int row_top = row * row_height() + column_header().height();
  220. int row_bottom = row * row_height() + row_height() + column_header().height();
  221. if ((m_rubber_band_origin > row_top && m_rubber_band_current < row_top) || (m_rubber_band_origin > row_bottom && m_rubber_band_current < row_bottom)) {
  222. add_selection(index);
  223. }
  224. }
  225. set_suppress_update_on_selection_change(false);
  226. update();
  227. }
  228. AbstractTableView::mousemove_event(event);
  229. }
  230. void TableView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  231. {
  232. if (!model())
  233. return;
  234. auto& model = *this->model();
  235. switch (movement) {
  236. case CursorMovement::Left:
  237. move_cursor_relative(0, -1, selection_update);
  238. break;
  239. case CursorMovement::Right:
  240. move_cursor_relative(0, 1, selection_update);
  241. break;
  242. case CursorMovement::Up:
  243. move_cursor_relative(-1, 0, selection_update);
  244. break;
  245. case CursorMovement::Down:
  246. move_cursor_relative(1, 0, selection_update);
  247. break;
  248. case CursorMovement::Home: {
  249. auto index = model.index(0, 0);
  250. set_cursor(index, selection_update);
  251. break;
  252. }
  253. case CursorMovement::End: {
  254. auto index = model.index(model.row_count() - 1, 0);
  255. set_cursor(index, selection_update);
  256. break;
  257. }
  258. case CursorMovement::PageUp: {
  259. int items_per_page = visible_content_rect().height() / row_height();
  260. auto old_index = selection().first();
  261. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  262. if (model.is_within_range(new_index))
  263. set_cursor(new_index, selection_update);
  264. break;
  265. }
  266. case CursorMovement::PageDown: {
  267. int items_per_page = visible_content_rect().height() / row_height();
  268. auto old_index = selection().first();
  269. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  270. if (model.is_within_range(new_index))
  271. set_cursor(new_index, selection_update);
  272. break;
  273. }
  274. }
  275. }
  276. void TableView::set_grid_style(GridStyle style)
  277. {
  278. if (m_grid_style == style)
  279. return;
  280. m_grid_style = style;
  281. update();
  282. }
  283. }