From 042cc2c1261287c011317cab847bca2e8f87b44d Mon Sep 17 00:00:00 2001 From: Bruce Cowan <bruce@bcowan.eu> Date: Wed, 21 Mar 2018 14:08:15 +0000 Subject: [PATCH] Use notify::score, and other minor fixes Closes #11 --- src/rugby-list-store.c | 91 ++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 38 deletions(-) diff --git a/src/rugby-list-store.c b/src/rugby-list-store.c index 4e7a8da..8d8b38f 100644 --- a/src/rugby-list-store.c +++ b/src/rugby-list-store.c @@ -46,6 +46,8 @@ enum static GParamSpec *properties [N_PROPS]; +// Helper functions + static gint sort_func (gconstpointer a, gconstpointer b) @@ -71,9 +73,15 @@ sort_func (gconstpointer a, return atries - btries; } +// Callbacks + static void -store_populate (RugbyListStore *self) +notify_score_cb (GObject *gobject, + GParamSpec *pspec, + gpointer user_data) { + RugbyListStore *self = RUGBY_LIST_STORE (user_data); + gint max_tries = self->score / TRY_POINTS; gint max_utries = self->score / UTRY_POINTS; @@ -105,6 +113,43 @@ store_populate (RugbyListStore *self) g_list_model_items_changed (G_LIST_MODEL (self), 0, old_length, self->items->len); } +// GListModel implementation + +static GType +rugby_list_store_get_item_type (GListModel *list) +{ + return RUGBY_TYPE_POSSIBILITY; +} + +static guint +rugby_list_store_get_n_items (GListModel *list) +{ + RugbyListStore *self = RUGBY_LIST_STORE (list); + + return self->items->len; +} + +static gpointer +rugby_list_store_get_item (GListModel *list, + guint position) +{ + RugbyListStore *self = RUGBY_LIST_STORE (list); + + g_return_val_if_fail (position < self->items->len, NULL); + + return g_object_ref (g_ptr_array_index (self->items, position)); +} + +static void +rugby_list_store_iface_init (GListModelInterface *iface) +{ + iface->get_item_type = rugby_list_store_get_item_type; + iface->get_n_items = rugby_list_store_get_n_items; + iface->get_item = rugby_list_store_get_item; +} + +// Class functions + static void rugby_list_store_finalize (GObject *object) { @@ -126,7 +171,7 @@ rugby_list_store_get_property (GObject *object, switch (prop_id) { case PROP_SCORE: - g_value_set_int (value, rugby_list_store_get_score (self)); + g_value_set_uint (value, rugby_list_store_get_score (self)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -144,47 +189,13 @@ rugby_list_store_set_property (GObject *object, switch (prop_id) { case PROP_SCORE: - rugby_list_store_set_score (self, g_value_get_int (value)); + rugby_list_store_set_score (self, g_value_get_uint (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); } } -static GType -rugby_list_store_get_item_type (GListModel *list) -{ - return RUGBY_TYPE_POSSIBILITY; -} - -static guint -rugby_list_store_get_n_items (GListModel *list) -{ - RugbyListStore *self = RUGBY_LIST_STORE (list); - - return self->items->len; -} - -static gpointer -rugby_list_store_get_item (GListModel *list, - guint position) -{ - RugbyListStore *self = RUGBY_LIST_STORE (list); - - if (position < self->items->len) - return g_object_ref (g_ptr_array_index (self->items, position)); - else - return NULL; -} - -static void -rugby_list_store_iface_init (GListModelInterface *iface) -{ - iface->get_item_type = rugby_list_store_get_item_type; - iface->get_n_items = rugby_list_store_get_n_items; - iface->get_item = rugby_list_store_get_item; -} - static void rugby_list_store_class_init (RugbyListStoreClass *klass) { @@ -210,8 +221,13 @@ rugby_list_store_init (RugbyListStore *self) { self->score = 0; self->items = g_ptr_array_new_with_free_func (g_object_unref); + + g_signal_connect (self, "notify::score", + G_CALLBACK (notify_score_cb), self); } +// Public functions + guint rugby_list_store_get_score (RugbyListStore *self) { @@ -229,7 +245,6 @@ rugby_list_store_set_score (RugbyListStore *self, if (score != self->score) { self->score = score; - store_populate (self); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SCORE]); } } -- GitLab