[gnome-software/wip/rancell/reviews] Use a custom widget to render review bars



commit abf63fbf75b0a3ab16db04c6ec47b77d38dee823
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Feb 9 16:52:21 2016 +1300

    Use a custom widget to render review bars

 src/Makefile.am            |    2 +
 src/gs-review-bar.c        |  107 ++++++++++++++++++++++++++++++++++++++++++++
 src/gs-review-bar.h        |   42 +++++++++++++++++
 src/gs-review-histogram.c  |   31 +++++++------
 src/gs-review-histogram.ui |   26 +++++++++--
 src/gtk-style.css          |    6 +++
 6 files changed, 194 insertions(+), 20 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index cbbcefc..172f10b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -144,6 +144,8 @@ gnome_software_SOURCES =                            \
        gs-progress-button.h                            \
        gs-review.c                                     \
        gs-review.h                                     \
+       gs-review-bar.c                                 \
+       gs-review-bar.h                                 \
        gs-review-dialog.c                              \
        gs-review-dialog.h                              \
        gs-review-histogram.c                           \
diff --git a/src/gs-review-bar.c b/src/gs-review-bar.c
new file mode 100644
index 0000000..d9ee437
--- /dev/null
+++ b/src/gs-review-bar.c
@@ -0,0 +1,107 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2016 Canonical Ltd.
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <math.h>
+
+#include "gs-review-bar.h"
+
+struct _GsReviewBar
+{
+       GtkBin           parent_instance;
+       gdouble          fraction;
+};
+
+G_DEFINE_TYPE (GsReviewBar, gs_review_bar, GTK_TYPE_BIN)
+
+/**
+ * gs_review_bar_set_fraction:
+ **/
+void
+gs_review_bar_set_fraction (GsReviewBar *bar, gdouble fraction)
+{
+       g_return_if_fail (GS_IS_REVIEW_BAR (bar));
+       bar->fraction = fraction;
+}
+
+/**
+ * gs_review_bar_init:
+ **/
+static void
+gs_review_bar_init (GsReviewBar *bar)
+{
+}
+
+/**
+ * gs_review_bar_draw:
+ **/
+static gboolean
+gs_review_bar_draw (GtkWidget *widget, cairo_t *cr)
+{
+       GtkStyleContext *context;
+       gdouble y_offset, bar_height;
+       GdkRGBA color;
+
+       context = gtk_widget_get_style_context (widget);
+
+       // Don't fill the complete height (too heavy beside GtkLabel of that height)
+       y_offset = floor (0.15 * gtk_widget_get_allocated_height (widget));
+       bar_height = gtk_widget_get_allocated_height (widget) - (y_offset * 2);
+
+       gtk_render_background (context, cr,
+                              0, y_offset,
+                              gtk_widget_get_allocated_width (widget),
+                              bar_height);
+
+       cairo_rectangle (cr,
+                        0, y_offset,
+                        round (GS_REVIEW_BAR (widget)->fraction * gtk_widget_get_allocated_width (widget)),
+                        bar_height);
+       gtk_style_context_get_color (context, gtk_widget_get_state_flags (widget), &color);
+       cairo_set_source_rgba (cr, color.red, color.green, color.blue, color.alpha);
+       cairo_fill (cr);
+
+       return GTK_WIDGET_CLASS (gs_review_bar_parent_class)->draw (widget, cr);
+}
+
+/**
+ * gs_review_bar_class_init:
+ **/
+static void
+gs_review_bar_class_init (GsReviewBarClass *klass)
+{
+       GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+       widget_class->draw = gs_review_bar_draw;
+}
+
+/**
+ * gs_review_bar_new:
+ **/
+GtkWidget *
+gs_review_bar_new (void)
+{
+       GsReviewBar *bar;
+       bar = g_object_new (GS_TYPE_REVIEW_BAR, NULL);
+       return GTK_WIDGET (bar);
+}
+
+/* vim: set noexpandtab: */
diff --git a/src/gs-review-bar.h b/src/gs-review-bar.h
new file mode 100644
index 0000000..4510dc8
--- /dev/null
+++ b/src/gs-review-bar.h
@@ -0,0 +1,42 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2016 Canonical Ltd.
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef GS_REVIEW_BAR_H
+#define GS_REVIEW_BAR_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_REVIEW_BAR (gs_review_bar_get_type ())
+
+G_DECLARE_FINAL_TYPE (GsReviewBar, gs_review_bar, GS, REVIEW_BAR, GtkBin)
+
+GtkWidget      *gs_review_bar_new              (void);
+
+void            gs_review_bar_set_fraction     (GsReviewBar    *bar,
+                                                gdouble         fraction);
+
+G_END_DECLS
+
+#endif /* GS_REVIEW_BAR_H */
+
+/* vim: set noexpandtab: */
diff --git a/src/gs-review-histogram.c b/src/gs-review-histogram.c
index 3ad12b7..4a763b5 100644
--- a/src/gs-review-histogram.c
+++ b/src/gs-review-histogram.c
@@ -25,14 +25,15 @@
 #include <gtk/gtk.h>
 
 #include "gs-review-histogram.h"
