Skip to content
Snippets Groups Projects
Commit bed55750 authored by Bruce Cowan's avatar Bruce Cowan
Browse files

Custom GListModel

parent 0bb36124
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ sources = files(
'rugby.c',
'rugby-application.c',
'rugby-app-window.c',
'rugby-list-store.c',
'rugby-possibility.c',
'rugby-possibility-widget.c',
'rugby-scoring.c'
......
......@@ -20,6 +20,7 @@
#include "rugby-application.h"
#include "rugby-app-window.h"
#include "rugby-list-store.h"
#include "rugby-possibility.h"
#include "rugby-possibility-widget.h"
#include "rugby-scoring.h"
......@@ -28,7 +29,7 @@ struct _RugbyAppWindow
{
GtkApplicationWindow parent;
GListStore *store;
RugbyListStore *store;
GtkWidget *tryfilter;
GtkWidget *kickfilter;
......@@ -37,7 +38,7 @@ struct _RugbyAppWindow
GtkWidget *listbox;
};
G_DEFINE_TYPE (RugbyAppWindow, rugby_app_window, GTK_TYPE_APPLICATION_WINDOW);
G_DEFINE_TYPE (RugbyAppWindow, rugby_app_window, GTK_TYPE_APPLICATION_WINDOW)
static void
scorespin_value_changed_cb (GtkSpinButton *spin,
......@@ -45,7 +46,7 @@ scorespin_value_changed_cb (GtkSpinButton *spin,
{
gint score = gtk_spin_button_get_value_as_int (spin);
rugby_scoring_get_possibilities (self->store, score);
rugby_list_store_set_score (self->store, score);
gint max_tries = MAX (rugby_scoring_get_max_tries (score), 1.0);
gtk_range_set_range (GTK_RANGE (self->tryscale), 0.0, max_tries);
......@@ -75,11 +76,11 @@ rugby_app_window_init (RugbyAppWindow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
self->store = g_list_store_new (RUGBY_TYPE_POSSIBILITY);
self->store = rugby_list_store_new ();
gtk_list_box_bind_model (GTK_LIST_BOX (self->listbox),
G_LIST_MODEL (self->store),
listbox_widget_func,
self,
NULL,
NULL);
g_object_bind_property (self->tryfilter, "active",
......
/* rugby-list-store.c
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.eu>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "rugby-list-store.h"
#include "rugby-possibility.h"
#include "rugby-scoring.h"
#include <gio/gio.h>
struct _RugbyListStore
{
GObject parent_instance;
guint score;
GPtrArray *items;
};
static void rugby_list_store_iface_init (GListModelInterface *iface);
G_DEFINE_TYPE_WITH_CODE (RugbyListStore, rugby_list_store, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, rugby_list_store_iface_init))
enum
{
PROP_0,
PROP_SCORE,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
static gint
sort_func (gconstpointer a,
gconstpointer b)
{
gint atries, autries;
gint btries, butries;
g_object_get ((gpointer) a,
"tries", &atries,
"utries", &autries,
NULL);
g_object_get ((gpointer) b,
"tries", &btries,
"utries", &butries,
NULL);
gint trydiff = (btries + butries) - (atries + autries);
// Sort by total tries first, then converted
if (trydiff != 0)
return trydiff;
else
return atries - btries;
}
static void
store_populate (RugbyListStore *self)
{
gint max_tries = self->score / TRY_POINTS;
gint max_utries = self->score / UTRY_POINTS;
guint old_length = self->items->len;
g_ptr_array_remove_range (self->items, 0, self->items->len);
for (gint tries = 0; tries <= max_tries; tries++)
{
for (gint utries = 0; utries <= max_utries; utries++)
{
gint left = self->score - (tries * TRY_POINTS) - (utries * UTRY_POINTS);
if (left < 0)
break;
if (left % KICK_POINTS == 0)
{
gint kicks = left / KICK_POINTS;
RugbyPossibility *possibility = rugby_possibility_new (tries,
utries,
kicks);
g_ptr_array_add (self->items, possibility);
}
}
}
g_ptr_array_sort (self->items, sort_func);
g_list_model_items_changed (G_LIST_MODEL (self), 0, old_length, self->items->len);
}
static void
rugby_list_store_finalize (GObject *object)
{
RugbyListStore *self = RUGBY_LIST_STORE (object);
g_ptr_array_unref (self->items);
G_OBJECT_CLASS (rugby_list_store_parent_class)->finalize (object);
}
static void
rugby_list_store_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
RugbyListStore *self = RUGBY_LIST_STORE (object);
switch (prop_id)
{
case PROP_SCORE:
g_value_set_int (value, rugby_list_store_get_score (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_list_store_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
RugbyListStore *self = RUGBY_LIST_STORE (object);
switch (prop_id)
{
case PROP_SCORE:
rugby_list_store_set_score (self, g_value_get_int (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)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = rugby_list_store_finalize;
object_class->get_property = rugby_list_store_get_property;
object_class->set_property = rugby_list_store_set_property;
properties[PROP_SCORE] = g_param_spec_uint ("score",
"Score",
"Score of the match",
0, 200, 0,
G_PARAM_READWRITE);
g_object_class_install_properties (object_class,
N_PROPS,
properties);
}
static void
rugby_list_store_init (RugbyListStore *self)
{
self->score = 0;
self->items = g_ptr_array_new_with_free_func (g_object_unref);
}
guint
rugby_list_store_get_score (RugbyListStore *self)
{
g_return_val_if_fail (RUGBY_IS_LIST_STORE (self), 0);
return self->score;
}
void
rugby_list_store_set_score (RugbyListStore *self,
guint score)
{
g_return_if_fail (RUGBY_IS_LIST_STORE (self));
if (score != self->score)
{
self->score = score;
store_populate (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SCORE]);
}
}
RugbyListStore *
rugby_list_store_new (void)
{
return g_object_new (RUGBY_TYPE_LIST_STORE, NULL);
}
/* rugby-list-store.h
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.eu>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include <glib-object.h>
G_BEGIN_DECLS
#define RUGBY_TYPE_LIST_STORE (rugby_list_store_get_type())
G_DECLARE_FINAL_TYPE (RugbyListStore, rugby_list_store, RUGBY, LIST_STORE, GObject)
RugbyListStore *rugby_list_store_new (void);
guint rugby_list_store_get_score (RugbyListStore *self);
void rugby_list_store_set_score (RugbyListStore *self,
guint score);
G_END_DECLS
......@@ -19,65 +19,6 @@
#include "rugby-possibility.h"
#include "rugby-scoring.h"
static gint
compare_func (gconstpointer a,
gconstpointer b,
gpointer user_data)
{
gint atries, autries;
gint btries, butries;
g_object_get ((gpointer) a,
"tries", &atries,
"utries", &autries,
NULL);
g_object_get ((gpointer) b,
"tries", &btries,
"utries", &butries,
NULL);
gint trydiff = (atries + autries) - (btries + butries);
// Sort by total tries first, then converted
if (trydiff != 0)
return trydiff;
else
return atries - btries;
}
void
rugby_scoring_get_possibilities (GListStore *store,
gint score)
{
gint max_tries = score / TRY_POINTS;
gint max_utries = score / UTRY_POINTS;
g_list_store_remove_all (store);
for (gint tries = 0; tries <= max_tries; tries++)
{
for (gint utries = 0; utries <= max_utries; utries++)
{
gint left = score - (tries * TRY_POINTS) - (utries * UTRY_POINTS);
if (left < 0)
break;
if (left % KICK_POINTS == 0)
{
gint kicks = left / KICK_POINTS;
g_autoptr(RugbyPossibility) possibility = rugby_possibility_new (tries,
utries,
kicks);
g_list_store_append (store, possibility);
}
}
}
g_list_store_sort (store, compare_func, NULL);
}
gint
rugby_scoring_get_max_tries (gint score)
{
......
......@@ -26,15 +26,6 @@ G_BEGIN_DECLS
#define UTRY_POINTS 5
#define KICK_POINTS 3
typedef enum
{
RUGBY_SCORE_TYPE_TRY,
RUGBY_SCORE_TYPE_UTRY,
RUGBY_SCORE_TYPE_KICK
} RugbyScoreType;
void rugby_scoring_get_possibilities (GListStore *model,
gint score);
gint rugby_scoring_get_max_tries (gint score);
gint rugby_scoring_get_max_kicks (gint score);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment