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>
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 append_variable_names (string, rd->dict, GTK_TREE_VIEW (rd->indep_vars), 0);
161 g_string_append (string, "\n\t/DEPENDENT=\t");
162 append_variable_names (string, rd->dict, GTK_TREE_VIEW (rd->dep_vars), 0);
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 (GObject *o, gpointer data)
227 struct regression_dialog rd;
229 GtkBuilder *xml = builder_new ("regression.ui");
230 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
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 *dep_selector = get_widget_assert (xml, "dep-selector");
238 GtkWidget *indep_selector = get_widget_assert (xml, "indep-selector");
239 GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
240 GtkWidget *save_button = get_widget_assert (xml, "save-button");
243 rd.stat_view = get_widget_assert (xml, "stat-view");
245 g_object_get (de->data_editor, "var-store", &vs, NULL);
248 put_checkbox_items_in_treeview (GTK_TREE_VIEW(rd.stat_view),
254 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
256 g_object_set (source, "dictionary", vs->dict, NULL);
258 set_dest_model (GTK_TREE_VIEW (dest_dep), vs->dict);
259 set_dest_model (GTK_TREE_VIEW (dest_indep), vs->dict);
261 psppire_selector_set_subjects (PSPPIRE_SELECTOR (dep_selector),
264 insert_source_row_into_tree_view,
268 psppire_selector_set_subjects (PSPPIRE_SELECTOR (indep_selector),
271 insert_source_row_into_tree_view,
275 rd.dep_vars = GTK_TREE_VIEW (dest_dep);
276 rd.indep_vars = GTK_TREE_VIEW (dest_indep);
278 rd.save_dialog = get_widget_assert (xml, "save-dialog");
279 rd.pred_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pred-button"));
280 rd.resid_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "resid-button"));
281 rd.stat_dialog = get_widget_assert (xml, "statistics-dialog");
283 rd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (rd.stat_view));
284 rd.current_opts.pred = FALSE;
285 rd.current_opts.resid = FALSE;
287 gtk_window_set_transient_for (GTK_WINDOW (rd.save_dialog), GTK_WINDOW (de));
288 gtk_window_set_transient_for (GTK_WINDOW (rd.stat_dialog), GTK_WINDOW (de));
290 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &rd);
292 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
293 dialog_state_valid, &rd);
295 g_signal_connect_swapped (stat_button, "clicked",
296 G_CALLBACK (on_statistics_clicked), &rd);
297 g_signal_connect_swapped (save_button, "clicked",
298 G_CALLBACK (on_save_clicked), &rd);
300 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
305 case GTK_RESPONSE_OK:
307 gchar *syntax = generate_syntax (&rd);
309 struct getl_interface *sss = create_syntax_string_source (syntax);
310 execute_syntax (sss);
315 case PSPPIRE_RESPONSE_PASTE:
317 gchar *syntax = generate_syntax (&rd);
318 paste_syntax_in_new_window (syntax);
327 g_object_unref (xml);