acc0ce0245e793f0be9fba315f900b1ccbb2b7dc
[pspp] / src / ui / gui / psppire-dialog-action-regression.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2010, 2011, 2012  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include "psppire-dialog-action-regression.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include "helper.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
27
28 #include "psppire-dialog.h"
29 #include "builder-wrapper.h"
30 #include "psppire-checkbox-treeview.h"
31 #include "psppire-dict.h"
32 #include "libpspp/str.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 #define REGRESSION_STATS                          \
40   RG (COEFF, N_("Coeff"))                         \
41   RG (CI, N_("Conf. Interval"))                 \
42   RG (R, N_("R"))                                 \
43   RG (ANOVA, N_("Anova"))                         \
44   RG (BCOV, N_("Bcov"))
45
46 enum
47   {
48 #define RG(NAME, LABEL) RG_##NAME,
49     REGRESSION_STATS
50 #undef RG
51     N_REGRESSION_STATS
52   };
53
54 enum
55   {
56 #define RG(NAME, LABEL) B_RG_##NAME = 1u << RG_##NAME,
57     REGRESSION_STATS
58 #undef RG
59     B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
60     B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
61   };
62
63 static const struct checkbox_entry_item stats[] =
64   {
65 #define RG(NAME, LABEL) {#NAME, LABEL},
66     REGRESSION_STATS
67 #undef RG
68   };
69
70 static void
71 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class);
72
73 G_DEFINE_TYPE (PsppireDialogActionRegression, psppire_dialog_action_regression, PSPPIRE_TYPE_DIALOG_ACTION);
74
75 static gboolean
76 dialog_state_valid (gpointer data)
77 {
78   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (data);
79   GtkTreeModel *dep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
80   GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
81
82   GtkTreeIter notused;
83
84   return (gtk_tree_model_get_iter_first (dep_vars, &notused)
85     && gtk_tree_model_get_iter_first (indep_vars, &notused));
86 }
87
88 static void
89 refresh (PsppireDialogAction *rd_)
90 {
91   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (rd_);
92
93   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
94   gtk_list_store_clear (GTK_LIST_STORE (liststore));
95
96   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
97   gtk_list_store_clear (GTK_LIST_STORE (liststore));
98 }
99
100 static void
101 on_statistics_clicked (PsppireDialogActionRegression *rd)
102 {
103   int ret;
104   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
105
106   /* Take a backup copy of the existing model */
107   GtkListStore *backup_model = clone_list_store (GTK_LIST_STORE (model));
108
109   ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
110
111   if ( ret != PSPPIRE_RESPONSE_CONTINUE )
112     {
113       /* If the user chose to abandon his changes, then replace the model, from the backup */
114       gtk_tree_view_set_model (GTK_TREE_VIEW (rd->stat_view) , GTK_TREE_MODEL (backup_model));
115     }
116   g_object_unref (backup_model);
117 }
118
119 static void
120 on_save_clicked (PsppireDialogActionRegression *rd)
121 {
122   int ret;
123   if (rd->pred)
124     {
125       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->pred_button), TRUE);
126     }
127   if (rd->resid)
128     {
129       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->resid_button), TRUE);
130     }
131
132   ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
133
134   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
135     {
136       rd->pred = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->pred_button)) == TRUE)
137         ? TRUE : FALSE;
138       rd->resid = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->resid_button)) == TRUE)
139         ? TRUE : FALSE;
140     }
141 }
142
143
144 static void
145 psppire_dialog_action_regression_activate (GtkAction *a)
146 {
147   PsppireDialogActionRegression *act = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
148   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
149
150   GtkBuilder *xml = builder_new ("regression.ui");
151   GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
152   GtkWidget *save_button = get_widget_assert (xml, "save-button");
153
154   pda->dialog = get_widget_assert   (xml, "regression-dialog");
155   pda->source = get_widget_assert   (xml, "dict-view");
156
157   act->dep_vars  = get_widget_assert   (xml, "dep-view");
158   act->indep_vars  = get_widget_assert   (xml, "indep-view");
159   act->stat_view = get_widget_assert (xml, "stat-view");
160   act->stat_dialog = get_widget_assert (xml, "statistics-dialog");
161   act->save_dialog = get_widget_assert (xml, "save-dialog");
162   act->pred_button = get_widget_assert (xml, "pred-button");
163   act->resid_button = get_widget_assert (xml, "resid-button");
164
165   g_object_unref (xml);
166
167   psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
168                                   B_RG_STATS_DEFAULT,
169                                   N_REGRESSION_STATS,
170                                   stats);
171
172   psppire_dialog_action_set_refresh (pda, refresh);
173
174   psppire_dialog_action_set_valid_predicate (pda,
175                                         dialog_state_valid);
176
177   g_signal_connect_swapped (stat_button, "clicked",
178                             G_CALLBACK (on_statistics_clicked),  act);
179
180   g_signal_connect_swapped (save_button, "clicked",
181                             G_CALLBACK (on_save_clicked),  act);
182
183
184   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_regression_parent_class)->activate)
185     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_regression_parent_class)->activate (pda);
186 }
187
188
189
190 static char *
191 generate_syntax (PsppireDialogAction *a)
192 {
193   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
194   gchar *text = NULL;
195
196   gint i;
197   int n;
198   guint selected;
199   GtkTreeIter iter;
200   gboolean ok;
201
202   GString *string = g_string_new ("REGRESSION");
203
204   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
205
206   g_string_append (string, "\n\t/VARIABLES=");
207   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
208   g_string_append (string, "\n\t/DEPENDENT=\t");
209   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
210
211   selected = 0;
212   for (i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok; 
213        i++, ok = gtk_tree_model_iter_next (model, &iter))
214     {
215       gboolean toggled;
216       gtk_tree_model_get (model, &iter,
217                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
218       if (toggled) 
219         selected |= 1u << i; 
220       else 
221         selected &= ~(1u << i);
222     }
223
224   if (selected)
225     {
226       g_string_append (string, "\n\t/STATISTICS=");
227       n = 0;
228       for (i = 0; i < N_REGRESSION_STATS; i++)
229         if (selected & (1u << i))
230           {
231             if (n++)
232               g_string_append (string, " ");
233             g_string_append (string, stats[i].name);
234           }
235     }
236
237   if (rd->pred || rd->resid)
238     {
239       g_string_append (string, "\n\t/SAVE=");
240       if (rd->pred)
241         g_string_append (string, " PRED");
242       if (rd->resid)
243         g_string_append (string, " RESID");
244     }
245
246   g_string_append (string, ".\n");
247
248   text = string->str;
249
250   g_string_free (string, FALSE);
251
252   return text;
253 }
254
255 static void
256 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class)
257 {
258   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
259
260   action_class->activate = psppire_dialog_action_regression_activate;
261
262   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
263 }
264
265
266 static void
267 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)
268 {
269 }
270