GTreeView.cpp 19 KB

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