1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010 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/>. */
19 #include "checkbox-treeview.h"
20 #include "descriptives-dialog.h"
21 #include "psppire-var-view.h"
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>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
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")) \
46 DS (SEMEAN, N_("Standard error")) \
47 DS (VARIANCE, N_("Variance")) \
48 DS (KURTOSIS, N_("Kurtosis")) \
49 DS (SKEWNESS, N_("Skewness"))
53 #define DS(NAME, LABEL) DS_##NAME,
61 #define DS(NAME, LABEL) B_DS_##NAME = 1u << DS_##NAME,
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
69 static const struct checkbox_entry_item stats[] =
71 #define DS(NAME, LABEL) {#NAME, LABEL},
76 struct descriptives_dialog
78 GtkTreeView *stat_vars;
81 GtkToggleButton *exclude_missing_listwise;
82 GtkToggleButton *include_user_missing;
83 GtkToggleButton *save_z_scores;
87 refresh (PsppireDialog *dialog, struct descriptives_dialog *scd)
89 GtkTreeModel *liststore;
94 liststore = gtk_tree_view_get_model (scd->stat_vars);
95 gtk_list_store_clear (GTK_LIST_STORE (liststore));
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);
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);
109 generate_syntax (const struct descriptives_dialog *scd)
114 unsigned int selected;
116 bool listwise, include;
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);
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)
127 g_string_append (string, "\n /MISSING=");
130 g_string_append (string, "LISTWISE");
132 g_string_append (string, " ");
135 g_string_append (string, "INCLUDE");
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))
143 gtk_tree_model_get (scd->stats, &iter,
144 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
149 if (selected != B_DS_DEFAULT)
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");
159 if ((selected & B_DS_DEFAULT) == B_DS_DEFAULT)
161 g_string_append (string, "DEFAULT");
162 selected &= ~B_DS_DEFAULT;
165 for (i = 0; i < N_DESCRIPTIVE_STATS; i++)
166 if (selected & (1u << i))
169 g_string_append (string, " ");
170 g_string_append (string, stats[i].name);
175 if (gtk_toggle_button_get_active (scd->save_z_scores))
176 g_string_append (string, "\n /SAVE");
178 g_string_append (string, ".");
180 if (gtk_toggle_button_get_active (scd->save_z_scores))
181 g_string_append (string, "\nEXECUTE.");
185 g_string_free (string, FALSE);
191 /* Dialog is valid iff at least one variable has been selected */
193 dialog_state_valid (gpointer data)
195 struct descriptives_dialog *dd = data;
197 GtkTreeModel *vars = gtk_tree_view_get_model (dd->stat_vars);
201 return gtk_tree_model_get_iter_first (vars, ¬used);
204 /* Pops up the Descriptives dialog box */
206 descriptives_dialog (PsppireDataWindow *de)
210 struct descriptives_dialog scd;
212 GtkBuilder *xml = builder_new ("descriptives.ui");
214 GtkWidget *dialog = get_widget_assert (xml, "descriptives-dialog");
217 GtkWidget *source = get_widget_assert (xml, "all-variables");
218 GtkWidget *dest = get_widget_assert (xml, "stat-variables");
220 GtkWidget *stats_treeview = get_widget_assert (xml, "statistics");
222 PsppireVarStore *vs = NULL;
225 g_object_get (de->data_editor, "var-store", &vs, NULL);
226 g_object_get (vs, "dictionary", &dict, NULL);
228 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
231 g_object_set (source, "model", dict,
232 "predicate", var_is_numeric, NULL);
234 put_checkbox_items_in_treeview (GTK_TREE_VIEW (stats_treeview),
236 N_DESCRIPTIVE_STATS, stats);
238 scd.stat_vars = GTK_TREE_VIEW (dest);
239 scd.stats = gtk_tree_view_get_model (GTK_TREE_VIEW (stats_treeview));
241 g_object_get (vs, "dictionary", &scd.dict, NULL);
243 scd.include_user_missing =
244 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "include_user_missing"));
245 scd.exclude_missing_listwise =
246 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "exclude_missing_listwise"));
248 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "save_z_scores"));
250 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &scd);
252 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
253 dialog_state_valid, &scd);
255 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
260 case GTK_RESPONSE_OK:
262 gchar *syntax = generate_syntax (&scd);
264 struct getl_interface *sss = create_syntax_string_source (syntax);
265 execute_syntax (sss);
270 case PSPPIRE_RESPONSE_PASTE:
272 gchar *syntax = generate_syntax (&scd);
273 paste_syntax_to_window (syntax);
281 g_object_unref (xml);