GTreeView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. set_should_hide_unnecessary_scrollbars(true);
  28. }
  29. GTreeView::~GTreeView()
  30. {
  31. }
  32. GModelIndex GTreeView::index_at_content_position(const Point& position, bool& is_toggle) const
  33. {
  34. is_toggle = false;
  35. if (!model())
  36. return {};
  37. GModelIndex result;
  38. traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect& toggle_rect, int) {
  39. if (rect.contains(position)) {
  40. result = index;
  41. return IterationDecision::Break;
  42. }
  43. if (toggle_rect.contains(position)) {
  44. result = index;
  45. is_toggle = true;
  46. return IterationDecision::Break;
  47. }
  48. return IterationDecision::Continue;
  49. });
  50. return result;
  51. }
  52. void GTreeView::mousedown_event(GMouseEvent& event)
  53. {
  54. if (!model())
  55. return;
  56. auto& model = *this->model();
  57. auto adjusted_position = event.position().translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  58. bool is_toggle;
  59. auto index = index_at_content_position(adjusted_position, is_toggle);
  60. if (!index.is_valid()) {
  61. if (event.button() == GMouseButton::Left && !(event.modifiers() & Mod_Ctrl))
  62. selection().clear();
  63. return;
  64. }
  65. if (event.button() == GMouseButton::Left) {
  66. if (event.modifiers() & Mod_Ctrl) {
  67. selection().toggle(index);
  68. } else {
  69. selection().set(index);
  70. }
  71. if (is_toggle && model.row_count(index))
  72. toggle_index(index);
  73. }
  74. }
  75. void GTreeView::doubleclick_event(GMouseEvent& event)
  76. {
  77. if (!model())
  78. return;
  79. auto& model = *this->model();
  80. auto adjusted_position = event.position().translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  81. bool is_toggle;
  82. auto index = index_at_content_position(adjusted_position, is_toggle);
  83. if (!index.is_valid())
  84. return;
  85. if (event.button() == GMouseButton::Left) {
  86. if (selection().first() != index)
  87. selection().set(index);
  88. if (model.row_count(index))
  89. toggle_index(index);
  90. }
  91. }
  92. void GTreeView::toggle_index(const GModelIndex& index)
  93. {
  94. ASSERT(model()->row_count(index));
  95. auto& metadata = ensure_metadata_for_index(index);
  96. metadata.open = !metadata.open;
  97. update_content_size();
  98. update();
  99. }
  100. template<typename Callback>
  101. void GTreeView::traverse_in_paint_order(Callback callback) const
  102. {
  103. ASSERT(model());
  104. auto& model = *this->model();
  105. int indent_level = 1;
  106. int y_offset = 0;
  107. Function<IterationDecision(const GModelIndex&)> traverse_index = [&](const GModelIndex& index) {
  108. int row_count_at_index = model.row_count(index);
  109. if (index.is_valid()) {
  110. auto& metadata = ensure_metadata_for_index(index);
  111. int x_offset = indent_level * indent_width_in_pixels();
  112. auto node_text = model.data(index, GModel::Role::Display).to_string();
  113. Rect rect = {
  114. x_offset, y_offset,
  115. icon_size() + icon_spacing() + text_padding() + font().width(node_text) + text_padding(), item_height()
  116. };
  117. Rect toggle_rect;
  118. if (row_count_at_index > 0) {
  119. int toggle_x = indent_width_in_pixels() * indent_level - icon_size() / 2 - 4;
  120. toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() };
  121. toggle_rect.center_vertically_within(rect);
  122. }
  123. if (callback(index, rect, toggle_rect, indent_level) == IterationDecision::Break)
  124. return IterationDecision::Break;
  125. y_offset += item_height();
  126. // NOTE: Skip traversing children if this index is closed!
  127. if (!metadata.open)
  128. return IterationDecision::Continue;
  129. }
  130. ++indent_level;
  131. int row_count = model.row_count(index);
  132. for (int i = 0; i < row_count; ++i) {
  133. if (traverse_index(model.index(i, 0, index)) == IterationDecision::Break)
  134. return IterationDecision::Break;
  135. }
  136. --indent_level;
  137. return IterationDecision::Continue;
  138. };
  139. int root_count = model.row_count();
  140. for (int root_index = 0; root_index < root_count; ++root_index) {
  141. if (traverse_index(model.index(root_index, 0, GModelIndex())) == IterationDecision::Break)
  142. break;
  143. }
  144. }
  145. void GTreeView::paint_event(GPaintEvent& event)
  146. {
  147. GFrame::paint_event(event);
  148. GPainter painter(*this);
  149. painter.add_clip_rect(frame_inner_rect());
  150. painter.add_clip_rect(event.rect());
  151. painter.fill_rect(event.rect(), Color::White);
  152. painter.translate(frame_inner_rect().location());
  153. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  154. if (!model())
  155. return;
  156. auto& model = *this->model();
  157. auto visible_content_rect = this->visible_content_rect();
  158. traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect& toggle_rect, int indent_level) {
  159. if (!rect.intersects(visible_content_rect))
  160. return IterationDecision::Continue;
  161. #ifdef DEBUG_ITEM_RECTS
  162. painter.fill_rect(rect, Color::WarmGray);
  163. #endif
  164. Color background_color = Color::from_rgb(0xffffff);
  165. Color text_color = Color::from_rgb(0x000000);
  166. Rect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
  167. auto icon = model.data(index, GModel::Role::Icon);
  168. if (icon.is_icon()) {
  169. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size()))
  170. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  171. }
  172. Rect text_rect = {
  173. icon_rect.right() + 1 + icon_spacing(), rect.y(),
  174. rect.width() - icon_size() - icon_spacing(), rect.height()
  175. };
  176. if (selection().contains(index)) {
  177. background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  178. text_color = Color::from_rgb(0xffffff);
  179. painter.fill_rect(text_rect, background_color);
  180. }
  181. auto node_text = model.data(index, GModel::Role::Display).to_string();
  182. painter.draw_text(text_rect, node_text, TextAlignment::Center, text_color);
  183. auto index_at_indent = index;
  184. for (int i = indent_level; i >= 0; --i) {
  185. auto parent_of_index_at_indent = index_at_indent.parent();
  186. bool index_at_indent_is_last_in_parent = index_at_indent.row() == model.row_count(parent_of_index_at_indent) - 1;
  187. Point a { indent_width_in_pixels() * i - icon_size() / 2, rect.y() - 2 };
  188. Point b { a.x(), a.y() + item_height() - 1 };
  189. if (index_at_indent_is_last_in_parent)
  190. b.set_y(rect.center().y());
  191. if (!(i != indent_level && index_at_indent_is_last_in_parent))
  192. painter.draw_line(a, b, Color::MidGray);
  193. if (i == indent_level) {
  194. Point c { a.x(), rect.center().y() };
  195. Point d { c.x() + icon_size() / 2, c.y() };
  196. painter.draw_line(c, d, Color::MidGray);
  197. }
  198. index_at_indent = parent_of_index_at_indent;
  199. }
  200. if (!toggle_rect.is_empty()) {
  201. auto& metadata = ensure_metadata_for_index(index);
  202. if (metadata.open)
  203. painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect());
  204. else
  205. painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect());
  206. }
  207. return IterationDecision::Continue;
  208. });
  209. }
  210. void GTreeView::scroll_into_view(const GModelIndex& a_index, Orientation orientation)
  211. {
  212. if (!a_index.is_valid())
  213. return;
  214. Rect found_rect;
  215. traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect&, int) {
  216. if (index == a_index) {
  217. found_rect = rect;
  218. return IterationDecision::Break;
  219. }
  220. return IterationDecision::Continue;
  221. });
  222. GScrollableWidget::scroll_into_view(found_rect, orientation);
  223. }
  224. void GTreeView::did_update_model()
  225. {
  226. GAbstractView::did_update_model();
  227. update_content_size();
  228. update();
  229. }
  230. void GTreeView::did_update_selection()
  231. {
  232. GAbstractView::did_update_selection();
  233. ASSERT(model());
  234. auto index = selection().first();
  235. if (!index.is_valid())
  236. return;
  237. bool opened_any = false;
  238. for (auto current = index; current.is_valid(); current = current.parent()) {
  239. auto& metadata_for_ancestor = ensure_metadata_for_index(current);
  240. if (!metadata_for_ancestor.open) {
  241. metadata_for_ancestor.open = true;
  242. opened_any = true;
  243. }
  244. }
  245. if (opened_any)
  246. update_content_size();
  247. update();
  248. if (activates_on_selection())
  249. activate(index);
  250. }
  251. void GTreeView::update_content_size()
  252. {
  253. int height = 0;
  254. int width = 0;
  255. traverse_in_paint_order([&](const GModelIndex&, const Rect& rect, const Rect&, int) {
  256. width = max(width, rect.right());
  257. height += rect.height();
  258. return IterationDecision::Continue;
  259. });
  260. set_content_size({ width, height });
  261. }
  262. void GTreeView::keydown_event(GKeyEvent& event)
  263. {
  264. if (!model())
  265. return;
  266. auto cursor_index = selection().first();
  267. if (event.key() == KeyCode::Key_Space) {
  268. if (model()->row_count(cursor_index))
  269. toggle_index(cursor_index);
  270. return;
  271. }
  272. if (event.key() == KeyCode::Key_Up) {
  273. GModelIndex previous_index;
  274. GModelIndex found_index;
  275. traverse_in_paint_order([&](const GModelIndex& index, const Rect&, const Rect&, int) {
  276. if (index == cursor_index) {
  277. found_index = previous_index;
  278. return IterationDecision::Break;
  279. }
  280. previous_index = index;
  281. return IterationDecision::Continue;
  282. });
  283. if (found_index.is_valid()) {
  284. selection().set(found_index);
  285. update();
  286. }
  287. return;
  288. }
  289. if (event.key() == KeyCode::Key_Down) {
  290. GModelIndex previous_index;
  291. GModelIndex found_index;
  292. traverse_in_paint_order([&](const GModelIndex& index, const Rect&, const Rect&, int) {
  293. if (previous_index == cursor_index) {
  294. found_index = index;
  295. return IterationDecision::Break;
  296. }
  297. previous_index = index;
  298. return IterationDecision::Continue;
  299. });
  300. if (found_index.is_valid())
  301. selection().set(found_index);
  302. return;
  303. }
  304. }
  305. void GTreeView::context_menu_event(GContextMenuEvent& event)
  306. {
  307. if (!model())
  308. return;
  309. auto adjusted_position = event.position().translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  310. bool is_toggle;
  311. auto index = index_at_content_position(adjusted_position, is_toggle);
  312. if (index.is_valid()) {
  313. if (on_context_menu_request)
  314. on_context_menu_request(index, event);
  315. }
  316. GAbstractView::context_menu_event(event);
  317. }