TreeMapWidget.cpp 14 KB

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