psppire-dialog-action-crosstabs: Do not add PIVOT or NOPIVOT to command.
[pspp] / src / ui / gui / psppire-dialog-action-crosstabs.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012  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-crosstabs.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include "helper.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
27
28 #include "psppire-dialog.h"
29 #include "builder-wrapper.h"
30 #include "psppire-checkbox-treeview.h"
31 #include "psppire-dict.h"
32 #include "libpspp/str.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 static void
40 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class);
41
42 G_DEFINE_TYPE (PsppireDialogActionCrosstabs, psppire_dialog_action_crosstabs, PSPPIRE_TYPE_DIALOG_ACTION);
43
44 static gboolean
45 dialog_state_valid (gpointer data)
46 {
47   PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (data);
48
49   GtkTreeModel *row_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_rows));
50   GtkTreeModel *col_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_cols));
51
52   GtkTreeIter notused;
53
54   return (gtk_tree_model_get_iter_first (row_vars, &notused)
55     && gtk_tree_model_get_iter_first (col_vars, &notused));
56 }
57
58 static void
59 refresh (PsppireDialogAction *rd_)
60 {
61   PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (rd_);
62
63   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_rows));
64   gtk_list_store_clear (GTK_LIST_STORE (liststore));
65
66   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_cols));
67   gtk_list_store_clear (GTK_LIST_STORE (liststore));
68 }
69
70 #define CROSSTABS_STATS                                             \
71   CS (CHISQ, N_("Chisq"), N_("Pearson chi-square, "                 \
72    "likelihood ratio, Fisher’s exact test, continuity correction, " \
73    "linear-by-linear association."))                                \
74   CS (PHI, N_("Phi and Cramer's V"), NULL)                          \
75   CS (CC, N_("CC"), N_("Contingency coefficient"))                  \
76   CS (LAMBDA, N_("Lambda"), NULL)                                   \
77   CS (UC, N_("UC"), N_("Uncertainty coefficient"))                  \
78   CS (BTAU, N_("BTau"), N_("Kendall's Tau-b"))                      \
79   CS (CTAU, N_("CTau"), N_("Kendall's Tau-c"))                      \
80   CS (RISK, N_("Risk"), N_("Relative Risk estimate"))               \
81   CS (GAMMA, N_("Gamma"), NULL)                                     \
82   CS (D, N_("D"), N_("Somer's d"))                                  \
83   CS (KAPPA, N_("Kappa"), N_("Cohen's Kappa"))                      \
84   CS (ETA, N_("Eta"), NULL)                                         \
85   CS (CORR, N_("Corr"), N_("Spearman correlation, Pearson's r"))    \
86   CS (STATS_NONE, N_("None"), NULL)
87
88
89 #define CROSSTABS_CELLS                                               \
90   CS (COUNT, N_("Count"), N_("Frequency Count"))                      \
91   CS (ROW, N_("Row"), N_("Row percent"))                              \
92   CS (COLUMN, N_("Column"), N_("Column percent"))                     \
93   CS (TOTAL, N_("Total"), N_("Total percent"))                        \
94   CS (EXPECTED, N_("Expected"), N_("Expected value"))                 \
95   CS (RESIDUAL, N_("Residual"), NULL)                                 \
96   CS (SRESIDUAL, N_("Std. Residual"), N_("Standardized Residual"))    \
97   CS (ASRESIDUAL, N_("Adjusted Std. Residual"), NULL)                 \
98   CS (CELLS_NONE, N_("None"), NULL)
99
100 enum
101   {
102 #define CS(NAME, LABEL, TOOLTIP) CS_##NAME,
103     CROSSTABS_STATS
104 #undef CS
105     N_CROSSTABS_STATS
106   };
107
108 enum
109   {
110 #define CS(NAME, LABEL, TOOLTIP) CS_##NAME,
111     CROSSTABS_CELLS
112 #undef CS
113     N_CROSSTABS_CELLS
114   };
115
116 enum
117   {
118 #define CS(NAME, LABEL, TOOLTIP) B_CS_##NAME = 1u << CS_##NAME,
119     CROSSTABS_STATS
120     CROSSTABS_CELLS
121 #undef CS
122     B_CS_STATS_ALL = (1u << N_CROSSTABS_STATS) - 1,
123     B_CS_CELLS_ALL = (1u << N_CROSSTABS_CELLS) - 1,
124     B_CS_STATS_DEFAULT = B_CS_CHISQ,
125     B_CS_CELL_DEFAULT = B_CS_COUNT | B_CS_ROW | B_CS_COLUMN | B_CS_TOTAL,
126     B_CS_NONE
127   };
128
129 static const struct checkbox_entry_item stats[] =
130   {
131 #define CS(NAME, LABEL, TOOLTIP) {#NAME, LABEL, TOOLTIP},
132     CROSSTABS_STATS \
133     CS(NONE, N_("None"), NULL)
134 #undef CS
135   };
136
137 static const struct checkbox_entry_item cells[] =
138   {
139 #define CS(NAME, LABEL, TOOLTIP) {#NAME, LABEL, TOOLTIP},
140     CROSSTABS_CELLS \
141     CS(NONE, N_("None"), NULL)
142 #undef CS
143   };
144
145 static void
146 on_format_clicked (PsppireDialogActionCrosstabs *cd)
147 {
148   int ret;
149
150   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->avalue_button), cd->format_options_avalue);
151   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->table_button), cd->format_options_table);
152   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cd->pivot_button), cd->format_options_pivot);
153
154   ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
155
156   if (ret == PSPPIRE_RESPONSE_CONTINUE)
157     {
158       cd->format_options_avalue =
159         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->avalue_button));
160
161       cd->format_options_table =
162         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->table_button));
163
164       cd->format_options_pivot =
165         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->pivot_button));
166     }
167 }
168
169 static void
170 on_cell_clicked (PsppireDialogActionCrosstabs *cd)
171 {
172   GtkListStore *liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
173
174   gint ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
175
176   if (ret == PSPPIRE_RESPONSE_CONTINUE)
177     {
178       g_object_unref (liststore);
179     }
180   else
181     {
182       gtk_tree_view_set_model (GTK_TREE_VIEW (cd->cell_view) , GTK_TREE_MODEL (liststore));
183       cd->cell = GTK_TREE_MODEL (liststore);
184     }
185 }
186
187
188 static void
189 on_statistics_clicked (PsppireDialogActionCrosstabs *cd)
190 {
191   GtkListStore *liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
192
193   gint ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
194
195   if (ret == PSPPIRE_RESPONSE_CONTINUE)
196     {
197       g_object_unref (liststore);
198     }
199   else
200     {
201       gtk_tree_view_set_model (GTK_TREE_VIEW (cd->stat_view) , GTK_TREE_MODEL (liststore));
202       cd->stat = GTK_TREE_MODEL (liststore);
203     }
204 }
205
206
207 static GtkBuilder *
208 psppire_dialog_action_crosstabs_activate (PsppireDialogAction *a, GVariant *param)
209 {
210   PsppireDialogActionCrosstabs *act = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
211   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
212
213   GtkBuilder *xml = builder_new ("crosstabs.ui");
214
215   pda->dialog = get_widget_assert   (xml, "crosstabs-dialog");
216   pda->source = get_widget_assert   (xml, "dict-treeview");
217
218   act->dest_rows =   get_widget_assert   (xml, "rows");
219   act->dest_cols =   get_widget_assert   (xml, "cols");
220   act->format_button = get_widget_assert (xml, "format-button");
221   act->stat_button = get_widget_assert (xml, "stats-button");
222   act->cell_button = get_widget_assert (xml, "cell-button");
223   act->stat_view =   get_widget_assert (xml, "stats-view");
224   act->cell_view =   get_widget_assert (xml, "cell-view");
225   act->cell_dialog = get_widget_assert (xml, "cell-dialog");
226   act->stat_dialog = get_widget_assert (xml, "stat-dialog");
227   act->format_dialog = get_widget_assert (xml, "format-dialog");
228
229   act->avalue_button = get_widget_assert (xml, "ascending");
230   act->table_button = get_widget_assert (xml, "print-tables");
231   act->pivot_button = get_widget_assert (xml, "pivot");
232
233   act->format_options_avalue = TRUE;
234   act->format_options_table = TRUE;
235   act->format_options_pivot = TRUE;
236
237   psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->cell_view),
238                                       B_CS_CELL_DEFAULT,
239                                       N_CROSSTABS_CELLS,
240                                       cells);
241
242   act->cell = gtk_tree_view_get_model (GTK_TREE_VIEW (act->cell_view));
243
244   psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
245                                       B_CS_STATS_DEFAULT,
246                                       N_CROSSTABS_STATS,
247                                       stats);
248
249   act->stat = gtk_tree_view_get_model (GTK_TREE_VIEW (act->stat_view));
250
251   psppire_dialog_action_set_refresh (pda, refresh);
252
253   psppire_dialog_action_set_valid_predicate (pda,
254                                              dialog_state_valid);
255
256   g_signal_connect_swapped (act->cell_button, "clicked",
257                             G_CALLBACK (on_cell_clicked), act);
258
259   g_signal_connect_swapped (act->stat_button, "clicked",
260                             G_CALLBACK (on_statistics_clicked), act);
261
262   g_signal_connect_swapped (act->format_button, "clicked",
263                             G_CALLBACK (on_format_clicked), act);
264
265   return xml;
266 }
267
268
269
270 static char *
271 generate_syntax (const PsppireDialogAction *a)
272 {
273   PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
274   gchar *text = NULL;
275   int i, n;
276   guint selected;
277   GString *string = g_string_new ("CROSSTABS ");
278   gboolean ok;
279   GtkTreeIter iter;
280
281   g_string_append (string, "\n\t/TABLES=");
282   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->dest_rows), 0, string);
283   g_string_append (string, "\tBY\t");
284   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (cd->dest_cols), 0, string);
285
286
287   g_string_append (string, "\n\t/FORMAT=");
288
289   if (cd->format_options_avalue)
290     g_string_append (string, "AVALUE");
291   else
292     g_string_append (string, "DVALUE");
293   g_string_append (string, " ");
294
295   if (cd->format_options_table)
296     g_string_append (string, "TABLES");
297   else
298     g_string_append (string, "NOTABLES");
299
300   selected = 0;
301   for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok;
302        i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
303     {
304       gboolean toggled;
305       gtk_tree_model_get (cd->stat, &iter,
306                           CHECKBOX_COLUMN_SELECTED, &toggled, -1);
307       if (toggled)
308         selected |= 1u << i;
309       else
310         selected &= ~(1u << i);
311     }
312
313   if (!(selected & (1u << CS_STATS_NONE)))
314     {
315       if (selected)
316         {
317           g_string_append (string, "\n\t/STATISTICS=");
318           n = 0;
319           for (i = 0; i < N_CROSSTABS_STATS; i++)
320             if (selected & (1u << i))
321               {
322                 if (n++)
323                   g_string_append (string, " ");
324                 g_string_append (string, stats[i].name);
325               }
326         }
327     }
328
329   selected = 0;
330   for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok;
331        i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
332     {
333       gboolean toggled;
334       gtk_tree_model_get (cd->cell, &iter,
335                           CHECKBOX_COLUMN_SELECTED, &toggled, -1);
336       if (toggled)
337         selected |= 1u << i;
338       else
339         selected &= ~(1u << i);
340     }
341
342
343
344   g_string_append (string, "\n\t/CELLS=");
345   if (selected & (1u << CS_CELLS_NONE))
346     g_string_append (string, "NONE");
347   else
348     {
349       n = 0;
350       for (i = 0; i < N_CROSSTABS_CELLS; i++)
351         if (selected & (1u << i))
352           {
353             if (n++)
354               g_string_append (string, " ");
355             g_string_append (string, cells[i].name);
356           }
357     }
358
359   g_string_append (string, ".\n");
360
361   text = string->str;
362
363   g_string_free (string, FALSE);
364
365   return text;
366 }
367
368 static void
369 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class)
370 {
371   PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_crosstabs_activate;
372   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
373 }
374
375
376 static void
377 psppire_dialog_action_crosstabs_init (PsppireDialogActionCrosstabs *act)
378 {
379 }