From 808c633068bb29b0fe55d447da93525510e6b10c Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Fri, 23 Jun 2023 18:05:02 -0700 Subject: [PATCH] libsysprof-gtk: start on new XY series This will extract the values using the expressions instead of manually building series as we previously would need to do. --- .../sysprof-xy-series-item-private.h | 33 +++ src/libsysprof-gtk/sysprof-xy-series-item.c | 142 ++++++++++ src/libsysprof-gtk/sysprof-xy-series-item.h | 43 +++ src/libsysprof-gtk/sysprof-xy-series.c | 246 ++++++++++++++++++ src/libsysprof-gtk/sysprof-xy-series.h | 50 ++++ 5 files changed, 514 insertions(+) create mode 100644 src/libsysprof-gtk/sysprof-xy-series-item-private.h create mode 100644 src/libsysprof-gtk/sysprof-xy-series-item.c create mode 100644 src/libsysprof-gtk/sysprof-xy-series-item.h create mode 100644 src/libsysprof-gtk/sysprof-xy-series.c create mode 100644 src/libsysprof-gtk/sysprof-xy-series.h diff --git a/src/libsysprof-gtk/sysprof-xy-series-item-private.h b/src/libsysprof-gtk/sysprof-xy-series-item-private.h new file mode 100644 index 00000000..be99977c --- /dev/null +++ b/src/libsysprof-gtk/sysprof-xy-series-item-private.h @@ -0,0 +1,33 @@ +/* sysprof-xy-series-item-private.h + * + * Copyright 2023 Christian Hergert + * + * 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +#include "sysprof-xy-series-item.h" + +G_BEGIN_DECLS + +SysprofXYSeriesItem *_sysprof_xy_series_item_new (GObject *item, + GtkExpression *x_expression, + GtkExpression *y_expression); + +G_END_DECLS diff --git a/src/libsysprof-gtk/sysprof-xy-series-item.c b/src/libsysprof-gtk/sysprof-xy-series-item.c new file mode 100644 index 00000000..87b4c5d2 --- /dev/null +++ b/src/libsysprof-gtk/sysprof-xy-series-item.c @@ -0,0 +1,142 @@ +/* sysprof-xy-series-item.c + * + * Copyright 2023 Christian Hergert + * + * 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include "sysprof-xy-series-item-private.h" + +struct _SysprofXYSeriesItem +{ + GObject parent_instance; + GtkExpression *x_expression; + GtkExpression *y_expression; + GObject *item; +}; + +enum { + PROP_0, + PROP_ITEM, + N_PROPS +}; + +G_DEFINE_FINAL_TYPE (SysprofXYSeriesItem, sysprof_xy_series_item, G_TYPE_OBJECT) + +static GParamSpec *properties [N_PROPS]; + +static void +sysprof_xy_series_item_finalize (GObject *object) +{ + SysprofXYSeriesItem *self = (SysprofXYSeriesItem *)object; + + g_clear_object (&self->item); + g_clear_pointer (&self->x_expression, gtk_expression_unref); + g_clear_pointer (&self->y_expression, gtk_expression_unref); + + G_OBJECT_CLASS (sysprof_xy_series_item_parent_class)->finalize (object); +} + +static void +sysprof_xy_series_item_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SysprofXYSeriesItem *self = SYSPROF_XY_SERIES_ITEM (object); + + switch (prop_id) + { + case PROP_ITEM: + g_value_set_object (value, self->item); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_xy_series_item_class_init (SysprofXYSeriesItemClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = sysprof_xy_series_item_finalize; + object_class->get_property = sysprof_xy_series_item_get_property; + + properties [PROP_ITEM] = + g_param_spec_object ("item", NULL, NULL, + G_TYPE_OBJECT, + (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, N_PROPS, properties); +} + +static void +sysprof_xy_series_item_init (SysprofXYSeriesItem *self) +{ +} + +SysprofXYSeriesItem * +_sysprof_xy_series_item_new (GObject *item, + GtkExpression *x_expression, + GtkExpression *y_expression) +{ + SysprofXYSeriesItem *self; + + self = g_object_new (SYSPROF_TYPE_XY_SERIES_ITEM, NULL); + self->item = item; + self->x_expression = x_expression; + self->y_expression = y_expression; + + return self; +} + +/** + * sysprof_xy_series_item_get_item: + * @self: a #SysprofXYSeriesItem + * + * Gets the item used to generate X/Y values. + * + * Returns: (transfer none): a #GObject + */ +gpointer +sysprof_xy_series_item_get_item (SysprofXYSeriesItem *self) +{ + g_return_val_if_fail (SYSPROF_IS_XY_SERIES_ITEM (self), NULL); + + return self->item; +} + +void +sysprof_xy_series_item_get_x_value (SysprofXYSeriesItem *self, + GValue *value) +{ + g_return_if_fail (SYSPROF_IS_XY_SERIES_ITEM (self)); + + gtk_expression_evaluate (self->x_expression, self->item, value); +} + +void +sysprof_xy_series_item_get_y_value (SysprofXYSeriesItem *self, + GValue *value) +{ + g_return_if_fail (SYSPROF_IS_XY_SERIES_ITEM (self)); + + gtk_expression_evaluate (self->y_expression, self->item, value); +} diff --git a/src/libsysprof-gtk/sysprof-xy-series-item.h b/src/libsysprof-gtk/sysprof-xy-series-item.h new file mode 100644 index 00000000..a994ea04 --- /dev/null +++ b/src/libsysprof-gtk/sysprof-xy-series-item.h @@ -0,0 +1,43 @@ +/* sysprof-xy-series-item.h + * + * Copyright 2023 Christian Hergert + * + * 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +#include + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_XY_SERIES_ITEM (sysprof_xy_series_item_get_type()) + +SYSPROF_AVAILABLE_IN_ALL +G_DECLARE_FINAL_TYPE (SysprofXYSeriesItem, sysprof_xy_series_item, SYSPROF, XY_SERIES_ITEM, GObject) + +SYSPROF_AVAILABLE_IN_ALL +void sysprof_xy_series_item_get_x_value (SysprofXYSeriesItem *self, + GValue *x_value); +SYSPROF_AVAILABLE_IN_ALL +void sysprof_xy_series_item_get_y_value (SysprofXYSeriesItem *self, + GValue *y_value); +SYSPROF_AVAILABLE_IN_ALL +gpointer sysprof_xy_series_item_get_item (SysprofXYSeriesItem *self); + +G_END_DECLS diff --git a/src/libsysprof-gtk/sysprof-xy-series.c b/src/libsysprof-gtk/sysprof-xy-series.c new file mode 100644 index 00000000..c1fa358a --- /dev/null +++ b/src/libsysprof-gtk/sysprof-xy-series.c @@ -0,0 +1,246 @@ +/* sysprof-xy-series.c + * + * Copyright 2023 Christian Hergert + * + * 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include "sysprof-xy-series.h" +#include "sysprof-xy-series-item-private.h" + +struct _SysprofXYSeries +{ + SysprofSeries parent_instance; + GtkExpression *x_expression; + GtkExpression *y_expression; +}; + +enum { + PROP_0, + PROP_X_EXPRESSION, + PROP_Y_EXPRESSION, + N_PROPS +}; + +G_DEFINE_FINAL_TYPE (SysprofXYSeries, sysprof_xy_series, SYSPROF_TYPE_SERIES) + +static GParamSpec *properties [N_PROPS]; + +static gpointer +sysprof_xy_series_get_series_item (SysprofXYSeries *self, + gpointer item) +{ + return _sysprof_xy_series_item_new (item, + gtk_expression_ref (self->x_expression), + gtk_expression_ref (self->y_expression)); +} + +static void +sysprof_xy_series_finalize (GObject *object) +{ + SysprofXYSeries *self = (SysprofXYSeries *)object; + + g_clear_pointer (&self->x_expression, gtk_expression_unref); + g_clear_pointer (&self->y_expression, gtk_expression_unref); + + G_OBJECT_CLASS (sysprof_xy_series_parent_class)->finalize (object); +} + +static void +sysprof_xy_series_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SysprofXYSeries *self = SYSPROF_XY_SERIES (object); + + switch (prop_id) + { + case PROP_X_EXPRESSION: + gtk_value_set_expression (value, self->x_expression); + break; + + case PROP_Y_EXPRESSION: + gtk_value_set_expression (value, self->y_expression); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_xy_series_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + SysprofXYSeries *self = SYSPROF_XY_SERIES (object); + + switch (prop_id) + { + case PROP_X_EXPRESSION: + sysprof_xy_series_set_x_expression (self, gtk_value_get_expression (value)); + break; + + case PROP_Y_EXPRESSION: + sysprof_xy_series_set_y_expression (self, gtk_value_get_expression (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_xy_series_class_init (SysprofXYSeriesClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + SysprofSeriesClass *series_class = SYSPROF_SERIES_CLASS (klass); + + object_class->finalize = sysprof_xy_series_finalize; + object_class->get_property = sysprof_xy_series_get_property; + object_class->set_property = sysprof_xy_series_set_property; + + series_class->series_item_type = SYSPROF_TYPE_XY_SERIES_ITEM; + series_class->get_series_item = sysprof_xy_series_get_series_item; + + properties [PROP_X_EXPRESSION] = + gtk_param_spec_expression ("x-expression", NULL, NULL, + (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); + + properties [PROP_Y_EXPRESSION] = + gtk_param_spec_expression ("y-expression", NULL, NULL, + (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, N_PROPS, properties); +} + +static void +sysprof_xy_series_init (SysprofXYSeries *self) +{ +} + +/** + * sysprof_xy_series_new: + * @title: (nullable): a title for the series + * @model: (transfer full) (nullable): a #GListModel for the series + * @x_expression: (transfer full) (nullable): a #GtkExpression for + * extracting the X value from @model items. + * @y_expression: (transfer full) (nullable): a #GtkExpression for + * extracting the Y value from @model items. + * + * A #SysprofSeries which contains X,Y value pairs. + * + * Returns: (transfer full): a #SysprofSeries + */ +SysprofSeries * +sysprof_xy_series_new (const char *title, + GListModel *model, + GtkExpression *x_expression, + GtkExpression *y_expression) +{ + SysprofXYSeries *xy; + + xy = g_object_new (SYSPROF_TYPE_XY_SERIES, + "title", title, + "model", model, + "x-expression", x_expression, + "y-expression", y_expression, + NULL); + + g_clear_pointer (&x_expression, gtk_expression_unref); + g_clear_pointer (&y_expression, gtk_expression_unref); + g_clear_object (&model); + + return SYSPROF_SERIES (xy); +} + +/** + * sysprof_xy_series_get_x_expression: + * @self: a #SysprofXYSeries + * + * Gets the #SysprofXYSeries:x-expression property. + * + * This is used to extract the X coordinate from items in the #GListModel. + * + * Returns: (transfer none) (nullable): a #GtkExpression or %NULL + */ +GtkExpression * +sysprof_xy_series_get_x_expression (SysprofXYSeries *self) +{ + g_return_val_if_fail (SYSPROF_IS_XY_SERIES (self), NULL); + + return self->x_expression; +} + +void +sysprof_xy_series_set_x_expression (SysprofXYSeries *self, + GtkExpression *x_expression) +{ + g_return_if_fail (SYSPROF_IS_XY_SERIES (self)); + + if (self->x_expression == x_expression) + return; + + if (x_expression) + gtk_expression_ref (x_expression); + + g_clear_pointer (&self->x_expression, gtk_expression_unref); + + self->x_expression = x_expression; + + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_X_EXPRESSION]); +} + +/** + * sysprof_xy_series_get_y_expression: + * @self: a #SysprofXYSeries + * + * Gets the #SysprofXYSeries:y-expression property. + * + * This is used to extract the Y coordinate from items in the #GListModel. + * + * Returns: (transfer none) (nullable): a #GtkExpression or %NULL + */ +GtkExpression * +sysprof_xy_series_get_y_expression (SysprofXYSeries *self) +{ + g_return_val_if_fail (SYSPROF_IS_XY_SERIES (self), NULL); + + return self->y_expression; +} + +void +sysprof_xy_series_set_y_expression (SysprofXYSeries *self, + GtkExpression *x_expression) +{ + g_return_if_fail (SYSPROF_IS_XY_SERIES (self)); + + if (self->x_expression == x_expression) + return; + + if (x_expression) + gtk_expression_ref (x_expression); + + g_clear_pointer (&self->x_expression, gtk_expression_unref); + + self->x_expression = x_expression; + + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_X_EXPRESSION]); +} diff --git a/src/libsysprof-gtk/sysprof-xy-series.h b/src/libsysprof-gtk/sysprof-xy-series.h new file mode 100644 index 00000000..42906b59 --- /dev/null +++ b/src/libsysprof-gtk/sysprof-xy-series.h @@ -0,0 +1,50 @@ +/* sysprof-xy-series.h + * + * Copyright 2023 Christian Hergert + * + * 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +#include "sysprof-series.h" + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_XY_SERIES (sysprof_xy_series_get_type()) + +SYSPROF_AVAILABLE_IN_ALL +G_DECLARE_FINAL_TYPE (SysprofXYSeries, sysprof_xy_series, SYSPROF, XY_SERIES, SysprofSeries) + +SYSPROF_AVAILABLE_IN_ALL +SysprofSeries *sysprof_xy_series_new (const char *title, + GListModel *model, + GtkExpression *x_expression, + GtkExpression *y_expression); +SYSPROF_AVAILABLE_IN_ALL +GtkExpression *sysprof_xy_series_get_x_expression (SysprofXYSeries *self); +SYSPROF_AVAILABLE_IN_ALL +void sysprof_xy_series_set_x_expression (SysprofXYSeries *self, + GtkExpression *x_expression); +SYSPROF_AVAILABLE_IN_ALL +GtkExpression *sysprof_xy_series_get_y_expression (SysprofXYSeries *self); +SYSPROF_AVAILABLE_IN_ALL +void sysprof_xy_series_set_y_expression (SysprofXYSeries *self, + GtkExpression *y_expression); + +G_END_DECLS