TreeMapWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "TreeMapWidget.h"
  7. #include <AK/NumberFormat.h>
  8. #include <LibGUI/ConnectionToWindowServer.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/Font.h>
  11. #include <WindowServer/WindowManager.h>
  12. REGISTER_WIDGET(SpaceAnalyzer, TreeMapWidget)
  13. namespace SpaceAnalyzer {
  14. static const Color colors[] = {
  15. Color(253, 231, 37),
  16. Color(148, 216, 64),
  17. Color(60, 188, 117),
  18. Color(31, 150, 139),
  19. Color(45, 112, 142),
  20. Color(63, 71, 136),
  21. Color(85, 121, 104),
  22. };
  23. static float get_normalized_aspect_ratio(float a, float b)
  24. {
  25. if (a < b) {
  26. return a / b;
  27. } else {
  28. return b / a;
  29. }
  30. }
  31. static bool node_is_leaf(const TreeMapNode& node)
  32. {
  33. return node.num_children() == 0;
  34. }
  35. bool TreeMapWidget::rect_can_contain_label(const Gfx::IntRect& rect) const
  36. {
  37. return rect.height() >= font().presentation_size() && rect.width() > 20;
  38. }
  39. void TreeMapWidget::paint_cell_frame(GUI::Painter& painter, const TreeMapNode& node, const Gfx::IntRect& cell_rect, const Gfx::IntRect& inner_rect, int depth, HasLabel has_label) const
  40. {
  41. if (cell_rect.width() <= 2 || cell_rect.height() <= 2) {
  42. painter.fill_rect(cell_rect, Color::Black);
  43. return;
  44. }
  45. Gfx::IntRect remainder = cell_rect;
  46. Color color = colors[depth % (sizeof(colors) / sizeof(colors[0]))];
  47. if (m_selected_node_cache == &node) {
  48. color = color.darkened(0.8f);
  49. }
  50. // Draw borders.
  51. painter.fill_rect(remainder.take_from_right(1), Color::Black);
  52. painter.fill_rect(remainder.take_from_bottom(1), Color::Black);
  53. // Draw highlights.
  54. painter.fill_rect(remainder.take_from_right(1), color.darkened());
  55. painter.fill_rect(remainder.take_from_bottom(1), color.darkened());
  56. painter.fill_rect(remainder.take_from_top(1), color.lightened());
  57. painter.fill_rect(remainder.take_from_left(1), color.lightened());
  58. // Paint the background.
  59. if (inner_rect.is_empty()) {
  60. painter.fill_rect(remainder, color);
  61. } else {
  62. // Draw black edges above and to the left of the inner_rect.
  63. Gfx::IntRect border_rect = inner_rect.inflated(2, 2);
  64. Gfx::IntRect hammer_rect = border_rect;
  65. hammer_rect.set_width(hammer_rect.width() - 1);
  66. hammer_rect.set_height(hammer_rect.height() - 1);
  67. painter.fill_rect(border_rect.take_from_top(1), Color::Black);
  68. painter.fill_rect(border_rect.take_from_left(1), Color::Black);
  69. for (auto& shard : remainder.shatter(hammer_rect)) {
  70. painter.fill_rect(shard, color);
  71. }
  72. }
  73. // Paint text.
  74. if (has_label == HasLabel::Yes) {
  75. Gfx::IntRect text_rect = remainder;
  76. text_rect.shrink(4, 4);
  77. painter.clear_clip_rect();
  78. painter.add_clip_rect(text_rect);
  79. if (node_is_leaf(node)) {
  80. painter.draw_text(text_rect, node.name(), font(), Gfx::TextAlignment::TopLeft, Color::Black);
  81. text_rect.take_from_top(font().presentation_size() + 1);
  82. painter.draw_text(text_rect, human_readable_size(node.area()), font(), Gfx::TextAlignment::TopLeft, Color::Black);
  83. } else {
  84. painter.draw_text(text_rect, String::formatted("{} - {}", node.name(), human_readable_size(node.area())), font(), Gfx::TextAlignment::TopLeft, Color::Black);
  85. }
  86. painter.clear_clip_rect();
  87. }
  88. }
  89. template<typename Function>
  90. void TreeMapWidget::lay_out_children(const TreeMapNode& node, const Gfx::IntRect& rect, int depth, Function callback)
  91. {
  92. if (node.num_children() == 0) {
  93. return;
  94. }
  95. // Check if the children are sorted yet, if not do that now.
  96. for (size_t k = 0; k < node.num_children() - 1; k++) {
  97. if (node.child_at(k).area() < node.child_at(k + 1).area()) {
  98. node.sort_children_by_area();
  99. break;
  100. }
  101. }
  102. i64 total_area = node.area();
  103. Gfx::IntRect canvas = rect;
  104. bool remaining_nodes_are_too_small = false;
  105. for (size_t i = 0; !remaining_nodes_are_too_small && i < node.num_children(); i++) {
  106. const i64 i_node_area = node.child_at(i).area();
  107. if (i_node_area == 0)
  108. break;
  109. const size_t long_side_size = max(canvas.width(), canvas.height());
  110. const size_t short_side_size = min(canvas.width(), canvas.height());
  111. size_t row_or_column_size = long_side_size * i_node_area / total_area;
  112. i64 node_area_sum = i_node_area;
  113. size_t k = i + 1;
  114. // Try to add nodes to this row or column so long as the worst aspect ratio of
  115. // the new set of nodes is better than the worst aspect ratio of the current set.
  116. {
  117. float best_worst_aspect_ratio_so_far = get_normalized_aspect_ratio(row_or_column_size, short_side_size);
  118. for (; k < node.num_children(); k++) {
  119. // Do a preliminary calculation of the worst aspect ratio of the nodes at index i and k
  120. // if that aspect ratio is better than the 'best_worst_aspect_ratio_so_far' we keep it,
  121. // otherwise it is discarded.
  122. i64 k_node_area = node.child_at(k).area();
  123. if (k_node_area == 0) {
  124. break;
  125. }
  126. i64 new_node_area_sum = node_area_sum + k_node_area;
  127. size_t new_row_or_column_size = long_side_size * new_node_area_sum / total_area;
  128. size_t i_node_size = short_side_size * i_node_area / new_node_area_sum;
  129. size_t k_node_size = short_side_size * k_node_area / new_node_area_sum;
  130. float i_node_aspect_ratio = get_normalized_aspect_ratio(new_row_or_column_size, i_node_size);
  131. float k_node_aspect_ratio = get_normalized_aspect_ratio(new_row_or_column_size, k_node_size);
  132. float new_worst_aspect_ratio = min(i_node_aspect_ratio, k_node_aspect_ratio);
  133. if (new_worst_aspect_ratio < best_worst_aspect_ratio_so_far) {
  134. break;
  135. }
  136. best_worst_aspect_ratio_so_far = new_worst_aspect_ratio;
  137. node_area_sum = new_node_area_sum;
  138. row_or_column_size = new_row_or_column_size;
  139. }
  140. }
  141. // Paint the elements from 'i' up to and including 'k-1'.
  142. {
  143. const size_t fixed_side_size = row_or_column_size;
  144. i64 placement_area = node_area_sum;
  145. size_t main_dim = short_side_size;
  146. // Lay out nodes in a row or column.
  147. Orientation orientation = canvas.width() > canvas.height() ? Orientation::Horizontal : Orientation::Vertical;
  148. Gfx::IntRect layout_rect = canvas;
  149. layout_rect.set_primary_size_for_orientation(orientation, fixed_side_size);
  150. for (size_t q = i; q < k; q++) {
  151. auto& child = node.child_at(q);
  152. size_t node_size = main_dim * child.area() / placement_area;
  153. Gfx::IntRect cell_rect = layout_rect;
  154. cell_rect.set_secondary_size_for_orientation(orientation, node_size);
  155. Gfx::IntRect inner_rect;
  156. HasLabel has_label = HasLabel::No;
  157. if (child.num_children() != 0 && rect.height() >= 8 && rect.width() >= 8) {
  158. inner_rect = cell_rect;
  159. inner_rect.shrink(4, 4); // border and shading
  160. if (rect_can_contain_label(inner_rect)) {
  161. const int margin = 5;
  162. has_label = HasLabel::Yes;
  163. inner_rect.set_y(inner_rect.y() + font().presentation_size() + margin);
  164. inner_rect.set_height(inner_rect.height() - (font().presentation_size() + margin * 2));
  165. inner_rect.set_x(inner_rect.x() + margin);
  166. inner_rect.set_width(inner_rect.width() - margin * 2);
  167. }
  168. } else if (rect_can_contain_label(cell_rect)) {
  169. has_label = HasLabel::Yes;
  170. }
  171. callback(child, q, cell_rect, inner_rect, depth, has_label, IsRemainder::No);
  172. if (cell_rect.width() * cell_rect.height() < 16) {
  173. remaining_nodes_are_too_small = true;
  174. } else if (!inner_rect.is_empty()) {
  175. lay_out_children(child, inner_rect, depth + 1, callback);
  176. }
  177. layout_rect.set_secondary_offset_for_orientation(orientation, layout_rect.secondary_offset_for_orientation(orientation) + node_size);
  178. main_dim -= node_size;
  179. placement_area -= child.area();
  180. }
  181. canvas.set_primary_offset_for_orientation(orientation, canvas.primary_offset_for_orientation(orientation) + fixed_side_size);
  182. canvas.set_primary_size_for_orientation(orientation, canvas.primary_size_for_orientation(orientation) - fixed_side_size);
  183. }
  184. // Consume nodes that were added to this row or column.
  185. i = k - 1;
  186. total_area -= node_area_sum;
  187. }
  188. // If not the entire canvas was filled with nodes, fill the remaining area with a dither pattern.
  189. if (!canvas.is_empty()) {
  190. callback(node, 0, canvas, Gfx::IntRect(), depth, HasLabel::No, IsRemainder::Yes);
  191. }
  192. }
  193. const TreeMapNode* TreeMapWidget::path_node(size_t n) const
  194. {
  195. if (!m_tree.ptr())
  196. return nullptr;
  197. const TreeMapNode* iter = &m_tree->root();
  198. size_t path_index = 0;
  199. while (iter && path_index < m_path.size() && path_index < n) {
  200. size_t child_index = m_path[path_index];
  201. if (child_index >= iter->num_children()) {
  202. return nullptr;
  203. }
  204. iter = &iter->child_at(child_index);
  205. path_index++;
  206. }
  207. return iter;
  208. }
  209. void TreeMapWidget::paint_event(GUI::PaintEvent& event)
  210. {
  211. GUI::Frame::paint_event(event);
  212. GUI::Painter painter(*this);
  213. m_selected_node_cache = path_node(m_path.size());
  214. const TreeMapNode* node = path_node(m_viewpoint);
  215. if (!node) {
  216. painter.fill_rect(frame_inner_rect(), Color::MidGray);
  217. } else if (node_is_leaf(*node)) {
  218. paint_cell_frame(painter, *node, frame_inner_rect(), Gfx::IntRect(), m_viewpoint - 1, HasLabel::Yes);
  219. } else {
  220. lay_out_children(*node, frame_inner_rect(), m_viewpoint, [&](const TreeMapNode& node, int, const Gfx::IntRect& rect, const Gfx::IntRect& inner_rect, int depth, HasLabel has_label, IsRemainder remainder) {
  221. if (remainder == IsRemainder::No) {
  222. paint_cell_frame(painter, node, rect, inner_rect, depth, has_label);
  223. } else {
  224. Color color = colors[depth % (sizeof(colors) / sizeof(colors[0]))];
  225. Gfx::IntRect dither_rect = rect;
  226. painter.fill_rect(dither_rect.take_from_right(1), Color::Black);
  227. painter.fill_rect(dither_rect.take_from_bottom(1), Color::Black);
  228. painter.fill_rect_with_dither_pattern(dither_rect, color, Color::Black);
  229. }
  230. });
  231. }
  232. }
  233. Vector<int> TreeMapWidget::path_to_position(const Gfx::IntPoint& position)
  234. {
  235. const TreeMapNode* node = path_node(m_viewpoint);
  236. if (!node) {
  237. return {};
  238. }
  239. Vector<int> path;
  240. lay_out_children(*node, frame_inner_rect(), m_viewpoint, [&](const TreeMapNode&, int index, const Gfx::IntRect& rect, const Gfx::IntRect&, int, HasLabel, IsRemainder is_remainder) {
  241. if (is_remainder == IsRemainder::No && rect.contains(position)) {
  242. path.append(index);
  243. }
  244. });
  245. return path;
  246. }
  247. void TreeMapWidget::mousedown_event(GUI::MouseEvent& event)
  248. {
  249. const TreeMapNode* node = path_node(m_viewpoint);
  250. if (node && !node_is_leaf(*node)) {
  251. Vector<int> path = path_to_position(event.position());
  252. if (!path.is_empty()) {
  253. m_path.shrink(m_viewpoint);
  254. m_path.extend(path);
  255. if (on_path_change) {
  256. on_path_change();
  257. }
  258. update();
  259. }
  260. }
  261. }
  262. void TreeMapWidget::doubleclick_event(GUI::MouseEvent& event)
  263. {
  264. if (event.button() != GUI::MouseButton::Primary)
  265. return;
  266. const TreeMapNode* node = path_node(m_viewpoint);
  267. if (node && !node_is_leaf(*node)) {
  268. Vector<int> path = path_to_position(event.position());
  269. m_path.shrink(m_viewpoint);
  270. m_path.extend(path);
  271. m_viewpoint = m_path.size();
  272. if (on_path_change) {
  273. on_path_change();
  274. }
  275. update();
  276. }
  277. }
  278. void TreeMapWidget::mousewheel_event(GUI::MouseEvent& event)
  279. {
  280. int delta = event.wheel_delta_y();
  281. // FIXME: The wheel_delta_y is premultiplied in the window server, we actually want a raw value here.
  282. int step_size = GUI::ConnectionToWindowServer::the().get_scroll_step_size();
  283. if (delta > 0) {
  284. size_t step_back = delta / step_size;
  285. if (step_back > m_viewpoint)
  286. step_back = m_viewpoint;
  287. set_viewpoint(m_viewpoint - step_back);
  288. } else {
  289. size_t step_up = (-delta) / step_size;
  290. set_viewpoint(m_viewpoint + step_up);
  291. }
  292. }
  293. void TreeMapWidget::context_menu_event(GUI::ContextMenuEvent& context_menu_event)
  294. {
  295. if (on_context_menu_request)
  296. on_context_menu_request(context_menu_event);
  297. }
  298. void TreeMapWidget::set_tree(RefPtr<TreeMap> tree)
  299. {
  300. m_tree = tree;
  301. m_path.clear();
  302. m_viewpoint = 0;
  303. if (on_path_change) {
  304. on_path_change();
  305. }
  306. update();
  307. }
  308. void TreeMapWidget::set_viewpoint(size_t viewpoint)
  309. {
  310. if (viewpoint > m_path.size())
  311. viewpoint = m_path.size();
  312. m_viewpoint = viewpoint;
  313. if (on_path_change) {
  314. on_path_change();
  315. }
  316. update();
  317. }
  318. size_t TreeMapWidget::path_size() const
  319. {
  320. return m_path.size() + 1;
  321. }
  322. size_t TreeMapWidget::viewpoint() const
  323. {
  324. return m_viewpoint;
  325. }
  326. }