c6343d5bbb7ccfc57d90bc64a6049a51594a5de5
[pspp-builds.git] / src / ui / gui / descriptives-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011  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 #include <config.h>
18
19 #include "checkbox-treeview.h"
20 #include "descriptives-dialog.h"
21 #include "psppire-var-view.h"
22
23 #include <gtk/gtk.h>
24 #include <stdlib.h>
25
26 #include <ui/gui/psppire-data-window.h>
27 #include <ui/gui/dialog-common.h>
28 #include <ui/gui/dict-display.h>
29 #include <ui/gui/helper.h>
30 #include <ui/gui/psppire-dialog.h>
31 #include <ui/gui/psppire-var-store.h>
32 #include "executor.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38 #define DESCRIPTIVE_STATS                       \
39   DS (MEAN, N_("Mean"))                         \
40   DS (STDDEV, N_("Standard deviation"))         \
41   DS (MINIMUM, N_("Minimum"))                   \
42   DS (MAXIMUM, N_("Maximum"))                   \
43   DS (RANGE, N_("Range"))                       \
44   DS (SUM, N_("Sum"))                           \
45   DS (SEMEAN, N_("Standard error"))             \
46   DS (VARIANCE, N_("Variance"))                 \
47   DS (KURTOSIS, N_("Kurtosis"))                 \
48   DS (SKEWNESS, N_("Skewness"))
49
50 enum
51   {
52 #define DS(NAME, LABEL) DS_##NAME,
53     DESCRIPTIVE_STATS
54 #undef DS
55     N_DESCRIPTIVE_STATS
56   };
57
58 enum
59   {
60 #define DS(NAME, LABEL) B_DS_##NAME = 1u << DS_##NAME,
61     DESCRIPTIVE_STATS
62 #undef DS
63     B_DS_ALL = (1u << N_DESCRIPTIVE_STATS) - 1,
64     B_DS_DEFAULT = B_DS_MEAN | B_DS_STDDEV | B_DS_MINIMUM | B_DS_MAXIMUM
65   };
66
67
68 static const struct checkbox_entry_item stats[] =
69   {
70 #define DS(NAME, LABEL) {#NAME, LABEL},
71     DESCRIPTIVE_STATS
72 #undef DS
73   };
74
75 struct descriptives_dialog
76 {
77   GtkTreeView *stat_vars;
78   GtkTreeModel *stats;
79   PsppireDict *dict;
80   GtkToggleButton *exclude_missing_listwise;
81   GtkToggleButton *include_user_missing;
82   GtkToggleButton *save_z_scores;
83 };
84
85 static void
86 refresh (PsppireDialog *dialog, struct descriptives_dialog *scd)
87 {
88   GtkTreeModel *liststore;
89   GtkTreeIter iter;
90   size_t i;
91   bool ok;
92
93   liststore = gtk_tree_view_get_model (scd->stat_vars);
94   gtk_list_store_clear (GTK_LIST_STORE (liststore));
95
96   for (i = 0, ok = gtk_tree_model_get_iter_first (scd->stats, &iter); ok;
97        i++, ok = gtk_tree_model_iter_next (scd->stats, &iter))
98     gtk_list_store_set (GTK_LIST_STORE (scd->stats), &iter,
99                         CHECKBOX_COLUMN_SELECTED,
100                         (B_DS_DEFAULT & (1u << i)) ? true : false, -1);
101
102   gtk_toggle_button_set_active (scd->exclude_missing_listwise, false);
103   gtk_toggle_button_set_active (scd->include_user_missing, false);
104   gtk_toggle_button_set_active (scd->save_z_scores, false);
105 }
106
107 static char *
108 generate_syntax (const struct descriptives_dialog *scd)
109 {
110   gchar *text;
111   GString *string;
112   GtkTreeIter iter;
113   unsigned int selected;
114   size_t i;
115   bool listwise, include;
116   bool ok;
117
118   string = g_string_new ("DESCRIPTIVES");
119   g_string_append (string, "\n    /VARIABLES=");
120   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (scd->stat_vars), 0, string);
121
122   listwise = gtk_toggle_button_get_active (scd->exclude_missing_listwise);
123   include = gtk_toggle_button_get_active (scd->include_user_missing);
124   if (listwise || include)
125     {
126       g_string_append (string, "\n    /MISSING=");
127       if (listwise)
128         {
129           g_string_append (string, "LISTWISE");
130           if (include)
131             g_string_append (string, " ");
132         }
133       if (include)
134         g_string_append (string, "INCLUDE");
135     }
136
137   selected = 0;
138   for (i = 0, ok = gtk_tree_model_get_iter_first (scd->stats, &iter); ok;
139        i++, ok = gtk_tree_model_iter_next (scd->stats, &iter))
140     {
141       gboolean toggled;
142       gtk_tree_model_get (scd->stats, &iter,
143                           CHECKBOX_COLUMN_SELECTED, &toggled, -1);
144       if (toggled)
145         selected |= 1u << i;
146     }
147
148   if (selected != B_DS_DEFAULT)
149     {
150       g_string_append (string, "\n    /STATISTICS=");
151       if (selected == B_DS_ALL)
152         g_string_append (string, "ALL");
153       else if (selected == 0)
154         g_string_append (string, "NONE");
155       else
156         {
157           int n = 0;
158           if ((selected & B_DS_DEFAULT) == B_DS_DEFAULT)
159             {
160               g_string_append (string, "DEFAULT");
161               selected &= ~B_DS_DEFAULT;
162               n++;
163             }
164           for (i = 0; i < N_DESCRIPTIVE_STATS; i++)
165             if (selected & (1u << i))
166               {
167                 if (n++)
168                   g_string_append (string, " ");
169                 g_string_append (string, stats[i].name);
170               }
171         }
172     }
173
174   if (gtk_toggle_button_get_active (scd->save_z_scores))
175     g_string_append (string, "\n    /SAVE");
176
177   g_string_append (string, ".");
178
179   if (gtk_toggle_button_get_active (scd->save_z_scores))
180     g_string_append (string, "\nEXECUTE.");
181
182   text = string->str;
183
184   g_string_free (string, FALSE);
185
186   return text;
187 }
188
189
190 /* Dialog is valid iff at least one variable has been selected */
191 static gboolean
192 dialog_state_valid (gpointer data)
193 {
194   struct descriptives_dialog *dd = data;
195
196   GtkTreeModel *vars = gtk_tree_view_get_model (dd->stat_vars);
197
198   GtkTreeIter notused;
199
200   return gtk_tree_model_get_iter_first (vars, &notused);
201 }
202
203 /* Pops up the Descriptives dialog box */
204 void
205 descriptives_dialog (PsppireDataWindow *de)
206 {
207   gint response;
208
209   struct descriptives_dialog scd;
210
211   GtkBuilder *xml = builder_new ("descriptives.ui");
212
213   GtkWidget *dialog = get_widget_assert   (xml, "descriptives-dialog");
214
215
216   GtkWidget *source = get_widget_assert   (xml, "all-variables");
217   GtkWidget *dest =   get_widget_assert   (xml, "stat-variables");
218
219   GtkWidget *stats_treeview = get_widget_assert    (xml, "statistics");
220
221   PsppireVarStore *vs = NULL;
222   PsppireDict *dict;
223
224   g_object_get (de->data_editor, "var-store", &vs, NULL);
225   g_object_get (vs, "dictionary", &dict, NULL);
226
227   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
228
229
230   g_object_set (source, "model", dict,
231         "predicate", var_is_numeric, NULL);
232
233   put_checkbox_items_in_treeview (GTK_TREE_VIEW (stats_treeview),
234                                   B_DS_DEFAULT,
235                                   N_DESCRIPTIVE_STATS, stats);
236
237   scd.stat_vars = GTK_TREE_VIEW (dest);
238   scd.stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
239   
240   g_object_get (vs, "dictionary", &scd.dict, NULL);
241   
242   scd.include_user_missing =
243     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "include_user_missing"));
244   scd.exclude_missing_listwise =
245     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "exclude_missing_listwise"));
246   scd.save_z_scores =
247     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "save_z_scores"));
248
249   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &scd);
250
251   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
252                                       dialog_state_valid, &scd);
253
254   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
255
256
257   switch (response)
258     {
259     case GTK_RESPONSE_OK:
260       g_free (execute_syntax_string (de, generate_syntax (&scd)));
261       break;
262     case PSPPIRE_RESPONSE_PASTE:
263       g_free (paste_syntax_to_window (generate_syntax (&scd)));
264       break;
265     default:
266       break;
267     }
268
269   g_object_unref (xml);
270 }