1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2010, 2011, 2012 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/>. */
20 #include "psppire-dialog-action-regression.h"
21 #include "psppire-value-entry.h"
23 #include "dialog-common.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
28 #include "psppire-dialog.h"
29 #include "builder-wrapper.h"
30 #include "checkbox-treeview.h"
31 #include "psppire-dict.h"
32 #include "libpspp/str.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},
69 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class);
71 G_DEFINE_TYPE (PsppireDialogActionRegression, psppire_dialog_action_regression, PSPPIRE_TYPE_DIALOG_ACTION);
74 dialog_state_valid (gpointer data)
76 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (data);
77 GtkTreeModel *dep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
78 GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
82 return (gtk_tree_model_get_iter_first (dep_vars, ¬used)
83 && gtk_tree_model_get_iter_first (indep_vars, ¬used));
87 refresh (PsppireDialogAction *rd_)
89 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (rd_);
91 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
92 gtk_list_store_clear (GTK_LIST_STORE (liststore));
94 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
95 gtk_list_store_clear (GTK_LIST_STORE (liststore));
99 on_statistics_clicked (PsppireDialogActionRegression *rd)
102 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
104 /* Take a backup copy of the existing model */
105 GtkListStore *backup_model = clone_list_store (GTK_LIST_STORE (model));
107 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
109 if ( ret != PSPPIRE_RESPONSE_CONTINUE )
111 /* If the user chose to abandon his changes, then replace the model, from the backup */
112 gtk_tree_view_set_model (GTK_TREE_VIEW (rd->stat_view) , GTK_TREE_MODEL (backup_model));
114 g_object_unref (backup_model);
118 on_save_clicked (PsppireDialogActionRegression *rd)
123 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->pred_button), TRUE);
127 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->resid_button), TRUE);
130 ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
132 if ( ret == PSPPIRE_RESPONSE_CONTINUE )
134 rd->pred = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->pred_button)) == TRUE)
136 rd->resid = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->resid_button)) == TRUE)
143 psppire_dialog_action_regression_activate (GtkAction *a)
145 PsppireDialogActionRegression *act = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
146 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
148 GtkBuilder *xml = builder_new ("regression.ui");
149 GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
150 GtkWidget *save_button = get_widget_assert (xml, "save-button");
152 pda->dialog = get_widget_assert (xml, "regression-dialog");
153 pda->source = get_widget_assert (xml, "dict-view");
155 act->dep_vars = get_widget_assert (xml, "dep-view");
156 act->indep_vars = get_widget_assert (xml, "indep-view");
157 act->stat_view = get_widget_assert (xml, "stat-view");
158 act->stat_dialog = get_widget_assert (xml, "statistics-dialog");
159 act->save_dialog = get_widget_assert (xml, "save-dialog");
160 act->pred_button = get_widget_assert (xml, "pred-button");
161 act->resid_button = get_widget_assert (xml, "resid-button");
163 g_object_unref (xml);
165 put_checkbox_items_in_treeview (GTK_TREE_VIEW (act->stat_view),
170 psppire_dialog_action_set_refresh (pda, refresh);
172 psppire_dialog_action_set_valid_predicate (pda,
175 g_signal_connect_swapped (stat_button, "clicked",
176 G_CALLBACK (on_statistics_clicked), act);
178 g_signal_connect_swapped (save_button, "clicked",
179 G_CALLBACK (on_save_clicked), act);
182 if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_regression_parent_class)->activate)
183 PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_regression_parent_class)->activate (pda);
189 generate_syntax (PsppireDialogAction *a)
191 PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
200 GString *string = g_string_new ("REGRESSION");
202 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
204 g_string_append (string, "\n\t/VARIABLES=");
205 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
206 g_string_append (string, "\n\t/DEPENDENT=\t");
207 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
210 for (i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok;
211 i++, ok = gtk_tree_model_iter_next (model, &iter))
214 gtk_tree_model_get (model, &iter,
215 CHECKBOX_COLUMN_SELECTED, &toggled, -1);
219 selected &= ~(1u << i);
224 g_string_append (string, "\n\t/STATISTICS=");
226 for (i = 0; i < N_REGRESSION_STATS; i++)
227 if (selected & (1u << i))
230 g_string_append (string, " ");
231 g_string_append (string, stats[i].name);
235 if (rd->pred || rd->resid)
237 g_string_append (string, "\n\t/SAVE=");
239 g_string_append (string, " PRED");
241 g_string_append (string, " RESID");
244 g_string_append (string, ".\n");
248 g_string_free (string, FALSE);
254 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class)
256 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
258 action_class->activate = psppire_dialog_action_regression_activate;
260 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
265 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)