Regression Dialog: Convert to PsppireDialogAction object
[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 "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 (R, N_("R"))                                 \
42   RG (ANOVA, N_("Anova"))                         \
43   RG (BCOV, N_("Bcov"))
44 enum
45   {
46 #define RG(NAME, LABEL) RG_##NAME,
47     REGRESSION_STATS
48 #undef RG
49     N_REGRESSION_STATS
50   };
51
52 enum
53   {
54 #define RG(NAME, LABEL) B_RG_##NAME = 1u << RG_##NAME,
55     REGRESSION_STATS
56 #undef RG
57     B_RG_STATS_ALL = (1u << N_REGRESSION_STATS) - 1,
58     B_RG_STATS_DEFAULT = B_RG_ANOVA | B_RG_COEFF | B_RG_R
59   };
60
61 static const struct checkbox_entry_item stats[] =
62   {
63 #define RG(NAME, LABEL) {#NAME, LABEL},
64     REGRESSION_STATS
65 #undef RG
66   };
67
68 static void
69 psppire_dialog_action_regression_class_init (PsppireDialogActionRegressionClass *class);
70
71 G_DEFINE_TYPE (PsppireDialogActionRegression, psppire_dialog_action_regression, PSPPIRE_TYPE_DIALOG_ACTION);
72
73 static gboolean
74 dialog_state_valid (gpointer data)
75 {
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));
79
80   GtkTreeIter notused;
81
82   return (gtk_tree_model_get_iter_first (dep_vars, &notused)
83     && gtk_tree_model_get_iter_first (indep_vars, &notused));
84 }
85
86 static void
87 refresh (PsppireDialogAction *rd_)
88 {
89   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (rd_);
90
91   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->dep_vars));
92   gtk_list_store_clear (GTK_LIST_STORE (liststore));
93
94   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
95   gtk_list_store_clear (GTK_LIST_STORE (liststore));
96 }
97
98 static void
99 on_statistics_clicked (PsppireDialogActionRegression *rd)
100 {
101   int ret;
102   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
103
104   /* Take a backup copy of the existing model */
105   GtkListStore *backup_model = clone_list_store (GTK_LIST_STORE (model));
106
107   ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->stat_dialog));
108
109   if ( ret != PSPPIRE_RESPONSE_CONTINUE )
110     {
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));
113     }
114   g_object_unref (backup_model);
115 }
116
117 static void
118 on_save_clicked (PsppireDialogActionRegression *rd)
119 {
120   int ret;
121   if (rd->pred)
122     {
123       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->pred_button), TRUE);
124     }
125   if (rd->resid)
126     {
127       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->resid_button), TRUE);
128     }
129
130   ret = psppire_dialog_run (PSPPIRE_DIALOG (rd->save_dialog));
131
132   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
133     {
134       rd->pred = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->pred_button)) == TRUE)
135         ? TRUE : FALSE;
136       rd->resid = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->resid_button)) == TRUE)
137         ? TRUE : FALSE;
138     }
139 }
140
141
142 static void
143 psppire_dialog_action_regression_activate (GtkAction *a)
144 {
145   PsppireDialogActionRegression *act = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
146   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
147
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");
151
152   pda->dialog = get_widget_assert   (xml, "regression-dialog");
153   pda->source = get_widget_assert   (xml, "dict-view");
154
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");
162
163   g_object_unref (xml);
164
165   put_checkbox_items_in_treeview (GTK_TREE_VIEW (act->stat_view),
166                                   B_RG_STATS_DEFAULT,
167                                   N_REGRESSION_STATS,
168                                   stats);
169
170   psppire_dialog_action_set_refresh (pda, refresh);
171
172   psppire_dialog_action_set_valid_predicate (pda,
173                                         dialog_state_valid);
174
175   g_signal_connect_swapped (stat_button, "clicked",
176                             G_CALLBACK (on_statistics_clicked),  act);
177
178   g_signal_connect_swapped (save_button, "clicked",
179                             G_CALLBACK (on_save_clicked),  act);
180
181
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);
184 }
185
186
187
188 static char *
189 generate_syntax (PsppireDialogAction *a)
190 {
191   PsppireDialogActionRegression *rd = PSPPIRE_DIALOG_ACTION_REGRESSION (a);
192   gchar *text = NULL;
193
194   gint i;
195   int n;
196   guint selected;
197   GtkTreeIter iter;
198   gboolean ok;
199
200   GString *string = g_string_new ("REGRESSION");
201
202   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
203
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);
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   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
257
258   action_class->activate = psppire_dialog_action_regression_activate;
259
260   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
261 }
262
263
264 static void
265 psppire_dialog_action_regression_init (PsppireDialogActionRegression *act)
266 {
267 }
268