[gnome-builder] rg: add RgCpuGraph



commit 872c54ea55cf5b7d129b7e5a4cdf42ac2a851338
Author: Christian Hergert <christian hergert me>
Date:   Mon Jun 1 21:58:35 2015 -0700

    rg: add RgCpuGraph
    
    This simplifies creating a cpu graph by adding a subclass to do all the
    heavy lifting. It manages the RgCpuTable as well. This should allow
    consumers to create them with just .ui templates.

 contrib/rg/Makefile.am    |    2 +
 contrib/rg/rg-cpu-graph.c |  172 +++++++++++++++++++++++++++++++++++++++++++++
 contrib/rg/rg-cpu-graph.h |   34 +++++++++
 data/theme/Adwaita.css    |    7 ++
 po/POTFILES.in            |    1 +
 tests/test-cpu-graph.c    |   39 +---------
 6 files changed, 220 insertions(+), 35 deletions(-)
---
diff --git a/contrib/rg/Makefile.am b/contrib/rg/Makefile.am
index 75b01fb..166d7b5 100644
--- a/contrib/rg/Makefile.am
+++ b/contrib/rg/Makefile.am
@@ -4,6 +4,8 @@ librg_la_SOURCES = \
        rg-column.c \
        rg-column.h \
        rg-column-private.h \
+       rg-cpu-graph.c \
+       rg-cpu-graph.h \
        rg-cpu-table.c \
        rg-cpu-table.h \
        rg-graph.c \
