TreeView.cpp 28 KB

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