[gnome-power-manager] introducing divs_x to set the number of x-axis ticks depending on the time range chosen; this also f



commit c2555d73b6550996eefcf559525e73b37cf2502f
Author: Ivo Pletikosić <ivo pletikosic gmail com>
Date:   Wed Apr 10 02:52:25 2019 -0400

    introducing divs_x to set the number of x-axis ticks depending on the time range chosen; this also fixes 
the bug of disparate axis range and combo box option after a restart

 src/egg-graph-widget.c | 31 +++++++++++++++++-------
 src/gpm-statistics.c   | 64 ++++++++++++++++++++++++++++++++++----------------
 2 files changed, 67 insertions(+), 28 deletions(-)
---
diff --git a/src/egg-graph-widget.c b/src/egg-graph-widget.c
index 7c2fc9ed..2a99053c 100644
--- a/src/egg-graph-widget.c
+++ b/src/egg-graph-widget.c
@@ -47,8 +47,10 @@ typedef struct {
        gint                     box_width;
        gint                     box_height;
 
-       gdouble                  unit_x; /* 10th width of graph */
-       gdouble                  unit_y; /* 10th width of graph */
+       guint                    divs_x; /* number of divisions */
+
+       gdouble                  unit_x; /* box pixels per x unit */
+       gdouble                  unit_y; /* box pixels per y unit */
 
        EggGraphWidgetKind       type_x;
        EggGraphWidgetKind       type_y;
@@ -76,6 +78,7 @@ enum
        PROP_TYPE_Y,
        PROP_AUTORANGE_X,
        PROP_AUTORANGE_Y,
+       PROP_DIVS_X,
        PROP_START_X,
        PROP_START_Y,
        PROP_STOP_X,
@@ -156,6 +159,9 @@ up_graph_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec
        case PROP_AUTORANGE_Y:
                g_value_set_boolean (value, priv->autorange_y);
                break;
+       case PROP_DIVS_X:
+               g_value_set_uint (value, priv->divs_x);
+               break;
        case PROP_START_X:
                g_value_set_double (value, priv->start_x);
                break;
@@ -199,6 +205,9 @@ up_graph_set_property (GObject *object, guint prop_id, const GValue *value, GPar
        case PROP_AUTORANGE_Y:
                priv->autorange_y = g_value_get_boolean (value);
                break;
+       case PROP_DIVS_X:
+               priv->divs_x = g_value_get_uint (value);
+               break;
        case PROP_START_X:
                priv->start_x = g_value_get_double (value);
                break;
@@ -267,6 +276,11 @@ egg_graph_widget_class_init (EggGraphWidgetClass *class)
                                         g_param_spec_boolean ("autorange-y", NULL, NULL,
                                                               TRUE,
                                                               G_PARAM_READWRITE));
+       g_object_class_install_property (object_class,
+                                        PROP_DIVS_X,
+                                        g_param_spec_uint ("divs-x", NULL, NULL,
+                                                          4, 16, 4,
+                                                          G_PARAM_READWRITE));
        g_object_class_install_property (object_class,
                                         PROP_START_X,
                                         g_param_spec_double ("start-x", NULL, NULL,
@@ -300,6 +314,7 @@ egg_graph_widget_init (EggGraphWidget *graph)
        priv->start_y = 0;
        priv->stop_x = 60;
        priv->stop_y = 100;
+       priv->divs_x = 4;
        priv->use_grid = TRUE;
        priv->use_legend = FALSE;
        priv->legend_list = g_ptr_array_new_with_free_func ((GDestroyNotify) 
egg_graph_widget_key_legend_data_free);
@@ -445,7 +460,7 @@ egg_graph_widget_draw_grid (EggGraphWidget *graph, cairo_t *cr)
        guint i;
        gdouble b;
        gdouble dotted[] = {1., 2.};
-       gdouble divwidth  = (gdouble)priv->box_width / 10.0f;
+       gdouble divwidth  = (gdouble)priv->box_width / (gdouble)priv->divs_x;
        gdouble divheight = (gdouble)priv->box_height / 10.0f;
 
        cairo_save (cr);
@@ -455,7 +470,7 @@ egg_graph_widget_draw_grid (EggGraphWidget *graph, cairo_t *cr)
 
        /* do vertical lines */
        cairo_set_source_rgb (cr, 0.1, 0.1, 0.1);
-       for (i = 1; i < 10; i++) {
+       for (i = 1; i < priv->divs_x; i++) {
                b = priv->box_x + ((gdouble) i * divwidth);
                cairo_move_to (cr, (gint)b + 0.5f, priv->box_y);
                cairo_line_to (cr, (gint)b + 0.5f, priv->box_y + priv->box_height);
@@ -480,7 +495,7 @@ egg_graph_widget_draw_labels (EggGraphWidget *graph, cairo_t *cr)
        guint i;
        gdouble b;
        gdouble value;
-       gdouble divwidth  = (gdouble)priv->box_width / 10.0f;
+       gdouble divwidth  = (gdouble)priv->box_width / (gdouble)priv->divs_x;
        gdouble divheight = (gdouble)priv->box_height / 10.0f;
        gdouble length_x = priv->stop_x - priv->start_x;
        gdouble length_y = priv->stop_y - priv->start_y;
@@ -492,10 +507,10 @@ egg_graph_widget_draw_labels (EggGraphWidget *graph, cairo_t *cr)
 
        /* do x text */
        cairo_set_source_rgb (cr, 0.2f, 0.2f, 0.2f);
-       for (i = 0; i < 11; i++) {
+       for (i = 0; i < priv->divs_x + 1; i++) {
                g_autofree gchar *text = NULL;
                b = priv->box_x + ((gdouble) i * divwidth);
-               value = ((length_x / 10.0f) * (gdouble) i) + (gdouble) priv->start_x;
+               value = ((length_x / (gdouble)priv->divs_x) * (gdouble) i) + (gdouble) priv->start_x;
                text = egg_graph_widget_get_axis_label (priv->type_x, value);
 
                pango_layout_set_text (priv->layout, text, -1);
@@ -503,7 +518,7 @@ egg_graph_widget_draw_labels (EggGraphWidget *graph, cairo_t *cr)
                /* have data points 0 and 10 bounded, but 1..9 centered */
                if (i == 0)
                        offsetx = 2.0;
-               else if (i == 10)
+               else if (i == priv->divs_x)
                        offsetx = ink_rect.width;
                else
                        offsetx = (ink_rect.width / 2.0f);
diff --git a/src/gpm-statistics.c b/src/gpm-statistics.c
index 9d61cc1d..dce87689 100644
--- a/src/gpm-statistics.c
+++ b/src/gpm-statistics.c
@@ -48,6 +48,7 @@ gchar *current_device = NULL;
 static const gchar *history_type;
 static const gchar *stats_type;
 static guint history_time;
+static guint divs_x;
 static GSettings *settings;
 static gfloat sigma_smoothing = 0.0f;
 static GtkWidget *graph_history = NULL;
@@ -78,18 +79,24 @@ enum {
 #define GPM_HISTORY_TIME_FULL_VALUE            "time-full"
 #define GPM_HISTORY_TIME_EMPTY_VALUE           "time-empty"
 
-#define GPM_HISTORY_MINUTE_TEXT                        _("10 minutes")
-#define GPM_HISTORY_HOUR_TEXT                  _("2 hours")
-#define GPM_HISTORY_HOURS_TEXT                 _("6 hours")
+#define GPM_HISTORY_MINUTE_TEXT                        _("30 minutes")
+#define GPM_HISTORY_HOUR_TEXT                  _("3 hours")
+#define GPM_HISTORY_HOURS_TEXT                 _("8 hours")
 #define GPM_HISTORY_DAY_TEXT                   _("1 day")
 #define GPM_HISTORY_WEEK_TEXT                  _("1 week")
 
-#define GPM_HISTORY_MINUTE_VALUE               10*60
-#define GPM_HISTORY_HOUR_VALUE                 2*60*60
-#define GPM_HISTORY_HOURS_VALUE                        6*60*60
+#define GPM_HISTORY_MINUTE_VALUE               30*60
+#define GPM_HISTORY_HOUR_VALUE                 3*60*60
+#define GPM_HISTORY_HOURS_VALUE                        8*60*60
 #define GPM_HISTORY_DAY_VALUE                  24*60*60
 #define GPM_HISTORY_WEEK_VALUE                 7*24*60*60
 
+#define GPM_HISTORY_MINUTE_DIVS                        6  /* 5 min tick */
+#define GPM_HISTORY_HOUR_DIVS                  6  /* 30 min tick */
+#define GPM_HISTORY_HOURS_DIVS                 8  /* 1 hr tick */
+#define GPM_HISTORY_DAY_DIVS                   12 /* 2 hr tick */
+#define GPM_HISTORY_WEEK_DIVS                  7  /* 1 day tick */
+
 /* TRANSLATORS: what we've observed about the device */
 #define GPM_STATS_CHARGE_DATA_TEXT             _("Charge profile")
 #define GPM_STATS_DISCHARGE_DATA_TEXT          _("Discharge profile")
@@ -729,6 +736,7 @@ gpm_stats_update_info_page_history (UpDevice *device)
                              "type-x", EGG_GRAPH_WIDGET_KIND_TIME,
                              "type-y", EGG_GRAPH_WIDGET_KIND_PERCENTAGE,
                              "autorange-x", FALSE,
+                             "divs-x", (guint) divs_x,
                              "start-x", -(gdouble) history_time,
                              "stop-x", (gdouble) 0.f,
                              "autorange-y", FALSE,
@@ -740,6 +748,7 @@ gpm_stats_update_info_page_history (UpDevice *device)
                              "type-x", EGG_GRAPH_WIDGET_KIND_TIME,
                              "type-y", EGG_GRAPH_WIDGET_KIND_POWER,
                              "autorange-x", FALSE,
+                             "divs-x", (guint) divs_x,
                              "start-x", -(gdouble) history_time,
                              "stop-x", (gdouble) 0.f,
                              "autorange-y", TRUE,
@@ -749,6 +758,7 @@ gpm_stats_update_info_page_history (UpDevice *device)
                              "type-x", EGG_GRAPH_WIDGET_KIND_TIME,
                              "type-y", EGG_GRAPH_WIDGET_KIND_TIME,
                              "autorange-x", FALSE,
+                             "divs-x", (guint) divs_x,
                              "start-x", -(gdouble) history_time,
                              "stop-x", (gdouble) 0.f,
                              "autorange-y", TRUE,
@@ -849,6 +859,7 @@ gpm_stats_update_info_page_stats (UpDevice *device)
                g_object_set (graph_statistics,
                              "type-x", EGG_GRAPH_WIDGET_KIND_PERCENTAGE,
                              "type-y", EGG_GRAPH_WIDGET_KIND_FACTOR,
+                             "divs-x", 10,
                              "autorange-x", TRUE,
                              "autorange-y", TRUE,
                              NULL);
@@ -856,6 +867,7 @@ gpm_stats_update_info_page_stats (UpDevice *device)
                g_object_set (graph_statistics,
                              "type-x", EGG_GRAPH_WIDGET_KIND_PERCENTAGE,
                              "type-y", EGG_GRAPH_WIDGET_KIND_PERCENTAGE,
+                             "divs-x", 10,
                              "autorange-x", TRUE,
                              "autorange-y", TRUE,
                              NULL);
@@ -1218,17 +1230,22 @@ gpm_stats_range_combo_changed (GtkWidget *widget, gpointer data)
 {
        g_autofree gchar *value = NULL;
        value = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (widget));
-       if (g_strcmp0 (value, GPM_HISTORY_MINUTE_TEXT) == 0)
+       if (g_strcmp0 (value, GPM_HISTORY_MINUTE_TEXT) == 0) {
                history_time = GPM_HISTORY_MINUTE_VALUE;
-       else if (g_strcmp0 (value, GPM_HISTORY_HOUR_TEXT) == 0)
+               divs_x = GPM_HISTORY_MINUTE_DIVS;
+       } else if (g_strcmp0 (value, GPM_HISTORY_HOUR_TEXT) == 0) {
                history_time = GPM_HISTORY_HOUR_VALUE;
-       else if (g_strcmp0 (value, GPM_HISTORY_HOURS_TEXT) == 0)
+               divs_x = GPM_HISTORY_HOUR_DIVS;
+       } else if (g_strcmp0 (value, GPM_HISTORY_HOURS_TEXT) == 0) {
                history_time = GPM_HISTORY_HOURS_VALUE;
-       else if (g_strcmp0 (value, GPM_HISTORY_DAY_TEXT) == 0)
+               divs_x = GPM_HISTORY_HOURS_DIVS;
+       } else if (g_strcmp0 (value, GPM_HISTORY_DAY_TEXT) == 0) {
                history_time = GPM_HISTORY_DAY_VALUE;
-       else if (g_strcmp0 (value, GPM_HISTORY_WEEK_TEXT) == 0)
+               divs_x = GPM_HISTORY_DAY_DIVS;
+       } else if (g_strcmp0 (value, GPM_HISTORY_WEEK_TEXT) == 0) {
                history_time = GPM_HISTORY_WEEK_VALUE;
-       else
+               divs_x = GPM_HISTORY_WEEK_DIVS;
+       } else
                g_assert (FALSE);
 
        /* save to gconf */
@@ -1466,8 +1483,6 @@ gpm_stats_startup_cb (GApplication *application,
        history_time = g_settings_get_int (settings, GPM_SETTINGS_INFO_HISTORY_TIME);
        if (history_type == NULL)
                history_type = GPM_HISTORY_CHARGE_VALUE;
-       if (history_time == 0)
-               history_time = GPM_HISTORY_HOUR_VALUE;
 
        stats_type = g_settings_get_string (settings, GPM_SETTINGS_INFO_STATS_TYPE);
        if (stats_type == NULL)
@@ -1496,8 +1511,6 @@ gpm_stats_startup_cb (GApplication *application,
                gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 1);
        else if (g_strcmp0 (stats_type, GPM_STATS_CHARGE_DATA_VALUE) == 0)
                gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 2);
-       else if (g_strcmp0 (stats_type, GPM_STATS_CHARGE_DATA_VALUE) == 0)
-               gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 0);
        else
                gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 3);
        g_signal_connect (G_OBJECT (widget), "changed",
@@ -1509,13 +1522,24 @@ gpm_stats_startup_cb (GApplication *application,
        gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (widget), GPM_HISTORY_HOURS_TEXT);
        gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (widget), GPM_HISTORY_DAY_TEXT);
        gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (widget), GPM_HISTORY_WEEK_TEXT);
-       gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 1);
-       if (history_time == GPM_HISTORY_MINUTE_VALUE)
+
+       if (history_time == GPM_HISTORY_MINUTE_VALUE) {
                gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 0);
-       else if (history_time == GPM_HISTORY_HOUR_VALUE)
+               divs_x = GPM_HISTORY_MINUTE_DIVS;
+       } else if (history_time == GPM_HISTORY_HOUR_VALUE) {
                gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 1);
-       else
+               divs_x = GPM_HISTORY_HOUR_DIVS;
+       } else if (history_time == GPM_HISTORY_DAY_VALUE) {
+               gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 3); 
+               divs_x = GPM_HISTORY_DAY_DIVS;
+       } else if (history_time == GPM_HISTORY_WEEK_VALUE) {
+               gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 4);
+               divs_x = GPM_HISTORY_WEEK_DIVS;
+       } else { /* default */
                gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 2);
+               history_time = GPM_HISTORY_HOURS_VALUE;
+               divs_x = GPM_HISTORY_HOURS_DIVS;
+       }
        g_signal_connect (G_OBJECT (widget), "changed",
                          G_CALLBACK (gpm_stats_range_combo_changed), NULL);
 


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