Descriptives Dialog: Avoid multiple appendages to the statistics list box
[pspp] / src / ui / gui / psppire-dialog-action-descriptives.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2015  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-descriptives.h"
21
22 #include "psppire-checkbox-treeview.h"
23
24 #include "psppire-var-view.h"
25 #include "psppire-dict.h"
26 #include "psppire-dialog.h"
27 #include "builder-wrapper.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 static void psppire_dialog_action_descriptives_class_init      (PsppireDialogActionDescriptivesClass *class);
34
35 G_DEFINE_TYPE (PsppireDialogActionDescriptives, psppire_dialog_action_descriptives, PSPPIRE_TYPE_DIALOG_ACTION);
36
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
76 static char *
77 generate_syntax (PsppireDialogAction *act)
78 {
79   PsppireDialogActionDescriptives *scd = PSPPIRE_DIALOG_ACTION_DESCRIPTIVES (act);
80   gchar *text;
81   GString *string;
82   GtkTreeIter iter;
83   unsigned int selected;
84   size_t i;
85   bool listwise, include;
86   bool ok;
87
88   string = g_string_new ("DESCRIPTIVES");
89   g_string_append (string, "\n    /VARIABLES=");
90   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (scd->stat_vars), 0, string);
91
92   listwise = gtk_toggle_button_get_active (scd->exclude_missing_listwise);
93   include = gtk_toggle_button_get_active (scd->include_user_missing);
94   if (listwise || include)
95     {
96       g_string_append (string, "\n    /MISSING=");
97       if (listwise)
98         {
99           g_string_append (string, "LISTWISE");
100           if (include)
101             g_string_append (string, " ");
102         }
103       if (include)
104         g_string_append (string, "INCLUDE");
105     }
106
107   selected = 0;
108   for (i = 0, ok = gtk_tree_model_get_iter_first (scd->stats, &iter); ok;
109        i++, ok = gtk_tree_model_iter_next (scd->stats, &iter))
110     {
111       gboolean toggled;
112       gtk_tree_model_get (scd->stats, &iter,
113                           CHECKBOX_COLUMN_SELECTED, &toggled, -1);
114       if (toggled)
115         selected |= 1u << i;
116     }
117
118   if (selected != B_DS_DEFAULT)
119     {
120       g_string_append (string, "\n    /STATISTICS=");
121       if (selected == B_DS_ALL)
122         g_string_append (string, "ALL");
123       else if (selected == 0)
124         g_string_append (string, "NONE");
125       else
126         {
127           int n = 0;
128           if ((selected & B_DS_DEFAULT) == B_DS_DEFAULT)
129             {
130               g_string_append (string, "DEFAULT");
131               selected &= ~B_DS_DEFAULT;
132               n++;
133             }
134           for (i = 0; i < N_DESCRIPTIVE_STATS; i++)
135             if (selected & (1u << i))
136               {
137                 if (n++)
138                   g_string_append (string, " ");
139                 g_string_append (string, stats[i].name);
140               }
141         }
142     }
143
144   if (gtk_toggle_button_get_active (scd->save_z_scores))
145     g_string_append (string, "\n    /SAVE");
146
147   g_string_append (string, ".");
148
149   if (gtk_toggle_button_get_active (scd->save_z_scores))
150     g_string_append (string, "\nEXECUTE.");
151
152   text = string->str;
153
154   g_string_free (string, FALSE);
155
156   return text;
157 }
158
159 static gboolean
160 dialog_state_valid (gpointer data)
161 {
162   PsppireDialogActionDescriptives *dd = data;
163
164   GtkTreeModel *vars = gtk_tree_view_get_model (dd->stat_vars);
165
166   GtkTreeIter notused;
167
168   return gtk_tree_model_get_iter_first (vars, &notused);
169 }
170
171 static void
172 dialog_refresh (PsppireDialogAction *scd_)
173 {
174   PsppireDialogActionDescriptives *scd
175     = PSPPIRE_DIALOG_ACTION_DESCRIPTIVES (scd_);
176   GtkTreeModel *liststore;
177   GtkTreeIter iter;
178   size_t i;
179   bool ok;
180
181   liststore = gtk_tree_view_get_model (scd->stat_vars);
182   gtk_list_store_clear (GTK_LIST_STORE (liststore));
183
184   for (i = 0, ok = gtk_tree_model_get_iter_first (scd->stats, &iter); ok;
185        i++, ok = gtk_tree_model_iter_next (scd->stats, &iter))
186     gtk_list_store_set (GTK_LIST_STORE (scd->stats), &iter,
187                         CHECKBOX_COLUMN_SELECTED,
188                         (B_DS_DEFAULT & (1u << i)) ? true : false, -1);
189
190   gtk_toggle_button_set_active (scd->exclude_missing_listwise, false);
191   gtk_toggle_button_set_active (scd->include_user_missing, false);
192   gtk_toggle_button_set_active (scd->save_z_scores, false);
193 }
194
195
196 static void
197 psppire_dialog_action_descriptives_activate (GtkAction *a)
198 {
199   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
200   PsppireDialogActionDescriptives *act = PSPPIRE_DIALOG_ACTION_DESCRIPTIVES (a);
201
202   GHashTable *thing = psppire_dialog_action_get_pointer (pda);
203   GtkBuilder *xml = g_hash_table_lookup (thing, a);
204   if (!xml)
205     {
206       xml = builder_new ("descriptives.ui");
207       g_hash_table_insert (thing, a, xml);
208
209       GtkWidget *stats_treeview = get_widget_assert (xml, "statistics");
210       psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (stats_treeview),
211                                           B_DS_DEFAULT,
212                                           N_DESCRIPTIVE_STATS, stats);
213       act->stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
214     }
215
216   pda->dialog = get_widget_assert   (xml, "descriptives-dialog");
217   pda->source = get_widget_assert   (xml, "all-variables");
218   act->variables =   get_widget_assert   (xml, "stat-variables");
219
220   g_object_set (pda->source,
221                 "predicate", var_is_numeric, NULL);
222
223   act->stat_vars = GTK_TREE_VIEW (act->variables);
224   
225   act->include_user_missing =
226     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "include_user_missing"));
227   act->exclude_missing_listwise =
228     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "exclude_missing_listwise"));
229   act->save_z_scores =
230     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "save_z_scores"));
231
232   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
233   psppire_dialog_action_set_refresh (pda, dialog_refresh);
234
235   PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_descriptives_parent_class)->activate (pda);
236 }
237
238 static void
239 psppire_dialog_action_descriptives_class_init (PsppireDialogActionDescriptivesClass *class)
240 {
241   psppire_dialog_action_set_activation (class, psppire_dialog_action_descriptives_activate);
242
243   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
244 }
245
246 static void
247 psppire_dialog_action_descriptives_init (PsppireDialogActionDescriptives *act)
248 {
249 }