TreeView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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 constexpr (ITEM_RECTS_DEBUG)
  212. painter.fill_rect(rect, Color::WarmGray);
  213. bool is_selected_row = selection().contains(index);
  214. Color text_color = palette().color(foreground_role());
  215. if (is_selected_row && should_fill_selected_rows())
  216. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  217. Color background_color;
  218. Color key_column_background_color;
  219. if (is_selected_row) {
  220. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  221. key_column_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. key_column_background_color = Color(200, 200, 200);
  226. } else {
  227. background_color = palette().color(background_role());
  228. key_column_background_color = Color(220, 220, 220);
  229. }
  230. }
  231. int row_width = 0;
  232. for (int column_index = 0; column_index < model.column_count(); ++column_index) {
  233. if (!column_header().is_section_visible(column_index))
  234. continue;
  235. row_width += this->column_width(column_index) + horizontal_padding() * 2;
  236. }
  237. if (frame_inner_rect().width() > row_width) {
  238. row_width = frame_inner_rect().width();
  239. }
  240. Gfx::IntRect row_rect { 0, rect.y(), row_width, rect.height() };
  241. if (!is_selected_row || should_fill_selected_rows())
  242. painter.fill_rect(row_rect, background_color);
  243. int x_offset = 0;
  244. for (int column_index = 0; column_index < model.column_count(); ++column_index) {
  245. if (!column_header().is_section_visible(column_index))
  246. continue;
  247. int column_width = this->column_width(column_index);
  248. painter.draw_rect(toggle_rect, text_color);
  249. if (column_index != tree_column) {
  250. Gfx::IntRect cell_rect(horizontal_padding() + x_offset, rect.y(), column_width, row_height());
  251. auto cell_index = model.index(index.row(), column_index, index.parent());
  252. if (auto* delegate = column_painting_delegate(column_index)) {
  253. delegate->paint(painter, cell_rect, palette(), cell_index);
  254. } else {
  255. auto data = cell_index.data();
  256. if (data.is_bitmap()) {
  257. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  258. } else if (data.is_icon()) {
  259. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  260. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  261. } else {
  262. auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
  263. draw_item_text(painter, cell_index, is_selected_row, cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, Gfx::TextElision::Right);
  264. }
  265. }
  266. } else {
  267. // It's the tree column!
  268. Gfx::IntRect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
  269. Gfx::IntRect text_rect = {
  270. icon_rect.right() + 1 + icon_spacing(), rect.y(),
  271. rect.width() - icon_size() - icon_spacing(), rect.height()
  272. };
  273. painter.fill_rect(text_rect, background_color);
  274. auto icon = index.data(ModelRole::Icon);
  275. if (icon.is_icon()) {
  276. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
  277. if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row())
  278. painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
  279. else
  280. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  281. }
  282. }
  283. draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::Center, Gfx::TextElision::None);
  284. if (is_focused() && index == cursor_index()) {
  285. painter.draw_rect(text_rect, palette().color(background_role()));
  286. painter.draw_focus_rect(text_rect, palette().focus_outline());
  287. }
  288. auto index_at_indent = index;
  289. for (int i = indent_level; i > 0; --i) {
  290. auto parent_of_index_at_indent = index_at_indent.parent();
  291. bool index_at_indent_is_last_in_parent = index_at_indent.row() == model.row_count(parent_of_index_at_indent) - 1;
  292. Gfx::IntPoint a { tree_column_x_offset + horizontal_padding() + indent_width_in_pixels() * i - icon_size() / 2, rect.y() - 2 };
  293. Gfx::IntPoint b { a.x(), a.y() + row_height() - 1 };
  294. if (index_at_indent_is_last_in_parent)
  295. b.set_y(rect.center().y());
  296. if (!(i != indent_level && index_at_indent_is_last_in_parent))
  297. painter.draw_line(a, b, Color::MidGray);
  298. if (i == indent_level) {
  299. Gfx::IntPoint c { a.x(), rect.center().y() };
  300. Gfx::IntPoint d { c.x() + icon_size() / 2, c.y() };
  301. painter.draw_line(c, d, Color::MidGray);
  302. }
  303. index_at_indent = parent_of_index_at_indent;
  304. }
  305. if (!toggle_rect.is_empty()) {
  306. auto& metadata = ensure_metadata_for_index(index);
  307. if (metadata.open)
  308. painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect());
  309. else
  310. painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect());
  311. }
  312. if (has_pending_drop() && index == drop_candidate_index()) {
  313. painter.draw_rect(rect, palette().selection(), true);
  314. }
  315. }
  316. x_offset += column_width + horizontal_padding() * 2;
  317. }
  318. return IterationDecision::Continue;
  319. });
  320. }
  321. void TreeView::scroll_into_view(const ModelIndex& a_index, bool scroll_horizontally, bool scroll_vertically)
  322. {
  323. if (!a_index.is_valid())
  324. return;
  325. Gfx::IntRect found_rect;
  326. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect& rect, const Gfx::IntRect&, int) {
  327. if (index == a_index) {
  328. found_rect = rect;
  329. return IterationDecision::Break;
  330. }
  331. return IterationDecision::Continue;
  332. });
  333. ScrollableWidget::scroll_into_view(found_rect, scroll_horizontally, scroll_vertically);
  334. }
  335. void TreeView::model_did_update(unsigned flags)
  336. {
  337. m_view_metadata.clear();
  338. AbstractTableView::model_did_update(flags);
  339. }
  340. void TreeView::did_update_selection()
  341. {
  342. AbstractView::did_update_selection();
  343. if (!model())
  344. return;
  345. auto index = selection().first();
  346. if (!index.is_valid())
  347. return;
  348. if (activates_on_selection())
  349. activate(index);
  350. }
  351. void TreeView::keydown_event(KeyEvent& event)
  352. {
  353. if (!model())
  354. return AbstractTableView::keydown_event(event);
  355. if (event.key() == KeyCode::Key_Space) {
  356. if (model()->row_count(cursor_index()))
  357. toggle_index(cursor_index());
  358. return;
  359. }
  360. auto open_tree_node = [&](bool open, MetadataForIndex& metadata) {
  361. if (on_toggle)
  362. on_toggle(cursor_index(), open);
  363. metadata.open = open;
  364. update_column_sizes();
  365. update_content_size();
  366. update();
  367. };
  368. if (event.key() == KeyCode::Key_Left) {
  369. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  370. if (event.ctrl()) {
  371. collapse_tree(cursor_index());
  372. return;
  373. }
  374. auto& metadata = ensure_metadata_for_index(cursor_index());
  375. if (metadata.open) {
  376. open_tree_node(false, metadata);
  377. return;
  378. }
  379. }
  380. if (cursor_index().is_valid() && cursor_index().parent().is_valid()) {
  381. set_cursor(cursor_index().parent(), SelectionUpdate::Set);
  382. return;
  383. }
  384. }
  385. if (event.key() == KeyCode::Key_Right) {
  386. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  387. if (event.ctrl()) {
  388. expand_tree(cursor_index());
  389. return;
  390. }
  391. auto& metadata = ensure_metadata_for_index(cursor_index());
  392. if (!metadata.open) {
  393. open_tree_node(true, metadata);
  394. return;
  395. }
  396. auto new_cursor = model()->index(0, model()->tree_column(), cursor_index());
  397. set_cursor(new_cursor, SelectionUpdate::Set);
  398. return;
  399. }
  400. }
  401. if (event.key() == KeyCode::Key_Return) {
  402. if (cursor_index().is_valid() && model()->row_count(cursor_index())) {
  403. toggle_index(cursor_index());
  404. return;
  405. }
  406. }
  407. AbstractTableView::keydown_event(event);
  408. }
  409. void TreeView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  410. {
  411. switch (movement) {
  412. case CursorMovement::Up: {
  413. ModelIndex previous_index;
  414. ModelIndex found_index;
  415. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  416. if (index == cursor_index()) {
  417. found_index = previous_index;
  418. return IterationDecision::Break;
  419. }
  420. previous_index = index;
  421. return IterationDecision::Continue;
  422. });
  423. if (found_index.is_valid())
  424. set_cursor(found_index, selection_update);
  425. break;
  426. }
  427. case CursorMovement::Down: {
  428. ModelIndex previous_index;
  429. ModelIndex found_index;
  430. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  431. if (previous_index == cursor_index()) {
  432. found_index = index;
  433. return IterationDecision::Break;
  434. }
  435. previous_index = index;
  436. return IterationDecision::Continue;
  437. });
  438. if (found_index.is_valid())
  439. set_cursor(found_index, selection_update);
  440. return;
  441. }
  442. case CursorMovement::Home:
  443. // FIXME: Implement.
  444. break;
  445. case CursorMovement::End:
  446. // FIXME: Implement.
  447. break;
  448. case CursorMovement::PageUp:
  449. // FIXME: Implement.
  450. break;
  451. case CursorMovement::PageDown:
  452. // FIXME: Implement.
  453. break;
  454. case CursorMovement::Left:
  455. case CursorMovement::Right:
  456. // There is no left/right in a treeview, those keys expand/collapse items instead.
  457. break;
  458. }
  459. }
  460. int TreeView::item_count() const
  461. {
  462. int count = 0;
  463. traverse_in_paint_order([&](const ModelIndex&, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  464. ++count;
  465. return IterationDecision::Continue;
  466. });
  467. return count;
  468. }
  469. void TreeView::auto_resize_column(int column)
  470. {
  471. if (!model())
  472. return;
  473. if (!column_header().is_section_visible(column))
  474. return;
  475. auto& model = *this->model();
  476. int header_width = column_header().font().width(model.column_name(column));
  477. if (column == m_key_column && model.is_column_sortable(column))
  478. header_width += font().width(" \xE2\xAC\x86");
  479. int column_width = header_width;
  480. bool is_empty = true;
  481. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int indent_level) {
  482. auto cell_data = model.index(index.row(), column, index.parent()).data();
  483. int cell_width = 0;
  484. if (cell_data.is_icon()) {
  485. cell_width = cell_data.as_icon().bitmap_for_size(16)->width();
  486. } else if (cell_data.is_bitmap()) {
  487. cell_width = cell_data.as_bitmap().width();
  488. } else if (cell_data.is_valid()) {
  489. cell_width = font().width(cell_data.to_string());
  490. }
  491. if (is_empty && cell_width > 0)
  492. is_empty = false;
  493. if (column == model.tree_column())
  494. cell_width += horizontal_padding() * 2 + indent_level * indent_width_in_pixels() + icon_size() / 2;
  495. column_width = max(column_width, cell_width);
  496. return IterationDecision::Continue;
  497. });
  498. auto default_column_width = column_header().default_section_size(column);
  499. if (is_empty && column_header().is_default_section_size_initialized(column))
  500. column_header().set_section_size(column, default_column_width);
  501. else
  502. column_header().set_section_size(column, column_width);
  503. }
  504. void TreeView::update_column_sizes()
  505. {
  506. if (!model())
  507. return;
  508. auto& model = *this->model();
  509. int column_count = model.column_count();
  510. int tree_column = model.tree_column();
  511. for (int column = 0; column < column_count; ++column) {
  512. if (column == tree_column)
  513. continue;
  514. if (!column_header().is_section_visible(column))
  515. continue;
  516. int header_width = column_header().font().width(model.column_name(column));
  517. if (column == m_key_column && model.is_column_sortable(column))
  518. header_width += font().width(" \xE2\xAC\x86");
  519. int column_width = header_width;
  520. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  521. auto cell_data = model.index(index.row(), column, index.parent()).data();
  522. int cell_width = 0;
  523. if (cell_data.is_icon()) {
  524. cell_width = cell_data.as_icon().bitmap_for_size(16)->width();
  525. } else if (cell_data.is_bitmap()) {
  526. cell_width = cell_data.as_bitmap().width();
  527. } else if (cell_data.is_valid()) {
  528. cell_width = font().width(cell_data.to_string());
  529. }
  530. column_width = max(column_width, cell_width);
  531. return IterationDecision::Continue;
  532. });
  533. set_column_width(column, max(this->column_width(column), column_width));
  534. }
  535. int tree_column_header_width = column_header().font().width(model.column_name(tree_column));
  536. if (tree_column == m_key_column && model.is_column_sortable(tree_column))
  537. tree_column_header_width += font().width(" \xE2\xAC\x86");
  538. int tree_column_width = tree_column_header_width;
  539. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int indent_level) {
  540. auto cell_data = model.index(index.row(), tree_column, index.parent()).data();
  541. int cell_width = 0;
  542. if (cell_data.is_valid()) {
  543. cell_width = font().width(cell_data.to_string());
  544. cell_width += horizontal_padding() * 2 + indent_level * indent_width_in_pixels() + icon_size() / 2;
  545. }
  546. tree_column_width = max(tree_column_width, cell_width);
  547. return IterationDecision::Continue;
  548. });
  549. set_column_width(tree_column, tree_column_width);
  550. }
  551. int TreeView::tree_column_x_offset() const
  552. {
  553. int tree_column = model()->tree_column();
  554. int offset = 0;
  555. for (int i = 0; i < tree_column; ++i) {
  556. if (column_header().is_section_visible(i)) {
  557. offset += column_width(i);
  558. offset += horizontal_padding() * 2;
  559. }
  560. }
  561. return offset;
  562. }
  563. }