GraphWidget.cpp 6.4 KB

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