diff --git a/interface.ui b/interface.ui index ec53fe21428d993357ca2aafb51105f9e947ac47..cbe4810c878ec4f528409807845502c30400fe0b 100644 --- a/interface.ui +++ b/interface.ui @@ -38,6 +38,10 @@ <property name="upper">1</property> <property name="step_increment">1</property> </object> + <object class="GtkAdjustment" id="kickadj"> + <property name="upper">1</property> + <property name="step_increment">1</property> + </object> <object class="GtkAdjustment" id="scoreadj"> <property name="upper">150</property> <property name="step_increment">1</property> @@ -61,7 +65,7 @@ <property name="left_attach">0</property> <property name="top_attach">0</property> <property name="width">1</property> - <property name="height">1</property> + <property name="height">2</property> </packing> </child> <child> @@ -75,12 +79,13 @@ <property name="left_attach">1</property> <property name="top_attach">0</property> <property name="width">1</property> - <property name="height">1</property> + <property name="height">2</property> </packing> </child> <child> - <object class="GtkCheckButton" id="enablefilter"> + <object class="GtkCheckButton" id="tryfilter"> <property name="label" translatable="yes">_Try filter:</property> + <property name="use_action_appearance">False</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">False</property> @@ -97,7 +102,7 @@ </packing> </child> <child> - <object class="GtkScale" id="filterscale"> + <object class="GtkScale" id="tryscale"> <property name="visible">True</property> <property name="sensitive">False</property> <property name="can_focus">True</property> @@ -105,7 +110,6 @@ <property name="adjustment">filteradj</property> <property name="round_digits">0</property> <property name="digits">0</property> - <property name="orientation">horizontal</property> </object> <packing> <property name="left_attach">3</property> @@ -119,8 +123,8 @@ <property name="height_request">300</property> <property name="visible">True</property> <property name="can_focus">True</property> - <property name="hscrollbar_policy">never</property> <property name="vexpand">True</property> + <property name="hscrollbar_policy">never</property> <child> <object class="GtkTreeView" id="treeview"> <property name="width_request">400</property> @@ -137,7 +141,7 @@ </object> <packing> <property name="left_attach">0</property> - <property name="top_attach">1</property> + <property name="top_attach">2</property> <property name="width">4</property> <property name="height">1</property> </packing> @@ -150,11 +154,45 @@ </object> <packing> <property name="left_attach">0</property> - <property name="top_attach">2</property> + <property name="top_attach">3</property> <property name="width">4</property> <property name="height">1</property> </packing> </child> + <child> + <object class="GtkCheckButton" id="kickfilter"> + <property name="label" translatable="yes">Kick filter:</property> + <property name="use_action_appearance">False</property> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="receives_default">False</property> + <property name="use_action_appearance">False</property> + <property name="xalign">0</property> + <property name="draw_indicator">True</property> + </object> + <packing> + <property name="left_attach">2</property> + <property name="top_attach">1</property> + <property name="width">1</property> + <property name="height">1</property> + </packing> + </child> + <child> + <object class="GtkScale" id="kickscale"> + <property name="visible">True</property> + <property name="sensitive">False</property> + <property name="can_focus">True</property> + <property name="adjustment">kickadj</property> + <property name="round_digits">1</property> + <property name="digits">0</property> + </object> + <packing> + <property name="left_attach">3</property> + <property name="top_attach">1</property> + <property name="width">1</property> + <property name="height">1</property> + </packing> + </child> </object> </child> </object> diff --git a/rugby-application.c b/rugby-application.c index 087590e727a4bf013709f2d45a2919d0a0fc6471..eed9573ec8e0eed8e1833beba68e34daf4394e87 100644 --- a/rugby-application.c +++ b/rugby-application.c @@ -6,8 +6,10 @@ struct _RugbyApplicationPrivate { - GtkWidget *enablefilter; - GtkWidget *filterscale; + GtkWidget *tryfilter; + GtkWidget *tryscale; + GtkWidget *kickfilter; + GtkWidget *kickscale; RugbyScoreStore *store; GtkTreeModel *fmodel; }; @@ -19,31 +21,48 @@ scorespin_changed_cb (GtkSpinButton *spin, RugbyApplication *app) { gint score; - gint max; + gint max_tries; + gint max_kicks; score = gtk_spin_button_get_value_as_int (spin); rugby_score_store_set_score (app->priv->store, score); /* I'd rather not have to do this */ - max = MAX (rugby_scoring_get_max_tries (score), 1.0); - gtk_range_set_range (GTK_RANGE (app->priv->filterscale), 0.0, max); + max_tries = MAX (rugby_scoring_get_max_tries (score), 1.0); + gtk_range_set_range (GTK_RANGE (app->priv->tryscale), 0.0, max_tries); + + max_kicks = MAX (rugby_scoring_get_max_kicks (score), 1.0); + gtk_range_set_range (GTK_RANGE (app->priv->kickscale), 0.0, max_kicks); +} + +static void +tryfilter_toggled_cb (GtkToggleButton *toggle, + RugbyApplication *app) +{ + if (gtk_toggle_button_get_active (toggle)) + gtk_widget_set_sensitive (app->priv->tryscale, TRUE); + else + gtk_widget_set_sensitive (app->priv->tryscale, FALSE); + + gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (app->priv->fmodel)); } +/* TODO replace this with one function */ static void -enablefilter_toggled_cb (GtkToggleButton *toggle, - RugbyApplication *app) +kickfilter_toggled_cb (GtkToggleButton *toggle, + RugbyApplication *app) { if (gtk_toggle_button_get_active (toggle)) - gtk_widget_set_sensitive (app->priv->filterscale, TRUE); + gtk_widget_set_sensitive (app->priv->kickscale, TRUE); else - gtk_widget_set_sensitive (app->priv->filterscale, FALSE); + gtk_widget_set_sensitive (app->priv->kickscale, FALSE); gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (app->priv->fmodel)); } static void -filterscale_changed_cb (GtkRange *range, - RugbyApplication *app) +scale_changed_cb (GtkRange *range, + RugbyApplication *app) { gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (app->priv->fmodel)); } @@ -70,24 +89,29 @@ filter_func (GtkTreeModel *model, GtkTreeIter *iter, RugbyApplication *app) { - gint tries, utries; - gint current; - - if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->priv->enablefilter))) - { - current = (gint) gtk_range_get_value (GTK_RANGE (app->priv->filterscale)); - - gtk_tree_model_get (model, iter, - RUGBY_SCORE_STORE_TRIES, &tries, - RUGBY_SCORE_STORE_UTRIES, &utries, -1); - - if (tries + utries == current) - return TRUE; - else - return FALSE; - } - else - return TRUE; + RugbyApplicationPrivate *priv = app->priv; + gint ctries, ckicks; /* TODO fix these crap names */ + gint tries, utries, kicks; + gboolean try, kick; + + ctries = (gint) gtk_range_get_value (GTK_RANGE (app->priv->tryscale)); + ckicks = (gint) gtk_range_get_value (GTK_RANGE (app->priv->kickscale)); + gtk_tree_model_get (model, iter, + RUGBY_SCORE_STORE_TRIES, &tries, + RUGBY_SCORE_STORE_UTRIES, &utries, + RUGBY_SCORE_STORE_KICKS, &kicks, -1); + + try = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->tryfilter)); + kick = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->kickfilter)); + + if (try && kick) + return ((tries + utries == ctries) && (kicks == ckicks)) ? TRUE : FALSE; + else if (try) + return (tries + utries == ctries) ? TRUE : FALSE; + else if (kick) + return (kicks == ckicks) ? TRUE : FALSE; + + return TRUE; } static void @@ -170,13 +194,21 @@ rugby_application_activate (GApplication *app) g_signal_connect (score, "value-changed", G_CALLBACK (scorespin_changed_cb), app); - priv->enablefilter = GTK_WIDGET (gtk_builder_get_object (builder, "enablefilter")); - g_signal_connect (priv->enablefilter, "toggled", - G_CALLBACK (enablefilter_toggled_cb), app); + priv->tryfilter = GTK_WIDGET (gtk_builder_get_object (builder, "tryfilter")); + g_signal_connect (priv->tryfilter, "toggled", + G_CALLBACK (tryfilter_toggled_cb), app); + + priv->tryscale = GTK_WIDGET (gtk_builder_get_object (builder, "tryscale")); + g_signal_connect (priv->tryscale, "value-changed", + G_CALLBACK (scale_changed_cb), app); + + priv->kickfilter = GTK_WIDGET (gtk_builder_get_object (builder, "kickfilter")); + g_signal_connect (priv->kickfilter, "toggled", + G_CALLBACK (kickfilter_toggled_cb), app); - priv->filterscale = GTK_WIDGET (gtk_builder_get_object (builder, "filterscale")); - g_signal_connect (priv->filterscale, "value-changed", - G_CALLBACK (filterscale_changed_cb), app); + priv->kickscale = GTK_WIDGET (gtk_builder_get_object (builder, "kickscale")); + g_signal_connect (priv->kickscale, "value-changed", + G_CALLBACK (scale_changed_cb), app); tree = GTK_WIDGET (gtk_builder_get_object (builder, "treeview")); @@ -185,7 +217,7 @@ rugby_application_activate (GApplication *app) column = gtk_tree_view_column_new_with_attributes ("Score", renderer, "tries", RUGBY_SCORE_STORE_TRIES, "utries", RUGBY_SCORE_STORE_UTRIES, - "pens", RUGBY_SCORE_STORE_PENS, + "kicks", RUGBY_SCORE_STORE_KICKS, NULL); gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); diff --git a/rugby-cell-renderer-score.c b/rugby-cell-renderer-score.c index 040340e283072fd292aaac71629f7240a2cbbd8c..d7dbcc733737f946eccee9d1db9123a124648906 100644 --- a/rugby-cell-renderer-score.c +++ b/rugby-cell-renderer-score.c @@ -6,20 +6,20 @@ enum PROP_0, PROP_TRIES, PROP_UTRIES, - PROP_PENS + PROP_KICKS }; struct _RugbyCellRendererScorePrivate { gint tries; gint utries; - gint pens; + gint kicks; }; /* Style classes for the different types of score */ #define RUGBY_STYLE_CLASS_TRY "try" #define RUGBY_STYLE_CLASS_UTRY "utry" -#define RUGBY_STYLE_CLASS_PEN "pen" +#define RUGBY_STYLE_CLASS_KICK "kick" G_DEFINE_TYPE (RugbyCellRendererScore, rugby_cell_renderer_score, GTK_TYPE_CELL_RENDERER) @@ -39,8 +39,8 @@ rugby_cell_renderer_score_set_property (GObject *object, case PROP_UTRIES: priv->utries = g_value_get_int (value); break; - case PROP_PENS: - priv->pens = g_value_get_int (value); + case PROP_KICKS: + priv->kicks = g_value_get_int (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -64,8 +64,8 @@ rugby_cell_renderer_score_get_property (GObject *object, case PROP_UTRIES: g_value_set_int (value, priv->utries); break; - case PROP_PENS: - g_value_set_int (value, priv->pens); + case PROP_KICKS: + g_value_set_int (value, priv->kicks); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -131,8 +131,8 @@ render_bar (GtkStyleContext *context, case RUGBY_SCORE_TYPE_UTRY: gtk_style_context_add_class (context, RUGBY_STYLE_CLASS_UTRY); break; - case RUGBY_SCORE_TYPE_PEN: - gtk_style_context_add_class (context, RUGBY_STYLE_CLASS_PEN); + case RUGBY_SCORE_TYPE_KICK: + gtk_style_context_add_class (context, RUGBY_STYLE_CLASS_KICK); break; } @@ -154,7 +154,7 @@ rugby_cell_renderer_score_render (GtkCellRenderer *cell, gint xpad, ypad; gdouble x, y, total_w, w, h; GtkBorder padding, border; - gint tries, utries, pens; + gint tries, utries, kicks; gint total; gint i; @@ -184,8 +184,8 @@ rugby_cell_renderer_score_render (GtkCellRenderer *cell, tries = priv->tries; utries = priv->utries; - pens = priv->pens; - total = tries * TRY_POINTS + utries * UTRY_POINTS + pens * PEN_POINTS; + kicks = priv->kicks; + total = tries * TRY_POINTS + utries * UTRY_POINTS + kicks * KICK_POINTS; gtk_style_context_save (context); gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR); @@ -205,9 +205,9 @@ rugby_cell_renderer_score_render (GtkCellRenderer *cell, } w = total_w / (total / 3.0); - for (i = 0; i < pens; i++) + for (i = 0; i < kicks; i++) { - render_bar (context, cr, x, y, w, h, RUGBY_SCORE_TYPE_PEN); + render_bar (context, cr, x, y, w, h, RUGBY_SCORE_TYPE_KICK); x += w; } @@ -237,8 +237,8 @@ rugby_cell_renderer_score_class_init (RugbyCellRendererScoreClass *klass) 0, 50, 0, G_PARAM_READWRITE)); - g_object_class_install_property (obj_class, PROP_PENS, - g_param_spec_int ("pens", "Penalties", "Number of penalties", + g_object_class_install_property (obj_class, PROP_KICKS, + g_param_spec_int ("kicks", "Kicks", "Number of kicks", 0, 50, 0, G_PARAM_READWRITE)); @@ -252,7 +252,7 @@ rugby_cell_renderer_score_init (RugbyCellRendererScore *cellscore) priv->tries = 0; priv->utries = 0; - priv->pens = 0; + priv->kicks = 0; gtk_cell_renderer_set_padding (GTK_CELL_RENDERER (cellscore), 2, 2); diff --git a/rugby-score-store.c b/rugby-score-store.c index 49b48cde46cba99141fffea1d50609ee70b0c430..8607522bac7dde9eb8b2032ff08f4647b462e1e6 100644 --- a/rugby-score-store.c +++ b/rugby-score-store.c @@ -96,18 +96,17 @@ rugby_score_store_class_init (RugbyScoreStoreClass *klass) } static void -populate_store (GObject *object, - GParamSpec *pspec) +populate_store (RugbyScoreStore *store) { - GtkListStore *store = GTK_LIST_STORE (object); - RugbyScoreStorePrivate *priv = RUGBY_SCORE_STORE (object)->priv; + GtkListStore *lstore = GTK_LIST_STORE (store); + RugbyScoreStorePrivate *priv = RUGBY_SCORE_STORE (store)->priv; GVariant *possibilities; GVariant *possibility; GVariantIter iter; gsize total; /* Clear the store */ - gtk_list_store_clear (store); + gtk_list_store_clear (lstore); possibilities = rugby_scoring_get_possibilities (priv->score); if (possibilities == NULL) @@ -117,11 +116,11 @@ populate_store (GObject *object, while ((possibility = g_variant_iter_next_value (&iter))) { - gint tries, utries, pens; + gint tries, utries, kicks; GString *tooltip; GtkTreeIter iter; - g_variant_get (possibility, "(iii)", &tries, &utries, &pens); + g_variant_get (possibility, "(iii)", &tries, &utries, &kicks); g_variant_unref (possibility); tooltip = g_string_new_len (NULL, 20); @@ -129,20 +128,20 @@ populate_store (GObject *object, { g_string_append_printf (tooltip, "%d tries, %d converted", tries + utries, tries); - if (pens > 0) + if (kicks > 0) g_string_append_printf (tooltip, ", %d kicks", - pens); + kicks); } else if (utries == 0 && tries == 0) g_string_append_printf (tooltip, "%d kicks", - pens); + kicks); /* Put result in list store */ - gtk_list_store_append (store, &iter); - gtk_list_store_set (store, &iter, + gtk_list_store_append (lstore, &iter); + gtk_list_store_set (lstore, &iter, RUGBY_SCORE_STORE_TRIES, tries, RUGBY_SCORE_STORE_UTRIES, utries, - RUGBY_SCORE_STORE_PENS, pens, + RUGBY_SCORE_STORE_KICKS, kicks, RUGBY_SCORE_STORE_TOOLTIP, tooltip->str, -1); @@ -163,9 +162,6 @@ rugby_score_store_init (RugbyScoreStore *store) gtk_list_store_set_column_types (GTK_LIST_STORE (store), RUGBY_SCORE_STORE_COLUMNS, types); priv->score = 0; - g_signal_connect (store, "notify::score", - G_CALLBACK (populate_store), NULL); - store->priv = priv; } @@ -214,6 +210,7 @@ rugby_score_store_set_score (RugbyScoreStore *store, g_return_if_fail (score >= 0 && score <= 150); store->priv->score = score; - g_object_notify_by_pspec (G_OBJECT (store), pspecs[PROP_SCORE]); + + populate_store (store); } diff --git a/rugby-score-store.h b/rugby-score-store.h index fc9c8b02cb4f8ef5fd0b2b12241f578c283f897a..4ace5439ecfe7338d816861a15f4a43d69a3989d 100644 --- a/rugby-score-store.h +++ b/rugby-score-store.h @@ -9,7 +9,7 @@ enum { RUGBY_SCORE_STORE_TRIES, RUGBY_SCORE_STORE_UTRIES, - RUGBY_SCORE_STORE_PENS, + RUGBY_SCORE_STORE_KICKS, RUGBY_SCORE_STORE_TOOLTIP, RUGBY_SCORE_STORE_COLUMNS }; diff --git a/rugby-scoring.c b/rugby-scoring.c index 5a4b891ce9507969a29b8b6795636664e0019220..3dda681edc298d91c62a2b5e061077112da167d6 100644 --- a/rugby-scoring.c +++ b/rugby-scoring.c @@ -37,11 +37,11 @@ rugby_scoring_get_possibilities (gint score) continue; /* If the score after pens is 0, it's a possibility */ - if (left % PEN_POINTS == 0) + if (left % KICK_POINTS == 0) { gint pens; - pens = left / PEN_POINTS; + pens = left / KICK_POINTS; /* Add result to variant */ g_variant_builder_add (&builder, "(iii)", tries, utries, pens); @@ -70,3 +70,17 @@ rugby_scoring_get_max_tries (gint score) { return (score % 5 == 1) ? score / UTRY_POINTS - 1 : score / UTRY_POINTS; } + +/** + * rugby_scoring_get_max_kicks: + * @score: the score of a team + * + * Gets the maximum number of kicks (penalties and drop goals) for a given score. + * + * Returns: the maximum number of kicks possible + */ +gint +rugby_scoring_get_max_kicks (gint score) +{ + return (score % 3 == 1) ? score / KICK_POINTS : score / KICK_POINTS - 1; +} diff --git a/rugby-scoring.h b/rugby-scoring.h index aed276634abdef230596da3c1c4eb5c43b919b4f..c3207cd77d46ae04d24e99b0c46074f7250533a2 100644 --- a/rugby-scoring.h +++ b/rugby-scoring.h @@ -7,17 +7,18 @@ G_BEGIN_DECLS #define TRY_POINTS 7 #define UTRY_POINTS 5 -#define PEN_POINTS 3 +#define KICK_POINTS 3 typedef enum { RUGBY_SCORE_TYPE_TRY, RUGBY_SCORE_TYPE_UTRY, - RUGBY_SCORE_TYPE_PEN + RUGBY_SCORE_TYPE_KICK } RugbyScoreType; GVariant * rugby_scoring_get_possibilities (gint score); gint rugby_scoring_get_max_tries (gint score); +gint rugby_scoring_get_max_kicks (gint score); G_END_DECLS