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