1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2010, 2011 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 <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 <ui/gui/psppire-var-view.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
40 #define REGRESSION_STATS \
41 RG (COEFF, N_("Coeff")) \
43 RG (ANOVA, N_("Anova")) \
47 #define RG(NAME, LABEL) RG_##NAME,
55 #define RG(NAME, LABEL) B_RG_##NAME = 1u << RG_##NAME,
58 B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
59 B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
62 static const struct checkbox_entry_item stats[] =
64 #define RG(NAME, LABEL) {#NAME, LABEL},
74 struct regression_dialog
76 GtkTreeView *dep_vars;
77 GtkTreeView *indep_vars;
80 GtkToggleButton *resid_button;
81 GtkToggleButton *pred_button;
83 GtkWidget *stat_dialog;
84 GtkWidget *save_dialog;
88 struct save_options current_opts;
92 refresh (PsppireDialog *dialog, struct regression_dialog *rd)
94 GtkTreeModel *liststore = gtk_tree_view_get_model (rd->dep_vars);
95 gtk_list_store_clear (GTK_LIST_STORE (liststore));
97 liststore = gtk_tree_view_get_model (rd->indep_vars);
98 gtk_list_store_clear (GTK_LIST_STORE (liststore));
102 on_statistics_clicked (struct regression_dialog *rd)
104 GtkListStore *liststore;
107 liststore = clone_list_store (GTK_LIST_STORE (rd->stat));
109 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
111 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
113 g_object_unref (liststore);
117 g_object_unref (rd->stat);
118 gtk_tree_view_set_model (GTK_TREE_VIEW (rd->stat_view) , GTK_TREE_MODEL (liststore));
119 rd->stat = GTK_TREE_MODEL (liststore);
124 on_save_clicked (struct regression_dialog *rd)
127 if (rd->current_opts.pred)
129 gtk_toggle_button_set_active (rd->pred_button, TRUE);
131 if (rd->current_opts.resid)
133 gtk_toggle_button_set_active (rd->resid_button, TRUE);
136 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
138 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
140 rd->current_opts.pred = (gtk_toggle_button_get_active (rd->pred_button) == TRUE)
142 rd->current_opts.resid = (gtk_toggle_button_get_active (rd->resid_button) == TRUE)
148 generate_syntax (const struct regression_dialog *rd)
157 GString *string = g_string_new ("REGRESSION");
159 g_string_append (string, "\n\t/VARIABLES=");
160 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
161 g_string_append (string, "\n\t/DEPENDENT=\t");
162 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
165 for (i = 0, ok = gtk_tree_model_get_iter_first (rd->stat, &iter); ok;
166 i++, ok = gtk_tree_model_iter_next (rd->stat, &iter))
169 gtk_tree_model_get (rd->stat, &iter,
170 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
174 selected &= ~(1u << i);
179 g_string_append (string, "\n\t/STATISTICS=");
181 for (i = 0; i < N_REGRESSION_STATS; i++)
182 if (selected & (1u << i))
185 g_string_append (string, " ");
186 g_string_append (string, stats[i].name);
189 if (rd->current_opts.pred || rd->current_opts.resid)
191 g_string_append (string, "\n\t/SAVE=");
192 if (rd->current_opts.pred)
193 g_string_append (string, " PRED");
194 if (rd->current_opts.resid)
195 g_string_append (string, " RESID");
197 g_string_append (string, ".\n");
201 g_string_free (string, FALSE);
206 /* Dialog is valid iff at least one dependent and one independent variable have
209 dialog_state_valid (gpointer data)
211 struct regression_dialog *rd = data;
213 GtkTreeModel *dep_vars = gtk_tree_view_get_model (rd->dep_vars);
214 GtkTreeModel *indep_vars = gtk_tree_view_get_model (rd->indep_vars);
218 return (gtk_tree_model_get_iter_first (dep_vars, ¬used)
219 && gtk_tree_model_get_iter_first (indep_vars, ¬used));
222 /* Pops up the Regression dialog box */
224 regression_dialog (PsppireDataWindow *de)
227 struct regression_dialog rd;
229 GtkBuilder *xml = builder_new ("regression.ui");
232 GtkWidget *dialog = get_widget_assert (xml, "regression-dialog");
233 GtkWidget *source = get_widget_assert (xml, "dict-view");
234 GtkWidget *dest_dep = get_widget_assert (xml, "dep-view");
235 GtkWidget *dest_indep = get_widget_assert (xml, "indep-view");
236 GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
237 GtkWidget *save_button = get_widget_assert (xml, "save-button");
239 GtkWidget *dep_selector = get_widget_assert (xml, "dep-selector");
241 rd.stat_view = get_widget_assert (xml, "stat-view");
243 g_object_get (de->data_editor, "var-store", &vs, NULL);
246 put_checkbox_items_in_treeview (GTK_TREE_VIEW(rd.stat_view),
252 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
254 g_object_get (vs, "dictionary", &rd.dict, NULL);
255 g_object_set (source, "model", rd.dict, NULL);
257 rd.dep_vars = GTK_TREE_VIEW (dest_dep);
258 rd.indep_vars = GTK_TREE_VIEW (dest_indep);
260 psppire_selector_set_allow (PSPPIRE_SELECTOR (dep_selector), numeric_only);
262 rd.save_dialog = get_widget_assert (xml, "save-dialog");
263 rd.pred_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pred-button"));
264 rd.resid_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "resid-button"));
265 rd.stat_dialog = get_widget_assert (xml, "statistics-dialog");
267 rd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (rd.stat_view));
268 rd.current_opts.pred = FALSE;
269 rd.current_opts.resid = FALSE;
271 gtk_window_set_transient_for (GTK_WINDOW (rd.save_dialog), GTK_WINDOW (de));
272 gtk_window_set_transient_for (GTK_WINDOW (rd.stat_dialog), GTK_WINDOW (de));
274 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &rd);
276 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
277 dialog_state_valid, &rd);
279 g_signal_connect_swapped (stat_button, "clicked",
280 G_CALLBACK (on_statistics_clicked), &rd);
281 g_signal_connect_swapped (save_button, "clicked",
282 G_CALLBACK (on_save_clicked), &rd);
284 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
289 case GTK_RESPONSE_OK:
290 g_free (execute_syntax_string (de, generate_syntax (&rd)));
292 case PSPPIRE_RESPONSE_PASTE:
293 g_free (paste_syntax_to_window (generate_syntax (&rd)));
299 g_object_unref (xml);