[gnome-system-monitor/wip-charts] Added code to set the stroke color



commit 61f06b9f49b0c6e5ce77da7fc58c9eb1f17b1033
Author: Robert Roth <robert roth off gmail com>
Date:   Fri Nov 13 10:13:27 2020 +0200

    Added code to set the stroke color

 src/application.cpp        |   6 +++
 src/charts/gsm-cpu-graph.c | 128 ++++++++++++++++++++-------------------------
 src/charts/gsm-cpu-graph.h |   6 ++-
 src/interface.cpp          |  11 +++-
 4 files changed, 76 insertions(+), 75 deletions(-)
---
diff --git a/src/application.cpp b/src/application.cpp
index b4519a90..eb6cd3fc 100644
--- a/src/application.cpp
+++ b/src/application.cpp
@@ -20,6 +20,8 @@
 #include "util.h"
 #include "lsof.h"
 #include "disks.h"
+#include "charts/gsm-cpu-graph.h"
+
 
 static void
 cb_solaris_mode_changed (Gio::Settings& settings, Glib::ustring key, GsmApplication* app)
@@ -164,6 +166,10 @@ cb_color_changed (Gio::Settings& settings, Glib::ustring key, GsmApplication* ap
     if (key == GSM_SETTING_CPU_COLORS) {
         apply_cpu_color_settings(settings, app);
         for (int i = 0; i < app->config.num_cpus; i++) {
+            gchar* cpu = g_strdup_printf ("cpu%d", i);
+
+            gsm_cpu_graph_renderer_set_color (GSM_CPU_GRAPH (app->cpu_graph), cpu, 
&app->config.cpu_color[i]);
+            g_free (cpu);
 //            if(!gdk_rgba_equal(&app->cpu_graph->colors[i], &app->config.cpu_color[i])) {
                 //app->cpu_graph->colors[i] = app->config.cpu_color[i];
 //                break;
diff --git a/src/charts/gsm-cpu-graph.c b/src/charts/gsm-cpu-graph.c
index 08d57ec4..01c2beaa 100644
--- a/src/charts/gsm-cpu-graph.c
+++ b/src/charts/gsm-cpu-graph.c
@@ -25,17 +25,15 @@
 struct _GsmCpuGraph
 {
   DzlGraphView parent_instance;
-
-  gint64 timespan;
-  guint  max_samples;
+  GHashTable*  renderers;
+  GsmCpuModel* model;
 };
 
 G_DEFINE_TYPE (GsmCpuGraph, gsm_cpu_graph, DZL_TYPE_GRAPH_VIEW)
 
 enum {
   PROP_0,
-  PROP_MAX_SAMPLES,
-  PROP_TIMESPAN,
+  PROP_MODEL,
   LAST_PROP
 };
 
@@ -52,29 +50,32 @@ static const gchar *colors[] = {
   "#cc0000",
 };
 
+static void
+_gsm_cpu_graph_initialize_with_model(GsmCpuGraph *self);
+
 GtkWidget *
-gsm_cpu_graph_new_full (gint64 timespan,
-                        guint  max_samples)
+gsm_cpu_graph_new_full (GsmCpuModel *cpu_model)
 {
-  if (timespan <= 0)
-    timespan = 60L * G_USEC_PER_SEC;
-
-  if (max_samples < 1)
-    max_samples = 120;
-
   return g_object_new (GSM_TYPE_CPU_GRAPH,
-                       "max-samples", max_samples,
-                       "timespan", timespan,
+                       "model", cpu_model,
                        NULL);
 }
 
+void
+gsm_cpu_graph_renderer_set_color (GsmCpuGraph *cpu_graph, gchar* renderer_name, GdkRGBA *color)
+{
+  DzlGraphRenderer *renderer = DZL_GRAPH_RENDERER (g_hash_table_lookup (cpu_graph->renderers, 
renderer_name));
+  if (renderer == NULL) return;
+  g_object_set (renderer,
+                "stroke-color-rgba", color,
+                "line-width", 0.5,
+                NULL);
+}
+
 static void
 gsm_cpu_graph_constructed (GObject *object)
 {
-  static GsmCpuModel *model;
   GsmCpuGraph *self = (GsmCpuGraph *)object;
-  guint n_columns;
-  guint i;
 
   G_OBJECT_CLASS (gsm_cpu_graph_parent_class)->constructed (object);
 
@@ -82,34 +83,16 @@ gsm_cpu_graph_constructed (GObject *object)
    * Create a model, but allow it to be destroyed after the last
    * graph releases it. We will recreate it on demand.
    */
-  if (model == NULL)
+  if (self->model == NULL)
     {
-      model = g_object_new (GSM_TYPE_CPU_MODEL,
-                            "timespan", self->timespan,
-                            "max-samples", self->max_samples + 1,
+      self->model = g_object_new (GSM_TYPE_CPU_MODEL,
+                            "timespan", 3*G_TIME_SPAN_MINUTE,
+                            "max-samples", 180 + 1,
                             NULL);
-      g_object_add_weak_pointer (G_OBJECT (model), (gpointer *)&model);
-      dzl_graph_view_set_model (DZL_GRAPH_VIEW (self), DZL_GRAPH_MODEL (model));
-      g_object_unref (model);
-    }
-  else
-    {
-      dzl_graph_view_set_model (DZL_GRAPH_VIEW (self), DZL_GRAPH_MODEL (model));
     }
 
-  n_columns = dzl_graph_view_model_get_n_columns (DZL_GRAPH_MODEL (model));
-
-  for (i = 0; i < n_columns; i++)
-    {
-      DzlGraphRenderer *renderer;
-
-      renderer = g_object_new (DZL_TYPE_GRAPH_LINE_RENDERER,
-                               "column", i,
-                               "stroke-color", colors [i % G_N_ELEMENTS (colors)],
-                               NULL);
-      dzl_graph_view_add_renderer (DZL_GRAPH_VIEW (self), renderer);
-      g_clear_object (&renderer);
-    }
+  self->renderers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
+  _gsm_cpu_graph_initialize_with_model(self);
 }
 
 static void
@@ -122,12 +105,8 @@ gsm_cpu_graph_get_property (GObject    *object,
 
   switch (prop_id)
     {
-    case PROP_MAX_SAMPLES:
-      g_value_set_uint (value, self->max_samples);
-      break;
-
-    case PROP_TIMESPAN:
-      g_value_set_int64 (value, self->timespan);
+    case PROP_MODEL:
+      g_value_set_object (value, self->model);
       break;
 
     default:
@@ -145,13 +124,9 @@ gsm_cpu_graph_set_property (GObject      *object,
 
   switch (prop_id)
     {
-    case PROP_MAX_SAMPLES:
-      self->max_samples = g_value_get_uint (value);
-      break;
-
-    case PROP_TIMESPAN:
-      if (!(self->timespan = g_value_get_int64 (value)))
-        self->timespan = 60L * G_USEC_PER_SEC;
+    case PROP_MODEL:
+      self->model = g_value_get_object (value);
+      _gsm_cpu_graph_initialize_with_model (self);
       break;
 
     default:
@@ -159,6 +134,7 @@ gsm_cpu_graph_set_property (GObject      *object,
     }
 }
 
+
 static void
 gsm_cpu_graph_class_init (GsmCpuGraphClass *klass)
 {
@@ -168,28 +144,38 @@ gsm_cpu_graph_class_init (GsmCpuGraphClass *klass)
   object_class->get_property = gsm_cpu_graph_get_property;
   object_class->set_property = gsm_cpu_graph_set_property;
 
-  properties [PROP_TIMESPAN] =
-    g_param_spec_int64 ("timespan",
-                         "Timespan",
-                         "Timespan",
-                         0, G_MAXINT64,
-                         60L * G_USEC_PER_SEC,
+  properties [PROP_MODEL] =
+    g_param_spec_object ("model",
+                         "Model",
+                         "Model",
+                         GSM_TYPE_CPU_MODEL,
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
-  properties [PROP_MAX_SAMPLES] =
-    g_param_spec_uint ("max-samples",
-                       "Max Samples",
-                       "Max Samples",
-                       0, G_MAXUINT,
-                       120,
-                       (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-
   g_object_class_install_properties (object_class, LAST_PROP, properties);
 }
 
 static void
 gsm_cpu_graph_init (GsmCpuGraph *self)
 {
-  self->max_samples = 120;
-  self->timespan = 60L * G_USEC_PER_SEC;
+}
+
+static void
+_gsm_cpu_graph_initialize_with_model(GsmCpuGraph *self)
+{
+  guint n_columns;
+  guint i;
+  dzl_graph_view_set_model (DZL_GRAPH_VIEW (self), DZL_GRAPH_MODEL (self->model));
+
+  n_columns = dzl_graph_view_model_get_n_columns (DZL_GRAPH_MODEL (self->model));
+  for (i = 0; i < n_columns; i++)
+    {
+      DzlGraphRenderer *renderer;
+
+      renderer = g_object_new (DZL_TYPE_GRAPH_LINE_RENDERER,
+                               "column", i,
+                               "stroke-color", colors [i % G_N_ELEMENTS (colors)],
+                               NULL);
+      dzl_graph_view_add_renderer (DZL_GRAPH_VIEW (self), renderer);
+      g_hash_table_insert (self->renderers, g_strdup_printf("cpu%d", i), renderer);
+    }
 }
diff --git a/src/charts/gsm-cpu-graph.h b/src/charts/gsm-cpu-graph.h
index 5dd3233c..585d2195 100644
--- a/src/charts/gsm-cpu-graph.h
+++ b/src/charts/gsm-cpu-graph.h
@@ -20,6 +20,7 @@
 #define GSM_CPU_GRAPH_H
 
 #include <dazzle.h>
+#include "gsm-cpu-model.h"
 
 G_BEGIN_DECLS
 
@@ -27,8 +28,9 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GsmCpuGraph, gsm_cpu_graph, GSM, CPU_GRAPH, DzlGraphView)
 
-GtkWidget *gsm_cpu_graph_new_full (gint64 timespan,
-                                   guint  max_samples);
+GtkWidget *gsm_cpu_graph_new_full (GsmCpuModel *cpu_model);
+
+void gsm_cpu_graph_renderer_set_color (GsmCpuGraph *cpu_graph, gchar* renderer_name, GdkRGBA *color);
 
 G_END_DECLS
 
diff --git a/src/interface.cpp b/src/interface.cpp
index fdd5c9ae..85d6d039 100644
--- a/src/interface.cpp
+++ b/src/interface.cpp
@@ -45,6 +45,7 @@
 #include "settings-keys.h"
 #include "legacy/gsm_color_button.h"
 #include "charts/gsm-cpu-graph.h"
+#include "charts/gsm-cpu-model.h"
 
 static const char* LOAD_GRAPH_CSS = "\
 .loadgraph {\
@@ -218,6 +219,7 @@ create_sys_view (GsmApplication *app, GtkBuilder * builder)
     LoadGraph *mem_graph, *net_graph;
     //LoadGraph *cpu_graph;
     GtkWidget *dzl_graph;
+    GsmCpuModel *cpu_model;
 
     int i;
     gchar *title_text;
@@ -236,8 +238,13 @@ create_sys_view (GsmApplication *app, GtkBuilder * builder)
     cpu_expander = GTK_EXPANDER (gtk_builder_get_object (builder, "cpu_expander"));
     g_object_bind_property (cpu_expander, "expanded", cpu_expander, "vexpand", G_BINDING_DEFAULT);
 
-    dzl_graph = GTK_WIDGET(g_object_new (GSM_TYPE_CPU_GRAPH, "timespan", G_TIME_SPAN_MINUTE,
-                                                             "max-samples", app->config.graph_data_points + 
2, NULL));
+
+    cpu_model = g_object_new (GSM_TYPE_CPU_MODEL,
+                              "timespan", 3*G_TIME_SPAN_MINUTE,
+                              "max-samples", app->config.graph_data_points + 2,
+                              NULL);
+
+    dzl_graph = GTK_WIDGET(g_object_new (GSM_TYPE_CPU_GRAPH, "model", cpu_model, NULL));
     app->cpu_graph = dzl_graph;
     gtk_widget_set_size_request (GTK_WIDGET(dzl_graph), -1, 70);
     gtk_widget_show (dzl_graph);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]