Files
sysprof/src/libsysprof-gtk/sysprof-column-layer.c
2023-06-26 16:39:57 -07:00

325 lines
9.0 KiB
C

/* sysprof-column-layer.c
*
* Copyright 2023 Christian Hergert <chergert@redhat.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "config.h"
#include "sysprof-axis.h"
#include "sysprof-column-layer.h"
#include "sysprof-normalized-series.h"
#include "sysprof-xy-layer-private.h"
struct _SysprofColumnLayer
{
SysprofXYLayer parent_instance;
GdkRGBA color;
GdkRGBA hover_color;
};
enum {
PROP_0,
PROP_COLOR,
PROP_HOVER_COLOR,
N_PROPS
};
G_DEFINE_FINAL_TYPE (SysprofColumnLayer, sysprof_column_layer, SYSPROF_TYPE_XY_LAYER)
static GParamSpec *properties [N_PROPS];
static void
sysprof_column_layer_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
{
SysprofColumnLayer *self = (SysprofColumnLayer *)widget;
graphene_matrix_t flip_y;
const float *x_values;
const float *y_values;
guint n_values;
int width;
int height;
g_assert (SYSPROF_IS_COLUMN_LAYER (self));
g_assert (GTK_IS_SNAPSHOT (snapshot));
width = gtk_widget_get_width (widget);
height = gtk_widget_get_height (widget);
_sysprof_xy_layer_get_xy (SYSPROF_XY_LAYER (self), &x_values, &y_values, &n_values);
if (width == 0 || height == 0 || n_values == 0 || self->color.alpha == 0)
return;
gtk_snapshot_save (snapshot);
graphene_matrix_init_from_2d (&flip_y, 1, 0, 0, -1, 0, height);
gtk_snapshot_transform_matrix (snapshot, &flip_y);
for (guint i = 0; i < n_values; i++)
{
int line_height = ceilf (y_values[i] * height);
gtk_snapshot_append_color (snapshot,
&self->color,
&GRAPHENE_RECT_INIT (x_values[i] * width,
0,
1,
line_height));
}
gtk_snapshot_restore (snapshot);
}
#if 0
static const SysprofXYSeriesValue *
sysprof_column_layer_get_value_at_coord (SysprofColumnLayer *self,
double x,
double y,
graphene_rect_t *area)
{
const SysprofXYSeriesValue *values;
graphene_point_t point;
double min_x, max_x;
double min_y, max_y;
guint line_width;
guint n_values;
int width;
int height;
g_assert (SYSPROF_IS_COLUMN_LAYER (self));
width = gtk_widget_get_width (GTK_WIDGET (self));
height = gtk_widget_get_height (GTK_WIDGET (self));
if (width == 0 || height == 0)
return NULL;
if (self->series == NULL ||
!(values = sysprof_xy_series_get_values (self->series, &n_values)))
return NULL;
point = GRAPHENE_POINT_INIT (x, y);
sysprof_xy_series_get_range (self->series, &min_x, &min_y, &max_x, &max_y);
line_width = MAX (1, width / (max_x - min_x));
for (guint i = 0; i < n_values; i++)
{
const SysprofXYSeriesValue *v = &values[i];
int line_height = ceilf (v->y * height);
graphene_rect_t rect = GRAPHENE_RECT_INIT (v->x * width,
height - line_height,
line_width,
line_height);
if (graphene_rect_contains_point (&rect, &point))
{
if (area != NULL)
*area = rect;
return v;
}
}
return NULL;
}
#endif
static void
sysprof_column_layer_snapshot_motion (SysprofChartLayer *layer,
GtkSnapshot *snapshot,
double x,
double y)
{
#if 0
SysprofColumnLayer *self = (SysprofColumnLayer *)layer;
const SysprofXYSeriesValue *v;
graphene_rect_t rect;
g_assert (SYSPROF_IS_COLUMN_LAYER (self));
g_assert (GTK_IS_SNAPSHOT (snapshot));
if ((v = sysprof_column_layer_get_value_at_coord (self, x, y, &rect)))
gtk_snapshot_append_color (snapshot, &self->hover_color, &rect);
#endif
}
static gpointer
sysprof_column_layer_lookup_item (SysprofChartLayer *layer,
double x,
double y)
{
#if 0
SysprofColumnLayer *self = (SysprofColumnLayer *)layer;
const SysprofXYSeriesValue *v;
g_assert (SYSPROF_IS_COLUMN_LAYER (self));
if ((v = sysprof_column_layer_get_value_at_coord (self, x, y, NULL)))
return g_list_model_get_item (sysprof_xy_series_get_model (self->series), v->index);
#endif
return NULL;
}
static void
sysprof_column_layer_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
SysprofColumnLayer *self = SYSPROF_COLUMN_LAYER (object);
switch (prop_id)
{
case PROP_COLOR:
g_value_set_boxed (value, &self->color);
break;
case PROP_HOVER_COLOR:
g_value_set_boxed (value, &self->hover_color);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
sysprof_column_layer_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
SysprofColumnLayer *self = SYSPROF_COLUMN_LAYER (object);
switch (prop_id)
{
case PROP_COLOR:
sysprof_column_layer_set_color (self, g_value_get_boxed (value));
break;
case PROP_HOVER_COLOR:
sysprof_column_layer_set_hover_color (self, g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
sysprof_column_layer_class_init (SysprofColumnLayerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
SysprofChartLayerClass *chart_layer_class = SYSPROF_CHART_LAYER_CLASS (klass);
object_class->get_property = sysprof_column_layer_get_property;
object_class->set_property = sysprof_column_layer_set_property;
widget_class->snapshot = sysprof_column_layer_snapshot;
chart_layer_class->lookup_item = sysprof_column_layer_lookup_item;
chart_layer_class->snapshot_motion = sysprof_column_layer_snapshot_motion;
properties[PROP_COLOR] =
g_param_spec_boxed ("color", NULL, NULL,
GDK_TYPE_RGBA,
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
properties[PROP_HOVER_COLOR] =
g_param_spec_boxed ("hover-color", NULL, NULL,
GDK_TYPE_RGBA,
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
sysprof_column_layer_init (SysprofColumnLayer *self)
{
gdk_rgba_parse (&self->color, "#000");
gdk_rgba_parse (&self->hover_color, "#F00");
}
SysprofChartLayer *
sysprof_column_layer_new (void)
{
return g_object_new (SYSPROF_TYPE_COLUMN_LAYER, NULL);
}
const GdkRGBA *
sysprof_column_layer_get_color (SysprofColumnLayer *self)
{
g_return_val_if_fail (SYSPROF_IS_COLUMN_LAYER (self), NULL);
return &self->color;
}
void
sysprof_column_layer_set_color (SysprofColumnLayer *self,
const GdkRGBA *color)
{
static const GdkRGBA black = {0,0,0,1};
g_return_if_fail (SYSPROF_IS_COLUMN_LAYER (self));
if (color == NULL)
color = &black;
if (!gdk_rgba_equal (&self->color, color))
{
self->color = *color;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COLOR]);
gtk_widget_queue_draw (GTK_WIDGET (self));
}
}
const GdkRGBA *
sysprof_column_layer_get_hover_color (SysprofColumnLayer *self)
{
g_return_val_if_fail (SYSPROF_IS_COLUMN_LAYER (self), NULL);
return &self->hover_color;
}
void
sysprof_column_layer_set_hover_color (SysprofColumnLayer *self,
const GdkRGBA *hover_color)
{
static const GdkRGBA red = {1,0,0,1};
g_return_if_fail (SYSPROF_IS_COLUMN_LAYER (self));
if (hover_color == NULL)
hover_color = &red;
if (!gdk_rgba_equal (&self->hover_color, hover_color))
{
self->hover_color = *hover_color;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_HOVER_COLOR]);
gtk_widget_queue_draw (GTK_WIDGET (self));
}
}