TreeView.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 <AK/Debug.h>
  8. #include <LibCore/Object.h>
  9. #include <LibGUI/HeaderView.h>
  10. #include <LibGUI/Model.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/TreeView.h>
  13. #include <LibGfx/Bitmap.h>
  14. #include <LibGfx/Palette.h>
  15. REGISTER_WIDGET(GUI, TreeView)
  16. namespace GUI {
  17. struct TreeView::MetadataForIndex {
  18. bool open { false };
  19. };
  20. TreeView::MetadataForIndex& TreeView::ensure_metadata_for_index(ModelIndex const& index) const
  21. {
  22. VERIFY(index.is_valid());
  23. auto it = m_view_metadata.find(index);
  24. if (it != m_view_metadata.end())
  25. return *it->value;
  26. auto new_metadata = make<MetadataForIndex>();
  27. auto& new_metadata_ref = *new_metadata;
  28. m_view_metadata.set(index, move(new_metadata));
  29. return new_metadata_ref;
  30. }
  31. TreeView::TreeView()
  32. {
  33. REGISTER_BOOL_PROPERTY("should_fill_selected_rows", should_fill_selected_rows, set_should_fill_selected_rows);
  34. set_selection_behavior(SelectionBehavior::SelectItems);
  35. set_fill_with_background_color(true);
  36. set_background_role(ColorRole::Base);
  37. set_foreground_role(ColorRole::BaseText);
  38. set_column_headers_visible(false);
  39. m_expand_bitmap = Gfx::Bitmap::load_from_file("/res/icons/serenity/treeview-expand.png"sv).release_value_but_fixme_should_propagate_errors();
  40. m_collapse_bitmap = Gfx::Bitmap::load_from_file("/res/icons/serenity/treeview-collapse.png"sv).release_value_but_fixme_should_propagate_errors();
  41. }
  42. ModelIndex TreeView::index_at_event_position(Gfx::IntPoint a_position, bool& is_toggle) const
  43. {
  44. auto position = a_position.translated(0, -column_header().height()).translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  45. is_toggle = false;
  46. if (!model())
  47. return {};
  48. ModelIndex result;
  49. traverse_in_paint_order([&](ModelIndex const& index, Gfx::IntRect const& rect, Gfx::IntRect const& toggle_rect, int) {
  50. if (toggle_rect.contains(position)) {
  51. result = index;
  52. is_toggle = true;
  53. return IterationDecision::Break;
  54. }
  55. if (rect.contains_vertically(position.y())) {
  56. result = index;
  57. return IterationDecision::Break;
  58. }
  59. return IterationDecision::Continue;
  60. });
  61. return result;
  62. }
  63. void TreeView::doubleclick_event(MouseEvent& event)
  64. {
  65. if (!model())
  66. return;
  67. auto& model = *this->model();
  68. bool is_toggle;
  69. auto index = index_at_event_position(event.position(), is_toggle);
  70. if (!index.is_valid())
  71. return;
  72. if (event.button() == MouseButton::Primary) {
  73. set_cursor(index, SelectionUpdate::Set);
  74. if (model.row_count(index))
  75. toggle_index(index);
  76. else
  77. activate(index);
  78. }
  79. }
  80. void TreeView::set_open_state_of_all_in_subtree(ModelIndex const& root, bool open)
  81. {
  82. if (root.is_valid()) {
  83. ensure_metadata_for_index(root).open = open;
  84. if (model()->row_count(root)) {
  85. if (on_toggle)
  86. on_toggle(root, open);
  87. }
  88. }
  89. int row_count = model()->row_count(root);
  90. int column = model()->tree_column();
  91. for (int row = 0; row < row_count; ++row) {
  92. auto index = model()->index(row, column, root);
  93. set_open_state_of_all_in_subtree(index, open);
  94. }
  95. }
  96. void TreeView::expand_all_parents_of(ModelIndex const& index)
  97. {
  98. if (!model())
  99. return;
  100. auto current = index;
  101. while (current.is_valid()) {
  102. ensure_metadata_for_index(current).open = true;
  103. if (on_toggle)
  104. on_toggle(current, true);
  105. current = current.parent();
  106. }
  107. update_column_sizes();
  108. update_content_size();
  109. update();
  110. }
  111. void TreeView::expand_tree(ModelIndex const& root)
  112. {
  113. if (!model())
  114. return;
  115. set_open_state_of_all_in_subtree(root, true);
  116. update_column_sizes();
  117. update_content_size();
  118. update();
  119. }
  120. void TreeView::collapse_tree(ModelIndex const& root)
  121. {
  122. if (!model())
  123. return;
  124. set_open_state_of_all_in_subtree(root, false);
  125. update_column_sizes();
  126. update_content_size();
  127. update();
  128. }
  129. void TreeView::toggle_index(ModelIndex const& index)
  130. {
  131. VERIFY(model()->row_count(index));
  132. auto& metadata = ensure_metadata_for_index(index);
  133. metadata.open = !metadata.open;
  134. if (!metadata.open && index.is_parent_of(cursor_index()))
  135. set_cursor(index, SelectionUpdate::Set);
  136. if (on_toggle)
  137. on_toggle(index, metadata.open);
  138. update_column_sizes();
  139. update_content_size();
  140. update();
  141. }
  142. bool TreeView::is_toggled(ModelIndex const& index)
  143. {
  144. if (model()->row_count(index) == 0) {
  145. if (model()->parent_index(index).is_valid())
  146. return is_toggled(model()->parent_index(index));
  147. return false;
  148. }
  149. auto& metadata = ensure_metadata_for_index(index);
  150. return metadata.open;
  151. }
  152. template<typename Callback>
  153. void TreeView::traverse_in_paint_order(Callback callback) const
  154. {
  155. VERIFY(model());
  156. auto& model = *this->model();
  157. auto tree_column = model.tree_column();
  158. int indent_level = 1;
  159. int y_offset = 0;
  160. int tree_column_x_offset = this->tree_column_x_offset();
  161. Function<IterationDecision(ModelIndex const&)> traverse_index = [&](ModelIndex const& index) {
  162. int row_count_at_index = model.row_count(index);
  163. if (index.is_valid()) {
  164. auto& metadata = ensure_metadata_for_index(index);
  165. int x_offset = tree_column_x_offset + horizontal_padding() + indent_level * indent_width_in_pixels();
  166. auto node_text = index.data().to_deprecated_string();
  167. Gfx::IntRect rect = {
  168. x_offset, y_offset,
  169. static_cast<int>(ceilf(icon_size() + icon_spacing() + text_padding() + font_for_index(index)->width(node_text) + text_padding())), row_height()
  170. };
  171. Gfx::IntRect toggle_rect;
  172. if (row_count_at_index > 0) {
  173. int toggle_x = tree_column_x_offset + horizontal_padding() + (indent_width_in_pixels() * indent_level) - (icon_size() / 2) - 4;
  174. toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() };
  175. toggle_rect.center_vertically_within(rect);
  176. }
  177. if (callback(index, rect, toggle_rect, indent_level) == IterationDecision::Break)
  178. return IterationDecision::Break;
  179. y_offset += row_height();
  180. // NOTE: Skip traversing children if this index is closed!
  181. if (!metadata.open)
  182. return IterationDecision::Continue;
  183. }
  184. if (indent_level > 0 && !index.is_valid())
  185. return IterationDecision::Continue;
  186. ++indent_level;
  187. int row_count = model.row_count(index);
  188. for (int i = 0; i < row_count; ++i) {
  189. if (traverse_index(model.index(i, tree_column, index)) == IterationDecision::Break)
  190. return IterationDecision::Break;
  191. }
  192. --indent_level;
  193. return IterationDecision::Continue;
  194. };
  195. int root_count = model.row_count();
  196. for (int root_index = 0; root_index < root_count; ++root_index) {
  197. if (traverse_index(model.index(root_index, tree_column, ModelIndex())) == IterationDecision::Break)
  198. break;
  199. }
  200. }
  201. void TreeView::paint_event(PaintEvent& event)
  202. {
  203. Frame::paint_event(event);
  204. Painter painter(*this);
  205. painter.add_clip_rect(frame_inner_rect());
  206. painter.add_clip_rect(event.rect());
  207. if (fill_with_background_color())
  208. painter.fill_rect(event.rect(), palette().color(background_role()));
  209. if (!model())
  210. return;
  211. auto& model = *this->model();
  212. painter.translate(frame_inner_rect().location());
  213. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  214. auto visible_content_rect = this->visible_content_rect();
  215. int tree_column = model.tree_column();
  216. int column_count = model.column_count();
  217. int tree_column_x_offset = this->tree_column_x_offset();
  218. int y_offset = column_header().height();
  219. int painted_row_index = 0;
  220. traverse_in_paint_order([&](ModelIndex const& index, Gfx::IntRect const& a_rect, Gfx::IntRect const& a_toggle_rect, int indent_level) {
  221. if (!a_rect.intersects_vertically(visible_content_rect))
  222. return IterationDecision::Continue;
  223. auto rect = a_rect.translated(0, y_offset);
  224. auto toggle_rect = a_toggle_rect.translated(0, y_offset);
  225. if constexpr (ITEM_RECTS_DEBUG)
  226. painter.fill_rect(rect, Color::WarmGray);
  227. bool is_selected_row = selection().contains(index);
  228. Color text_color = palette().color(foreground_role());
  229. if (is_selected_row && should_fill_selected_rows())
  230. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  231. Color background_color;
  232. if (is_selected_row) {
  233. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  234. } else {
  235. if (alternating_row_colors() && (painted_row_index % 2)) {
  236. background_color = Color(220, 220, 220);
  237. } else {
  238. background_color = palette().color(background_role());
  239. }
  240. }
  241. int row_width = 0;
  242. for (int column_index = 0; column_index < column_count; ++column_index) {
  243. if (!column_header().is_section_visible(column_index))
  244. continue;
  245. row_width += this->column_width(column_index) + horizontal_padding() * 2;
  246. }
  247. if (frame_inner_rect().width() > row_width) {
  248. row_width = frame_inner_rect().width();
  249. }
  250. Gfx::IntRect row_rect { 0, rect.y(), row_width, rect.height() };
  251. if (!is_selected_row || should_fill_selected_rows())
  252. painter.fill_rect(row_rect, background_color);
  253. int x_offset = 0;
  254. for (int column_index = 0; column_index < column_count; ++column_index) {
  255. if (!column_header().is_section_visible(column_index))
  256. continue;
  257. int column_width = this->column_width(column_index);
  258. painter.draw_rect(toggle_rect, text_color);
  259. if (column_index != tree_column) {
  260. Gfx::IntRect cell_rect(horizontal_padding() + x_offset, rect.y(), column_width, row_height());
  261. auto cell_index = model.index(index.row(), column_index, index.parent());
  262. auto* delegate = column_painting_delegate(column_index);
  263. if (delegate && delegate->should_paint(cell_index)) {
  264. delegate->paint(painter, cell_rect, palette(), cell_index);
  265. } else {
  266. auto data = cell_index.data();
  267. if (data.is_bitmap()) {
  268. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  269. } else if (data.is_icon()) {
  270. if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
  271. auto opacity = cell_index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  272. painter.blit(cell_rect.location(), *bitmap, bitmap->rect(), opacity);
  273. }
  274. } else {
  275. auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
  276. 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);
  277. }
  278. }
  279. } else {
  280. // It's the tree column!
  281. int indent_width = indent_width_in_pixels() * indent_level;
  282. Gfx::IntRect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
  283. icon_rect.center_vertically_within(rect);
  284. Gfx::IntRect background_rect = {
  285. icon_rect.right() + 1 + icon_spacing(), rect.y(),
  286. min(rect.width(), column_width - indent_width) - icon_size() - icon_spacing(), rect.height()
  287. };
  288. Gfx::IntRect text_rect = background_rect.shrunken(text_padding() * 2, 0);
  289. painter.fill_rect(background_rect, background_color);
  290. auto icon = index.data(ModelRole::Icon);
  291. if (icon.is_icon()) {
  292. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
  293. if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row()) {
  294. painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
  295. } else {
  296. auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  297. painter.blit(icon_rect.location(), *bitmap, bitmap->rect(), opacity);
  298. }
  299. }
  300. }
  301. auto display_data = index.data();
  302. if (display_data.is_string() || display_data.is_u32() || display_data.is_i32() || display_data.is_u64() || display_data.is_i64() || display_data.is_bool() || display_data.is_float())
  303. draw_item_text(painter, index, is_selected_row, text_rect, display_data.to_deprecated_string(), font_for_index(index), Gfx::TextAlignment::CenterLeft, Gfx::TextElision::Right);
  304. if (selection_behavior() == SelectionBehavior::SelectItems && is_focused() && index == cursor_index()) {
  305. painter.draw_rect(background_rect, palette().color(background_role()));
  306. painter.draw_focus_rect(background_rect, palette().focus_outline());
  307. }
  308. auto index_at_indent = index;
  309. for (int i = indent_level; i > 0; --i) {
  310. auto parent_of_index_at_indent = index_at_indent.parent();
  311. bool index_at_indent_is_last_in_parent = index_at_indent.row() == model.row_count(parent_of_index_at_indent) - 1;
  312. Gfx::IntPoint a { tree_column_x_offset + horizontal_padding() + indent_width_in_pixels() * i - icon_size() / 2, rect.y() - 2 };
  313. Gfx::IntPoint b { a.x(), a.y() + row_height() - 1 };
  314. if (index_at_indent_is_last_in_parent)
  315. b.set_y(rect.center().y());
  316. if (!(i != indent_level && index_at_indent_is_last_in_parent))
  317. painter.draw_line(a, b, Color::MidGray);
  318. if (i == indent_level) {
  319. Gfx::IntPoint c { a.x(), rect.center().y() };
  320. Gfx::IntPoint d { c.x() + icon_size() / 2, c.y() };
  321. painter.draw_line(c, d, Color::MidGray);
  322. }
  323. index_at_indent = parent_of_index_at_indent;
  324. }
  325. if (!toggle_rect.is_empty()) {
  326. auto& metadata = ensure_metadata_for_index(index);
  327. if (metadata.open)
  328. painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect());
  329. else
  330. painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect());
  331. }
  332. if (has_pending_drop() && index == drop_candidate_index()) {
  333. painter.draw_rect(rect, palette().selection(), true);
  334. }
  335. }
  336. x_offset += column_width + horizontal_padding() * 2;
  337. }
  338. if (selection_behavior() == SelectionBehavior::SelectRows && is_focused() && index == cursor_index()) {
  339. painter.draw_rect(row_rect, palette().color(background_role()));
  340. painter.draw_focus_rect(row_rect, palette().focus_outline());
  341. }
  342. return IterationDecision::Continue;
  343. });
  344. }
  345. void TreeView::scroll_into_view(ModelIndex const& a_index, bool, bool scroll_vertically)
  346. {
  347. if (!a_index.is_valid())
  348. return;
  349. Gfx::IntRect found_rect;
  350. traverse_in_paint_order([&](ModelIndex const& index, Gfx::IntRect const& rect, Gfx::IntRect const&, int) {
  351. if (index == a_index) {
  352. found_rect = rect;
  353. return IterationDecision::Break;
  354. }
  355. return IterationDecision::Continue;
  356. });
  357. AbstractScrollableWidget::scroll_into_view(found_rect, false, scroll_vertically);
  358. }
  359. void TreeView::model_did_update(unsigned flags)
  360. {
  361. m_view_metadata.clear();
  362. AbstractTableView::model_did_update(flags);
  363. }
  364. void TreeView::did_update_selection()
  365. {
  366. AbstractView::did_update_selection();
  367. if (!model())
  368. return;
  369. auto index = selection().first();
  370. if (!index.is_valid())
  371. return;
  372. if (activates_on_selection())
  373. activate(index);
  374. }
  375. void TreeView::mousedown_event(MouseEvent& event)
  376. {
  377. if (!model())
  378. return AbstractView::mousedown_event(event);
  379. if (event.button() != MouseButton::Primary)
  380. return AbstractView::mousedown_event(event);
  381. bool is_toggle;
  382. auto index = index_at_event_position(event.position(), is_toggle);
  383. if (index.is_valid() && is_toggle && model()->row_count(index)) {
  384. if (event.alt()) {
  385. if (is_toggled(index)) {
  386. collapse_tree(index);
  387. } else {
  388. expand_tree(index);
  389. }
  390. return;
  391. }
  392. toggle_index(index);
  393. return;
  394. }
  395. AbstractView::mousedown_event(event);
  396. }
  397. void TreeView::keydown_event(KeyEvent& event)
  398. {
  399. if (!model())
  400. return AbstractTableView::keydown_event(event);
  401. if (event.key() == KeyCode::Key_Space) {
  402. if (model()->row_count(cursor_index()))
  403. toggle_index(cursor_index());
  404. return;
  405. }
  406. auto open_tree_node = [&](bool open, MetadataForIndex& metadata) {
  407. if (on_toggle)
  408. on_toggle(cursor_index(), open);
  409. metadata.open = open;
  410. update_column_sizes();
  411. update_content_size();
  412. update();
  413. };
  414. if (event.key() == KeyCode::Key_Left) {
  415. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  416. if (event.ctrl()) {
  417. collapse_tree(cursor_index());
  418. return;
  419. }
  420. auto& metadata = ensure_metadata_for_index(cursor_index());
  421. if (metadata.open) {
  422. open_tree_node(false, metadata);
  423. return;
  424. }
  425. }
  426. if (cursor_index().is_valid() && cursor_index().parent().is_valid()) {
  427. set_cursor(cursor_index().parent(), SelectionUpdate::Set);
  428. return;
  429. }
  430. }
  431. if (event.key() == KeyCode::Key_Right) {
  432. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  433. if (event.ctrl()) {
  434. expand_tree(cursor_index());
  435. return;
  436. }
  437. auto& metadata = ensure_metadata_for_index(cursor_index());
  438. if (!metadata.open) {
  439. open_tree_node(true, metadata);
  440. return;
  441. }
  442. auto new_cursor = model()->index(0, model()->tree_column(), cursor_index());
  443. set_cursor(new_cursor, SelectionUpdate::Set);
  444. return;
  445. }
  446. }
  447. if (event.key() == KeyCode::Key_Return) {
  448. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  449. toggle_index(cursor_index());
  450. return;
  451. }
  452. }
  453. AbstractTableView::keydown_event(event);
  454. }
  455. void TreeView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  456. {
  457. auto& model = *this->model();
  458. if (!cursor_index().is_valid())
  459. set_cursor(model.index(0, model.tree_column(), cursor_index()), SelectionUpdate::Set);
  460. auto find_last_index_in_tree = [&](const ModelIndex tree_index) -> ModelIndex {
  461. auto last_index = tree_index;
  462. size_t row_count = model.row_count(last_index);
  463. while (row_count > 0) {
  464. last_index = model.index(row_count - 1, model.tree_column(), last_index);
  465. if (last_index.is_valid()) {
  466. if (model.row_count(last_index) == 0)
  467. break;
  468. auto& metadata = ensure_metadata_for_index(last_index);
  469. if (!metadata.open)
  470. break;
  471. }
  472. row_count = model.row_count(last_index);
  473. }
  474. return last_index;
  475. };
  476. auto step_up = [&](const ModelIndex current_index) -> ModelIndex {
  477. // Traverse into parent index if we're at the top of our subtree
  478. if (current_index.row() == 0) {
  479. auto parent_index = current_index.parent();
  480. if (parent_index.is_valid())
  481. return parent_index;
  482. return current_index;
  483. }
  484. // If previous index is closed, move to it immediately
  485. auto previous_index = model.index(current_index.row() - 1, model.tree_column(), current_index.parent());
  486. if (model.row_count(previous_index) == 0)
  487. return previous_index;
  488. auto& metadata = ensure_metadata_for_index(previous_index);
  489. if (!metadata.open)
  490. return previous_index;
  491. // Return very last index inside of open previous index
  492. return find_last_index_in_tree(previous_index);
  493. };
  494. auto step_down = [&](const ModelIndex current_index) -> ModelIndex {
  495. if (!current_index.is_valid())
  496. return current_index;
  497. // Step in when node is open
  498. if (model.row_count(current_index) > 0) {
  499. auto& metadata = ensure_metadata_for_index(current_index);
  500. if (metadata.open)
  501. return model.index(0, model.tree_column(), current_index);
  502. }
  503. // Find the parent index in which we must step one down
  504. auto child_index = current_index;
  505. auto parent_index = child_index.parent();
  506. int row_count = model.row_count(parent_index);
  507. while (child_index.is_valid() && child_index.row() >= row_count - 1) {
  508. child_index = parent_index;
  509. parent_index = parent_index.parent();
  510. row_count = model.row_count(parent_index);
  511. }
  512. // Step one down
  513. if (!child_index.is_valid())
  514. return current_index;
  515. return model.index(child_index.row() + 1, child_index.column(), parent_index);
  516. };
  517. switch (movement) {
  518. case CursorMovement::Up: {
  519. auto new_index = step_up(cursor_index());
  520. if (new_index.is_valid())
  521. set_cursor(new_index, selection_update);
  522. break;
  523. }
  524. case CursorMovement::Down: {
  525. auto new_index = step_down(cursor_index());
  526. if (new_index.is_valid())
  527. set_cursor(new_index, selection_update);
  528. return;
  529. }
  530. case CursorMovement::Home: {
  531. ModelIndex first_index = model.index(0, model.tree_column(), ModelIndex());
  532. if (first_index.is_valid())
  533. set_cursor(first_index, selection_update);
  534. return;
  535. }
  536. case CursorMovement::End: {
  537. auto last_index = find_last_index_in_tree({});
  538. if (last_index.is_valid())
  539. set_cursor(last_index, selection_update);
  540. return;
  541. }
  542. case CursorMovement::PageUp: {
  543. int const items_per_page = visible_content_rect().height() / row_height();
  544. auto new_index = cursor_index();
  545. for (int step = 0; step < items_per_page; ++step)
  546. new_index = step_up(new_index);
  547. if (new_index.is_valid())
  548. set_cursor(new_index, selection_update);
  549. return;
  550. }
  551. case CursorMovement::PageDown: {
  552. int const items_per_page = visible_content_rect().height() / row_height();
  553. auto new_index = cursor_index();
  554. for (int step = 0; step < items_per_page; ++step)
  555. new_index = step_down(new_index);
  556. if (new_index.is_valid())
  557. set_cursor(new_index, selection_update);
  558. return;
  559. }
  560. case CursorMovement::Left:
  561. case CursorMovement::Right:
  562. // There is no left/right in a treeview, those keys expand/collapse items instead.
  563. break;
  564. }
  565. }
  566. int TreeView::item_count() const
  567. {
  568. int count = 0;
  569. traverse_in_paint_order([&](ModelIndex const&, Gfx::IntRect const&, Gfx::IntRect const&, int) {
  570. ++count;
  571. return IterationDecision::Continue;
  572. });
  573. return count;
  574. }
  575. void TreeView::auto_resize_column(int column)
  576. {
  577. if (!model())
  578. return;
  579. if (!column_header().is_section_visible(column))
  580. return;
  581. auto& model = *this->model();
  582. int header_width = column_header().font().width(model.column_name(column));
  583. if (column == m_key_column && model.is_column_sortable(column))
  584. header_width += HeaderView::sorting_arrow_width + HeaderView::sorting_arrow_offset;
  585. int column_width = header_width;
  586. bool is_empty = true;
  587. traverse_in_paint_order([&](ModelIndex const& index, Gfx::IntRect const&, Gfx::IntRect const&, int indent_level) {
  588. auto cell_data = model.index(index.row(), column, index.parent()).data();
  589. int cell_width = 0;
  590. if (cell_data.is_icon()) {
  591. cell_width = cell_data.as_icon().bitmap_for_size(16)->width();
  592. } else if (cell_data.is_bitmap()) {
  593. cell_width = cell_data.as_bitmap().width();
  594. } else if (cell_data.is_valid()) {
  595. cell_width = font().width(cell_data.to_deprecated_string());
  596. }
  597. if (is_empty && cell_width > 0)
  598. is_empty = false;
  599. if (column == model.tree_column())
  600. cell_width += horizontal_padding() * 2 + indent_level * indent_width_in_pixels() + icon_size() / 2;
  601. column_width = max(column_width, cell_width);
  602. return IterationDecision::Continue;
  603. });
  604. auto default_column_width = column_header().default_section_size(column);
  605. if (is_empty && column_header().is_default_section_size_initialized(column))
  606. column_header().set_section_size(column, default_column_width);
  607. else
  608. column_header().set_section_size(column, column_width);
  609. }
  610. void TreeView::update_column_sizes()
  611. {
  612. if (!model())
  613. return;
  614. auto& model = *this->model();
  615. int column_count = model.column_count();
  616. int tree_column = model.tree_column();
  617. for (int column = 0; column < column_count; ++column) {
  618. if (column == tree_column)
  619. continue;
  620. if (!column_header().is_section_visible(column))
  621. continue;
  622. int header_width = column_header().font().width(model.column_name(column));
  623. if (column == m_key_column && model.is_column_sortable(column))
  624. header_width += HeaderView::sorting_arrow_width + HeaderView::sorting_arrow_offset;
  625. int column_width = header_width;
  626. traverse_in_paint_order([&](ModelIndex const& index, Gfx::IntRect const&, Gfx::IntRect const&, int) {
  627. auto cell_data = model.index(index.row(), column, index.parent()).data();
  628. int cell_width = 0;
  629. if (cell_data.is_icon()) {
  630. cell_width = cell_data.as_icon().bitmap_for_size(16)->width();
  631. } else if (cell_data.is_bitmap()) {
  632. cell_width = cell_data.as_bitmap().width();
  633. } else if (cell_data.is_valid()) {
  634. cell_width = font().width(cell_data.to_deprecated_string());
  635. }
  636. column_width = max(column_width, cell_width);
  637. return IterationDecision::Continue;
  638. });
  639. set_column_width(column, max(this->column_width(column), column_width));
  640. }
  641. int tree_column_header_width = column_header().font().width(model.column_name(tree_column));
  642. if (tree_column == m_key_column && model.is_column_sortable(tree_column))
  643. tree_column_header_width += HeaderView::sorting_arrow_width + HeaderView::sorting_arrow_offset;
  644. int tree_column_width = tree_column_header_width;
  645. traverse_in_paint_order([&](ModelIndex const& index, Gfx::IntRect const&, Gfx::IntRect const&, int indent_level) {
  646. auto cell_data = model.index(index.row(), tree_column, index.parent()).data();
  647. int cell_width = 0;
  648. if (cell_data.is_valid()) {
  649. cell_width = font().width(cell_data.to_deprecated_string());
  650. cell_width += horizontal_padding() * 2 + indent_level * indent_width_in_pixels() + icon_size() / 2 + text_padding() * 2;
  651. }
  652. tree_column_width = max(tree_column_width, cell_width);
  653. return IterationDecision::Continue;
  654. });
  655. set_column_width(tree_column, tree_column_width);
  656. }
  657. int TreeView::tree_column_x_offset() const
  658. {
  659. int tree_column = model()->tree_column();
  660. int offset = 0;
  661. for (int i = 0; i < tree_column; ++i) {
  662. if (column_header().is_section_visible(i)) {
  663. offset += column_width(i);
  664. offset += horizontal_padding() * 2;
  665. }
  666. }
  667. return offset;
  668. }
  669. Gfx::IntRect TreeView::content_rect(ModelIndex const& index) const
  670. {
  671. if (!index.is_valid())
  672. return {};
  673. Gfx::IntRect found_rect;
  674. traverse_in_paint_order([&](ModelIndex const& current_index, Gfx::IntRect const& rect, Gfx::IntRect const&, int) {
  675. if (index == current_index) {
  676. found_rect = rect;
  677. found_rect.translate_by(0, column_header().height());
  678. return IterationDecision::Break;
  679. }
  680. return IterationDecision::Continue;
  681. });
  682. return found_rect;
  683. }
  684. int TreeView::minimum_column_width(int column)
  685. {
  686. if (column != model()->tree_column()) {
  687. return 2;
  688. }
  689. int maximum_indent_level = 1;
  690. traverse_in_paint_order([&](ModelIndex const&, Gfx::IntRect const&, Gfx::IntRect const&, int indent_level) {
  691. maximum_indent_level = max(maximum_indent_level, indent_level);
  692. return IterationDecision::Continue;
  693. });
  694. return indent_width_in_pixels() * maximum_indent_level + icon_size() + icon_spacing() + 2;
  695. }
  696. }