diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 15:18:46 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 15:18:46 +0000 |
commit | 56294d30a82ec2da6f9ce399740c1ef65a9ddef4 (patch) | |
tree | bbe3823e41495d026ba8edc6eeaef166edb7e2a2 /src/gs-review-histogram.c | |
parent | Initial commit. (diff) | |
download | gnome-software-56294d30a82ec2da6f9ce399740c1ef65a9ddef4.tar.xz gnome-software-56294d30a82ec2da6f9ce399740c1ef65a9ddef4.zip |
Adding upstream version 3.38.1.upstream/3.38.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/gs-review-histogram.c')
-rw-r--r-- | src/gs-review-histogram.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/gs-review-histogram.c b/src/gs-review-histogram.c new file mode 100644 index 0000000..5137f63 --- /dev/null +++ b/src/gs-review-histogram.c @@ -0,0 +1,112 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2016 Canonical Ltd. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include "gs-review-histogram.h" +#include "gs-review-bar.h" + +typedef struct +{ + GtkWidget *bar1; + GtkWidget *bar2; + GtkWidget *bar3; + GtkWidget *bar4; + GtkWidget *bar5; + GtkWidget *label_count1; + GtkWidget *label_count2; + GtkWidget *label_count3; + GtkWidget *label_count4; + GtkWidget *label_count5; + GtkWidget *label_total; +} GsReviewHistogramPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE (GsReviewHistogram, gs_review_histogram, GTK_TYPE_BIN) + +static void +set_label (GtkWidget *label, gint value) +{ + g_autofree gchar *text = NULL; + text = g_strdup_printf ("%i", value); + gtk_label_set_text (GTK_LABEL (label), text); +} + +void +gs_review_histogram_set_ratings (GsReviewHistogram *histogram, + GArray *review_ratings) +{ + GsReviewHistogramPrivate *priv = gs_review_histogram_get_instance_private (histogram); + gdouble fraction[6] = { 0.0f }; + guint32 max = 0; + guint32 total = 0; + + g_return_if_fail (GS_IS_REVIEW_HISTOGRAM (histogram)); + + /* sanity check */ + if (review_ratings->len != 6) { + g_warning ("ratings data incorrect expected 012345"); + return; + } + + /* idx 0 is '0 stars' which we don't support in the UI */ + for (guint i = 1; i < review_ratings->len; i++) { + guint32 c = g_array_index (review_ratings, guint32, i); + max = MAX (c, max); + } + for (guint i = 1; i < review_ratings->len; i++) { + guint32 c = g_array_index (review_ratings, guint32, i); + fraction[i] = max > 0 ? (gdouble) c / (gdouble) max : 0.f; + total += c; + } + + gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar5), fraction[5]); + set_label (priv->label_count5, g_array_index (review_ratings, guint, 5)); + gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar4), fraction[4]); + set_label (priv->label_count4, g_array_index (review_ratings, guint, 4)); + gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar3), fraction[3]); + set_label (priv->label_count3, g_array_index (review_ratings, guint, 3)); + gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar2), fraction[2]); + set_label (priv->label_count2, g_array_index (review_ratings, guint, 2)); + gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar1), fraction[1]); + set_label (priv->label_count1, g_array_index (review_ratings, guint, 1)); + set_label (priv->label_total, total); +} + +static void +gs_review_histogram_init (GsReviewHistogram *histogram) +{ + gtk_widget_init_template (GTK_WIDGET (histogram)); +} + +static void +gs_review_histogram_class_init (GsReviewHistogramClass *klass) +{ + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (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, 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); + gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, label_count2); + gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, label_count1); + gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, label_total); +} + +GtkWidget * +gs_review_histogram_new (void) +{ + GsReviewHistogram *histogram; + histogram = g_object_new (GS_TYPE_REVIEW_HISTOGRAM, NULL); + return GTK_WIDGET (histogram); +} |