Added a menu entry and dialog box for the FACTOR command.
[pspp-builds.git] / src / ui / gui / factor-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009  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 "dialog-common.h"
20 #include <language/syntax-string-source.h>
21 #include <ui/syntax-gen.h>
22 #include <libpspp/str.h>
23
24 #include "factor-dialog.h"
25 #include "psppire-selector.h"
26 #include "psppire-dictview.h"
27 #include "psppire-dialog.h"
28
29 #include "psppire-data-window.h"
30 #include "psppire-var-view.h"
31
32 #include "widget-io.h"
33
34 #include "executor.h"
35 #include "helper.h"
36
37 #include <gtk/gtk.h>
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
42
43
44 struct extraction_parameters
45 {
46   gdouble mineigen;
47   gint n_factors;
48   gint iterations;
49
50   gboolean explicit_nfactors;  
51   gboolean covariance;
52
53   gboolean scree;
54   gboolean unrotated;
55
56   gboolean paf;
57 };
58
59
60 static const struct extraction_parameters default_extraction_parameters = {1.0, 0, 25, FALSE, TRUE, FALSE, TRUE, FALSE};
61
62 struct factor
63 {
64   GtkBuilder *xml;
65   PsppireDict *dict;
66
67   GtkWidget *variables;
68   PsppireDataWindow *de ;
69
70   /* The Extraction subdialog */
71   GtkWidget *extraction_dialog;
72
73   GtkWidget *n_factors;
74   GtkWidget *mineigen;
75   GtkWidget *iterations;
76
77   GtkWidget *nfactors_toggle;
78   GtkWidget *mineigen_toggle;
79
80   GtkWidget *covariance_toggle;
81   GtkWidget *correlation_toggle;
82
83   GtkWidget *scree_button;
84   GtkWidget *unrotated_button;
85
86   GtkWidget *extraction_combo;
87
88   struct extraction_parameters extraction;
89 };
90
91 static void
92 load_extraction_parameters (struct factor *fd, const struct extraction_parameters *p)
93 {
94   gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->mineigen), p->mineigen);
95   gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->n_factors), p->n_factors);
96   gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->iterations), p->iterations);
97
98   if (p->explicit_nfactors)
99     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->nfactors_toggle), TRUE);
100   else
101     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->mineigen_toggle), TRUE);
102
103   if (p->covariance)
104     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->covariance_toggle), TRUE);
105   else
106     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->correlation_toggle), TRUE);
107
108
109   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->scree_button), p->scree);
110   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->unrotated_button), p->unrotated);
111
112   if ( p->paf )
113     gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 1);
114   else
115     gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 0);
116     
117 }
118
119 static void
120 set_extraction_parameters (struct extraction_parameters *p, const struct factor *fd)
121 {
122   p->mineigen = gtk_spin_button_get_value (GTK_SPIN_BUTTON (fd->mineigen));
123   p->n_factors = gtk_spin_button_get_value (GTK_SPIN_BUTTON (fd->n_factors));
124   p->iterations = gtk_spin_button_get_value (GTK_SPIN_BUTTON (fd->iterations));
125
126   p->explicit_nfactors = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->nfactors_toggle));
127   p->covariance = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->covariance_toggle));
128
129   p->scree = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->scree_button));
130   p->unrotated = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->unrotated_button));
131
132   p->paf = (gtk_combo_box_get_active (GTK_COMBO_BOX (fd->extraction_combo)) == 1);
133 }
134
135 static void run_extractions_subdialog (struct factor *fd)
136 {
137   struct extraction_parameters *ex = &fd->extraction;
138
139   gint response = psppire_dialog_run (PSPPIRE_DIALOG (fd->extraction_dialog));
140
141   if ( response == PSPPIRE_RESPONSE_CONTINUE )
142     {
143       /* Set the parameters from their respective widgets */
144       set_extraction_parameters (ex, fd);
145     }
146   else
147     {
148       /* Cancelled.  Reset the widgets to their old state */
149       load_extraction_parameters (fd, ex);
150     }
151 }
152
153
154 static char * generate_syntax (const struct factor *rd);
155
156
157 static void
158 refresh (struct factor *fd)
159 {
160   GtkTreeModel *liststore =
161     gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
162   gtk_list_store_clear (GTK_LIST_STORE (liststore));
163
164   load_extraction_parameters (fd, &default_extraction_parameters);
165 }
166
167
168 static gboolean
169 dialog_state_valid (gpointer data)
170 {
171   struct factor *fd = data;
172
173   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
174
175   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
176     return FALSE;
177
178   return TRUE;
179 }
180
181 static void
182 on_show (struct factor *fd, GtkWidget *dialog)
183 {
184   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
185
186   gint n_vars = gtk_tree_model_iter_n_children (liststore, NULL);
187
188   gtk_spin_button_set_range (GTK_SPIN_BUTTON (fd->n_factors), 1, n_vars - 1);
189 }
190
191
192 static void
193 on_extract_toggle (GtkToggleButton *button, struct factor *f)
194 {
195   gboolean active = gtk_toggle_button_get_active (button);
196
197   gtk_widget_set_sensitive (GTK_WIDGET (f->n_factors), active);
198   gtk_widget_set_sensitive (GTK_WIDGET (f->mineigen), ! active);
199 }
200
201 /* Pops up the Factor dialog box */
202 void
203 factor_dialog (GObject *o, gpointer data)
204 {
205   struct factor fd;
206   gint response;
207
208   PsppireVarStore *vs;
209
210   GtkWidget *dialog ;
211   GtkWidget *source ;
212   GtkWidget *extraction_button ;
213
214   fd.xml = builder_new ("factor.ui");
215   
216   dialog = get_widget_assert   (fd.xml, "factor-dialog");
217   source = get_widget_assert   (fd.xml, "dict-view");
218   extraction_button = get_widget_assert (fd.xml, "button-extraction");
219
220   fd.extraction_dialog = get_widget_assert (fd.xml, "extractions-dialog");
221
222   fd.de = PSPPIRE_DATA_WINDOW (data);
223
224   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &fd);
225
226   {
227     GtkWidget *hbox = get_widget_assert (fd.xml, "hbox6");
228     GtkWidget *eigenvalue_extraction = widget_scanf (_("Eigenvalues over %4.2f times the mean eigenvalue"), &fd.mineigen);
229
230     fd.nfactors_toggle = get_widget_assert (fd.xml, "nfactors-radiobutton");
231     fd.mineigen_toggle = get_widget_assert (fd.xml, "mineigen-radiobutton");
232     fd.n_factors = get_widget_assert (fd.xml, "spinbutton-nfactors");
233     fd.iterations = get_widget_assert (fd.xml, "spinbutton-iterations");
234     fd.covariance_toggle = get_widget_assert (fd.xml,  "covariance-radiobutton");
235     fd.correlation_toggle = get_widget_assert (fd.xml, "correlations-radiobutton");
236
237     fd.scree_button = get_widget_assert (fd.xml, "scree-button");
238     fd.unrotated_button = get_widget_assert (fd.xml, "unrotated-button");
239     fd.extraction_combo = get_widget_assert (fd.xml, "combobox1");
240
241     gtk_container_add (GTK_CONTAINER (hbox), eigenvalue_extraction);
242
243     g_signal_connect (fd.nfactors_toggle, "toggled", G_CALLBACK (on_extract_toggle), &fd);
244
245     gtk_widget_show_all (eigenvalue_extraction);
246   }
247
248   g_signal_connect_swapped (extraction_button, "clicked", G_CALLBACK (run_extractions_subdialog), &fd);
249
250   g_signal_connect_swapped (fd.extraction_dialog, "show", G_CALLBACK (on_show), &fd);
251
252   fd.variables = get_widget_assert   (fd.xml, "psppire-var-view1");
253
254   g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
255
256   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
257   gtk_window_set_transient_for (GTK_WINDOW (fd.extraction_dialog), GTK_WINDOW (fd.de));
258
259   g_object_get (vs, "dictionary", &fd.dict, NULL);
260   g_object_set (source, "model", fd.dict, NULL);
261
262
263   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
264                                       dialog_state_valid, &fd);
265
266   psppire_selector_set_allow (PSPPIRE_SELECTOR (get_widget_assert (fd.xml, "dep-selector")),
267                               numeric_only);
268
269   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
270
271   switch (response)
272     {
273     case GTK_RESPONSE_OK:
274       {
275         gchar *syntax = generate_syntax (&fd);
276
277         struct getl_interface *sss = create_syntax_string_source (syntax);
278         execute_syntax (sss);
279
280         g_free (syntax);
281       }
282       break;
283     case PSPPIRE_RESPONSE_PASTE:
284       {
285         gchar *syntax = generate_syntax (&fd);
286         paste_syntax_in_new_window (syntax);
287
288         g_free (syntax);
289       }
290       break;
291     default:
292       break;
293     }
294
295   g_object_unref (fd.xml);
296 }
297
298
299 \f
300
301 static char *
302 generate_syntax (const struct factor *rd)
303 {
304   gchar *text;
305
306   GString *string = g_string_new ("FACTOR ");
307
308   g_string_append (string, "VARIABLES =  ");
309
310   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
311
312   g_string_append (string, "\n\t/EXTRACTION =");
313   if ( rd->extraction.paf)
314     g_string_append (string, "PAF");
315   else
316     g_string_append (string, "PC");
317
318
319   g_string_append (string, "\n\t/METHOD = ");
320   if ( rd->extraction.covariance )
321     g_string_append (string, "COVARIANCE");
322   else
323     g_string_append (string, "CORRELATION");
324
325
326   g_string_append (string, "\n\t/CRITERIA = ");
327   if ( rd->extraction.explicit_nfactors )
328     g_string_append_printf (string, "FACTORS (%d)", rd->extraction.n_factors);
329   else
330     g_string_append_printf (string, "MINEIGEN (%g)", rd->extraction.mineigen);
331     
332
333   if ( rd->extraction.scree )
334     {
335       g_string_append (string, "\n\t/PLOT = ");
336       g_string_append (string, "EIGEN");
337     }
338
339   g_string_append (string, "\n\t/PRINT = ");
340   g_string_append (string, "INITIAL ");
341   if ( rd->extraction.unrotated )  
342     g_string_append (string, "EXTRACTION ");
343
344
345   g_string_append (string, ".\n");
346
347   text = string->str;
348
349   g_string_free (string, FALSE);
350
351   return text;
352 }