TreeView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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/Scrollbar.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_fill_with_background_color(true);
  33. set_background_role(ColorRole::Base);
  34. set_foreground_role(ColorRole::BaseText);
  35. set_column_headers_visible(false);
  36. m_expand_bitmap = Gfx::Bitmap::load_from_file("/res/icons/serenity/treeview-expand.png");
  37. m_collapse_bitmap = Gfx::Bitmap::load_from_file("/res/icons/serenity/treeview-collapse.png");
  38. }
  39. TreeView::~TreeView()
  40. {
  41. }
  42. ModelIndex TreeView::index_at_event_position(const 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([&](const ModelIndex& index, const Gfx::IntRect& rect, const Gfx::IntRect& 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::Left) {
  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(const ModelIndex& 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(const ModelIndex& 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(const ModelIndex& 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(const ModelIndex& 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(const ModelIndex& index)
  130. {
  131. VERIFY(model()->row_count(index));
  132. auto& metadata = ensure_metadata_for_index(index);
  133. metadata.open = !metadata.open;
  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. int indent_level = 1;
  146. int y_offset = 0;
  147. int tree_column_x_offset = this->tree_column_x_offset();
  148. Function<IterationDecision(const ModelIndex&)> traverse_index = [&](const ModelIndex& index) {
  149. int row_count_at_index = model.row_count(index);
  150. if (index.is_valid()) {
  151. auto& metadata = ensure_metadata_for_index(index);
  152. int x_offset = tree_column_x_offset + horizontal_padding() + indent_level * indent_width_in_pixels();
  153. auto node_text = index.data().to_string();
  154. Gfx::IntRect rect = {
  155. x_offset, y_offset,
  156. icon_size() + icon_spacing() + text_padding() + font_for_index(index)->width(node_text) + text_padding(), row_height()
  157. };
  158. Gfx::IntRect toggle_rect;
  159. if (row_count_at_index > 0) {
  160. int toggle_x = tree_column_x_offset + horizontal_padding() + (indent_width_in_pixels() * indent_level) - (icon_size() / 2) - 4;
  161. toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() };
  162. toggle_rect.center_vertically_within(rect);
  163. }
  164. if (callback(index, rect, toggle_rect, indent_level) == IterationDecision::Break)
  165. return IterationDecision::Break;
  166. y_offset += row_height();
  167. // NOTE: Skip traversing children if this index is closed!
  168. if (!metadata.open)
  169. return IterationDecision::Continue;
  170. }
  171. if (indent_level > 0 && !index.is_valid())
  172. return IterationDecision::Continue;
  173. ++indent_level;
  174. int row_count = model.row_count(index);
  175. for (int i = 0; i < row_count; ++i) {
  176. if (traverse_index(model.index(i, model.tree_column(), index)) == IterationDecision::Break)
  177. return IterationDecision::Break;
  178. }
  179. --indent_level;
  180. return IterationDecision::Continue;
  181. };
  182. int root_count = model.row_count();
  183. for (int root_index = 0; root_index < root_count; ++root_index) {
  184. if (traverse_index(model.index(root_index, model.tree_column(), ModelIndex())) == IterationDecision::Break)
  185. break;
  186. }
  187. }
  188. void TreeView::paint_event(PaintEvent& event)
  189. {
  190. Frame::paint_event(event);
  191. Painter painter(*this);
  192. painter.add_clip_rect(frame_inner_rect());
  193. painter.add_clip_rect(event.rect());
  194. if (fill_with_background_color())
  195. painter.fill_rect(event.rect(), palette().color(background_role()));
  196. if (!model())
  197. return;
  198. auto& model = *this->model();
  199. painter.translate(frame_inner_rect().location());
  200. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  201. auto visible_content_rect = this->visible_content_rect();
  202. int tree_column = model.tree_column();
  203. int tree_column_x_offset = this->tree_column_x_offset();
  204. int y_offset = column_header().height();
  205. int painted_row_index = 0;
  206. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect& a_rect, const Gfx::IntRect& a_toggle_rect, int indent_level) {
  207. if (!a_rect.intersects_vertically(visible_content_rect))
  208. return IterationDecision::Continue;
  209. auto rect = a_rect.translated(0, y_offset);
  210. auto toggle_rect = a_toggle_rect.translated(0, y_offset);
  211. #if ITEM_RECTS_DEBUG
  212. painter.fill_rect(rect, Color::WarmGray);
  213. #endif
  214. bool is_selected_row = selection().contains(index);
  215. Color text_color = palette().color(foreground_role());
  216. if (is_selected_row && should_fill_selected_rows())
  217. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  218. Color background_color;
  219. Color key_column_background_color;
  220. if (is_selected_row) {
  221. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  222. key_column_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. key_column_background_color = Color(200, 200, 200);
  227. } else {
  228. background_color = palette().color(background_role());
  229. key_column_background_color = Color(220, 220, 220);
  230. }
  231. }
  232. int row_width = 0;
  233. for (int column_index = 0; column_index < model.column_count(); ++column_index) {
  234. if (!column_header().is_section_visible(column_index))
  235. continue;
  236. row_width += this->column_width(column_index) + horizontal_padding() * 2;
  237. }
  238. if (frame_inner_rect().width() > row_width) {
  239. row_width = frame_inner_rect().width();
  240. }
  241. Gfx::IntRect row_rect { 0, rect.y(), row_width, rect.height() };
  242. if (!is_selected_row || should_fill_selected_rows())
  243. painter.fill_rect(row_rect, background_color);
  244. int x_offset = 0;
  245. for (int column_index = 0; column_index < model.column_count(); ++column_index) {
  246. if (!column_header().is_section_visible(column_index))
  247. continue;
  248. int column_width = this->column_width(column_index);
  249. painter.draw_rect(toggle_rect, text_color);
  250. if (column_index != tree_column) {
  251. Gfx::IntRect cell_rect(horizontal_padding() + x_offset, rect.y(), column_width, row_height());
  252. auto cell_index = model.index(index.row(), column_index, index.parent());
  253. if (auto* delegate = column_painting_delegate(column_index)) {
  254. delegate->paint(painter, cell_rect, palette(), cell_index);
  255. } else {
  256. auto data = cell_index.data();
  257. if (data.is_bitmap()) {
  258. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  259. } else if (data.is_icon()) {
  260. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  261. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  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. Gfx::IntRect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
  270. Gfx::IntRect text_rect = {
  271. icon_rect.right() + 1 + icon_spacing(), rect.y(),
  272. rect.width() - icon_size() - icon_spacing(), rect.height()
  273. };
  274. painter.fill_rect(text_rect, background_color);
  275. auto icon = index.data(ModelRole::Icon);
  276. if (icon.is_icon()) {
  277. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
  278. if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row())
  279. painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
  280. else
  281. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  282. }
  283. }
  284. draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::Center, Gfx::TextElision::None);
  285. if (is_focused() && index == cursor_index()) {
  286. painter.draw_rect(text_rect, palette().color(background_role()));
  287. painter.draw_focus_rect(text_rect, palette().focus_outline());
  288. }
  289. auto index_at_indent = index;
  290. for (int i = indent_level; i > 0; --i) {
  291. auto parent_of_index_at_indent = index_at_indent.parent();
  292. bool index_at_indent_is_last_in_parent = index_at_indent.row() == model.row_count(parent_of_index_at_indent) - 1;
  293. Gfx::IntPoint a { tree_column_x_offset + horizontal_padding() + indent_width_in_pixels() * i - icon_size() / 2, rect.y() - 2 };
  294. Gfx::IntPoint b { a.x(), a.y() + row_height() - 1 };
  295. if (index_at_indent_is_last_in_parent)
  296. b.set_y(rect.center().y());
  297. if (!(i != indent_level && index_at_indent_is_last_in_parent))
  298. painter.draw_line(a, b, Color::MidGray);
  299. if (i == indent_level) {
  300. Gfx::IntPoint c { a.x(), rect.center().y() };
  301. Gfx::IntPoint d { c.x() + icon_size() / 2, c.y() };
  302. painter.draw_line(c, d, Color::MidGray);
  303. }
  304. index_at_indent = parent_of_index_at_indent;
  305. }
  306. if (!toggle_rect.is_empty()) {
  307. auto& metadata = ensure_metadata_for_index(index);
  308. if (metadata.open)
  309. painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect());
  310. else
  311. painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect());
  312. }
  313. if (has_pending_drop() && index == drop_candidate_index()) {
  314. painter.draw_rect(rect, palette().selection(), true);
  315. }
  316. }
  317. x_offset += column_width + horizontal_padding() * 2;
  318. }
  319. return IterationDecision::Continue;
  320. });
  321. }
  322. void TreeView::scroll_into_view(const ModelIndex& a_index, bool scroll_horizontally, bool scroll_vertically)
  323. {
  324. if (!a_index.is_valid())
  325. return;
  326. Gfx::IntRect found_rect;
  327. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect& rect, const Gfx::IntRect&, int) {
  328. if (index == a_index) {
  329. found_rect = rect;
  330. return IterationDecision::Break;
  331. }
  332. return IterationDecision::Continue;
  333. });
  334. ScrollableWidget::scroll_into_view(found_rect, scroll_horizontally, scroll_vertically);
  335. }
  336. void TreeView::model_did_update(unsigned flags)
  337. {
  338. m_view_metadata.clear();
  339. AbstractTableView::model_did_update(flags);
  340. }
  341. void TreeView::did_update_selection()
  342. {
  343. AbstractView::did_update_selection();
  344. if (!model())
  345. return;
  346. auto index = selection().first();
  347. if (!index.is_valid())
  348. return;
  349. if (activates_on_selection())
  350. activate(index);
  351. }
  352. void TreeView::keydown_event(KeyEvent& event)
  353. {
  354. if (!model())
  355. return AbstractTableView::keydown_event(event);
  356. if (event.key() == KeyCode::Key_Space) {
  357. if (model()->row_count(cursor_index()))
  358. toggle_index(cursor_index());
  359. return;
  360. }
  361. auto open_tree_node = [&](bool open, MetadataForIndex& metadata) {
  362. if (on_toggle)
  363. on_toggle(cursor_index(), open);
  364. metadata.open = open;
  365. update_column_sizes();
  366. update_content_size();
  367. update();
  368. };
  369. if (event.key() == KeyCode::Key_Left) {
  370. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  371. if (event.ctrl()) {
  372. collapse_tree(cursor_index());
  373. return;
  374. }
  375. auto& metadata = ensure_metadata_for_index(cursor_index());
  376. if (metadata.open) {
  377. open_tree_node(false, metadata);
  378. return;
  379. }
  380. }
  381. if (cursor_index().is_valid() && cursor_index().parent().is_valid()) {
  382. set_cursor(cursor_index().parent(), SelectionUpdate::Set);
  383. return;
  384. }
  385. }
  386. if (event.key() == KeyCode::Key_Right) {
  387. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  388. if (event.ctrl()) {
  389. expand_tree(cursor_index());
  390. return;
  391. }
  392. auto& metadata = ensure_metadata_for_index(cursor_index());
  393. if (!metadata.open) {
  394. open_tree_node(true, metadata);
  395. return;
  396. }
  397. auto new_cursor = model()->index(0, model()->tree_column(), cursor_index());
  398. set_cursor(new_cursor, SelectionUpdate::Set);
  399. return;
  400. }
  401. }
  402. if (event.key() == KeyCode::Key_Return) {
  403. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  404. toggle_index(cursor_index());
  405. return;
  406. }
  407. }
  408. AbstractTableView::keydown_event(event);
  409. }
  410. void TreeView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  411. {
  412. switch (movement) {
  413. case CursorMovement::Up: {
  414. ModelIndex previous_index;
  415. ModelIndex found_index;
  416. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  417. if (index == cursor_index()) {
  418. found_index = previous_index;
  419. return IterationDecision::Break;
  420. }
  421. previous_index = index;
  422. return IterationDecision::Continue;
  423. });
  424. if (found_index.is_valid())
  425. set_cursor(found_index, selection_update);
  426. break;
  427. }
  428. case CursorMovement::Down: {
  429. ModelIndex previous_index;
  430. ModelIndex found_index;
  431. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  432. if (previous_index == cursor_index()) {
  433. found_index = index;
  434. return IterationDecision::Break;
  435. }
  436. previous_index = index;
  437. return IterationDecision::Continue;
  438. });
  439. if (found_index.is_valid())
  440. set_cursor(found_index, selection_update);
  441. return;
  442. }
  443. case CursorMovement::Home:
  444. // FIXME: Implement.
  445. break;
  446. case CursorMovement::End:
  447. // FIXME: Implement.
  448. break;
  449. case CursorMovement::PageUp:
  450. // FIXME: Implement.
  451. break;
  452. case CursorMovement::PageDown:
  453. // FIXME: Implement.
  454. break;
  455. case CursorMovement::Left:
  456. case CursorMovement::Right:
  457. // There is no left/right in a treeview, those keys expand/collapse items instead.
  458. break;
  459. }
  460. }
  461. int TreeView::item_count() const
  462. {
  463. int count = 0;
  464. traverse_in_paint_order([&](const ModelIndex&, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  465. ++count;
  466. return IterationDecision::Continue;
  467. });
  468. return count;
  469. }
  470. void TreeView::auto_resize_column(int column)
  471. {
  472. if (!model())
  473. return;
  474. if (!column_header().is_section_visible(column))
  475. return;
  476. auto& model = *this->model();
  477. int header_width = column_header().font().width(model.column_name(column));
  478. if (column == m_key_column && model.is_column_sortable(column))
  479. header_width += font().width(" \xE2\xAC\x86");
  480. int column_width = header_width;
  481. bool is_empty = true;
  482. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int indent_level) {
  483. auto cell_data = model.index(index.row(), column, index.parent()).data();
  484. int cell_width = 0;
  485. if (cell_data.is_icon()) {
  486. cell_width = cell_data.as_icon().bitmap_for_size(16)->width();
  487. } else if (cell_data.is_bitmap()) {
  488. cell_width = cell_data.as_bitmap().width();
  489. } else if (cell_data.is_valid()) {
  490. cell_width = font().width(cell_data.to_string());
  491. }
  492. if (is_empty && cell_width > 0)
  493. is_empty = false;
  494. if (column == model.tree_column())
  495. cell_width += horizontal_padding() * 2 + indent_level * indent_width_in_pixels() + icon_size() / 2;
  496. column_width = max(column_width, cell_width);
  497. return IterationDecision::Continue;
  498. });
  499. auto default_column_width = column_header().default_section_size(column);
  500. if (is_empty && column_header().is_default_section_size_initialized(column))
  501. column_header().set_section_size(column, default_column_width);
  502. else
  503. column_header().set_section_size(column, column_width);
  504. }
  505. void TreeView::update_column_sizes()
  506. {
  507. if (!model())
  508. return;
  509. auto& model = *this->model();
  510. int column_count = model.column_count();
  511. int tree_column = model.tree_column();
  512. for (int column = 0; column < column_count; ++column) {
  513. if (column == tree_column)
  514. continue;
  515. if (!column_header().is_section_visible(column))
  516. continue;
  517. int header_width = column_header().font().width(model.column_name(column));
  518. if (column == m_key_column && model.is_column_sortable(column))
  519. header_width += font().width(" \xE2\xAC\x86");
  520. int column_width = header_width;
  521. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  522. auto cell_data = model.index(index.row(), column, index.parent()).data();
  523. int cell_width = 0;
  524. if (cell_data.is_icon()) {
  525. cell_width = cell_data.as_icon().bitmap_for_size(16)->width();
  526. } else if (cell_data.is_bitmap()) {
  527. cell_width = cell_data.as_bitmap().width();
  528. } else if (cell_data.is_valid()) {
  529. cell_width = font().width(cell_data.to_string());
  530. }
  531. column_width = max(column_width, cell_width);
  532. return IterationDecision::Continue;
  533. });
  534. set_column_width(column, max(this->column_width(column), column_width));
  535. }
  536. int tree_column_header_width = column_header().font().width(model.column_name(tree_column));
  537. if (tree_column == m_key_column && model.is_column_sortable(tree_column))
  538. tree_column_header_width += font().width(" \xE2\xAC\x86");
  539. int tree_column_width = tree_column_header_width;
  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(), tree_column, index.parent()).data();
  542. int cell_width = 0;
  543. if (cell_data.is_valid()) {
  544. cell_width = font().width(cell_data.to_string());
  545. cell_width += horizontal_padding() * 2 + indent_level * indent_width_in_pixels() + icon_size() / 2;
  546. }
  547. tree_column_width = max(tree_column_width, cell_width);
  548. return IterationDecision::Continue;
  549. });
  550. set_column_width(tree_column, tree_column_width);
  551. }
  552. int TreeView::tree_column_x_offset() const
  553. {
  554. int tree_column = model()->tree_column();
  555. int offset = 0;
  556. for (int i = 0; i < tree_column; ++i) {
  557. if (column_header().is_section_visible(i)) {
  558. offset += column_width(i);
  559. offset += horizontal_padding() * 2;
  560. }
  561. }
  562. return offset;
  563. }
  564. }