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

Use a GListStore directly and simplify

parent c3e5eb35
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ datadir = get_option('datadir')
gnome = import('gnome')
gio_dep = dependency('gio-2.0', version: '>= 2.44')
gio_dep = dependency('gio-2.0', version: '>= 2.46')
gtk_dep = dependency('gtk4', version: '>= 4.0')
conf = configuration_data()
......@@ -28,8 +28,8 @@ config_h = declare_dependency(
)
cflags = []
cflags += ['-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_44',
'-DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_44']
cflags += ['-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_46',
'-DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_46']
cflags += ['-DGDK_VERSION_MIN_REQUIRED=GDK_VERSION_4_0',
'-DGDK_VERSION_MAX_ALLOWED=GDK_VERSION_4_0']
......
......@@ -14,10 +14,10 @@ struct _RugbyListStore
{
GObject parent_instance;
GSettings *settings;
GPtrArray *items;
int score;
GListStore *items;
GSettings *settings;
};
static void rugby_list_store_list_model_iface_init (GListModelInterface *iface);
......@@ -28,8 +28,7 @@ G_DEFINE_TYPE_WITH_CODE (RugbyListStore, rugby_list_store, G_TYPE_OBJECT,
enum
{
PROP_0,
PROP_SCORE,
PROP_SCORE = 1,
N_PROPS
};
......@@ -38,17 +37,18 @@ static GParamSpec *properties[N_PROPS];
// Helper functions
static int
sort_func (gconstpointer a,
gconstpointer b)
sort_func ( gconstpointer a,
gconstpointer b,
G_GNUC_UNUSED gpointer user_data)
{
int atries, autries;
int btries, butries;
g_object_get (*((gpointer *) a),
g_object_get ((gpointer) a,
"tries", &atries,
"utries", &autries,
NULL);
g_object_get (*((gpointer *) b),
g_object_get ((gpointer) b,
"tries", &btries,
"utries", &butries,
NULL);
......@@ -69,12 +69,12 @@ process_data (RugbyListStore *self)
int utry_points = g_settings_get_int (self->settings, "utry-points");
int kick_points = g_settings_get_int (self->settings, "kick-points");
unsigned old_length = g_list_model_get_n_items (G_LIST_MODEL (self->items));
g_list_store_remove_all (self->items);
int max_tries = self->score / try_points;
int max_utries = self->score / utry_points;
unsigned old_length = self->items->len;
g_ptr_array_remove_range (self->items, 0, self->items->len);
for (int tries = 0; tries <= max_tries; tries++)
{
for (int utries = 0; utries <= max_utries; utries++)
......@@ -91,24 +91,20 @@ process_data (RugbyListStore *self)
RugbyPossibility *possibility = rugby_possibility_new (tries,
utries,
kicks);
g_ptr_array_add (self->items, possibility);
g_list_store_append (self->items, possibility);
g_object_unref (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
on_score_changed (RugbyListStore *self)
{
process_data (self);
g_list_store_sort (self->items, sort_func, NULL);
g_list_model_items_changed (G_LIST_MODEL (self), 0, old_length,
g_list_model_get_n_items (G_LIST_MODEL (self->items)));
}
static void
on_settings_changed (GSettings G_GNUC_UNUSED *settings,
char G_GNUC_UNUSED *key,
on_settings_changed (G_GNUC_UNUSED GSettings *settings,
G_GNUC_UNUSED char *key,
gpointer user_data)
{
process_data (RUGBY_LIST_STORE (user_data));
......@@ -127,7 +123,7 @@ rugby_list_store_get_n_items (GListModel *list)
{
RugbyListStore *self = RUGBY_LIST_STORE (list);
return self->items->len;
return g_list_model_get_n_items (G_LIST_MODEL (self->items));
}
static gpointer
......@@ -136,9 +132,7 @@ rugby_list_store_get_item (GListModel *list,
{
RugbyListStore *self = RUGBY_LIST_STORE (list);
g_assert (position < self->items->len);
return g_object_ref (g_ptr_array_index (self->items, position));
return g_list_model_get_item (G_LIST_MODEL (self->items), position);
}
static void
......@@ -157,20 +151,11 @@ rugby_list_store_dispose (GObject *object)
RugbyListStore *self = RUGBY_LIST_STORE (object);
g_clear_object (&self->settings);
g_clear_object (&self->items);
G_OBJECT_CLASS (rugby_list_store_parent_class)->dispose (object);
}
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,
unsigned prop_id,
......@@ -213,7 +198,6 @@ rugby_list_store_class_init (RugbyListStoreClass *klass)
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = rugby_list_store_dispose;
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;
......@@ -234,7 +218,7 @@ rugby_list_store_init (RugbyListStore *self)
G_CALLBACK (on_settings_changed), self);
self->score = 0;
self->items = g_ptr_array_new_with_free_func (g_object_unref);
self->items = g_list_store_new (RUGBY_TYPE_POSSIBILITY);
}
// Public functions
......@@ -256,7 +240,7 @@ rugby_list_store_set_score (RugbyListStore *self,
if (score != self->score)
{
self->score = score;
on_score_changed (self);
process_data (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_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