From c61766d8776c398ccbda78d831211e6b837169eb Mon Sep 17 00:00:00 2001 From: Bruce Cowan <bruce@bcowan.me.uk> Date: Sun, 14 Mar 2021 11:53:52 +0000 Subject: [PATCH] Make possibility widget a ListBoxRow --- src/meson.build | 4 +- src/rugby-app-window.c | 7 +- ...ility-widget.c => rugby-possibility-row.c} | 108 ++++++++++-------- src/rugby-possibility-row.h | 21 ++++ src/rugby-possibility-widget.h | 21 ---- 5 files changed, 86 insertions(+), 75 deletions(-) rename src/{rugby-possibility-widget.c => rugby-possibility-row.c} (58%) create mode 100644 src/rugby-possibility-row.h delete mode 100644 src/rugby-possibility-widget.h diff --git a/src/meson.build b/src/meson.build index bace1d8..d01b95b 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2016-2020 Bruce Cowan <bruce@bcowan.me.uk> +# SPDX-FileCopyrightText: 2016-2021 Bruce Cowan <bruce@bcowan.me.uk> # # SPDX-License-Identifier: CC0-1.0 sources = [ @@ -6,7 +6,7 @@ sources = [ 'rugby-app-window.c', 'rugby-list-store.c', 'rugby-possibility.c', - 'rugby-possibility-widget.c', + 'rugby-possibility-row.c', 'rugby-pref-window.c', ] diff --git a/src/rugby-app-window.c b/src/rugby-app-window.c index c15b822..35a591f 100644 --- a/src/rugby-app-window.c +++ b/src/rugby-app-window.c @@ -1,5 +1,6 @@ /* - * SPDX-FileCopyrightText: 2017, 2018 Bruce Cowan <bruce@bcowan.me.uk> + * SPDX-FileCopyrightText: 2017-2021 Bruce Cowan <bruce@bcowan.me.uk> + * * SPDX-License-Identifier: GPL-3.0-or-later */ @@ -8,7 +9,7 @@ #include "rugby-list-store.h" #include "rugby-possibility.h" -#include "rugby-possibility-widget.h" +#include "rugby-possibility-row.h" struct _RugbyAppWindow { @@ -28,7 +29,7 @@ listbox_widget_func ( gpointer item, { RugbyPossibility *possibility = RUGBY_POSSIBILITY (item); - return rugby_possibility_widget_new (possibility); + return rugby_possibility_row_new (possibility); } static void diff --git a/src/rugby-possibility-widget.c b/src/rugby-possibility-row.c similarity index 58% rename from src/rugby-possibility-widget.c rename to src/rugby-possibility-row.c index 7267afb..13c7d8a 100644 --- a/src/rugby-possibility-widget.c +++ b/src/rugby-possibility-row.c @@ -5,19 +5,20 @@ */ #include "config.h" -#include "rugby-possibility-widget.h" + +#include "rugby-possibility-row.h" #include <glib/gi18n.h> -struct _RugbyPossibilityWidget +struct _RugbyPossibilityRow { - GtkWidget parent_instance; + GtkListBoxRow parent_instance; GSettings *settings; RugbyPossibility *possibility; }; -G_DEFINE_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, GTK_TYPE_WIDGET) +G_DEFINE_TYPE (RugbyPossibilityRow, rugby_possibility_row, GTK_TYPE_LIST_BOX_ROW) enum { @@ -29,23 +30,23 @@ enum static GParamSpec *properties[N_PROPS]; static void -rugby_possibility_widget_dispose (GObject *object) +rugby_possibility_row_dispose (GObject *object) { - RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object); + RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object); g_clear_object (&self->settings); g_clear_object (&self->possibility); - G_OBJECT_CLASS (rugby_possibility_widget_parent_class)->dispose (object); + G_OBJECT_CLASS (rugby_possibility_row_parent_class)->dispose (object); } static void -rugby_possibility_widget_get_property (GObject *object, - unsigned prop_id, - GValue *value, - GParamSpec *pspec) +rugby_possibility_row_get_property (GObject *object, + unsigned prop_id, + GValue *value, + GParamSpec *pspec) { - RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object); + RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object); switch (prop_id) { @@ -58,12 +59,12 @@ rugby_possibility_widget_get_property (GObject *object, } static void -rugby_possibility_widget_set_property (GObject *object, - unsigned prop_id, - const GValue *value, - GParamSpec *pspec) +rugby_possibility_row_set_property (GObject *object, + unsigned prop_id, + const GValue *value, + GParamSpec *pspec) { - RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object); + RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object); switch (prop_id) { @@ -77,10 +78,10 @@ rugby_possibility_widget_set_property (GObject *object, static void render_bar (GtkSnapshot *snapshot, - double x, - double y, - double w, - double h, + float x, + float y, + float w, + float h, const GdkRGBA color) { GskRoundedRect rounded; @@ -97,15 +98,15 @@ render_bar (GtkSnapshot *snapshot, GdkRGBA black = { 0.0, 0.0, 0.0, 0.2 }; gtk_snapshot_append_border (snapshot, &rounded, - (float[]) { 2.f, 2.f, 2.f, 2.f }, + (float[]) { 2.0, 2.0, 2.0, 2.0 }, (GdkRGBA[]) { black, black, black, black }); } static void -rugby_possibility_widget_snapshot (GtkWidget *widget, - GtkSnapshot *snapshot) +rugby_possibility_row_snapshot (GtkWidget *widget, + GtkSnapshot *snapshot) { - RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (widget); + RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (widget); int try_points = g_settings_get_int (self->settings, "try-points"); int utry_points = g_settings_get_int (self->settings, "utry-points"); @@ -113,7 +114,7 @@ rugby_possibility_widget_snapshot (GtkWidget *widget, int width = gtk_widget_get_width (widget); int height = gtk_widget_get_height (widget); - double x = 0.0; + float x = 0.0; int tries, utries, kicks; g_object_get (self->possibility, @@ -124,37 +125,41 @@ rugby_possibility_widget_snapshot (GtkWidget *widget, int score = tries * try_points + utries * utry_points + kicks * kick_points; // Tries - double w = width / (score / (double) try_points); + float w = width / (score / (float) try_points); + for (int i = 0; i < tries; i++) { // Green - render_bar (snapshot, x, 0.0, w, height, (GdkRGBA) { 0.20, 0.82, 0.48, 1.0 }); + render_bar (snapshot, x, 0.0, w, height, + (GdkRGBA) { 0.20, 0.82, 0.48, 1.0 }); x += w; } // Unconverted tries - w = width / (score / (double) utry_points); + w = width / (score / (float) utry_points); for (int i = 0; i < utries; i++) { // Red - render_bar (snapshot, x, 0.0, w, height, (GdkRGBA) { 0.88, 0.11, 0.14, 1.0 }); + render_bar (snapshot, x, 0.0, w, height, + (GdkRGBA) { 0.88, 0.11, 0.14, 1.0 }); x += w; } // Unconverted kicks - w = width / (score / (double) kick_points); + w = width / (score / (float) kick_points); for (int i = 0; i < kicks; i++) { // Yellow - render_bar (snapshot, x, 0.0, w, height, (GdkRGBA) { 0.97, 0.83, 0.18, 1.0 }); + render_bar (snapshot, x, 0.0, w, height, + (GdkRGBA) { 0.96, 0.83, 0.18, 1.0 }); x += w; } } static void -rugby_possibility_widget_constructed (GObject *obj) +rugby_possibility_row_constructed (GObject *obj) { - RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (obj); + RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (obj); int tries, utries, kicks; g_autoptr (GString) tooltip = g_string_new (NULL); @@ -166,11 +171,16 @@ rugby_possibility_widget_constructed (GObject *obj) if ((tries + utries) > 0) { - g_string_append_printf (tooltip, - ngettext ("%d try, %d converted", - "%d tries, %d converted", - tries + utries), - tries + utries, tries); + if (tries == 1 && utries == 0) + g_string_printf (tooltip, "1 converted try"); + else if (tries == 0 && utries == 1) + g_string_printf (tooltip, "1 unconverted try"); + else + g_string_append_printf (tooltip, + ngettext ("%d try, %d converted", + "%d tries, %d converted", + tries + utries), + tries + utries, tries); if (kicks > 0) g_string_append_printf (tooltip, @@ -189,21 +199,21 @@ rugby_possibility_widget_constructed (GObject *obj) gtk_widget_set_tooltip_text (GTK_WIDGET (self), tooltip->str); - G_OBJECT_CLASS (rugby_possibility_widget_parent_class)->constructed (obj); + G_OBJECT_CLASS (rugby_possibility_row_parent_class)->constructed (obj); } static void -rugby_possibility_widget_class_init (RugbyPossibilityWidgetClass *klass) +rugby_possibility_row_class_init (RugbyPossibilityRowClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); - object_class->constructed = rugby_possibility_widget_constructed; - object_class->dispose = rugby_possibility_widget_dispose; - object_class->get_property = rugby_possibility_widget_get_property; - object_class->set_property = rugby_possibility_widget_set_property; + object_class->constructed = rugby_possibility_row_constructed; + object_class->dispose = rugby_possibility_row_dispose; + object_class->get_property = rugby_possibility_row_get_property; + object_class->set_property = rugby_possibility_row_set_property; - widget_class->snapshot = rugby_possibility_widget_snapshot; + widget_class->snapshot = rugby_possibility_row_snapshot; properties[PROP_POSSIBILITY] = g_param_spec_object ("possibility", @@ -218,15 +228,15 @@ rugby_possibility_widget_class_init (RugbyPossibilityWidgetClass *klass) } static void -rugby_possibility_widget_init (RugbyPossibilityWidget *self) +rugby_possibility_row_init (RugbyPossibilityRow *self) { self->settings = g_settings_new ("uk.me.bcowan.Rugby"); } GtkWidget * -rugby_possibility_widget_new (RugbyPossibility *possibility) +rugby_possibility_row_new (RugbyPossibility *possibility) { - return g_object_new (RUGBY_TYPE_POSSIBILITY_WIDGET, + return g_object_new (RUGBY_TYPE_POSSIBILITY_ROW, "possibility", possibility, NULL); } diff --git a/src/rugby-possibility-row.h b/src/rugby-possibility-row.h new file mode 100644 index 0000000..868f048 --- /dev/null +++ b/src/rugby-possibility-row.h @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2018-2021 Bruce Cowan <bruce@bcowan.me.uk> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include <gtk/gtk.h> + +#include "rugby-possibility.h" + +G_BEGIN_DECLS + +#define RUGBY_TYPE_POSSIBILITY_ROW (rugby_possibility_row_get_type()) + +G_DECLARE_FINAL_TYPE (RugbyPossibilityRow, rugby_possibility_row, RUGBY, POSSIBILITY_ROW, GtkListBoxRow) + +GtkWidget * rugby_possibility_row_new (RugbyPossibility *possibility); + +G_END_DECLS diff --git a/src/rugby-possibility-widget.h b/src/rugby-possibility-widget.h deleted file mode 100644 index 3c40b81..0000000 --- a/src/rugby-possibility-widget.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2018-2021 Bruce Cowan <bruce@bcowan.me.uk> - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ - -#pragma once - -#include <gtk/gtk.h> - -#include "rugby-possibility.h" - -G_BEGIN_DECLS - -#define RUGBY_TYPE_POSSIBILITY_WIDGET (rugby_possibility_widget_get_type()) - -G_DECLARE_FINAL_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, RUGBY, POSSIBILITY_WIDGET, GtkWidget) - -GtkWidget * rugby_possibility_widget_new (RugbyPossibility *possibility); - -G_END_DECLS -- GitLab