1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2012, 2013, 2014, 2019 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "psppire-dialog-action-oneway.h"
24 #include "psppire-var-view.h"
25 #include "psppire-acr.h"
27 #include "psppire-dialog.h"
28 #include "builder-wrapper.h"
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) msgid
36 #define POSTHOC_BONFERRONI 0x01
37 #define POSTHOC_GH 0x02
38 #define POSTHOC_LSD 0x04
39 #define POSTHOC_SCHEFFE 0x08
40 #define POSTHOC_SIDAK 0x10
41 #define POSTHOC_TUKEY 0x20
43 static void next (GtkWidget *widget, PsppireDialogActionOneway *);
44 static void prev (GtkWidget *widget, PsppireDialogActionOneway *);
45 static void run_posthoc_dialog (PsppireDialogActionOneway *ow);
46 static void run_contrasts_dialog (PsppireDialogActionOneway *csd);
47 static void push_new_store (GArray *contrast_stack, PsppireDialogActionOneway *csd);
50 static void psppire_dialog_action_oneway_init (PsppireDialogActionOneway *act);
51 static void psppire_dialog_action_oneway_class_init (PsppireDialogActionOnewayClass *class);
53 G_DEFINE_TYPE (PsppireDialogActionOneway, psppire_dialog_action_oneway, PSPPIRE_TYPE_DIALOG_ACTION);
57 generate_syntax (const PsppireDialogAction *act)
59 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (act);
63 gboolean descriptives = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->descriptives));
64 gboolean homogeneity = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->homogeneity));
67 ds_init_cstr (&dss, "ONEWAY /VARIABLES=");
69 psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (ow->vars_treeview), 0, &dss);
71 ds_put_cstr (&dss, " BY ");
73 ds_put_cstr (&dss, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
75 if (descriptives || homogeneity)
77 ds_put_cstr (&dss, "\n\t/STATISTICS=");
79 ds_put_cstr (&dss, "DESCRIPTIVES ");
81 ds_put_cstr (&dss, "HOMOGENEITY ");
85 ds_put_cstr (&dss, "\n\t/POSTHOC=");
86 if (ow->posthoc & POSTHOC_BONFERRONI)
87 ds_put_cstr (&dss, "BONFERRONI ");
88 if (ow->posthoc & POSTHOC_GH)
89 ds_put_cstr (&dss, "GH ");
90 if (ow->posthoc & POSTHOC_LSD)
91 ds_put_cstr (&dss, "LSD ");
92 if (ow->posthoc & POSTHOC_SCHEFFE)
93 ds_put_cstr (&dss, "SCHEFFE ");
94 if (ow->posthoc & POSTHOC_SIDAK)
95 ds_put_cstr (&dss, "SIDAK ");
96 if (ow->posthoc & POSTHOC_TUKEY)
97 ds_put_cstr (&dss, "TUKEY ");
99 for (i = 0 ; i < ow->contrasts_array->len ; ++i)
101 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
105 ds_put_cstr (&dss, "\n\t/CONTRAST=");
107 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
110 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
114 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
116 ds_put_c_format (&dss, " %.*g", DBL_DIG + 1, v);
120 ds_put_cstr (&dss, ".\n");
122 text = ds_steal_cstr (&dss);
130 dialog_state_valid (gpointer data)
132 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (data);
135 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
139 if (!gtk_tree_model_get_iter_first (vars, ¬used))
142 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
150 refresh (PsppireDialogAction *rd_)
152 PsppireDialogActionOneway *ow = PSPPIRE_DIALOG_ACTION_ONEWAY (rd_);
154 GtkTreeModel *model =
155 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
157 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
159 gtk_list_store_clear (GTK_LIST_STORE (model));
163 /* Callback for when the list store currently associated with the
164 treeview has changed. It sets the widgets of the subdialog
165 to reflect the store's new state.
168 list_store_changed (PsppireDialogActionOneway *csd)
173 GtkTreeModel *ls = NULL;
175 g_strdup_printf (_("Contrast %d of %d"),
176 csd->c, csd->temp_contrasts->len);
178 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
182 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
185 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
187 psppire_acr_set_model (PSPPIRE_ACR (csd->acr), GTK_LIST_STORE (ls));
189 /* Sensitive iff the liststore has two items or more */
190 gtk_widget_set_sensitive (csd->next,
191 gtk_tree_model_iter_nth_child
192 (ls, &iter, NULL, 1));
194 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
196 ok = gtk_tree_model_iter_next (ls, &iter)
200 gtk_tree_model_get (ls, &iter, 0, &v, -1);
204 text = g_strdup_printf ("%.*g", DBL_DIG + 1, total);
206 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
212 /* Copy the contrasts array into the local array */
214 clone_contrasts_array (GArray *src_array)
219 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
222 for (i = 0 ; i < src_array->len ; ++i)
225 GtkTreeIter src_iter;
226 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
229 /* Refuse to copy empty stores */
230 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
234 dest = clone_list_store (src);
236 g_array_append_val (dest_array, dest);
244 psppire_dialog_action_oneway_activate (PsppireDialogAction *a, GVariant *param)
246 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
247 PsppireDialogActionOneway *act = PSPPIRE_DIALOG_ACTION_ONEWAY (a);
249 GtkBuilder *xml = builder_new ("oneway.ui");
251 GtkWidget *contrasts_button = get_widget_assert (xml, "contrasts-button");
253 GtkWidget *posthoc_button= get_widget_assert (xml, "posthoc-button");
254 GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
256 pda->dialog = get_widget_assert (xml, "oneway-anova-dialog");
257 pda->source = get_widget_assert (xml, "oneway-anova-treeview1");
259 act->vars_treeview = get_widget_assert (xml, "oneway-anova-treeview2");
260 act->factor_entry = get_widget_assert (xml, "oneway-anova-entry");
262 act->descriptives = get_widget_assert (xml, "checkbutton1");
263 act->homogeneity = get_widget_assert (xml, "checkbutton2");
265 /* Posthoc tests dialog */
266 act->posthoc_dialog = get_widget_assert (xml, "posthoc-dialog");
268 act->bonferroni_button = get_widget_assert (xml, "bonferroni-button");
269 act->gh_button = get_widget_assert (xml, "gh-button");
270 act->lsd_button = get_widget_assert (xml, "lsd-button");
271 act->scheffe_button = get_widget_assert (xml, "scheffe-button");
272 act->sidak_button = get_widget_assert (xml, "sidak-button");
273 act->tukey_button = get_widget_assert (xml, "tukey-button");
275 g_signal_connect_swapped (posthoc_button, "clicked",
276 G_CALLBACK (run_posthoc_dialog), act);
278 /* Contrast dialog */
279 act->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
281 act->next = get_widget_assert (xml, "next-button");
282 act->prev = get_widget_assert (xml, "prev-button");
283 act->ctotal = get_widget_assert (xml, "entry2");
284 act->acr = get_widget_assert (xml, "psppire-acr1");
285 act->stack_label = get_widget_assert (xml, "contrast-stack-label");
286 act->contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
289 g_signal_connect (act->next, "clicked", G_CALLBACK (next), act);
290 g_signal_connect (act->prev, "clicked", G_CALLBACK (prev), act);
292 psppire_acr_set_entry (PSPPIRE_ACR (act->acr), entry);
294 gtk_window_set_transient_for (GTK_WINDOW (act->contrasts_dialog),
295 GTK_WINDOW (pda->toplevel));
298 g_signal_connect_swapped (contrasts_button, "clicked",
299 G_CALLBACK (run_contrasts_dialog), act);
302 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
303 psppire_dialog_action_set_refresh (pda, refresh);
308 psppire_dialog_action_oneway_class_init (PsppireDialogActionOnewayClass *class)
310 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_oneway_activate;
311 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
316 psppire_dialog_action_oneway_init (PsppireDialogActionOneway *act)
318 act->contrasts_array = NULL;
324 run_posthoc_dialog (PsppireDialogActionOneway *ow)
328 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ow->bonferroni_button),
329 ow->posthoc & POSTHOC_BONFERRONI);
331 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ow->gh_button),
332 ow->posthoc & POSTHOC_GH);
334 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ow->lsd_button),
335 ow->posthoc & POSTHOC_LSD);
337 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ow->scheffe_button),
338 ow->posthoc & POSTHOC_SCHEFFE);
340 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ow->sidak_button),
341 ow->posthoc & POSTHOC_SIDAK);
343 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ow->tukey_button),
344 ow->posthoc & POSTHOC_TUKEY);
346 response = psppire_dialog_run (PSPPIRE_DIALOG (ow->posthoc_dialog));
348 if (response == PSPPIRE_RESPONSE_CONTINUE)
351 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->bonferroni_button)))
352 ow->posthoc |= POSTHOC_BONFERRONI;
354 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->gh_button)))
355 ow->posthoc |= POSTHOC_GH;
357 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->lsd_button)))
358 ow->posthoc |= POSTHOC_LSD;
360 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->scheffe_button)))
361 ow->posthoc |= POSTHOC_SCHEFFE;
363 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->sidak_button)))
364 ow->posthoc |= POSTHOC_SIDAK;
366 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ow->tukey_button)))
367 ow->posthoc |= POSTHOC_TUKEY;
372 run_contrasts_dialog (PsppireDialogActionOneway *csd)
376 csd->temp_contrasts = clone_contrasts_array (csd->contrasts_array);
380 push_new_store (csd->temp_contrasts, csd);
382 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
384 if (response == PSPPIRE_RESPONSE_CONTINUE)
386 csd->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
392 push_new_store (GArray *contrast_stack, PsppireDialogActionOneway *csd)
394 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
396 g_array_append_val (contrast_stack, ls);
398 g_signal_connect_swapped (ls, "row-deleted",
399 G_CALLBACK (list_store_changed), csd);
401 g_signal_connect_swapped (ls, "row-changed",
402 G_CALLBACK (list_store_changed), csd);
404 list_store_changed (csd);
409 next (GtkWidget *widget, PsppireDialogActionOneway *csd)
411 if (csd->c >= csd->temp_contrasts->len)
412 push_new_store (csd->temp_contrasts, csd);
416 list_store_changed (csd);
421 prev (GtkWidget *widget, PsppireDialogActionOneway *csd)
426 list_store_changed (csd);