psppire-dialog-action-regression: Fix unused variable warning.
[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   RG (TOL,   N_("Tol"),            N_("Show the variance inflation factor and its reciprocal"))
46
47 enum
48   {
49 #define RG(NAME, LABEL, TOOLTIP) RG_##NAME,
50     REGRESSION_STATS
51 #undef RG
52     N_REGRESSION_STATS
53   };
54
55 enum
56   {
57 #define RG(NAME, LABEL, TOOLTIP) B_RG_##NAME = 1u << RG_##NAME,
58     REGRESSION_STATS
59 #undef RG
60     B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
61     B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
62   };
63
64 static const struct checkbox_entry_item stats[] =
65   {
66 #define RG(NAME, LABEL, TOOLTIP) {#NAME, LABEL, TOOLTIP},
67     REGRESSION_STATS
68 #undef RG
69   };
70
71 static void
72 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class);
73
74 G_DEFINE_TYPE (PsppireDialogActionRegression, psppire_dialog_action_regression, PSPPIRE_TYPE_DIALOG_ACTION);
75
76 static gboolean
77 dialog_state_valid (gpointer data)
78 {
79   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (data);
80   GtkTreeModel *dep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
81   GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
82
83   GtkTreeIter notused;
84
85   return (gtk_tree_model_get_iter_first (dep_vars, &notused)
86     && gtk_tree_model_get_iter_first (indep_vars, &notused));
87 }
88
89 static void
90 refresh (PsppireDialogAction *rd_)
91 {
92   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (rd_);
93
94   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
95   gtk_list_store_clear (GTK_LIST_STORE (liststore));
96
97   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
98   gtk_list_store_clear (GTK_LIST_STORE (liststore));
99 }
100
101 static void
102 on_statistics_clicked (PsppireDialogActionRegression *rd)
103 {
104   int ret;
105   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
106
107   /* Take a backup copy of the existing model */
108   GtkListStore *backup_model = clone_list_store (GTK_LIST_STORE (model));
109
110   ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
111
112   if (ret != PSPPIRE_RESPONSE_CONTINUE)
113     {
114       /* If the user chose to abandon his changes, then replace the model, from the backup */
115       gtk_tree_view_set_model (GTK_TREE_VIEW (rd->stat_view) , GTK_TREE_MODEL (backup_model));
116     }
117   g_object_unref (backup_model);
118 }
119
120 static void
121 on_save_clicked (PsppireDialogActionRegression *rd)
122 {
123   int ret;
124   if (rd->pred)
125     {
126       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->pred_button), TRUE);
127     }
128   if (rd->resid)
129     {
130       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->resid_button), TRUE);
131     }
132
133   ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
134
135   if (ret == PSPPIRE_RESPONSE_CONTINUE)
136     {
137       rd->pred = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->pred_button)) == TRUE)
138         ? TRUE : FALSE;
139       rd->resid = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->resid_button)) == TRUE)
140         ? TRUE : FALSE;
141     }
142 }
143
144
145 static GtkBuilder *
146 psppire_dialog_action_regression_activate (PsppireDialogAction *a, GVariant *param)
147 {
148   PsppireDialogActionRegression *act = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
149   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
150
151   GtkBuilder *xml = builder_new ("regression.ui");
152
153   GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
154   GtkWidget *save_button = get_widget_assert (xml, "save-button");
155
156   pda->dialog = get_widget_assert   (xml, "regression-dialog");
157   pda->source = get_widget_assert   (xml, "dict-view");
158
159   act->dep_vars  = get_widget_assert   (xml, "dep-view");
160   act->indep_vars  = get_widget_assert   (xml, "indep-view");
161   act->stat_view = get_widget_assert (xml, "stat-view");
162   act->stat_dialog = get_widget_assert (xml, "statistics-dialog");
163   act->save_dialog = get_widget_assert (xml, "save-dialog");
164   act->pred_button = get_widget_assert (xml, "pred-button");
165   act->resid_button = get_widget_assert (xml, "resid-button");
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   return xml;
184 }
185
186
187
188 static char *
189 generate_syntax (const PsppireDialogAction *a)
190 {
191   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
192   gchar *text = NULL;
193
194   guint selected;
195   GtkTreeIter iter;
196
197   GString *string = g_string_new ("REGRESSION");
198
199   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
200
201   g_string_append (string, "\n\t/VARIABLES=");
202   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
203   g_string_append (string, "\n\t/DEPENDENT=");
204   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
205   g_string_append (string, "\n\t/METHOD=ENTER");
206
207   selected = 0;
208   for (gint i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok;
209        i++, ok = gtk_tree_model_iter_next (model, &iter))
210     {
211       gboolean toggled;
212       gtk_tree_model_get (model, &iter,
213                           CHECKBOX_COLUMN_SELECTED, &toggled, -1);
214       if (toggled)
215         selected |= 1u << i;
216       else
217         selected &= ~(1u << i);
218     }
219
220   if (selected)
221     {
222       g_string_append (string, "\n\t/STATISTICS=");
223       int n = 0;
224       for (gint i = 0; i < N_REGRESSION_STATS; i++)
225         if (selected & (1u << i))
226           {
227             if (n++)
228               g_string_append (string, " ");
229             g_string_append (string, stats[i].name);
230           }
231     }
232
233   if (rd->pred || rd->resid)
234     {
235       g_string_append (string, "\n\t/SAVE=");
236       if (rd->pred)
237         g_string_append (string, " PRED");
238       if (rd->resid)
239         g_string_append (string, " RESID");
240     }
241
242   g_string_append (string, ".\n");
243
244   text = string->str;
245
246   g_string_free (string, FALSE);
247
248   return text;
249 }
250
251 static void
252 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class)
253 {
254   PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_regression_activate;
255
256   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
257 }
258
259
260 static void
261 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)
262 {
263 }
264