TreeView.cpp 21 KB

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