a867fe016d05131f0ff759e3536118c5ae60c855
[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
22 #include <gtk/gtk.h>
23 #include <stdlib.h>
24
25 #include <language/syntax-string-source.h>
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   append_variable_names (string, scd->dict, GTK_TREE_VIEW (scd->stat_vars), 0);
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   text = string->str;
180
181   g_string_free (string, FALSE);
182
183   return text;
184 }
185
186
187 /* Dialog is valid iff at least one variable has been selected */
188 static gboolean
189 dialog_state_valid (gpointer data)
190 {
191   struct descriptives_dialog *dd = data;
192
193   GtkTreeModel *vars = gtk_tree_view_get_model (dd->stat_vars);
194
195   GtkTreeIter notused;
196
197   return gtk_tree_model_get_iter_first (vars, &notused);
198 }
199
200 /* Pops up the Descriptives dialog box */
201 void
202 descriptives_dialog (GObject *o, gpointer data)
203 {
204   gint response;
205   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
206
207   struct descriptives_dialog scd;
208
209   GtkBuilder *xml = builder_new ("descriptives-dialog.ui");
210
211   GtkWidget *dialog = get_widget_assert   (xml, "descriptives-dialog");
212
213
214   GtkWidget *source = get_widget_assert   (xml, "all-variables");
215   GtkWidget *selector = get_widget_assert (xml, "stat-var-selector");
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
222   g_object_get (de->data_editor, "var-store", &vs, NULL);
223
224   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
225
226   g_object_set (source, "dictionary", vs->dict,
227         "predicate", var_is_numeric, NULL);
228
229   set_dest_model (GTK_TREE_VIEW (dest), vs->dict);
230
231
232   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
233                                  source,
234                                  dest,
235                                  insert_source_row_into_tree_view,
236                                  NULL,
237                                  NULL);
238
239   put_checkbox_items_in_treeview (GTK_TREE_VIEW (stats_treeview),
240                                   B_DS_DEFAULT,
241                                   N_DESCRIPTIVE_STATS, stats);
242
243   scd.stat_vars = GTK_TREE_VIEW (dest);
244   scd.stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
245   scd.dict = vs->dict;
246   scd.include_user_missing =
247     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "include_user_missing"));
248   scd.exclude_missing_listwise =
249     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "exclude_missing_listwise"));
250   scd.save_z_scores =
251     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "save_z_scores"));
252
253   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &scd);
254
255   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
256                                       dialog_state_valid, &scd);
257
258   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
259
260
261   switch (response)
262     {
263     case GTK_RESPONSE_OK:
264       {
265         gchar *syntax = generate_syntax (&scd);
266
267         struct getl_interface *sss = create_syntax_string_source (syntax);
268         execute_syntax (sss);
269
270         g_free (syntax);
271       }
272       break;
273     case PSPPIRE_RESPONSE_PASTE:
274       {
275         gchar *syntax = generate_syntax (&scd);
276         paste_syntax_in_new_window (syntax);
277         g_free (syntax);
278       }
279       break;
280     default:
281       break;
282     }
283
284   g_object_unref (xml);
285 }