Use a more reliable way of setting the initial state of PsppireDialogAction objects.
[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"),          N_("Show the regression coefficients"))      \
41   RG (CI,    N_("Conf. Interval"), N_("Show the confidence interval for the regression coefficients"))   \
42   RG (R,     N_("R"),              N_("Show the correlation between observed and predicted values")) \
43   RG (ANOVA, N_("Anova"),          N_("Show the analysis of variance table"))  \
44   RG (BCOV,  N_("Bcov"),           N_("Show the variance coefficient matrix"))
45
46 enum
47   {
48 #define RG(NAME, LABEL, TOOLTIP) RG_##NAME,
49     REGRESSION_STATS
50 #undef RG
51     N_REGRESSION_STATS
52   };
53
54 enum
55   {
56 #define RG(NAME, LABEL, TOOLTIP) 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, TOOLTIP) {#NAME, LABEL, TOOLTIP},
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 GtkBuilder *
145 psppire_dialog_action_regression_activate (PsppireDialogAction *a, GVariant *param)
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   
152   GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
153   GtkWidget *save_button = get_widget_assert (xml, "save-button");
154
155   pda->dialog = get_widget_assert   (xml, "regression-dialog");
156   pda->source = get_widget_assert   (xml, "dict-view");
157
158   act->dep_vars  = get_widget_assert   (xml, "dep-view");
159   act->indep_vars  = get_widget_assert   (xml, "indep-view");
160   act->stat_view = get_widget_assert (xml, "stat-view");
161   act->stat_dialog = get_widget_assert (xml, "statistics-dialog");
162   act->save_dialog = get_widget_assert (xml, "save-dialog");
163   act->pred_button = get_widget_assert (xml, "pred-button");
164   act->resid_button = get_widget_assert (xml, "resid-button");
165
166   psppire_checkbox_treeview_populate (PSPPIRE_CHECKBOX_TREEVIEW (act->stat_view),
167                                   B_RG_STATS_DEFAULT,
168                                   N_REGRESSION_STATS,
169                                   stats);
170
171   psppire_dialog_action_set_refresh (pda, refresh);
172
173   psppire_dialog_action_set_valid_predicate (pda,
174                                         dialog_state_valid);
175
176   g_signal_connect_swapped (stat_button, "clicked",
177                             G_CALLBACK (on_statistics_clicked),  act);
178
179   g_signal_connect_swapped (save_button, "clicked",
180                             G_CALLBACK (on_save_clicked),  act);
181
182   return xml;
183 }
184
185
186
187 static char *
188 generate_syntax (const PsppireDialogAction *a)
189 {
190   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
191   gchar *text = NULL;
192
193   gint i;
194   int n;
195   guint selected;
196   GtkTreeIter iter;
197   gboolean ok;
198
199   GString *string = g_string_new ("REGRESSION");
200
201   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
202
203   g_string_append (string, "\n\t/VARIABLES=");
204   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
205   g_string_append (string, "\n\t/DEPENDENT=");
206   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
207   g_string_append (string, "\n\t/METHOD=ENTER");
208
209   selected = 0;
210   for (i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok;
211        i++, ok = gtk_tree_model_iter_next (model, &iter))
212     {
213       gboolean toggled;
214       gtk_tree_model_get (model, &iter,
215                           CHECKBOX_COLUMN_SELECTED, &toggled, -1);
216       if (toggled)
217         selected |= 1u << i;
218       else
219         selected &= ~(1u << i);
220     }
221
222   if (selected)
223     {
224       g_string_append (string, "\n\t/STATISTICS=");
225       n = 0;
226       for (i = 0; i < N_REGRESSION_STATS; i++)
227         if (selected & (1u << i))
228           {
229             if (n++)
230               g_string_append (string, " ");
231             g_string_append (string, stats[i].name);
232           }
233     }
234
235   if (rd->pred || rd->resid)
236     {
237       g_string_append (string, "\n\t/SAVE=");
238       if (rd->pred)
239         g_string_append (string, " PRED");
240       if (rd->resid)
241         g_string_append (string, " RESID");
242     }
243
244   g_string_append (string, ".\n");
245
246   text = string->str;
247
248   g_string_free (string, FALSE);
249
250   return text;
251 }
252
253 static void
254 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class)
255 {
256   PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_regression_activate;
257
258   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
259 }
260
261
262 static void
263 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)
264 {
265 }
266