TreeView.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 <LibGUI/HeaderView.h>
  27. #include <LibGUI/Model.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/ScrollBar.h>
  30. #include <LibGUI/TreeView.h>
  31. #include <LibGfx/Bitmap.h>
  32. #include <LibGfx/Palette.h>
  33. //#define DEBUG_ITEM_RECTS
  34. namespace GUI {
  35. struct TreeView::MetadataForIndex {
  36. bool open { false };
  37. };
  38. TreeView::MetadataForIndex& TreeView::ensure_metadata_for_index(const ModelIndex& index) const
  39. {
  40. ASSERT(index.is_valid());
  41. auto it = m_view_metadata.find(index.internal_data());
  42. if (it != m_view_metadata.end())
  43. return *it->value;
  44. auto new_metadata = make<MetadataForIndex>();
  45. auto& new_metadata_ref = *new_metadata;
  46. m_view_metadata.set(index.internal_data(), move(new_metadata));
  47. return new_metadata_ref;
  48. }
  49. TreeView::TreeView()
  50. {
  51. set_fill_with_background_color(true);
  52. set_background_role(ColorRole::Base);
  53. set_foreground_role(ColorRole::BaseText);
  54. set_column_headers_visible(false);
  55. m_expand_bitmap = Gfx::Bitmap::load_from_file("/res/icons/serenity/treeview-expand.png");
  56. m_collapse_bitmap = Gfx::Bitmap::load_from_file("/res/icons/serenity/treeview-collapse.png");
  57. }
  58. TreeView::~TreeView()
  59. {
  60. }
  61. ModelIndex TreeView::index_at_event_position(const Gfx::IntPoint& a_position, bool& is_toggle) const
  62. {
  63. auto position = a_position.translated(0, -column_header().height()).translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  64. is_toggle = false;
  65. if (!model())
  66. return {};
  67. ModelIndex result;
  68. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect& rect, const Gfx::IntRect& toggle_rect, int) {
  69. if (toggle_rect.contains(position)) {
  70. result = index;
  71. is_toggle = true;
  72. return IterationDecision::Break;
  73. }
  74. if (rect.contains_vertically(position.y())) {
  75. result = index;
  76. return IterationDecision::Break;
  77. }
  78. return IterationDecision::Continue;
  79. });
  80. return result;
  81. }
  82. void TreeView::doubleclick_event(MouseEvent& event)
  83. {
  84. if (!model())
  85. return;
  86. auto& model = *this->model();
  87. bool is_toggle;
  88. auto index = index_at_event_position(event.position(), is_toggle);
  89. if (!index.is_valid())
  90. return;
  91. if (event.button() == MouseButton::Left) {
  92. if (selection().first() != index)
  93. selection().set(index);
  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_tree(const ModelIndex& root)
  117. {
  118. if (!model())
  119. return;
  120. set_open_state_of_all_in_subtree(root, true);
  121. update_column_sizes();
  122. update_content_size();
  123. update();
  124. }
  125. void TreeView::collapse_tree(const ModelIndex& root)
  126. {
  127. if (!model())
  128. return;
  129. set_open_state_of_all_in_subtree(root, false);
  130. update_column_sizes();
  131. update_content_size();
  132. update();
  133. }
  134. void TreeView::toggle_index(const ModelIndex& index)
  135. {
  136. ASSERT(model()->row_count(index));
  137. auto& metadata = ensure_metadata_for_index(index);
  138. metadata.open = !metadata.open;
  139. if (on_toggle)
  140. on_toggle(index, metadata.open);
  141. update_column_sizes();
  142. update_content_size();
  143. update();
  144. }
  145. template<typename Callback>
  146. void TreeView::traverse_in_paint_order(Callback callback) const
  147. {
  148. ASSERT(model());
  149. auto& model = *this->model();
  150. int indent_level = 1;
  151. int y_offset = 0;
  152. int tree_column_x_offset = this->tree_column_x_offset();
  153. Function<IterationDecision(const ModelIndex&)> traverse_index = [&](const ModelIndex& index) {
  154. int row_count_at_index = model.row_count(index);
  155. if (index.is_valid()) {
  156. auto& metadata = ensure_metadata_for_index(index);
  157. int x_offset = tree_column_x_offset + horizontal_padding() + indent_level * indent_width_in_pixels();
  158. auto node_text = index.data().to_string();
  159. Gfx::IntRect rect = {
  160. x_offset, y_offset,
  161. icon_size() + icon_spacing() + text_padding() + font_for_index(index)->width(node_text) + text_padding(), row_height()
  162. };
  163. Gfx::IntRect toggle_rect;
  164. if (row_count_at_index > 0) {
  165. int toggle_x = tree_column_x_offset + horizontal_padding() + (indent_width_in_pixels() * indent_level) - (icon_size() / 2) - 4;
  166. toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() };
  167. toggle_rect.center_vertically_within(rect);
  168. }
  169. if (callback(index, rect, toggle_rect, indent_level) == IterationDecision::Break)
  170. return IterationDecision::Break;
  171. y_offset += row_height();
  172. // NOTE: Skip traversing children if this index is closed!
  173. if (!metadata.open)
  174. return IterationDecision::Continue;
  175. }
  176. if (indent_level > 0 && !index.is_valid())
  177. return IterationDecision::Continue;
  178. ++indent_level;
  179. int row_count = model.row_count(index);
  180. for (int i = 0; i < row_count; ++i) {
  181. if (traverse_index(model.index(i, model.tree_column(), index)) == IterationDecision::Break)
  182. return IterationDecision::Break;
  183. }
  184. --indent_level;
  185. return IterationDecision::Continue;
  186. };
  187. int root_count = model.row_count();
  188. for (int root_index = 0; root_index < root_count; ++root_index) {
  189. if (traverse_index(model.index(root_index, model.tree_column(), ModelIndex())) == IterationDecision::Break)
  190. break;
  191. }
  192. }
  193. void TreeView::paint_event(PaintEvent& event)
  194. {
  195. Frame::paint_event(event);
  196. Painter painter(*this);
  197. painter.add_clip_rect(frame_inner_rect());
  198. painter.add_clip_rect(event.rect());
  199. if (fill_with_background_color())
  200. painter.fill_rect(event.rect(), palette().color(background_role()));
  201. if (!model())
  202. return;
  203. auto& model = *this->model();
  204. painter.translate(frame_inner_rect().location());
  205. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  206. auto visible_content_rect = this->visible_content_rect();
  207. int tree_column = model.tree_column();
  208. int tree_column_x_offset = this->tree_column_x_offset();
  209. int y_offset = column_header().height();
  210. int painted_row_index = 0;
  211. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect& a_rect, const Gfx::IntRect& a_toggle_rect, int indent_level) {
  212. if (!a_rect.intersects_vertically(visible_content_rect))
  213. return IterationDecision::Continue;
  214. auto rect = a_rect.translated(0, y_offset);
  215. auto toggle_rect = a_toggle_rect.translated(0, y_offset);
  216. #ifdef DEBUG_ITEM_RECTS
  217. painter.fill_rect(rect, Color::WarmGray);
  218. #endif
  219. bool is_selected_row = selection().contains(index);
  220. Color text_color = palette().color(foreground_role());
  221. if (is_selected_row)
  222. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  223. Color background_color;
  224. Color key_column_background_color;
  225. if (is_selected_row) {
  226. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  227. key_column_background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  228. } else {
  229. if (alternating_row_colors() && (painted_row_index % 2)) {
  230. background_color = Color(220, 220, 220);
  231. key_column_background_color = Color(200, 200, 200);
  232. } else {
  233. background_color = palette().color(background_role());
  234. key_column_background_color = Color(220, 220, 220);
  235. }
  236. }
  237. int row_width = 0;
  238. for (int column_index = 0; column_index < model.column_count(); ++column_index) {
  239. if (!column_header().is_section_visible(column_index))
  240. continue;
  241. row_width += this->column_width(column_index) + horizontal_padding() * 2;
  242. }
  243. if (frame_inner_rect().width() > row_width) {
  244. row_width = frame_inner_rect().width();
  245. }
  246. Gfx::IntRect row_rect { 0, rect.y(), row_width, rect.height() };
  247. painter.fill_rect(row_rect, background_color);
  248. int x_offset = 0;
  249. for (int column_index = 0; column_index < model.column_count(); ++column_index) {
  250. if (!column_header().is_section_visible(column_index))
  251. continue;
  252. int column_width = this->column_width(column_index);
  253. painter.draw_rect(toggle_rect, text_color);
  254. if (column_index != tree_column) {
  255. Gfx::IntRect cell_rect(horizontal_padding() + x_offset, rect.y(), column_width, row_height());
  256. auto cell_index = model.index(index.row(), column_index, index.parent());
  257. if (auto* delegate = column_painting_delegate(column_index)) {
  258. delegate->paint(painter, cell_rect, palette(), cell_index);
  259. } else {
  260. auto data = cell_index.data();
  261. if (data.is_bitmap()) {
  262. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  263. } else if (data.is_icon()) {
  264. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  265. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  266. } else {
  267. if (!is_selected_row)
  268. text_color = cell_index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
  269. auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
  270. painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, text_color, Gfx::TextElision::Right);
  271. }
  272. }
  273. } else {
  274. // It's the tree column!
  275. Gfx::IntRect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
  276. auto icon = index.data(ModelRole::Icon);
  277. if (icon.is_icon()) {
  278. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
  279. if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row())
  280. painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
  281. else
  282. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  283. }
  284. }
  285. Gfx::IntRect text_rect = {
  286. icon_rect.right() + 1 + icon_spacing(), rect.y(),
  287. rect.width() - icon_size() - icon_spacing(), rect.height()
  288. };
  289. auto node_text = index.data().to_string();
  290. painter.draw_text(text_rect, node_text, font_for_index(index), Gfx::TextAlignment::Center, text_color);
  291. auto index_at_indent = index;
  292. for (int i = indent_level; i > 0; --i) {
  293. auto parent_of_index_at_indent = index_at_indent.parent();
  294. bool index_at_indent_is_last_in_parent = index_at_indent.row() == model.row_count(parent_of_index_at_indent) - 1;
  295. Gfx::IntPoint a { tree_column_x_offset + horizontal_padding() + indent_width_in_pixels() * i - icon_size() / 2, rect.y() - 2 };
  296. Gfx::IntPoint b { a.x(), a.y() + row_height() - 1 };
  297. if (index_at_indent_is_last_in_parent)
  298. b.set_y(rect.center().y());
  299. if (!(i != indent_level && index_at_indent_is_last_in_parent))
  300. painter.draw_line(a, b, Color::MidGray);
  301. if (i == indent_level) {
  302. Gfx::IntPoint c { a.x(), rect.center().y() };
  303. Gfx::IntPoint d { c.x() + icon_size() / 2, c.y() };
  304. painter.draw_line(c, d, Color::MidGray);
  305. }
  306. index_at_indent = parent_of_index_at_indent;
  307. }
  308. if (!toggle_rect.is_empty()) {
  309. auto& metadata = ensure_metadata_for_index(index);
  310. if (metadata.open)
  311. painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect());
  312. else
  313. painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect());
  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, Orientation orientation)
  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, orientation);
  334. }
  335. void TreeView::did_update_model(unsigned flags)
  336. {
  337. m_view_metadata.clear();
  338. AbstractTableView::did_update_model(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 0
  349. bool opened_any = false;
  350. for (auto current = index; current.is_valid(); current = current.parent()) {
  351. auto& metadata_for_ancestor = ensure_metadata_for_index(current);
  352. if (!metadata_for_ancestor.open) {
  353. metadata_for_ancestor.open = true;
  354. opened_any = true;
  355. }
  356. }
  357. if (opened_any)
  358. update_content_size();
  359. update();
  360. #endif
  361. if (activates_on_selection())
  362. activate(index);
  363. }
  364. void TreeView::keydown_event(KeyEvent& event)
  365. {
  366. if (!model())
  367. return;
  368. auto cursor_index = selection().first();
  369. if (event.key() == KeyCode::Key_Space) {
  370. if (model()->row_count(cursor_index))
  371. toggle_index(cursor_index);
  372. return;
  373. }
  374. if (event.key() == KeyCode::Key_Up) {
  375. ModelIndex previous_index;
  376. ModelIndex found_index;
  377. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  378. if (index == cursor_index) {
  379. found_index = previous_index;
  380. return IterationDecision::Break;
  381. }
  382. previous_index = index;
  383. return IterationDecision::Continue;
  384. });
  385. if (found_index.is_valid()) {
  386. selection().set(found_index);
  387. scroll_into_view(selection().first(), Orientation::Vertical);
  388. update();
  389. }
  390. return;
  391. }
  392. if (event.key() == KeyCode::Key_Down) {
  393. ModelIndex previous_index;
  394. ModelIndex found_index;
  395. traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  396. if (previous_index == cursor_index) {
  397. found_index = index;
  398. return IterationDecision::Break;
  399. }
  400. previous_index = index;
  401. return IterationDecision::Continue;
  402. });
  403. if (found_index.is_valid()) {
  404. selection().set(found_index);
  405. scroll_into_view(selection().first(), Orientation::Vertical);
  406. update();
  407. }
  408. return;
  409. }
  410. auto open_tree_node = [&](bool open, MetadataForIndex& metadata) {
  411. if (on_toggle)
  412. on_toggle(cursor_index, open);
  413. metadata.open = open;
  414. update_column_sizes();
  415. update_content_size();
  416. update();
  417. };
  418. if (event.key() == KeyCode::Key_Left) {
  419. if (cursor_index.is_valid() && model()->row_count(cursor_index)) {
  420. if (event.ctrl()) {
  421. collapse_tree(cursor_index);
  422. return;
  423. }
  424. auto& metadata = ensure_metadata_for_index(cursor_index);
  425. if (metadata.open) {
  426. open_tree_node(false, metadata);
  427. return;
  428. }
  429. }
  430. if (cursor_index.is_valid() && cursor_index.parent().is_valid()) {
  431. selection().set(cursor_index.parent());
  432. scroll_into_view(selection().first(), Orientation::Vertical);
  433. return;
  434. }
  435. }
  436. if (event.key() == KeyCode::Key_Right) {
  437. if (cursor_index.is_valid() && model()->row_count(cursor_index)) {
  438. if (event.ctrl()) {
  439. expand_tree(cursor_index);
  440. return;
  441. }
  442. auto& metadata = ensure_metadata_for_index(cursor_index);
  443. if (!metadata.open) {
  444. open_tree_node(true, metadata);
  445. return;
  446. }
  447. selection().set(model()->index(0, model()->tree_column(), cursor_index));
  448. scroll_into_view(selection().first(), Orientation::Vertical);
  449. return;
  450. }
  451. }
  452. if (event.key() == KeyCode::Key_Return) {
  453. if (cursor_index.is_valid() && model()->row_count(cursor_index)) {
  454. toggle_index(cursor_index);
  455. return;
  456. }
  457. }
  458. }
  459. int TreeView::item_count() const
  460. {
  461. int count = 0;
  462. traverse_in_paint_order([&](const ModelIndex&, const Gfx::IntRect&, const Gfx::IntRect&, int) {
  463. ++count;
  464. return IterationDecision::Continue;
  465. });
  466. return count;
  467. }
  468. void TreeView::update_column_sizes()
  469. {
  470. if (!model())
  471. return;
  472. auto& model = *this->model();
  473. int column_count = model.column_count();
  474. int row_count = model.row_count();
  475. int tree_column = model.tree_column();
  476. int tree_column_x_offset = 0;
  477. for (int column = 0; column < column_count; ++column) {
  478. if (column == tree_column)
  479. continue;
  480. if (!column_header().is_section_visible(column))
  481. continue;
  482. int header_width = column_header().font().width(model.column_name(column));
  483. int column_width = header_width;
  484. for (int row = 0; row < row_count; ++row) {
  485. auto cell_data = model.index(row, column).data();
  486. int cell_width = 0;
  487. if (cell_data.is_bitmap()) {
  488. cell_width = cell_data.as_bitmap().width();
  489. } else {
  490. cell_width = font().width(cell_data.to_string());
  491. }
  492. column_width = max(column_width, cell_width);
  493. }
  494. set_column_width(column, max(this->column_width(column), column_width));
  495. if (column < tree_column)
  496. tree_column_x_offset += column_width;
  497. }
  498. int tree_column_header_width = column_header().font().width(model.column_name(tree_column));
  499. int tree_column_width = tree_column_header_width;
  500. traverse_in_paint_order([&](const ModelIndex&, const Gfx::IntRect& rect, const Gfx::IntRect&, int) {
  501. tree_column_width = max(rect.right() - tree_column_x_offset, tree_column_width);
  502. return IterationDecision::Continue;
  503. });
  504. set_column_width(tree_column, max(this->column_width(tree_column), tree_column_width));
  505. }
  506. int TreeView::tree_column_x_offset() const
  507. {
  508. int tree_column = model()->tree_column();
  509. int offset = 0;
  510. for (int i = 0; i < tree_column; ++i) {
  511. if (column_header().is_section_visible(i)) {
  512. offset += column_width(i);
  513. offset += horizontal_padding();
  514. }
  515. }
  516. return offset;
  517. }
  518. }