TreeView.cpp 19 KB

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