TreeView.cpp 26 KB

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