AbstractTableView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/AbstractTableView.h>
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/Button.h>
  10. #include <LibGUI/HeaderView.h>
  11. #include <LibGUI/Menu.h>
  12. #include <LibGUI/Model.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGUI/Window.h>
  15. #include <LibGfx/Palette.h>
  16. namespace GUI {
  17. AbstractTableView::AbstractTableView()
  18. {
  19. set_selection_behavior(SelectionBehavior::SelectRows);
  20. m_corner_button = add<Button>();
  21. m_corner_button->move_to_back();
  22. m_corner_button->set_background_role(Gfx::ColorRole::ThreedShadow1);
  23. m_corner_button->set_fill_with_background_color(true);
  24. m_column_header = add<HeaderView>(*this, Gfx::Orientation::Horizontal);
  25. m_column_header->move_to_back();
  26. m_column_header->on_resize_doubleclick = [this](auto column) {
  27. auto_resize_column(column);
  28. };
  29. m_row_header = add<HeaderView>(*this, Gfx::Orientation::Vertical);
  30. m_row_header->move_to_back();
  31. m_row_header->set_visible(false);
  32. set_should_hide_unnecessary_scrollbars(true);
  33. }
  34. void AbstractTableView::select_all()
  35. {
  36. selection().clear();
  37. for (int item_index = 0; item_index < item_count(); ++item_index) {
  38. auto index = model()->index(item_index);
  39. selection().add(index);
  40. }
  41. }
  42. void AbstractTableView::auto_resize_column(int column)
  43. {
  44. if (!model())
  45. return;
  46. if (!column_header().is_section_visible(column))
  47. return;
  48. auto& model = *this->model();
  49. int row_count = model.row_count();
  50. int header_width = m_column_header->font().width(model.column_name(column));
  51. if (column == m_key_column && model.is_column_sortable(column))
  52. header_width += font().width(" \xE2\xAC\x86");
  53. int column_width = header_width;
  54. bool is_empty = true;
  55. for (int row = 0; row < row_count; ++row) {
  56. auto cell_data = model.index(row, column).data();
  57. int cell_width = 0;
  58. if (cell_data.is_icon()) {
  59. cell_width = cell_data.as_icon().bitmap_for_size(16)->width();
  60. } else if (cell_data.is_bitmap()) {
  61. cell_width = cell_data.as_bitmap().width();
  62. } else if (cell_data.is_valid()) {
  63. cell_width = font().width(cell_data.to_string());
  64. }
  65. if (is_empty && cell_width > 0)
  66. is_empty = false;
  67. column_width = max(column_width, cell_width);
  68. }
  69. auto default_column_size = column_header().default_section_size(column);
  70. if (is_empty && column_header().is_default_section_size_initialized(column))
  71. column_header().set_section_size(column, default_column_size);
  72. else
  73. column_header().set_section_size(column, column_width);
  74. }
  75. void AbstractTableView::update_column_sizes()
  76. {
  77. if (!model())
  78. return;
  79. auto& model = *this->model();
  80. int column_count = model.column_count();
  81. int row_count = model.row_count();
  82. for (int column = 0; column < column_count; ++column) {
  83. if (!column_header().is_section_visible(column))
  84. continue;
  85. int header_width = m_column_header->font().width(model.column_name(column));
  86. if (column == m_key_column && model.is_column_sortable(column))
  87. header_width += font().width(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
  88. int column_width = header_width;
  89. for (int row = 0; row < row_count; ++row) {
  90. auto cell_data = model.index(row, column).data();
  91. int cell_width = 0;
  92. if (cell_data.is_icon()) {
  93. if (auto bitmap = cell_data.as_icon().bitmap_for_size(16))
  94. cell_width = bitmap->width();
  95. } else if (cell_data.is_bitmap()) {
  96. cell_width = cell_data.as_bitmap().width();
  97. } else if (cell_data.is_valid()) {
  98. cell_width = font().width(cell_data.to_string());
  99. }
  100. column_width = max(column_width, cell_width);
  101. }
  102. column_header().set_section_size(column, max(m_column_header->section_size(column), column_width));
  103. }
  104. }
  105. void AbstractTableView::update_row_sizes()
  106. {
  107. if (!model())
  108. return;
  109. auto& model = *this->model();
  110. int row_count = model.row_count();
  111. for (int row = 0; row < row_count; ++row) {
  112. if (!row_header().is_section_visible(row))
  113. continue;
  114. row_header().set_section_size(row, row_height());
  115. }
  116. }
  117. void AbstractTableView::update_content_size()
  118. {
  119. if (!model())
  120. return set_content_size({});
  121. int content_width = 0;
  122. int column_count = model()->column_count();
  123. for (int i = 0; i < column_count; ++i) {
  124. if (column_header().is_section_visible(i))
  125. content_width += column_width(i) + horizontal_padding() * 2;
  126. }
  127. int content_height = item_count() * row_height();
  128. set_content_size({ content_width, content_height });
  129. int row_width = row_header().is_visible() ? row_header().width() : 0;
  130. int column_height = column_header().is_visible() ? column_header().height() : 0;
  131. set_size_occupied_by_fixed_elements({ row_width, column_height });
  132. layout_headers();
  133. }
  134. TableCellPaintingDelegate* AbstractTableView::column_painting_delegate(int column) const
  135. {
  136. // FIXME: This should return a const pointer I think..
  137. return const_cast<TableCellPaintingDelegate*>(m_column_painting_delegate.get(column).value_or(nullptr));
  138. }
  139. void AbstractTableView::set_column_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate> delegate)
  140. {
  141. if (!delegate)
  142. m_column_painting_delegate.remove(column);
  143. else
  144. m_column_painting_delegate.set(column, move(delegate));
  145. }
  146. int AbstractTableView::column_width(int column_index) const
  147. {
  148. if (!model())
  149. return 0;
  150. return m_column_header->section_size(column_index);
  151. }
  152. void AbstractTableView::set_column_width(int column, int width)
  153. {
  154. column_header().set_section_size(column, width);
  155. }
  156. int AbstractTableView::minimum_column_width(int)
  157. {
  158. return 2;
  159. }
  160. int AbstractTableView::minimum_row_height(int)
  161. {
  162. return 2;
  163. }
  164. Gfx::TextAlignment AbstractTableView::column_header_alignment(int column_index) const
  165. {
  166. if (!model())
  167. return Gfx::TextAlignment::CenterLeft;
  168. return m_column_header->section_alignment(column_index);
  169. }
  170. void AbstractTableView::set_column_header_alignment(int column, Gfx::TextAlignment alignment)
  171. {
  172. column_header().set_section_alignment(column, alignment);
  173. }
  174. void AbstractTableView::mousedown_event(MouseEvent& event)
  175. {
  176. m_tab_moves = 0;
  177. if (!model())
  178. return AbstractView::mousedown_event(event);
  179. if (event.button() != MouseButton::Primary)
  180. return AbstractView::mousedown_event(event);
  181. bool is_toggle;
  182. auto index = index_at_event_position(event.position(), is_toggle);
  183. if (index.is_valid() && is_toggle && model()->row_count(index)) {
  184. toggle_index(index);
  185. return;
  186. }
  187. AbstractView::mousedown_event(event);
  188. }
  189. ModelIndex AbstractTableView::index_at_event_position(const Gfx::IntPoint& position, bool& is_toggle) const
  190. {
  191. is_toggle = false;
  192. if (!model())
  193. return {};
  194. auto adjusted_position = this->adjusted_position(position);
  195. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  196. if (!row_rect(row).contains(adjusted_position))
  197. continue;
  198. for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) {
  199. if (!content_rect(row, column).contains(adjusted_position))
  200. continue;
  201. return model()->index(row, column);
  202. }
  203. return model()->index(row, 0);
  204. }
  205. return {};
  206. }
  207. ModelIndex AbstractTableView::index_at_event_position(const Gfx::IntPoint& position) const
  208. {
  209. bool is_toggle;
  210. auto index = index_at_event_position(position, is_toggle);
  211. return is_toggle ? ModelIndex() : index;
  212. }
  213. int AbstractTableView::item_count() const
  214. {
  215. if (!model())
  216. return 0;
  217. return model()->row_count();
  218. }
  219. void AbstractTableView::move_cursor_relative(int vertical_steps, int horizontal_steps, SelectionUpdate selection_update)
  220. {
  221. if (!model())
  222. return;
  223. auto& model = *this->model();
  224. ModelIndex new_index;
  225. if (cursor_index().is_valid()) {
  226. new_index = model.index(cursor_index().row() + vertical_steps, cursor_index().column() + horizontal_steps);
  227. } else {
  228. new_index = model.index(0, 0);
  229. }
  230. if (new_index.is_valid()) {
  231. set_cursor(new_index, selection_update);
  232. }
  233. }
  234. void AbstractTableView::scroll_into_view(const ModelIndex& index, bool scroll_horizontally, bool scroll_vertically)
  235. {
  236. Gfx::IntRect rect;
  237. switch (selection_behavior()) {
  238. case SelectionBehavior::SelectItems:
  239. rect = content_rect(index);
  240. if (row_header().is_visible())
  241. rect.set_left(rect.left() - row_header().width());
  242. break;
  243. case SelectionBehavior::SelectRows:
  244. rect = row_rect(index.row());
  245. break;
  246. }
  247. if (column_header().is_visible())
  248. rect.set_top(rect.top() - column_header().height());
  249. AbstractScrollableWidget::scroll_into_view(rect, scroll_horizontally, scroll_vertically);
  250. }
  251. void AbstractTableView::context_menu_event(ContextMenuEvent& event)
  252. {
  253. if (!model())
  254. return;
  255. bool is_toggle;
  256. auto index = index_at_event_position(event.position(), is_toggle);
  257. if (index.is_valid()) {
  258. if (!selection().contains(index))
  259. selection().set(index);
  260. } else {
  261. selection().clear();
  262. }
  263. if (on_context_menu_request)
  264. on_context_menu_request(index, event);
  265. }
  266. Gfx::IntRect AbstractTableView::paint_invalidation_rect(ModelIndex const& index) const
  267. {
  268. if (!index.is_valid())
  269. return {};
  270. return row_rect(index.row());
  271. }
  272. Gfx::IntRect AbstractTableView::content_rect(int row, int column) const
  273. {
  274. auto row_rect = this->row_rect(row);
  275. int x = 0;
  276. for (int i = 0; i < column; ++i)
  277. x += column_width(i) + horizontal_padding() * 2;
  278. return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, row_height() };
  279. }
  280. Gfx::IntRect AbstractTableView::content_rect(const ModelIndex& index) const
  281. {
  282. return content_rect(index.row(), index.column());
  283. }
  284. Gfx::IntRect AbstractTableView::row_rect(int item_index) const
  285. {
  286. return { row_header().is_visible() ? row_header().width() : 0,
  287. (column_header().is_visible() ? column_header().height() : 0) + (item_index * row_height()),
  288. max(content_size().width(), width()),
  289. row_height() };
  290. }
  291. Gfx::IntPoint AbstractTableView::adjusted_position(const Gfx::IntPoint& position) const
  292. {
  293. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  294. }
  295. void AbstractTableView::model_did_update(unsigned flags)
  296. {
  297. AbstractView::model_did_update(flags);
  298. update_row_sizes();
  299. update_column_sizes();
  300. update_content_size();
  301. update();
  302. }
  303. void AbstractTableView::resize_event(ResizeEvent& event)
  304. {
  305. AbstractView::resize_event(event);
  306. layout_headers();
  307. }
  308. void AbstractTableView::header_did_change_section_size(Badge<HeaderView>, Gfx::Orientation, int, int)
  309. {
  310. update_content_size();
  311. update();
  312. }
  313. void AbstractTableView::header_did_change_section_visibility(Badge<HeaderView>, Gfx::Orientation, int, bool)
  314. {
  315. update_content_size();
  316. update();
  317. }
  318. void AbstractTableView::set_default_column_width(int column, int width)
  319. {
  320. column_header().set_default_section_size(column, width);
  321. }
  322. void AbstractTableView::set_column_visible(int column, bool visible)
  323. {
  324. column_header().set_section_visible(column, visible);
  325. }
  326. void AbstractTableView::set_column_headers_visible(bool visible)
  327. {
  328. column_header().set_visible(visible);
  329. }
  330. void AbstractTableView::did_scroll()
  331. {
  332. AbstractView::did_scroll();
  333. layout_headers();
  334. }
  335. void AbstractTableView::layout_headers()
  336. {
  337. if (column_header().is_visible()) {
  338. int row_header_width = row_header().is_visible() ? row_header().width() : 0;
  339. int vertical_scrollbar_width = vertical_scrollbar().is_visible() ? vertical_scrollbar().width() : 0;
  340. int x = frame_thickness() + row_header_width - horizontal_scrollbar().value();
  341. int y = frame_thickness();
  342. int width = max(content_width(), rect().width() - frame_thickness() * 2 - row_header_width - vertical_scrollbar_width);
  343. column_header().set_relative_rect(x, y, width, column_header().min_size().height());
  344. }
  345. if (row_header().is_visible()) {
  346. int column_header_height = column_header().is_visible() ? column_header().height() : 0;
  347. int horizontal_scrollbar_height = horizontal_scrollbar().is_visible() ? horizontal_scrollbar().height() : 0;
  348. int x = frame_thickness();
  349. int y = frame_thickness() + column_header_height - vertical_scrollbar().value();
  350. int height = max(content_height(), rect().height() - frame_thickness() * 2 - column_header_height - horizontal_scrollbar_height);
  351. row_header().set_relative_rect(x, y, row_header().min_size().width(), height);
  352. }
  353. if (row_header().is_visible() && column_header().is_visible()) {
  354. m_corner_button->set_relative_rect(frame_thickness(), frame_thickness(), row_header().width(), column_header().height());
  355. m_corner_button->set_visible(true);
  356. } else {
  357. m_corner_button->set_visible(false);
  358. }
  359. }
  360. void AbstractTableView::keydown_event(KeyEvent& event)
  361. {
  362. if (is_tab_key_navigation_enabled()) {
  363. if (!event.modifiers() && event.key() == KeyCode::Key_Tab) {
  364. move_cursor(CursorMovement::Right, SelectionUpdate::Set);
  365. event.accept();
  366. ++m_tab_moves;
  367. return;
  368. } else if (is_navigation(event)) {
  369. if (event.key() == KeyCode::Key_Return) {
  370. move_cursor_relative(0, -m_tab_moves, SelectionUpdate::Set);
  371. }
  372. m_tab_moves = 0;
  373. }
  374. if (event.modifiers() == KeyModifier::Mod_Shift && event.key() == KeyCode::Key_Tab) {
  375. move_cursor(CursorMovement::Left, SelectionUpdate::Set);
  376. event.accept();
  377. return;
  378. }
  379. }
  380. AbstractView::keydown_event(event);
  381. }
  382. bool AbstractTableView::is_navigation(GUI::KeyEvent& event)
  383. {
  384. switch (event.key()) {
  385. case KeyCode::Key_Tab:
  386. case KeyCode::Key_Left:
  387. case KeyCode::Key_Right:
  388. case KeyCode::Key_Up:
  389. case KeyCode::Key_Down:
  390. case KeyCode::Key_Return:
  391. case KeyCode::Key_Home:
  392. case KeyCode::Key_End:
  393. case KeyCode::Key_PageUp:
  394. case KeyCode::Key_PageDown:
  395. return true;
  396. default:
  397. return false;
  398. }
  399. }
  400. Gfx::IntPoint AbstractTableView::automatic_scroll_delta_from_position(const Gfx::IntPoint& pos) const
  401. {
  402. if (pos.y() > column_header().height() + autoscroll_threshold())
  403. return AbstractScrollableWidget::automatic_scroll_delta_from_position(pos);
  404. Gfx::IntPoint position_excluding_header = { pos.x(), pos.y() - column_header().height() };
  405. return AbstractScrollableWidget::automatic_scroll_delta_from_position(position_excluding_header);
  406. }
  407. }