+#include "gs-review-bar.h"
 
 typedef struct
 {
-       GtkWidget       *progress5;
-       GtkWidget       *progress4;
-       GtkWidget       *progress3;
-       GtkWidget       *progress2;
-       GtkWidget       *progress1;
+       GtkWidget       *bar5;
+       GtkWidget       *bar4;
+       GtkWidget       *bar3;
+       GtkWidget       *bar2;
+       GtkWidget       *bar1;
        GtkWidget       *label_count5;
        GtkWidget       *label_count4;
        GtkWidget       *label_count3;
@@ -76,15 +77,15 @@ gs_review_histogram_set_ratings (GsReviewHistogram *histogram,
        max = count4 > max ? count4 : max;
        max = count5 > max ? count5 : max;
 
-       gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress5), count5 / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar5), count5 / max);
        set_label (priv->label_count5, count5);
-       gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress4), count4 / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar4), count4 / max);
        set_label (priv->label_count4, count4);
-       gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress3), count3 / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar3), count3 / max);
        set_label (priv->label_count3, count3);
-       gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress2), count2 / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar2), count2 / max);
        set_label (priv->label_count2, count2);
-       gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress1), count1 / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar1), count1 / max);
        set_label (priv->label_count1, count1);
        set_label (priv->label_total, count1 + count2 + count3 + count4 + count5);
 }
@@ -102,11 +103,11 @@ gs_review_histogram_class_init (GsReviewHistogramClass *klass)
 
        gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Software/gs-review-histogram.ui");
 
-       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, progress5);
-       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, progress4);
-       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, progress3);
-       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, progress2);
-       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, progress1);
+       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, bar5);
+       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, bar4);
+       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, bar3);
+       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, bar2);
+       gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, bar1);
        gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, label_count5);
        gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, label_count4);
        gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, label_count3);
diff --git a/src/gs-review-histogram.ui b/src/gs-review-histogram.ui
index 3dc75b0..45d7523 100644
--- a/src/gs-review-histogram.ui
+++ b/src/gs-review-histogram.ui
@@ -37,9 +37,13 @@
           </packing>
         </child>
         <child>
-          <object class="GtkProgressBar" id="progress5">
+          <object class="GsReviewBar" id="bar5">
             <property name="visible">True</property>
             <property name="margin-left">25</property>
+            <property name="width-request">120</property>
+            <style>
+              <class name="reviewbar"/>
+            </style>
           </object>
           <packing>
             <property name="left-attach">2</property>
@@ -89,9 +93,12 @@
           </packing>
         </child>
         <child>
-          <object class="GtkProgressBar" id="progress4">
+          <object class="GsReviewBar" id="bar4">
             <property name="visible">True</property>
             <property name="margin-left">25</property>
+            <style>
+              <class name="reviewbar"/>
+            </style>
           </object>
           <packing>
             <property name="left-attach">2</property>
@@ -141,9 +148,12 @@
           </packing>
         </child>
         <child>
-          <object class="GtkProgressBar" id="progress3">
+          <object class="GsReviewBar" id="bar3">
             <property name="visible">True</property>
             <property name="margin-left">25</property>
+            <style>
+              <class name="reviewbar"/>
+            </style>
           </object>
           <packing>
             <property name="left-attach">2</property>
@@ -193,9 +203,12 @@
           </packing>
         </child>
         <child>
-          <object class="GtkProgressBar" id="progress2">
+          <object class="GsReviewBar" id="bar2">
             <property name="visible">True</property>
             <property name="margin-left">25</property>
+            <style>
+              <class name="reviewbar"/>
+            </style>
           </object>
           <packing>
             <property name="left-attach">2</property>
@@ -245,9 +258,12 @@
           </packing>
         </child>
         <child>
-          <object class="GtkProgressBar" id="progress1">
+          <object class="GsReviewBar" id="bar1">
             <property name="visible">True</property>
             <property name="margin-left">25</property>
+            <style>
+              <class name="reviewbar"/>
+            </style>
           </object>
           <packing>
             <property name="left-attach">2</property>
diff --git a/src/gtk-style.css b/src/gtk-style.css
index 78a9696..3a8032e 100644
--- a/src/gtk-style.css
+++ b/src/gtk-style.css
@@ -147,6 +147,12 @@
        font-weight: bold;
 }
 
+.reviewbar {
+       background-image: none;
+       background-color: #babdb6;
+       color: #555753;
+}
+
 .error-label {
        text-shadow: none;
 }


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