TreeMapWidget.cpp 14 KB

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