TreeMapWidget.cpp 14 KB

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