GraphWidget.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "GraphWidget.h"
  8. #include <LibCore/Object.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/Painter.h>
  11. #include <LibGfx/Font/Font.h>
  12. #include <LibGfx/Palette.h>
  13. #include <LibGfx/Path.h>
  14. #include <LibGfx/SystemTheme.h>
  15. REGISTER_WIDGET(SystemMonitor, GraphWidget)
  16. namespace SystemMonitor {
  17. GraphWidget::GraphWidget()
  18. {
  19. REGISTER_BOOL_PROPERTY("stack_values", stack_values, set_stack_values);
  20. }
  21. void GraphWidget::set_stack_values(bool stack_values)
  22. {
  23. m_stack_values = stack_values;
  24. update();
  25. }
  26. void GraphWidget::add_value(Vector<u64, 1>&& value)
  27. {
  28. m_values.enqueue(move(value));
  29. update();
  30. }
  31. void GraphWidget::paint_event(GUI::PaintEvent& event)
  32. {
  33. auto const& system_palette = GUI::Application::the()->palette();
  34. GUI::Frame::paint_event(event);
  35. GUI::Painter painter(*this);
  36. painter.add_clip_rect(event.rect());
  37. painter.add_clip_rect(frame_inner_rect());
  38. painter.fill_rect(event.rect(), palette().base());
  39. auto inner_rect = frame_inner_rect();
  40. float scale = (float)inner_rect.height() / (float)m_max;
  41. if (!m_values.is_empty()) {
  42. // Draw one set of values at a time
  43. for (size_t k = 0; k < m_value_format.size(); k++) {
  44. auto const& format = m_value_format[k];
  45. if (format.graph_color_role == ColorRole::Base) {
  46. continue;
  47. }
  48. auto const& line_color = system_palette.color(format.graph_color_role);
  49. auto const& background_color = line_color.with_alpha(0x7f);
  50. m_calculated_points.clear_with_capacity();
  51. for (size_t i = 0; i < m_values.size(); i++) {
  52. int x = inner_rect.right() - i * 2;
  53. if (x < 0)
  54. break;
  55. auto const& current_values = m_values.at(m_values.size() - i - 1);
  56. if (current_values.size() <= k) {
  57. // Don't have a data point
  58. m_calculated_points.append({ -1, -1 });
  59. continue;
  60. }
  61. float value = current_values[k];
  62. if (m_stack_values) {
  63. for (size_t l = k + 1; l < current_values.size(); l++)
  64. value += current_values[l];
  65. }
  66. float scaled_value = value * scale;
  67. Gfx::IntPoint current_point { x, inner_rect.bottom() - 1 - (int)scaled_value };
  68. m_calculated_points.append(current_point);
  69. }
  70. VERIFY(m_calculated_points.size() <= m_values.size());
  71. if (format.graph_color_role != ColorRole::Base) {
  72. // Fill the background for the area we have values for
  73. Gfx::Path path;
  74. size_t points_in_path = 0;
  75. bool started_path = false;
  76. Gfx::IntPoint const* current_point = nullptr;
  77. Gfx::IntPoint const* first_point = nullptr;
  78. auto check_fill_area = [&]() {
  79. if (!started_path)
  80. return;
  81. if (points_in_path > 1) {
  82. VERIFY(current_point);
  83. VERIFY(first_point);
  84. path.line_to({ current_point->x() - 1, inner_rect.bottom() });
  85. path.line_to({ first_point->x() + 1, inner_rect.bottom() });
  86. path.close();
  87. painter.fill_path(path, background_color, Gfx::Painter::WindingRule::EvenOdd);
  88. } else if (points_in_path == 1 && current_point) {
  89. // Can't fill any area, we only have one data point.
  90. // Just draw a vertical line as a "fill"...
  91. painter.draw_line(*current_point, { current_point->x(), inner_rect.bottom() - 1 }, background_color);
  92. }
  93. path = {};
  94. points_in_path = 0;
  95. first_point = nullptr;
  96. started_path = false;
  97. };
  98. for (size_t i = 0; i < m_calculated_points.size(); i++) {
  99. current_point = &m_calculated_points[i];
  100. if (current_point->x() < 0) {
  101. check_fill_area();
  102. continue;
  103. }
  104. if (!started_path) {
  105. path.move_to({ current_point->x() + 1, current_point->y() });
  106. points_in_path = 1;
  107. first_point = current_point;
  108. started_path = true;
  109. } else {
  110. path.line_to({ current_point->x(), current_point->y() });
  111. points_in_path++;
  112. }
  113. }
  114. check_fill_area();
  115. }
  116. if (format.graph_color_role != ColorRole::Base) {
  117. // Draw the line for the data points we have
  118. Gfx::IntPoint const* previous_point = nullptr;
  119. for (size_t i = 0; i < m_calculated_points.size(); i++) {
  120. auto const& current_point = m_calculated_points[i];
  121. if (current_point.x() < 0) {
  122. previous_point = nullptr;
  123. continue;
  124. }
  125. if (previous_point)
  126. painter.draw_line(*previous_point, current_point, line_color);
  127. previous_point = &current_point;
  128. }
  129. }
  130. }
  131. }
  132. if (!m_values.is_empty() && !m_value_format.is_empty()) {
  133. auto const& current_values = m_values.last();
  134. int y = 0;
  135. for (size_t i = 0; i < min(m_value_format.size(), current_values.size()); i++) {
  136. auto const& format = m_value_format[i];
  137. auto const& graph_color = system_palette.color(format.graph_color_role);
  138. if (!format.text_formatter)
  139. continue;
  140. auto constrain_rect = inner_rect.shrunken(8, 8);
  141. auto text_rect = constrain_rect.translated(0, y).intersected(constrain_rect);
  142. text_rect.set_height(font().pixel_size_rounded_up());
  143. auto text = format.text_formatter(current_values[i]);
  144. if (format.text_shadow_color != Color::Transparent)
  145. painter.draw_text(text_rect.translated(1, 1), text, Gfx::TextAlignment::CenterRight, format.text_shadow_color);
  146. painter.draw_text(text_rect, text, Gfx::TextAlignment::CenterRight, graph_color);
  147. y += text_rect.height() + 4;
  148. }
  149. }
  150. }
  151. }