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"
25 #include <language/syntax-string-source.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/helper.h>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
39 #define REGRESSION_STATS \
40 RG (COEFF, N_("Coeff")) \
42 RG (ANOVA, N_("Anova")) \
46 #define RG(NAME, LABEL) RG_##NAME,
54 #define RG(NAME, LABEL) B_RG_##NAME = 1u << RG_##NAME,
57 B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
58 B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
61 static const struct checkbox_entry_item stats[] =
63 #define RG(NAME, LABEL) {#NAME, LABEL},
73 struct regression_dialog
75 GtkTreeView *dep_vars;
76 GtkTreeView *indep_vars;
79 GtkToggleButton *resid_button;
80 GtkToggleButton *pred_button;
82 GtkWidget *stat_dialog;
83 GtkWidget *save_dialog;
87 struct save_options current_opts;
91 refresh (PsppireDialog *dialog, struct regression_dialog *rd)
93 GtkTreeModel *liststore = gtk_tree_view_get_model (rd->dep_vars);
94 gtk_list_store_clear (GTK_LIST_STORE (liststore));
96 liststore = gtk_tree_view_get_model (rd->indep_vars);
97 gtk_list_store_clear (GTK_LIST_STORE (liststore));
101 on_statistics_clicked (struct regression_dialog *rd)
103 GtkListStore *liststore;
106 liststore = clone_list_store (GTK_LIST_STORE (rd->stat));
108 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
110 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
112 g_object_unref (liststore);
116 g_object_unref (rd->stat);
117 gtk_tree_view_set_model (GTK_TREE_VIEW (rd->stat_view) , GTK_TREE_MODEL (liststore));
118 rd->stat = GTK_TREE_MODEL (liststore);
123 on_save_clicked (struct regression_dialog *rd)
126 if (rd->current_opts.pred)
128 gtk_toggle_button_set_active (rd->pred_button, TRUE);
130 if (rd->current_opts.resid)
132 gtk_toggle_button_set_active (rd->resid_button, TRUE);
135 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
137 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
139 rd->current_opts.pred = (gtk_toggle_button_get_active (rd->pred_button) == TRUE)
141 rd->current_opts.resid = (gtk_toggle_button_get_active (rd->resid_button) == TRUE)
147 generate_syntax (const struct regression_dialog *rd)
156 GString *string = g_string_new ("REGRESSION");
158 g_string_append (string, "\n\t/VARIABLES=");
159 append_variable_names (string, rd->dict, GTK_TREE_VIEW (rd->indep_vars), 0);
160 g_string_append (string, "\n\t/DEPENDENT=\t");
161 append_variable_names (string, rd->dict, GTK_TREE_VIEW (rd->dep_vars), 0);
164 for (i = 0, ok = gtk_tree_model_get_iter_first (rd->stat, &iter); ok;
165 i++, ok = gtk_tree_model_iter_next (rd->stat, &iter))
168 gtk_tree_model_get (rd->stat, &iter,
169 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
173 selected &= ~(1u << i);
178 g_string_append (string, "\n\t/STATISTICS=");
180 for (i = 0; i < N_REGRESSION_STATS; i++)
181 if (selected & (1u << i))
184 g_string_append (string, " ");
185 g_string_append (string, stats[i].name);
188 if (rd->current_opts.pred || rd->current_opts.resid)
190 g_string_append (string, "\n\t/SAVE=");
191 if (rd->current_opts.pred)
192 g_string_append (string, " PRED");
193 if (rd->current_opts.resid)
194 g_string_append (string, " RESID");
196 g_string_append (string, ".\n");
200 g_string_free (string, FALSE);
205 /* Dialog is valid iff at least one dependent and one independent variable have
208 dialog_state_valid (gpointer data)
210 struct regression_dialog *rd = data;
212 GtkTreeModel *dep_vars = gtk_tree_view_get_model (rd->dep_vars);
213 GtkTreeModel *indep_vars = gtk_tree_view_get_model (rd->indep_vars);
217 return (gtk_tree_model_get_iter_first (dep_vars, ¬used)
218 && gtk_tree_model_get_iter_first (indep_vars, ¬used));
221 /* Pops up the Regression dialog box */
223 regression_dialog (GObject *o, gpointer data)
226 struct regression_dialog rd;
228 GtkBuilder *xml = builder_new ("regression.ui");
229 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
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 *dep_selector = get_widget_assert (xml, "dep-selector");
237 GtkWidget *indep_selector = get_widget_assert (xml, "indep-selector");
238 GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
239 GtkWidget *save_button = get_widget_assert (xml, "save-button");
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 attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
257 GTK_SELECTION_MULTIPLE, NULL);
259 set_dest_model (GTK_TREE_VIEW (dest_dep), vs->dict);
260 set_dest_model (GTK_TREE_VIEW (dest_indep), vs->dict);
262 psppire_selector_set_subjects (PSPPIRE_SELECTOR (dep_selector),
265 insert_source_row_into_tree_view,
269 psppire_selector_set_subjects (PSPPIRE_SELECTOR (indep_selector),
272 insert_source_row_into_tree_view,
276 rd.dep_vars = GTK_TREE_VIEW (dest_dep);
277 rd.indep_vars = GTK_TREE_VIEW (dest_indep);
279 rd.save_dialog = get_widget_assert (xml, "save-dialog");
280 rd.pred_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pred-button"));
281 rd.resid_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "resid-button"));
282 rd.stat_dialog = get_widget_assert (xml, "statistics-dialog");
284 rd.stat = gtk_tree_view_get_model (GTK_TREE_VIEW (rd.stat_view));
285 rd.current_opts.pred = FALSE;
286 rd.current_opts.resid = FALSE;
288 gtk_window_set_transient_for (GTK_WINDOW (rd.save_dialog), GTK_WINDOW (de));
289 gtk_window_set_transient_for (GTK_WINDOW (rd.stat_dialog), GTK_WINDOW (de));
291 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &rd);
293 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
294 dialog_state_valid, &rd);
296 g_signal_connect_swapped (stat_button, "clicked",
297 G_CALLBACK (on_statistics_clicked), &rd);
298 g_signal_connect_swapped (save_button, "clicked",
299 G_CALLBACK (on_save_clicked), &rd);
301 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
306 case GTK_RESPONSE_OK:
308 gchar *syntax = generate_syntax (&rd);
310 struct getl_interface *sss = create_syntax_string_source (syntax);
311 execute_syntax (sss);
316 case PSPPIRE_RESPONSE_PASTE:
318 gchar *syntax = generate_syntax (&rd);
319 paste_syntax_in_new_window (syntax);
328 g_object_unref (xml);