Replace more uses of 'cnt' by 'n'.
[pspp] / src / ui / gui / psppire-dialog-action-examine.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012, 2020  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include "psppire-dialog-action-examine.h"
21
22 #include "psppire-var-view.h"
23 #include "dialog-common.h"
24 #include "psppire-selector.h"
25 #include "psppire-dict.h"
26 #include "psppire-dialog.h"
27 #include "builder-wrapper.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 static void psppire_dialog_action_examine_class_init      (PsppireDialogActionExamineClass *class);
34
35 G_DEFINE_TYPE (PsppireDialogActionExamine, psppire_dialog_action_examine, PSPPIRE_TYPE_DIALOG_ACTION);
36
37
38 #define     STAT_DESCRIPTIVES  0x01
39 #define     STAT_EXTREMES      0x02
40 #define     STAT_PERCENTILES   0x04
41
42 static void
43 run_stats_dialog (PsppireDialogActionExamine *ed)
44 {
45   gint response;
46
47   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->descriptives_button),
48                                 ed->stats & STAT_DESCRIPTIVES);
49
50   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->extremes_button),
51                                 ed->stats & STAT_EXTREMES);
52
53   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->percentiles_button),
54                                 ed->stats & STAT_PERCENTILES);
55
56   response = psppire_dialog_run (PSPPIRE_DIALOG (ed->stats_dialog));
57
58   if (response == PSPPIRE_RESPONSE_CONTINUE)
59     {
60       ed->stats = 0;
61       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->descriptives_button)))
62         ed->stats |= STAT_DESCRIPTIVES;
63
64       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->extremes_button)))
65         ed->stats |= STAT_EXTREMES;
66
67       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->percentiles_button)))
68         ed->stats |= STAT_PERCENTILES;
69     }
70 }
71
72 static void
73 run_opts_dialog (PsppireDialogActionExamine *ed)
74 {
75   gint response;
76
77   switch (ed->opts)
78     {
79     case OPT_LISTWISE:
80       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->listwise), TRUE);
81       break;
82     case OPT_PAIRWISE:
83       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->pairwise), TRUE);
84       break;
85     case OPT_REPORT:
86       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->report), TRUE);
87       break;
88     default:
89       g_assert_not_reached ();
90       break;
91     };
92
93   response = psppire_dialog_run (PSPPIRE_DIALOG (ed->opts_dialog));
94
95   if (response == PSPPIRE_RESPONSE_CONTINUE)
96     {
97       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->listwise)))
98         ed->opts = OPT_LISTWISE;
99       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->pairwise)))
100         ed->opts = OPT_PAIRWISE;
101       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->report)))
102         ed->opts = OPT_REPORT;
103   }
104 }
105
106 static void
107 run_plots_dialog (PsppireDialogActionExamine *ed)
108 {
109   gint response;
110
111   switch (ed->boxplots)
112     {
113     case BOXPLOT_FACTORS:
114       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->boxplot_factors_button), TRUE);
115       break;
116     case BOXPLOT_DEPENDENTS:
117       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->boxplot_dependents_button), TRUE);
118       break;
119     case BOXPLOT_NONE:
120       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->boxplot_none_button), TRUE);
121       break;
122     default:
123       g_assert_not_reached ();
124       break;
125     };
126
127   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->histogram_button), ed->histogram);
128   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->npplots_button), ed->npplots);
129
130   g_signal_connect (ed->spread_trans_button, "toggled",
131                     G_CALLBACK (set_sensitivity_from_toggle),
132                     ed->spread_power_combo);
133   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->spread_trans_button), FALSE);
134   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->spread_trans_button), TRUE);
135
136   switch (ed->spreadlevel)
137     {
138     case SPREAD_NONE:
139       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->spread_none_button), TRUE);
140       break;
141     case SPREAD_POWER:
142       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->spread_power_button), TRUE);
143       break;
144     case SPREAD_TRANS:
145       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->spread_trans_button), TRUE);
146       break;
147     case SPREAD_UNTRANS:
148       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->spread_untrans_button), TRUE);
149       break;
150     default:
151       g_assert_not_reached ();
152       break;
153     }
154
155   switch (ed->spreadpower)
156     {
157     case SPREADPOWER_NATLOG:
158       gtk_combo_box_set_active_id (GTK_COMBO_BOX (ed->spread_power_combo), "natlog");
159       break;
160     case SPREADPOWER_CUBE:
161       gtk_combo_box_set_active_id (GTK_COMBO_BOX (ed->spread_power_combo), "cube");
162       break;
163     case SPREADPOWER_SQUARE:
164       gtk_combo_box_set_active_id (GTK_COMBO_BOX (ed->spread_power_combo), "square");
165       break;
166     case SPREADPOWER_SQUAREROOT:
167       gtk_combo_box_set_active_id (GTK_COMBO_BOX (ed->spread_power_combo), "squareroot");
168       break;
169     case SPREADPOWER_RECROOT:
170       gtk_combo_box_set_active_id (GTK_COMBO_BOX (ed->spread_power_combo), "recroot");
171       break;
172     case SPREADPOWER_RECIPROCAL:
173       gtk_combo_box_set_active_id (GTK_COMBO_BOX (ed->spread_power_combo), "reciprocal");
174       break;
175     }
176
177   response = psppire_dialog_run (PSPPIRE_DIALOG (ed->plots_dialog));
178
179   if (response == PSPPIRE_RESPONSE_CONTINUE)
180     {
181       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->boxplot_factors_button)))
182         ed->boxplots = BOXPLOT_FACTORS;
183       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->boxplot_dependents_button)))
184         ed->boxplots = BOXPLOT_DEPENDENTS;
185       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->boxplot_none_button)))
186         ed->boxplots = BOXPLOT_NONE;
187
188       ed->histogram = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->histogram_button));
189       ed->npplots   = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->npplots_button));
190
191       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->spread_none_button)))
192         ed->spreadlevel = SPREAD_NONE;
193       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->spread_power_button)))
194         ed->spreadlevel = SPREAD_POWER;
195       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->spread_trans_button)))
196         ed->spreadlevel = SPREAD_TRANS;
197       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->spread_untrans_button)))
198         ed->spreadlevel = SPREAD_UNTRANS;
199
200       if (0 == strcmp (gtk_combo_box_get_active_id (GTK_COMBO_BOX (ed->spread_power_combo)), "natlog"))
201         ed->spreadpower = SPREADPOWER_NATLOG;
202       else if (0 == strcmp (gtk_combo_box_get_active_id (GTK_COMBO_BOX (ed->spread_power_combo)), "cube"))
203         ed->spreadpower = SPREADPOWER_CUBE;
204       else if (0 == strcmp (gtk_combo_box_get_active_id (GTK_COMBO_BOX (ed->spread_power_combo)), "square"))
205         ed->spreadpower = SPREADPOWER_SQUARE;
206       else if (0 == strcmp (gtk_combo_box_get_active_id (GTK_COMBO_BOX (ed->spread_power_combo)), "squareroot"))
207         ed->spreadpower = SPREADPOWER_SQUAREROOT;
208       else if (0 == strcmp (gtk_combo_box_get_active_id (GTK_COMBO_BOX (ed->spread_power_combo)), "recroot"))
209         ed->spreadpower = SPREADPOWER_RECROOT;
210       else if (0 == strcmp (gtk_combo_box_get_active_id (GTK_COMBO_BOX (ed->spread_power_combo)), "reciprocal"))
211         ed->spreadpower = SPREADPOWER_RECIPROCAL;
212     }
213 }
214
215 static char *
216 generate_syntax (const PsppireDialogAction *act)
217 {
218   PsppireDialogActionExamine *ed  = PSPPIRE_DIALOG_ACTION_EXAMINE (act);
219
220   const char *label;
221   gchar *text = NULL;
222   GString *str = g_string_new ("EXAMINE ");
223   bool show_stats = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->display_stats_button));
224   bool show_plots = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->display_plots_button));
225
226   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->display_both_button)))
227     {
228       show_stats = true;
229       show_plots = true;
230     }
231
232   g_string_append (str, "\n\t/VARIABLES=");
233   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (ed->variables), 0, str);
234
235   if (0  < gtk_tree_model_iter_n_children
236        (gtk_tree_view_get_model (GTK_TREE_VIEW (ed->factors)), NULL))
237     {
238       g_string_append (str, "\n\tBY ");
239       psppire_var_view_append_names (PSPPIRE_VAR_VIEW (ed->factors), 0, str);
240     }
241
242   label = gtk_entry_get_text (GTK_ENTRY (ed->id_var));
243   if (0 != strcmp (label, ""))
244     {
245       g_string_append (str, "\n\t/ID = ");
246       g_string_append (str, label);
247     }
248
249   if (show_stats)
250     {
251       if (ed->stats & (STAT_DESCRIPTIVES | STAT_EXTREMES))
252         {
253           g_string_append (str, "\n\t/STATISTICS =");
254
255           if (ed->stats & STAT_DESCRIPTIVES)
256             g_string_append (str, " DESCRIPTIVES");
257
258           if (ed->stats & STAT_EXTREMES)
259             g_string_append (str, " EXTREME");
260         }
261
262       if (ed->stats & STAT_PERCENTILES)
263         g_string_append (str, "\n\t/PERCENTILES");
264     }
265
266   if (show_plots &&
267       ((ed->boxplots != BOXPLOT_NONE) ||
268        ed->histogram ||
269        ed->npplots ||
270        (ed->spreadlevel != SPREAD_NONE)))
271     {
272       g_string_append (str, "\n\t/PLOT =");
273
274       if (ed->boxplots != BOXPLOT_NONE)
275         g_string_append (str, " BOXPLOT");
276       if (ed->histogram)
277         g_string_append (str, " HISTOGRAM");
278       if (ed->npplots)
279         g_string_append (str, " NPPLOT");
280       if (ed->spreadlevel != SPREAD_NONE)
281         {
282           g_string_append (str, " SPREADLEVEL");
283           if (ed->spreadlevel != SPREAD_POWER)
284             {
285               const gchar *power = NULL;
286               if (ed->spreadlevel == SPREAD_TRANS)
287                 switch (ed->spreadpower)
288                   {
289                   case SPREADPOWER_NATLOG:
290                     power = "0";
291                     break;
292                   case SPREADPOWER_CUBE:
293                     power = "3";
294                     break;
295                   case SPREADPOWER_SQUARE:
296                     power = "2";
297                     break;
298                   case SPREADPOWER_SQUAREROOT:
299                     power = "0.5";
300                     break;
301                   case SPREADPOWER_RECROOT:
302                     power = "-0.5";
303                     break;
304                   case SPREADPOWER_RECIPROCAL:
305                     power = "-1";
306                     break;
307                   default:
308                     g_assert_not_reached ();
309                     break;
310                   }
311               else
312                 power = "1";
313               g_string_append_printf(str, " (%s)",power);
314             }
315         }
316       if (ed->boxplots == BOXPLOT_FACTORS)
317         g_string_append (str, "\n\t/COMPARE = GROUPS");
318       if (ed->boxplots == BOXPLOT_DEPENDENTS)
319         g_string_append (str, "\n\t/COMPARE = VARIABLES");
320     }
321
322   g_string_append (str, "\n\t/MISSING=");
323   switch (ed->opts)
324     {
325     case OPT_REPORT:
326       g_string_append (str, "REPORT");
327       break;
328     case OPT_PAIRWISE:
329       g_string_append (str, "PAIRWISE");
330       break;
331     default:
332       g_string_append (str, "LISTWISE");
333       break;
334     };
335
336   g_string_append (str, ".");
337   text = str->str;
338
339   g_string_free (str, FALSE);
340
341   return text;
342 }
343
344 static gboolean
345 dialog_state_valid (PsppireDialogAction *da)
346 {
347   PsppireDialogActionExamine *pae  = PSPPIRE_DIALOG_ACTION_EXAMINE (da);
348   GtkTreeIter notused;
349   GtkTreeModel *vars =
350     gtk_tree_view_get_model (GTK_TREE_VIEW (pae->variables));
351
352   return gtk_tree_model_get_iter_first (vars, &notused);
353 }
354
355 static void
356 dialog_refresh (PsppireDialogAction *da)
357 {
358   PsppireDialogActionExamine *dae  = PSPPIRE_DIALOG_ACTION_EXAMINE (da);
359   GtkTreeModel *liststore = NULL;
360
361   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dae->variables));
362   gtk_list_store_clear (GTK_LIST_STORE (liststore));
363
364   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dae->factors));
365   gtk_list_store_clear (GTK_LIST_STORE (liststore));
366
367   gtk_entry_set_text (GTK_ENTRY (dae->id_var), "");
368   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dae->display_both_button), TRUE);
369
370   dae->stats = 0x00;
371   dae->opts = OPT_LISTWISE;
372   dae->boxplots = BOXPLOT_FACTORS;
373   dae->histogram = TRUE;
374   dae->npplots = FALSE;
375   dae->spreadlevel = SPREAD_NONE;
376   dae->spreadpower = SPREADPOWER_NATLOG;
377 }
378
379 static GtkBuilder *
380 psppire_dialog_action_examine_activate (PsppireDialogAction *a, GVariant *param)
381 {
382   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
383   PsppireDialogActionExamine *act = PSPPIRE_DIALOG_ACTION_EXAMINE (a);
384
385   GtkBuilder *xml = builder_new ("examine.ui");
386
387   GtkWidget *stats_button = get_widget_assert (xml, "stats-button");
388   GtkWidget *opts_button = get_widget_assert (xml, "opts-button");
389   GtkWidget *plots_button = get_widget_assert (xml, "plots-button");
390
391   g_signal_connect_swapped (stats_button, "clicked",
392                             G_CALLBACK (run_stats_dialog), act);
393
394   g_signal_connect_swapped (opts_button, "clicked",
395                             G_CALLBACK (run_opts_dialog), act);
396   g_signal_connect_swapped (plots_button, "clicked",
397                             G_CALLBACK (run_plots_dialog), act);
398
399   GtkWidget *dep_sel = get_widget_assert (xml, "psppire-selector1");
400   GtkWidget *dep_sel2 = get_widget_assert (xml, "psppire-selector2");
401   GtkWidget *dep_sel3 = get_widget_assert (xml, "psppire-selector3");
402   GtkWidget *table = get_widget_assert (xml, "table1");
403
404   pda->dialog    = get_widget_assert   (xml, "examine-dialog");
405   pda->source    = get_widget_assert   (xml, "treeview1");
406   act->variables = get_widget_assert   (xml, "treeview2");
407   act->factors   = get_widget_assert   (xml, "treeview3");
408   act->id_var    = get_widget_assert   (xml, "entry1");
409   act->display_both_button  = get_widget_assert (xml, "display-both-button");
410   act->display_stats_button = get_widget_assert (xml, "display-stats-button");
411   act->display_plots_button = get_widget_assert (xml, "display-plots-button");
412
413   /* Setting the focus chain like this is a pain.
414      But the default focus order seems to be somewhat odd. */
415   GList *list = NULL;
416   list = g_list_append (list, get_widget_assert (xml, "scrolledwindow1"));
417   list = g_list_append (list, dep_sel);
418   list = g_list_append (list, get_widget_assert (xml, "frame1"));
419   list = g_list_append (list, dep_sel2);
420   list = g_list_append (list, get_widget_assert (xml, "frame2"));
421   list = g_list_append (list, dep_sel3);
422   list = g_list_append (list, get_widget_assert (xml, "frame3"));
423   gtk_container_set_focus_chain (GTK_CONTAINER (table), list);
424   g_list_free (list);
425
426
427   act->stats_dialog        = get_widget_assert (xml, "statistics-dialog");
428   act->descriptives_button = get_widget_assert (xml, "descriptives-button");
429   act->extremes_button     = get_widget_assert (xml, "extremes-button");
430   act->percentiles_button  = get_widget_assert (xml, "percentiles-button");
431
432   act->opts_dialog = get_widget_assert (xml, "options-dialog");
433   act->listwise    = get_widget_assert (xml, "radiobutton1");
434   act->pairwise    = get_widget_assert (xml, "radiobutton2");
435   act->report      = get_widget_assert (xml, "radiobutton3");
436
437   act->plots_dialog              = get_widget_assert (xml, "plots-dialog");
438   act->boxplot_factors_button    = get_widget_assert (xml, "boxplot-factors-button");
439   act->boxplot_dependents_button = get_widget_assert (xml, "boxplot-dependents-button");
440   act->boxplot_none_button       = get_widget_assert (xml, "boxplot-none-button");
441   act->histogram_button          = get_widget_assert (xml, "histogram-button");
442   act->npplots_button            = get_widget_assert (xml, "npplots-button");
443   act->spread_none_button        = get_widget_assert (xml, "spread-none-button");
444   act->spread_power_button       = get_widget_assert (xml, "spread-power-button");
445   act->spread_trans_button       = get_widget_assert (xml, "spread-trans-button");
446   act->spread_untrans_button     = get_widget_assert (xml, "spread-untrans-button");
447   act->spread_power_combo        = get_widget_assert (xml, "spread-power-combo");
448
449   psppire_selector_set_allow (PSPPIRE_SELECTOR (dep_sel), numeric_only);
450
451   psppire_dialog_action_set_valid_predicate (pda, (void *) dialog_state_valid);
452   psppire_dialog_action_set_refresh (pda, dialog_refresh);
453   return xml;
454 }
455
456 static void
457 psppire_dialog_action_examine_class_init (PsppireDialogActionExamineClass *class)
458 {
459   PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_examine_activate;
460
461   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
462 }
463
464 static void
465 psppire_dialog_action_examine_init (PsppireDialogActionExamine *act)
466 {
467 }