06cdf82e5a0851cc9b56faaee9bb3b8e39b05343
[pspp] / src / ui / gui / regression-dialog.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 #include <config.h>
18
19 #include "checkbox-treeview.h"
20 #include "regression-dialog.h"
21 #include "executor.h"
22
23 #include <gtk/gtk.h>
24 #include <stdlib.h>
25
26 #include <ui/gui/psppire-data-window.h>
27 #include <ui/gui/dialog-common.h>
28 #include <ui/gui/dict-display.h>
29 #include <ui/gui/builder-wrapper.h>
30 #include <ui/gui/helper.h>
31 #include <ui/gui/psppire-dialog.h>
32 #include <ui/gui/psppire-var-store.h>
33 #include <ui/gui/psppire-var-view.h>
34
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
39
40
41 #define REGRESSION_STATS                          \
42   RG (COEFF, N_("Coeff"))                         \
43   RG (R, N_("R"))                                 \
44   RG (ANOVA, N_("Anova"))                         \
45   RG (BCOV, N_("Bcov"))
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 struct save_options
71 {
72   gboolean pred;
73   gboolean resid;
74 };
75 struct regression_dialog
76 {
77   GtkTreeView *dep_vars;
78   GtkTreeView *indep_vars;
79   PsppireDict *dict;
80
81   GtkToggleButton *resid_button;
82   GtkToggleButton *pred_button;
83
84   GtkWidget *stat_dialog;
85   GtkWidget *save_dialog;
86
87   GtkWidget *stat_view;
88   struct save_options current_opts;
89 };
90
91 static void
92 refresh (PsppireDialog *dialog, struct regression_dialog *rd)
93 {
94   GtkTreeModel *liststore = gtk_tree_view_get_model (rd->dep_vars);
95   gtk_list_store_clear (GTK_LIST_STORE (liststore));
96
97   liststore = gtk_tree_view_get_model (rd->indep_vars);
98   gtk_list_store_clear (GTK_LIST_STORE (liststore));
99 }
100
101 static void
102 on_statistics_clicked (struct regression_dialog *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 (struct regression_dialog *rd)
122 {
123   int ret;
124   if (rd->current_opts.pred)
125     {
126       gtk_toggle_button_set_active (rd->pred_button, TRUE);
127     }
128   if (rd->current_opts.resid)
129     {
130       gtk_toggle_button_set_active (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->current_opts.pred = (gtk_toggle_button_get_active (rd->pred_button) == TRUE)
138         ? TRUE : FALSE;
139       rd->current_opts.resid = (gtk_toggle_button_get_active (rd->resid_button) == TRUE)
140         ? TRUE : FALSE;
141     }
142 }
143
144 static char *
145 generate_syntax (const struct regression_dialog *rd)
146 {
147   gint i;
148   int n;
149   guint selected;
150   GtkTreeIter iter;
151   gboolean ok;
152
153   gchar *text;
154   GString *string = g_string_new ("REGRESSION");
155
156   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->stat_view));
157
158   g_string_append (string, "\n\t/VARIABLES=");
159   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
160   g_string_append (string, "\n\t/DEPENDENT=\t");
161   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->dep_vars), 0, string);
162
163   selected = 0;
164   for (i = 0, ok = gtk_tree_model_get_iter_first (model, &iter); ok; 
165        i++, ok = gtk_tree_model_iter_next (model, &iter))
166     {
167       gboolean toggled;
168       gtk_tree_model_get (model, &iter,
169                           CHECKBOX_COLUMN_SELECTED, &toggled, -1); 
170       if (toggled) 
171         selected |= 1u << i; 
172       else 
173         selected &= ~(1u << i);
174     }
175
176   if (selected)
177     {
178       g_string_append (string, "\n\t/STATISTICS=");
179       n = 0;
180       for (i = 0; i < N_REGRESSION_STATS; i++)
181         if (selected & (1u << i))
182           {
183             if (n++)
184               g_string_append (string, " ");
185             g_string_append (string, stats[i].name);
186           }
187     }
188   if (rd->current_opts.pred || rd->current_opts.resid)
189     {
190       g_string_append (string, "\n\t/SAVE=");
191       if (rd->current_opts.pred)
192         g_string_append (string, " PRED");
193       if (rd->current_opts.resid)
194         g_string_append (string, " RESID");
195     }
196   g_string_append (string, ".\n");
197
198   text = string->str;
199
200   g_string_free (string, FALSE);
201
202   return text;
203 }
204
205 /* Dialog is valid iff at least one dependent and one independent variable have
206    been selected. */
207 static gboolean
208 dialog_state_valid (gpointer data)
209 {
210   struct regression_dialog *rd = data;
211
212   GtkTreeModel *dep_vars = gtk_tree_view_get_model (rd->dep_vars);
213   GtkTreeModel *indep_vars = gtk_tree_view_get_model (rd->indep_vars);
214
215   GtkTreeIter notused;
216
217   return (gtk_tree_model_get_iter_first (dep_vars, &notused)
218     && gtk_tree_model_get_iter_first (indep_vars, &notused));
219 }
220
221 /* Pops up the Regression dialog box */
222 void
223 regression_dialog (PsppireDataWindow *de)
224 {
225   gint response;
226   struct regression_dialog rd;
227
228   GtkBuilder *xml = builder_new ("regression.ui");
229   PsppireVarStore *vs;
230
231   GtkWidget *dialog = get_widget_assert   (xml, "regression-dialog");
232   GtkWidget *source = get_widget_assert   (xml, "dict-view");
233   GtkWidget *dest_dep =   get_widget_assert   (xml, "dep-view");
234   GtkWidget *dest_indep =   get_widget_assert   (xml, "indep-view");
235   GtkWidget *stat_button = get_widget_assert (xml, "stat-button");
236   GtkWidget *save_button = get_widget_assert (xml, "save-button");
237
238   GtkWidget *dep_selector = get_widget_assert (xml, "dep-selector");
239
240   rd.stat_view = get_widget_assert (xml, "stat-view");
241
242   g_object_get (de->data_editor, "var-store", &vs, NULL);
243
244
245   put_checkbox_items_in_treeview (GTK_TREE_VIEW(rd.stat_view),
246                                   B_RG_STATS_DEFAULT,
247                                   N_REGRESSION_STATS,
248                                   stats
249                                   );
250
251   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
252
253   g_object_get (vs, "dictionary", &rd.dict, NULL);
254   g_object_set (source, "model", rd.dict, NULL);
255
256   rd.dep_vars = GTK_TREE_VIEW (dest_dep);
257   rd.indep_vars = GTK_TREE_VIEW (dest_indep);
258
259   psppire_selector_set_allow (PSPPIRE_SELECTOR (dep_selector), numeric_only);
260
261   rd.save_dialog = get_widget_assert (xml, "save-dialog");
262   rd.pred_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "pred-button"));
263   rd.resid_button = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "resid-button"));
264   rd.stat_dialog = get_widget_assert (xml, "statistics-dialog");
265
266   rd.current_opts.pred = FALSE;
267   rd.current_opts.resid = FALSE;
268
269   gtk_window_set_transient_for (GTK_WINDOW (rd.save_dialog), GTK_WINDOW (de));
270   gtk_window_set_transient_for (GTK_WINDOW (rd.stat_dialog), GTK_WINDOW (de));
271
272   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &rd);
273
274   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
275                                       dialog_state_valid, &rd);
276
277   g_signal_connect_swapped (stat_button, "clicked",
278                             G_CALLBACK (on_statistics_clicked),  &rd);
279   g_signal_connect_swapped (save_button, "clicked",
280                             G_CALLBACK (on_save_clicked),  &rd);
281
282   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
283
284
285   switch (response)
286     {
287     case GTK_RESPONSE_OK:
288       g_free (execute_syntax_string (de, generate_syntax (&rd)));
289       break;
290     case PSPPIRE_RESPONSE_PASTE:
291       g_free (paste_syntax_to_window (generate_syntax (&rd)));
292       break;
293     default:
294       break;
295     }
296
297   g_object_unref (xml);
298 }