TreeView.cpp 28 KB

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