GTreeView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include <LibGUI/GPainter.h>
  2. #include <LibGUI/GScrollBar.h>
  3. #include <LibGUI/GTreeView.h>
  4. //#define DEBUG_ITEM_RECTS
  5. struct GTreeView::MetadataForIndex {
  6. bool open { false };
  7. };
  8. GTreeView::MetadataForIndex& GTreeView::ensure_metadata_for_index(const GModelIndex& index) const
  9. {
  10. ASSERT(index.is_valid());
  11. auto it = m_view_metadata.find(index.internal_data());
  12. if (it != m_view_metadata.end())
  13. return *it->value;
  14. auto new_metadata = make<MetadataForIndex>();
  15. auto& new_metadata_ref = *new_metadata;
  16. m_view_metadata.set(index.internal_data(), move(new_metadata));
  17. return new_metadata_ref;
  18. }
  19. GTreeView::GTreeView(GWidget* parent)
  20. : GAbstractView(parent)
  21. {
  22. set_frame_shape(FrameShape::Container);
  23. set_frame_shadow(FrameShadow::Sunken);
  24. set_frame_thickness(2);
  25. m_expand_bitmap = GraphicsBitmap::load_from_file("/res/icons/treeview-expand.png");
  26. m_collapse_bitmap = GraphicsBitmap::load_from_file("/res/icons/treeview-collapse.png");
  27. }
  28. GTreeView::~GTreeView()
  29. {
  30. }
  31. GModelIndex GTreeView::index_at_content_position(const Point& position, bool& is_toggle) const
  32. {
  33. is_toggle = false;
  34. if (!model())
  35. return {};
  36. GModelIndex result;
  37. traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect& toggle_rect, int) {
  38. if (rect.contains(position)) {
  39. result = index;
  40. return IterationDecision::Break;
  41. }
  42. if (toggle_rect.contains(position)) {
  43. result = index;
  44. is_toggle = true;
  45. return IterationDecision::Break;
  46. }
  47. return IterationDecision::Continue;
  48. });
  49. return result;
  50. }
  51. void GTreeView::mousedown_event(GMouseEvent& event)
  52. {
  53. if (!model())
  54. return;
  55. auto& model = *this->model();
  56. auto adjusted_position = event.position().translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  57. bool is_toggle;
  58. auto index = index_at_content_position(adjusted_position, is_toggle);
  59. if (!index.is_valid())
  60. return;
  61. if (model.selected_index() != index) {
  62. model.set_selected_index(index);
  63. update();
  64. }
  65. if (is_toggle && model.row_count(index))
  66. toggle_index(index);
  67. }
  68. void GTreeView::toggle_index(const GModelIndex& index)
  69. {
  70. ASSERT(model()->row_count(index));
  71. auto& metadata = ensure_metadata_for_index(index);
  72. metadata.open = !metadata.open;
  73. update_content_size();
  74. update();
  75. }
  76. template<typename Callback>
  77. void GTreeView::traverse_in_paint_order(Callback callback) const
  78. {
  79. ASSERT(model());
  80. auto& model = *this->model();
  81. int indent_level = 1;
  82. int y_offset = 0;
  83. Function<IterationDecision(const GModelIndex&)> traverse_index = [&](const GModelIndex& index) {
  84. int row_count_at_index = model.row_count(index);
  85. if (index.is_valid()) {
  86. auto& metadata = ensure_metadata_for_index(index);
  87. int x_offset = indent_level * indent_width_in_pixels();
  88. auto node_text = model.data(index, GModel::Role::Display).to_string();
  89. Rect rect = {
  90. x_offset, y_offset,
  91. icon_size() + icon_spacing() + text_padding() + font().width(node_text) + text_padding(), item_height()
  92. };
  93. Rect toggle_rect;
  94. if (row_count_at_index > 0) {
  95. int toggle_x = indent_width_in_pixels() * indent_level - icon_size() / 2 - 4;
  96. toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() };
  97. toggle_rect.center_vertically_within(rect);
  98. }
  99. if (callback(index, rect, toggle_rect, indent_level) == IterationDecision::Break)
  100. return IterationDecision::Break;
  101. y_offset += item_height();
  102. // NOTE: Skip traversing children if this index is closed!
  103. if (!metadata.open)
  104. return IterationDecision::Continue;
  105. }
  106. ++indent_level;
  107. int row_count = model.row_count(index);
  108. for (int i = 0; i < row_count; ++i) {
  109. if (traverse_index(model.index(i, 0, index)) == IterationDecision::Break)
  110. return IterationDecision::Break;
  111. }
  112. --indent_level;
  113. return IterationDecision::Continue;
  114. };
  115. int root_count = model.row_count();
  116. for (int root_index = 0; root_index < root_count; ++root_index) {
  117. if (traverse_index(model.index(root_index, 0, GModelIndex())) == IterationDecision::Break)
  118. break;
  119. }
  120. }
  121. void GTreeView::paint_event(GPaintEvent& event)
  122. {
  123. GFrame::paint_event(event);
  124. GPainter painter(*this);
  125. painter.add_clip_rect(frame_inner_rect());
  126. painter.add_clip_rect(event.rect());
  127. painter.fill_rect(event.rect(), Color::White);
  128. painter.translate(frame_inner_rect().location());
  129. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  130. if (!model())
  131. return;
  132. auto& model = *this->model();
  133. auto visible_content_rect = this->visible_content_rect();
  134. traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect& toggle_rect, int indent_level) {
  135. if (!rect.intersects(visible_content_rect))
  136. return IterationDecision::Continue;
  137. #ifdef DEBUG_ITEM_RECTS
  138. painter.fill_rect(rect, Color::WarmGray);
  139. #endif
  140. Color background_color = Color::from_rgb(0xffffff);
  141. Color text_color = Color::from_rgb(0x000000);
  142. Rect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
  143. auto icon = model.data(index, GModel::Role::Icon);
  144. if (icon.is_icon()) {
  145. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size()))
  146. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  147. }
  148. Rect text_rect = {
  149. icon_rect.right() + 1 + icon_spacing(), rect.y(),
  150. rect.width() - icon_size() - icon_spacing(), rect.height()
  151. };
  152. if (index == model.selected_index()) {
  153. background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  154. text_color = Color::from_rgb(0xffffff);
  155. painter.fill_rect(text_rect, background_color);
  156. }
  157. auto node_text = model.data(index, GModel::Role::Display).to_string();
  158. painter.draw_text(text_rect, node_text, TextAlignment::Center, text_color);
  159. auto index_at_indent = index;
  160. for (int i = indent_level; i >= 0; --i) {
  161. auto parent_of_index_at_indent = index_at_indent.parent();
  162. bool index_at_indent_is_last_in_parent = index_at_indent.row() == model.row_count(parent_of_index_at_indent) - 1;
  163. Point a { indent_width_in_pixels() * i - icon_size() / 2, rect.y() - 2 };
  164. Point b { a.x(), a.y() + item_height() - 1 };
  165. if (index_at_indent_is_last_in_parent)
  166. b.set_y(rect.center().y());
  167. if (!(i != indent_level && index_at_indent_is_last_in_parent))
  168. painter.draw_line(a, b, Color::MidGray);
  169. if (i == indent_level) {
  170. Point c { a.x(), rect.center().y() };
  171. Point d { c.x() + icon_size() / 2, c.y() };
  172. painter.draw_line(c, d, Color::MidGray);
  173. }
  174. index_at_indent = parent_of_index_at_indent;
  175. }
  176. if (!toggle_rect.is_empty()) {
  177. auto& metadata = ensure_metadata_for_index(index);
  178. if (metadata.open)
  179. painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect());
  180. else
  181. painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect());
  182. }
  183. return IterationDecision::Continue;
  184. });
  185. }
  186. void GTreeView::scroll_into_view(const GModelIndex& a_index, Orientation orientation)
  187. {
  188. if (!a_index.is_valid())
  189. return;
  190. Rect found_rect;
  191. traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect&, int) {
  192. if (index == a_index) {
  193. found_rect = rect;
  194. return IterationDecision::Break;
  195. }
  196. return IterationDecision::Continue;
  197. });
  198. GScrollableWidget::scroll_into_view(found_rect, orientation);
  199. }
  200. void GTreeView::did_update_model()
  201. {
  202. GAbstractView::did_update_model();
  203. update_content_size();
  204. }
  205. void GTreeView::did_update_selection()
  206. {
  207. ASSERT(model());
  208. auto& model = *this->model();
  209. auto index = model.selected_index();
  210. if (!index.is_valid())
  211. return;
  212. bool opened_any = false;
  213. for (auto current = index; current.is_valid(); current = current.parent()) {
  214. auto& metadata_for_ancestor = ensure_metadata_for_index(current);
  215. if (!metadata_for_ancestor.open) {
  216. metadata_for_ancestor.open = true;
  217. opened_any = true;
  218. }
  219. }
  220. if (opened_any)
  221. update_content_size();
  222. update();
  223. if (activates_on_selection())
  224. activate(index);
  225. }
  226. void GTreeView::update_content_size()
  227. {
  228. int height = 0;
  229. int width = 0;
  230. traverse_in_paint_order([&](const GModelIndex&, const Rect& rect, const Rect&, int) {
  231. width = max(width, rect.right());
  232. height += rect.height();
  233. return IterationDecision::Continue;
  234. });
  235. set_content_size({ width, height });
  236. }
  237. void GTreeView::keydown_event(GKeyEvent& event)
  238. {
  239. if (!model())
  240. return;
  241. auto cursor_index = model()->selected_index();
  242. if (event.key() == KeyCode::Key_Space) {
  243. if (model()->row_count(cursor_index))
  244. toggle_index(cursor_index);
  245. return;
  246. }
  247. if (event.key() == KeyCode::Key_Up) {
  248. GModelIndex previous_index;
  249. GModelIndex found_index;
  250. traverse_in_paint_order([&](const GModelIndex& index, const Rect&, const Rect&, int) {
  251. if (index == cursor_index) {
  252. found_index = previous_index;
  253. return IterationDecision::Break;
  254. }
  255. previous_index = index;
  256. return IterationDecision::Continue;
  257. });
  258. if (found_index.is_valid()) {
  259. model()->set_selected_index(found_index);
  260. update();
  261. }
  262. return;
  263. }
  264. if (event.key() == KeyCode::Key_Down) {
  265. GModelIndex previous_index;
  266. GModelIndex found_index;
  267. traverse_in_paint_order([&](const GModelIndex& index, const Rect&, const Rect&, int) {
  268. if (previous_index == cursor_index) {
  269. found_index = index;
  270. return IterationDecision::Break;
  271. }
  272. previous_index = index;
  273. return IterationDecision::Continue;
  274. });
  275. if (found_index.is_valid())
  276. model()->set_selected_index(found_index);
  277. return;
  278. }
  279. }