TreeView.cpp 24 KB

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