1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2012 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-crosstabs.h"
21 #include "psppire-value-entry.h"
23 #include "dialog-common.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
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"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
40 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class);
42 G_DEFINE_TYPE (PsppireDialogActionCrosstabs, psppire_dialog_action_crosstabs, PSPPIRE_TYPE_DIALOG_ACTION);
45 dialog_state_valid (gpointer data)
47 PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (data);
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));
54 return (gtk_tree_model_get_iter_first (row_vars, ¬used)
55 && gtk_tree_model_get_iter_first (col_vars, ¬used));
59 refresh (PsppireDialogAction *rd_)
61 PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (rd_);
63 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_rows));
64 gtk_list_store_clear (GTK_LIST_STORE (liststore));
66 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (cd->dest_cols));
67 gtk_list_store_clear (GTK_LIST_STORE (liststore));
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)
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)
102 #define CS(NAME, LABEL, TOOLTIP) CS_##NAME,
110 #define CS(NAME, LABEL, TOOLTIP) CS_##NAME,
118 #define CS(NAME, LABEL, TOOLTIP) B_CS_##NAME = 1u << CS_##NAME,
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,
129 static const struct checkbox_entry_item stats[] =
131 #define CS(NAME, LABEL, TOOLTIP) {#NAME, LABEL, TOOLTIP},
133 CS(NONE, N_("None"), NULL)
137 static const struct checkbox_entry_item cells[] =
139 #define CS(NAME, LABEL, TOOLTIP) {#NAME, LABEL, TOOLTIP},
141 CS(NONE, N_("None"), NULL)
146 on_format_clicked (PsppireDialogActionCrosstabs *cd)
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);
154 ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->format_dialog));
156 if (ret == PSPPIRE_RESPONSE_CONTINUE)
158 cd->format_options_avalue =
159 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->avalue_button));
161 cd->format_options_table =
162 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->table_button));
164 cd->format_options_pivot =
165 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->pivot_button));
170 on_cell_clicked (PsppireDialogActionCrosstabs *cd)
172 GtkListStore *liststore = clone_list_store (GTK_LIST_STORE (cd->cell));
174 gint ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->cell_dialog));
176 if (ret == PSPPIRE_RESPONSE_CONTINUE)
178 g_object_unref (liststore);
182 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->cell_view) , GTK_TREE_MODEL (liststore));
183 cd->cell = GTK_TREE_MODEL (liststore);
189 on_statistics_clicked (PsppireDialogActionCrosstabs *cd)
191 GtkListStore *liststore = clone_list_store (GTK_LIST_STORE (cd->stat));
193 gint ret = psppire_dialog_run (PSPPIRE_DIALOG (cd->stat_dialog));
195 if (ret == PSPPIRE_RESPONSE_CONTINUE)
197 g_object_unref (liststore);
201 gtk_tree_view_set_model (GTK_TREE_VIEW (cd->stat_view) , GTK_TREE_MODEL (liststore));
202 cd->stat = GTK_TREE_MODEL (liststore);
208 psppire_dialog_action_crosstabs_activate (PsppireDialogAction *a, GVariant *param)
210 PsppireDialogActionCrosstabs *act = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
211 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
213 GtkBuilder *xml = builder_new ("crosstabs.ui");
215 pda->dialog = get_widget_assert (xml, "crosstabs-dialog");
216 pda->source = get_widget_assert (xml, "dict-treeview");
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");
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");
233 act->format_options_avalue = TRUE;
234 act->format_options_table = TRUE;
235 act->format_options_pivot = TRUE;
237 psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->cell_view),
242 act->cell = gtk_tree_view_get_model (GTK_TREE_VIEW (act->cell_view));
244 psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
249 act->stat = gtk_tree_view_get_model (GTK_TREE_VIEW (act->stat_view));
251 psppire_dialog_action_set_refresh (pda, refresh);
253 psppire_dialog_action_set_valid_predicate (pda,
256 g_signal_connect_swapped (act->cell_button, "clicked",
257 G_CALLBACK (on_cell_clicked), act);
259 g_signal_connect_swapped (act->stat_button, "clicked",
260 G_CALLBACK (on_statistics_clicked), act);
262 g_signal_connect_swapped (act->format_button, "clicked",
263 G_CALLBACK (on_format_clicked), act);
271 generate_syntax (const PsppireDialogAction *a)
273 PsppireDialogActionCrosstabs *cd = PSPPIRE_DIALOG_ACTION_CROSSTABS (a);
277 GString *string = g_string_new ("CROSSTABS ");
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);
287 g_string_append (string, "\n\t/FORMAT=");
289 if (cd->format_options_avalue)
290 g_string_append (string, "AVALUE");
292 g_string_append (string, "DVALUE");
293 g_string_append (string, " ");
295 if (cd->format_options_table)
296 g_string_append (string, "TABLES");
298 g_string_append (string, "NOTABLES");
299 g_string_append (string, " ");
301 if (cd->format_options_pivot)
302 g_string_append (string, "PIVOT");
304 g_string_append (string, "NOPIVOT");
308 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->stat, &iter); ok;
309 i++, ok = gtk_tree_model_iter_next (cd->stat, &iter))
312 gtk_tree_model_get (cd->stat, &iter,
313 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
317 selected &= ~(1u << i);
320 if (!(selected & (1u << CS_STATS_NONE)))
324 g_string_append (string, "\n\t/STATISTICS=");
326 for (i = 0; i < N_CROSSTABS_STATS; i++)
327 if (selected & (1u << i))
330 g_string_append (string, " ");
331 g_string_append (string, stats[i].name);
337 for (i = 0, ok = gtk_tree_model_get_iter_first (cd->cell, &iter); ok;
338 i++, ok = gtk_tree_model_iter_next (cd->cell, &iter))
341 gtk_tree_model_get (cd->cell, &iter,
342 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
346 selected &= ~(1u << i);
351 g_string_append (string, "\n\t/CELLS=");
352 if (selected & (1u << CS_CELLS_NONE))
353 g_string_append (string, "NONE");
357 for (i = 0; i < N_CROSSTABS_CELLS; i++)
358 if (selected & (1u << i))
361 g_string_append (string, " ");
362 g_string_append (string, cells[i].name);
366 g_string_append (string, ".\n");
370 g_string_free (string, FALSE);
376 psppire_dialog_action_crosstabs_class_init (PsppireDialogActionCrosstabsClass *class)
378 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_crosstabs_activate;
379 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
384 psppire_dialog_action_crosstabs_init (PsppireDialogActionCrosstabs *act)