1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008 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 "regression-dialog.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>
33 #include <ui/gui/psppire-var-view.h>
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
41 #define REGRESSION_STATS \
42 RG (COEFF, N_("Coeff")) \
44 RG (ANOVA, N_("Anova")) \
48 #define RG(NAME, LABEL) RG_##NAME,
56 #define RG(NAME, LABEL) B_RG_##NAME = 1u << RG_##NAME,
59 B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
60 B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
63 static const struct checkbox_entry_item stats[] =
65 #define RG(NAME, LABEL) {#NAME, LABEL},
75 struct regression_dialog
77 GtkTreeView *dep_vars;
78 GtkTreeView *indep_vars;
81 GtkToggleButton *resid_button;
82 GtkToggleButton *pred_button;
84 GtkWidget *stat_dialog;
85 GtkWidget *save_dialog;
89 struct save_options current_opts;
93 refresh (PsppireDialog *dialog, struct regression_dialog *rd)
95 GtkTreeModel *liststore = gtk_tree_view_get_model (rd->dep_vars);
96 gtk_list_store_clear (GTK_LIST_STORE (liststore));
98 liststore = gtk_tree_view_get_model (rd->indep_vars);
99 gtk_list_store_clear (GTK_LIST_STORE (liststore));
103 on_statistics_clicked (struct regression_dialog *rd)
105 GtkListStore *liststore;
108 liststore = clone_list_store (GTK_LIST_STORE (rd->stat));
110 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
112 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
114 g_object_unref (liststore);
118 g_object_unref (rd->stat);
119 gtk_tree_view_set_model (GTK_TREE_VIEW (rd->stat_view) , GTK_TREE_MODEL (liststore));
120 rd->stat = GTK_TREE_MODEL (liststore);
125 on_save_clicked (struct regression_dialog *rd)
128 if (rd->current_opts.pred)
130 gtk_toggle_button_set_active (rd->pred_button, TRUE);
132 if (rd->current_opts.resid)
134 gtk_toggle_button_set_active (rd->resid_button, TRUE);
137 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
139 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
141 rd->current_opts.pred = (gtk_toggle_button_get_active (rd->pred_button) == TRUE)
143 rd->current_opts.resid = (gtk_toggle_button_get_active (rd->resid_button) == TRUE)
149 generate_syntax (const struct regression_dialog *rd)
158 GString *string = g_string_new ("REGRESSION");
160 g_string_append (string, "\n\t/VARIABLES=");
161 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
162 g_string_append (string, "\n\t/DEPENDENT=\t");
163 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
166 for (i = 0, ok = gtk_tree_model_get_iter_first (rd->stat, &iter); ok;
167 i++, ok = gtk_tree_model_iter_next (rd->stat, &iter))
170 gtk_tree_model_get (rd->stat, &iter,
171 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
175 selected &= ~(1u << i);
180 g_string_append (string, "\n\t/STATISTICS=");
182 for (i = 0; i < N_REGRESSION_STATS; i++)
183 if (selected & (1u << i))
186 g_string_append (string, " ");
187 g_string_append (string, stats[i].name);
190 if (rd->current_opts.pred || rd->current_opts.resid)
192 g_string_append (string, "\n\t/SAVE=");
193 if (rd->current_opts.pred)
194 g_string_append (string, " PRED");
195 if (rd->current_opts.resid)
196 g_string_append (string, " RESID");
198 g_string_append (string, ".\n");
202 g_string_free (string, FALSE);
207 /* Dialog is valid iff at least one dependent and one independent variable have
210 dialog_state_valid (gpointer data)
212 struct regression_dialog *rd = data;
214 GtkTreeModel *dep_vars = gtk_tree_view_get_model (rd->dep_vars);
215 GtkTreeModel *indep_vars = gtk_tree_view_get_model (rd->indep_vars);
219 return (gtk_tree_model_get_iter_first (dep_vars, ¬used)
220 && gtk_tree_model_get_iter_first (indep_vars, ¬used));
223 /* Pops up the Regression dialog box */
225 regression_dialog (PsppireDataWindow *de)
228 struct regression_dialog rd;
230 GtkBuilder *xml = builder_new ("regression.ui");
233 GtkWidget *dialog = get_widget_assert (xml, "regression-dialog");
234 GtkWidget *source = get_widget_assert (xml, "dict-view");
235 GtkWidget *dest_dep = get_widget_assert (xml, "dep-view");
236 GtkWidget *dest_indep = get_widget_assert (xml, "indep-view");
237 GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
238 GtkWidget *save_button = get_widget_assert (xml, "save-button");
240 GtkWidget *dep_selector = get_widget_assert (xml, "dep-selector");
242 rd.stat_view = get_widget_assert (xml, "stat-view");
244 g_object_get (de->data_editor, "var-store", &vs, NULL);
247 put_checkbox_items_in_treeview (GTK_TREE_VIEW(rd.stat_view),
253 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
255 g_object_get (vs, "dictionary", &rd.dict, NULL);
256 g_object_set (source, "model", rd.dict, NULL);
258 rd.dep_vars = GTK_TREE_VIEW (dest_dep);
259 rd.indep_vars = GTK_TREE_VIEW (dest_indep);
261 psppire_selector_set_allow (PSPPIRE_SELECTOR (dep_selector), numeric_only);
263 rd.save_dialog = get_widget_assert (xml, "save-dialog");
264 rd.pred_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pred-button"));
265 rd.resid_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "resid-button"));
266 rd.stat_dialog = get_widget_assert (xml, "statistics-dialog");
268 rd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (rd.stat_view));
269 rd.current_opts.pred = FALSE;
270 rd.current_opts.resid = FALSE;
272 gtk_window_set_transient_for (GTK_WINDOW (rd.save_dialog), GTK_WINDOW (de));
273 gtk_window_set_transient_for (GTK_WINDOW (rd.stat_dialog), GTK_WINDOW (de));
275 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &rd);
277 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
278 dialog_state_valid, &rd);
280 g_signal_connect_swapped (stat_button, "clicked",
281 G_CALLBACK (on_statistics_clicked), &rd);
282 g_signal_connect_swapped (save_button, "clicked",
283 G_CALLBACK (on_save_clicked), &rd);
285 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
290 case GTK_RESPONSE_OK:
292 gchar *syntax = generate_syntax (&rd);
294 struct getl_interface *sss = create_syntax_string_source (syntax);
295 execute_syntax (sss);
300 case PSPPIRE_RESPONSE_PASTE:
302 gchar *syntax = generate_syntax (&rd);
303 paste_syntax_to_window (syntax);
312 g_object_unref (xml);