summaryrefslogtreecommitdiffstats
path: root/src/gs-review-histogram.c
blob: 0b7403478b0cb0136694d091e250f7e44e053831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* -*- 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 <math.h>

#include "gs-common.h"
#include "gs-review-histogram.h"
#include "gs-review-bar.h"
#include "gs-star-image.h"

typedef struct
{
	GtkWidget	*bar1;
	GtkWidget	*bar2;
	GtkWidget	*bar3;
	GtkWidget	*bar4;
	GtkWidget	*bar5;
	GtkWidget	*label_value;
	GtkWidget	*label_total;
	GtkWidget	*star_value_1;
	GtkWidget	*star_value_2;
	GtkWidget	*star_value_3;
	GtkWidget	*star_value_4;
	GtkWidget	*star_value_5;
} GsReviewHistogramPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (GsReviewHistogram, gs_review_histogram, GTK_TYPE_WIDGET)

void
gs_review_histogram_set_ratings (GsReviewHistogram *histogram,
				 gint rating_percent,
				 GArray *review_ratings)
{
	GsReviewHistogramPrivate *priv = gs_review_histogram_get_instance_private (histogram);
	g_autofree gchar *text = NULL;
	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]);
	gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar4), fraction[4]);
	gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar3), fraction[3]);
	gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar2), fraction[2]);
	gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar1), fraction[1]);

	text = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%u review total", "%u reviews total", total), total);
	gtk_label_set_text (GTK_LABEL (priv->label_total), text);

	g_clear_pointer (&text, g_free);

	/* Round explicitly, to avoid rounding inside the printf() call and to use
	   the same value also for the stars fraction. */
	fraction[0] = total > 0 ? round (((gdouble) rating_percent ) * 50.0 / 100.0) / 10.0 : 0.0;
	text = g_strdup_printf ("%.01f", fraction[0]);
	gtk_label_set_text (GTK_LABEL (priv->label_value), text);

	gs_star_image_set_fraction (GS_STAR_IMAGE (priv->star_value_1), CLAMP (fraction[0], 0.0, 1.0));
	gs_star_image_set_fraction (GS_STAR_IMAGE (priv->star_value_2), CLAMP (fraction[0], 1.0, 2.0) - 1.0);
	gs_star_image_set_fraction (GS_STAR_IMAGE (priv->star_value_3), CLAMP (fraction[0], 2.0, 3.0) - 2.0);
	gs_star_image_set_fraction (GS_STAR_IMAGE (priv->star_value_4), CLAMP (fraction[0], 3.0, 4.0) - 3.0);
	gs_star_image_set_fraction (GS_STAR_IMAGE (priv->star_value_5), CLAMP (fraction[0], 4.0, 5.0) - 4.0);
}

static void
gs_review_histogram_dispose (GObject *object)
{
	gs_widget_remove_all (GTK_WIDGET (object), NULL);

	G_OBJECT_CLASS (gs_review_histogram_parent_class)->dispose (object);
}

static void
gs_review_histogram_init (GsReviewHistogram *histogram)
{
	gtk_widget_init_template (GTK_WIDGET (histogram));
}

static void
gs_review_histogram_class_init (GsReviewHistogramClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

	object_class->dispose = gs_review_histogram_dispose;

	gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Software/gs-review-histogram.ui");
	gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);

	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_value);
	gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, label_total);
	gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, star_value_1);
	gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, star_value_2);
	gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, star_value_3);
	gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, star_value_4);
	gtk_widget_class_bind_template_child_private (widget_class, GsReviewHistogram, star_value_5);
}

GtkWidget *
gs_review_histogram_new (void)
{
	GsReviewHistogram *histogram;
	histogram = g_object_new (GS_TYPE_REVIEW_HISTOGRAM, NULL);
	return GTK_WIDGET (histogram);
}