diff --git a/contrib/rg/rg-cpu-graph.c b/contrib/rg/rg-cpu-graph.c
new file mode 100644
index 0000000..fc6861c
--- /dev/null
+++ b/contrib/rg/rg-cpu-graph.c
@@ -0,0 +1,172 @@
+/* rg-cpu-graph.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file 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
+ * Lesser 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/>.
+ */
+
+#include <glib/gi18n.h>
+
+#include "rg-cpu-graph.h"
+#include "rg-cpu-table.h"
+#include "rg-line-renderer.h"
+
+struct _RgCpuGraph
+{
+  RgGraph parent_instance;
+
+  gint64 timespan;
+  guint  max_samples;
+};
+
+G_DEFINE_TYPE (RgCpuGraph, rg_cpu_graph, RG_TYPE_GRAPH)
+
+enum {
+  PROP_0,
+  PROP_MAX_SAMPLES,
+  PROP_TIMESPAN,
+  LAST_PROP
+};
+
+static GParamSpec *gParamSpecs[LAST_PROP];
+
+static gchar *colors[] = {
+  "#73d216",
+  "#f57900",
+  "#3465a4",
+  "#ef2929",
+  "#75507b",
+  "#ce5c00",
+  "#c17d11",
+  "#ce5c00",
+};
+
+GtkWidget *
+rg_cpu_graph_new (void)
+{
+  return g_object_new (RG_TYPE_CPU_GRAPH, NULL);
+}
+
+static void
+rg_cpu_graph_constructed (GObject *object)
+{
+  RgCpuGraph *self = (RgCpuGraph *)object;
+  RgTable *table;
+  guint n_cpu;
+  guint i;
+
+  G_OBJECT_CLASS (rg_cpu_graph_parent_class)->constructed (object);
+
+  table = g_object_new (RG_TYPE_CPU_TABLE,
+                        "timespan", self->timespan,
+                        "max-samples", self->max_samples + 1,
+                        NULL);
+  rg_graph_set_table (RG_GRAPH (self), table);
+  g_clear_object (&table);
+
+  n_cpu = g_get_num_processors ();
+
+  for (i = 0; i < n_cpu; i++)
+    {
+      RgRenderer *renderer;
+
+      renderer = g_object_new (RG_TYPE_LINE_RENDERER,
+                               "column", i,
+                               "stroke-color", colors [i % G_N_ELEMENTS (colors)],
+                               NULL);
+      rg_graph_add_renderer (RG_GRAPH (self), renderer);
+      g_clear_object (&renderer);
+    }
+}
+
+static void
+rg_cpu_graph_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  RgCpuGraph *self = RG_CPU_GRAPH (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);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+rg_cpu_graph_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
+{
+  RgCpuGraph *self = RG_CPU_GRAPH (object);
+
+  switch (prop_id)
+    {
+    case PROP_MAX_SAMPLES:
+      self->max_samples = g_value_get_uint (value);
+      break;
+
+    case PROP_TIMESPAN:
+      self->timespan = g_value_get_int64 (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+rg_cpu_graph_class_init (RgCpuGraphClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed = rg_cpu_graph_constructed;
+  object_class->get_property = rg_cpu_graph_get_property;
+  object_class->set_property = rg_cpu_graph_set_property;
+
+  gParamSpecs [PROP_TIMESPAN] =
+    g_param_spec_int64 ("timespan",
+                         _("Timespan"),
+                         _("Timespan"),
+                         0, G_MAXINT64,
+                         0,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  gParamSpecs [PROP_MAX_SAMPLES] =
+    g_param_spec_uint ("max-samples",
+                       _("Max Samples"),
+                       _("Max Samples"),
+                       0, G_MAXUINT,
+                       120,
+                       (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
+}
+
+static void
+rg_cpu_graph_init (RgCpuGraph *self)
+{
+  self->max_samples = 120;
+  self->timespan = 60L * G_USEC_PER_SEC;
+}
diff --git a/contrib/rg/rg-cpu-graph.h b/contrib/rg/rg-cpu-graph.h
new file mode 100644
index 0000000..869cd81
--- /dev/null
+++ b/contrib/rg/rg-cpu-graph.h
@@ -0,0 +1,34 @@
+/* rg-cpu-graph.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file 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
+ * Lesser 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/>.
+ */
+
+#ifndef RG_CPU_GRAPH_H
+#define RG_CPU_GRAPH_H
+
+#include "rg-graph.h"
+
+G_BEGIN_DECLS
+
+#define RG_TYPE_CPU_GRAPH (rg_cpu_graph_get_type())
+
+G_DECLARE_FINAL_TYPE (RgCpuGraph, rg_cpu_graph, RG, CPU_GRAPH, RgGraph)
+
+GtkWidget *rg_cpu_graph_new (void);
+
+G_END_DECLS
+
+#endif /* RG_CPU_GRAPH_H */
diff --git a/data/theme/Adwaita.css b/data/theme/Adwaita.css
index 804ac54..5d4bec7 100644
--- a/data/theme/Adwaita.css
+++ b/data/theme/Adwaita.css
@@ -21,3 +21,10 @@ GbGreeterPillBox {
   background-color: #eeeeec;
   border-radius: 3px;
 }
+
+RgGraph {
+  background-color: #f6f7f8;
+  background-size: 8px 8px;
+  background-image: repeating-linear-gradient(0deg, #f0f1f2, #f0f1f2 1px, transparent 1px, transparent 8px),
+                    repeating-linear-gradient(-90deg, #f0f1f2, #f0f1f2 1px, transparent 1px, transparent 
8px);
+}
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 4a444e7..6211302 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -32,6 +32,7 @@ contrib/egg/egg-state-machine-action.c
 contrib/gedit/gedit-menu-stack-switcher.c
 contrib/nautilus/nautilus-floating-bar.c
 contrib/rg/rg-column.c
+contrib/rg/rg-cpu-graph.c
 contrib/rg/rg-graph.c
 contrib/rg/rg-line-renderer.c
 contrib/rg/rg-table.c
diff --git a/tests/test-cpu-graph.c b/tests/test-cpu-graph.c
index 3b8a7a3..e9d68fd 100644
--- a/tests/test-cpu-graph.c
+++ b/tests/test-cpu-graph.c
@@ -1,6 +1,5 @@
 #include "rg-graph.h"
-#include "rg-cpu-table.h"
-#include "rg-line-renderer.h"
+#include "rg-cpu-graph.h"
 
 #include <stdlib.h>
 
@@ -11,17 +10,6 @@
   "  background-image:repeating-linear-gradient(0deg, #f0f1f2, #f0f1f2 1px, transparent 1px, transparent 
8px),repeating-linear-gradient(-90deg, #f0f1f2, #f0f1f2 1px, transparent 1px, transparent 8px);\n" \
   "}"
 
-static gchar *colors[] = {
-  "#73d216",
-  "#f57900",
-  "#3465a4",
-  "#ef2929",
-  "#75507b",
-  "#ce5c00",
-  "#c17d11",
-  "#ce5c00",
-};
-
 int
 main (int argc,
       char *argv[])
@@ -38,11 +26,8 @@ main (int argc,
   GOptionContext *context;
   GtkWindow *window;
   RgGraph *graph;
-  RgTable *table;
-  RgRenderer *renderer;
   GtkCssProvider *provider;
   GError *error = NULL;
-  gsize i;
 
   context = g_option_context_new ("- a simple cpu graph");
   g_option_context_add_group (context, gtk_get_option_group (TRUE));
@@ -60,11 +45,6 @@ main (int argc,
   timespan = (gint64)seconds * G_USEC_PER_SEC;
   max_samples = seconds * samples;
 
-  table = g_object_new (RG_TYPE_CPU_TABLE,
-                        "timespan", timespan,
-                        "max-samples", max_samples,
-                        NULL);
-
   provider = gtk_css_provider_new ();
   gtk_css_provider_load_from_data (provider, CSS_DATA, -1, NULL);
   gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), GTK_STYLE_PROVIDER (provider), 
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
@@ -76,22 +56,11 @@ main (int argc,
                          "title", "CPU Graph",
                          NULL);
 
-  graph = g_object_new (RG_TYPE_GRAPH,
+  graph = g_object_new (RG_TYPE_CPU_GRAPH,
                         "visible", TRUE,
-                        "table", table,
+                        "timespan", timespan,
+                        "max-samples", max_samples,
                         NULL);
-
-  for (i = 0; i < g_get_num_processors (); i++)
-    {
-      renderer = g_object_new (RG_TYPE_LINE_RENDERER,
-                               "column", i,
-                               "stroke-color", colors[i % G_N_ELEMENTS (colors)],
-                               "line-width", 1.0,
-                               NULL);
-      rg_graph_add_renderer (graph, renderer);
-      g_object_unref (renderer);
-    }
-
   gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (graph));
 
   g_signal_connect (window, "delete-event", gtk_main_quit, NULL);


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