2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-05-06 04:21:23 +03:00
|
|
|
#include "GraphWidget.h"
|
2021-02-14 12:04:38 +03:00
|
|
|
#include <LibGUI/Application.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-15 01:53:11 +03:00
|
|
|
#include <LibGfx/Font.h>
|
2021-02-06 07:05:54 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2021-01-05 06:14:52 +03:00
|
|
|
#include <LibGfx/Path.h>
|
2021-02-06 07:05:54 +03:00
|
|
|
#include <LibGfx/SystemTheme.h>
|
2019-05-06 04:21:23 +03:00
|
|
|
|
2020-02-23 12:57:42 +03:00
|
|
|
GraphWidget::GraphWidget()
|
2019-05-06 04:21:23 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphWidget::~GraphWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-05 06:14:52 +03:00
|
|
|
void GraphWidget::add_value(Vector<int, 1>&& value)
|
2019-05-06 04:21:23 +03:00
|
|
|
{
|
2021-01-05 06:14:52 +03:00
|
|
|
m_values.enqueue(move(value));
|
2019-05-06 04:21:23 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void GraphWidget::paint_event(GUI::PaintEvent& event)
|
2019-05-06 04:21:23 +03:00
|
|
|
{
|
2021-02-14 12:04:38 +03:00
|
|
|
const auto& system_palette = GUI::Application::the()->palette();
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::Frame::paint_event(event);
|
|
|
|
GUI::Painter painter(*this);
|
2019-05-06 04:21:23 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
painter.add_clip_rect(frame_inner_rect());
|
2021-02-06 07:05:54 +03:00
|
|
|
painter.fill_rect(event.rect(), palette().base());
|
2019-05-06 04:21:23 +03:00
|
|
|
|
|
|
|
auto inner_rect = frame_inner_rect();
|
|
|
|
float scale = (float)inner_rect.height() / (float)m_max;
|
|
|
|
|
2021-01-05 06:14:52 +03:00
|
|
|
if (!m_values.is_empty()) {
|
|
|
|
// Draw one set of values at a time
|
|
|
|
for (size_t k = 0; k < m_value_format.size(); k++) {
|
|
|
|
const auto& format = m_value_format[k];
|
2021-02-14 12:04:38 +03:00
|
|
|
if (format.graph_color_role == ColorRole::Base) {
|
2021-01-05 06:14:52 +03:00
|
|
|
continue;
|
2021-02-14 12:04:38 +03:00
|
|
|
}
|
|
|
|
const auto& line_color = system_palette.color(format.graph_color_role);
|
|
|
|
const auto& background_color = line_color.with_alpha(0x7f);
|
2021-01-05 06:14:52 +03:00
|
|
|
m_calculated_points.clear_with_capacity();
|
|
|
|
for (size_t i = 0; i < m_values.size(); i++) {
|
|
|
|
int x = inner_rect.right() - (i * 2) + 1;
|
|
|
|
if (x < 0)
|
|
|
|
break;
|
|
|
|
const auto& current_values = m_values.at(m_values.size() - i - 1);
|
|
|
|
if (current_values.size() <= k) {
|
|
|
|
// Don't have a data point
|
|
|
|
m_calculated_points.append({ -1, -1 });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
float value = current_values[k];
|
|
|
|
if (m_stack_values) {
|
|
|
|
for (size_t l = k + 1; l < current_values.size(); l++)
|
|
|
|
value += current_values[l];
|
|
|
|
}
|
|
|
|
float scaled_value = value * scale;
|
|
|
|
Gfx::IntPoint current_point { x, inner_rect.bottom() - (int)scaled_value };
|
|
|
|
m_calculated_points.append(current_point);
|
|
|
|
}
|
|
|
|
ASSERT(m_calculated_points.size() <= m_values.size());
|
2021-02-14 12:04:38 +03:00
|
|
|
if (format.graph_color_role != ColorRole::Base) {
|
2021-01-05 06:14:52 +03:00
|
|
|
// Fill the background for the area we have values for
|
|
|
|
Gfx::Path path;
|
|
|
|
size_t points_in_path = 0;
|
|
|
|
bool started_path = false;
|
|
|
|
const Gfx::IntPoint* current_point = nullptr;
|
|
|
|
const Gfx::IntPoint* first_point = nullptr;
|
|
|
|
auto check_fill_area = [&]() {
|
|
|
|
if (!started_path)
|
|
|
|
return;
|
|
|
|
if (points_in_path > 1) {
|
|
|
|
ASSERT(current_point);
|
|
|
|
ASSERT(first_point);
|
|
|
|
path.line_to({ current_point->x() - 1, inner_rect.bottom() + 1 });
|
|
|
|
path.line_to({ first_point->x() + 1, inner_rect.bottom() + 1 });
|
|
|
|
path.close();
|
2021-02-14 12:04:38 +03:00
|
|
|
painter.fill_path(path, background_color, Gfx::Painter::WindingRule::EvenOdd);
|
2021-01-05 06:14:52 +03:00
|
|
|
} else if (points_in_path == 1 && current_point) {
|
|
|
|
// Can't fill any area, we only have one data point.
|
|
|
|
// Just draw a vertical line as a "fill"...
|
2021-02-14 12:04:38 +03:00
|
|
|
painter.draw_line(*current_point, { current_point->x(), inner_rect.bottom() }, background_color);
|
2021-01-05 06:14:52 +03:00
|
|
|
}
|
|
|
|
path = {};
|
|
|
|
points_in_path = 0;
|
|
|
|
first_point = nullptr;
|
|
|
|
started_path = false;
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < m_calculated_points.size(); i++) {
|
|
|
|
current_point = &m_calculated_points[i];
|
|
|
|
if (current_point->x() < 0) {
|
|
|
|
check_fill_area();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!started_path) {
|
|
|
|
path.move_to({ current_point->x() + 1, current_point->y() });
|
|
|
|
points_in_path = 1;
|
|
|
|
first_point = current_point;
|
|
|
|
started_path = true;
|
|
|
|
} else {
|
|
|
|
path.line_to({ current_point->x(), current_point->y() });
|
|
|
|
points_in_path++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check_fill_area();
|
|
|
|
}
|
2021-02-14 12:04:38 +03:00
|
|
|
if (format.graph_color_role != ColorRole::Base) {
|
2021-01-05 06:14:52 +03:00
|
|
|
// Draw the line for the data points we have
|
|
|
|
const Gfx::IntPoint* previous_point = nullptr;
|
|
|
|
for (size_t i = 0; i < m_calculated_points.size(); i++) {
|
|
|
|
const auto& current_point = m_calculated_points[i];
|
|
|
|
if (current_point.x() < 0) {
|
|
|
|
previous_point = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (previous_point)
|
2021-02-14 12:04:38 +03:00
|
|
|
painter.draw_line(*previous_point, current_point, line_color);
|
2021-01-05 06:14:52 +03:00
|
|
|
previous_point = ¤t_point;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 04:21:23 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 06:14:52 +03:00
|
|
|
if (!m_values.is_empty() && !m_value_format.is_empty()) {
|
|
|
|
const auto& current_values = m_values.last();
|
|
|
|
int y = 0;
|
|
|
|
for (size_t i = 0; i < min(m_value_format.size(), current_values.size()); i++) {
|
|
|
|
const auto& format = m_value_format[i];
|
2021-02-14 12:04:38 +03:00
|
|
|
const auto& graph_color = system_palette.color(format.graph_color_role);
|
2021-01-05 06:14:52 +03:00
|
|
|
if (!format.text_formatter)
|
|
|
|
continue;
|
|
|
|
auto constrain_rect = inner_rect.shrunken(8, 8);
|
|
|
|
auto text_rect = constrain_rect.translated(0, y).intersected(constrain_rect);
|
|
|
|
text_rect.set_height(font().glyph_height());
|
|
|
|
auto text = format.text_formatter(current_values[i]);
|
|
|
|
if (format.text_shadow_color != Color::Transparent)
|
|
|
|
painter.draw_text(text_rect.translated(1, 1), text.characters(), Gfx::TextAlignment::CenterRight, format.text_shadow_color);
|
2021-02-14 12:04:38 +03:00
|
|
|
painter.draw_text(text_rect, text.characters(), Gfx::TextAlignment::CenterRight, graph_color);
|
2021-01-05 06:14:52 +03:00
|
|
|
y += text_rect.height() + 4;
|
|
|
|
}
|
2019-05-06 04:21:23 +03:00
|
|
|
}
|
|
|
|
}
|