TreeView.cpp 21 KB

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