New file: builder-wrapper.h and builder-wrapper.c
[pspp-builds.git] / src / ui / gui / factor-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009, 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 "dialog-common.h"
20 #include <ui/syntax-gen.h>
21 #include <libpspp/str.h>
22
23 #include "factor-dialog.h"
24 #include "psppire-selector.h"
25 #include "psppire-dictview.h"
26 #include "psppire-dialog.h"
27
28 #include "psppire-data-window.h"
29 #include "psppire-var-view.h"
30
31 #include "psppire-scanf.h"
32
33 #include "executor.h"
34 #include "helper.h"
35 #include "builder-wrapper.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 n_iterations;
49
50   gboolean explicit_nfactors;  
51   gboolean covariance;
52
53   gboolean scree;
54   gboolean unrotated;
55
56   gboolean paf;
57 };
58
59 enum rotation_type {
60   ROT_NONE,
61   ROT_VARIMAX,
62   ROT_QUARTIMAX,
63   ROT_EQUIMAX
64 };
65
66 static const char *rot_method_syntax[] = 
67   {
68     "NOROTATE",
69     "VARIMAX",
70     "QUARTIMAX",
71     "EQUAMAX"
72   };
73
74 struct rotation_parameters
75 {
76   gboolean rotated_solution;
77   gint iterations;
78
79   enum rotation_type method;
80 };
81
82
83
84 static const struct extraction_parameters default_extraction_parameters = {1.0, 0, 25, FALSE, TRUE, FALSE, TRUE, FALSE};
85
86 static const struct rotation_parameters default_rotation_parameters = {TRUE, 25, ROT_VARIMAX};
87
88 struct factor
89 {
90   GtkBuilder *xml;
91   PsppireDict *dict;
92
93   GtkWidget *variables;
94   PsppireDataWindow *de ;
95
96   /* The Extraction subdialog */
97   GtkWidget *extraction_dialog;
98   GtkWidget *rotation_dialog;
99
100   GtkWidget *n_factors;
101   GtkWidget *mineigen;
102   GtkWidget *extract_iterations;
103
104   GtkWidget *nfactors_toggle;
105   GtkWidget *mineigen_toggle;
106
107   GtkWidget *covariance_toggle;
108   GtkWidget *correlation_toggle;
109
110   GtkWidget *scree_button;
111   GtkWidget *unrotated_button;
112
113   GtkWidget *extraction_combo;
114
115
116   /* Rotation Widgets */
117   GtkWidget *rotate_iterations;
118   GtkWidget *display_rotated_solution;
119   GtkWidget *rotation_none;
120   GtkWidget *rotation_varimax;
121   GtkWidget *rotation_quartimax;
122   GtkWidget *rotation_equimax;
123
124
125   struct extraction_parameters extraction;
126   struct rotation_parameters rotation;
127 };
128
129 static void
130 load_rotation_parameters (struct factor *fd, const struct rotation_parameters *p)
131 {
132   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->display_rotated_solution),
133                                 p->rotated_solution);
134   
135   gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->rotate_iterations),
136                              p->iterations);
137
138   switch (p->method)
139     {
140     case ROT_NONE:
141       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_none), TRUE);
142       break;
143     case ROT_VARIMAX:
144       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_varimax), TRUE);
145       break;
146     case ROT_QUARTIMAX:
147       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_quartimax), TRUE);
148       break;
149     case ROT_EQUIMAX:
150       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_equimax), TRUE);
151       break;
152     default:
153       g_assert_not_reached ();
154       break;
155     }
156 }
157
158 static void
159 load_extraction_parameters (struct factor *fd, const struct extraction_parameters *p)
160 {
161   gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->mineigen), p->mineigen);
162   gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->n_factors), p->n_factors);
163
164   gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->extract_iterations), p->n_iterations);
165
166
167   if (p->explicit_nfactors)
168     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->nfactors_toggle), TRUE);
169   else
170     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->mineigen_toggle), TRUE);
171
172   if (p->covariance)
173     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->covariance_toggle), TRUE);
174   else
175     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->correlation_toggle), TRUE);
176
177
178   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->scree_button), p->scree);
179   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->unrotated_button), p->unrotated);
180
181   if ( p->paf )
182     gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 1);
183   else
184     gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 0);
185     
186 }
187
188 static void
189 set_rotation_parameters (const struct factor *fd, struct rotation_parameters *p)
190 {
191   p->iterations = gtk_spin_button_get_value (GTK_SPIN_BUTTON (fd->rotate_iterations));
192   p->rotated_solution = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->display_rotated_solution));
193
194
195   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_none)))
196     p->method = ROT_NONE;
197
198   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_varimax)))
199     p->method = ROT_VARIMAX;
200
201   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_quartimax)))
202     p->method = ROT_QUARTIMAX;
203
204   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_equimax)))
205     p->method = ROT_EQUIMAX;
206 }
207
208 static void
209 set_extraction_parameters (const struct factor *fd, struct extraction_parameters *p)
210 {
211   p->mineigen = gtk_spin_button_get_value (GTK_SPIN_BUTTON (fd->mineigen));
212   p->n_factors = gtk_spin_button_get_value (GTK_SPIN_BUTTON (fd->n_factors));
213   p->n_iterations = gtk_spin_button_get_value (GTK_SPIN_BUTTON (fd->extract_iterations));
214
215   p->explicit_nfactors = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->nfactors_toggle));
216   p->covariance = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->covariance_toggle));
217
218   p->scree = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->scree_button));
219   p->unrotated = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->unrotated_button));
220
221   p->paf = (gtk_combo_box_get_active (GTK_COMBO_BOX (fd->extraction_combo)) == 1);
222 }
223
224 static void
225 run_extractions_subdialog (struct factor *fd)
226 {
227   struct extraction_parameters *ex = &fd->extraction;
228
229   gint response = psppire_dialog_run (PSPPIRE_DIALOG (fd->extraction_dialog));
230
231   if ( response == PSPPIRE_RESPONSE_CONTINUE )
232     {
233       /* Set the parameters from their respective widgets */
234       set_extraction_parameters (fd, ex);
235     }
236   else
237     {
238       /* Cancelled.  Reset the widgets to their old state */
239       load_extraction_parameters (fd, ex);
240     }
241 }
242
243 static void
244 run_rotations_subdialog (struct factor *fd)
245 {
246   struct rotation_parameters *rot = &fd->rotation;
247
248   gint response = psppire_dialog_run (PSPPIRE_DIALOG (fd->rotation_dialog));
249
250   if ( response == PSPPIRE_RESPONSE_CONTINUE )
251     {
252       /* Set the parameters from their respective widgets */
253       set_rotation_parameters (fd, rot);
254     }
255   else
256     {
257       /* Cancelled.  Reset the widgets to their old state */
258       load_rotation_parameters (fd, rot);
259     }
260 }
261
262
263 static char * generate_syntax (const struct factor *rd);
264
265
266 static void
267 refresh (struct factor *fd)
268 {
269   GtkTreeModel *liststore =
270     gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
271   gtk_list_store_clear (GTK_LIST_STORE (liststore));
272
273   load_extraction_parameters (fd, &default_extraction_parameters);
274   load_rotation_parameters (fd, &default_rotation_parameters);
275 }
276
277
278 static gboolean
279 dialog_state_valid (gpointer data)
280 {
281   struct factor *fd = data;
282
283   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
284
285   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
286     return FALSE;
287
288   return TRUE;
289 }
290
291 static void
292 on_show (struct factor *fd, GtkWidget *dialog)
293 {
294   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
295
296   gint n_vars = gtk_tree_model_iter_n_children (liststore, NULL);
297
298   gtk_spin_button_set_range (GTK_SPIN_BUTTON (fd->n_factors), 1, n_vars - 1);
299 }
300
301
302 static void
303 on_extract_toggle (GtkToggleButton *button, struct factor *f)
304 {
305   gboolean active = gtk_toggle_button_get_active (button);
306
307   gtk_widget_set_sensitive (GTK_WIDGET (f->n_factors), active);
308   gtk_widget_set_sensitive (GTK_WIDGET (f->mineigen), ! active);
309 }
310
311 /* Pops up the Factor dialog box */
312 void
313 factor_dialog (PsppireDataWindow *dw)
314 {
315   struct factor fd;
316   gint response;
317
318   PsppireVarStore *vs;
319
320   GtkWidget *dialog ;
321   GtkWidget *source ;
322   GtkWidget *extraction_button ;
323   GtkWidget *rotation_button ;
324
325   fd.xml = builder_new ("factor.ui");
326
327   fd.extraction = default_extraction_parameters;
328   fd.rotation = default_rotation_parameters;
329   
330   dialog = get_widget_assert   (fd.xml, "factor-dialog");
331   source = get_widget_assert   (fd.xml, "dict-view");
332   extraction_button = get_widget_assert (fd.xml, "button-extractions");
333   rotation_button = get_widget_assert (fd.xml, "button-rotations");
334
335   fd.extraction_dialog = get_widget_assert (fd.xml, "extractions-dialog");
336   fd.rotation_dialog = get_widget_assert (fd.xml, "rotations-dialog");
337
338   fd.de = dw;
339
340   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &fd);
341
342   {
343     GtkWidget *hbox = get_widget_assert (fd.xml, "hbox6");
344     GtkWidget *eigenvalue_extraction ;
345
346     fd.mineigen_toggle = get_widget_assert (fd.xml, "mineigen-radiobutton");
347
348     eigenvalue_extraction = psppire_scanf_new (_("_Eigenvalues over %4.2f times the mean eigenvalue"), &fd.mineigen);
349
350     g_object_set (eigenvalue_extraction,
351                   "use-underline", TRUE,
352                   "mnemonic-widget", fd.mineigen_toggle,
353                   NULL);
354
355     fd.nfactors_toggle = get_widget_assert (fd.xml, "nfactors-radiobutton");
356     fd.n_factors = get_widget_assert (fd.xml, "spinbutton-nfactors");
357     fd.extract_iterations = get_widget_assert (fd.xml, "spinbutton-extract-iterations");
358     fd.covariance_toggle = get_widget_assert (fd.xml,  "covariance-radiobutton");
359     fd.correlation_toggle = get_widget_assert (fd.xml, "correlations-radiobutton");
360
361     fd.scree_button = get_widget_assert (fd.xml, "scree-button");
362     fd.unrotated_button = get_widget_assert (fd.xml, "unrotated-button");
363     fd.extraction_combo = get_widget_assert (fd.xml, "combobox1");
364
365     gtk_container_add (GTK_CONTAINER (hbox), eigenvalue_extraction);
366
367     g_signal_connect (fd.nfactors_toggle, "toggled", G_CALLBACK (on_extract_toggle), &fd);
368
369     gtk_widget_show_all (eigenvalue_extraction);
370   }
371
372   {
373     fd.rotate_iterations = get_widget_assert (fd.xml, "spinbutton-rot-iterations");
374
375     fd.display_rotated_solution = get_widget_assert (fd.xml, "checkbutton-rotated-solution");
376
377     fd.rotation_none      = get_widget_assert (fd.xml, "radiobutton-none");
378     fd.rotation_varimax   = get_widget_assert (fd.xml, "radiobutton-varimax");
379     fd.rotation_quartimax = get_widget_assert (fd.xml, "radiobutton-quartimax");
380     fd.rotation_equimax   = get_widget_assert (fd.xml, "radiobutton-equimax");
381   }
382
383   g_signal_connect_swapped (extraction_button, "clicked", G_CALLBACK (run_extractions_subdialog), &fd);
384   g_signal_connect_swapped (rotation_button, "clicked", G_CALLBACK (run_rotations_subdialog), &fd);
385
386   g_signal_connect_swapped (fd.extraction_dialog, "show", G_CALLBACK (on_show), &fd);
387
388   fd.variables = get_widget_assert   (fd.xml, "psppire-var-view1");
389
390   g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
391
392   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
393   gtk_window_set_transient_for (GTK_WINDOW (fd.extraction_dialog), GTK_WINDOW (fd.de));
394   gtk_window_set_transient_for (GTK_WINDOW (fd.rotation_dialog), GTK_WINDOW (fd.de));
395
396   g_object_get (vs, "dictionary", &fd.dict, NULL);
397   g_object_set (source, "model", fd.dict, NULL);
398
399
400   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
401                                       dialog_state_valid, &fd);
402
403   psppire_selector_set_allow (PSPPIRE_SELECTOR (get_widget_assert (fd.xml, "dep-selector")),
404                               numeric_only);
405
406   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
407
408   switch (response)
409     {
410     case GTK_RESPONSE_OK:
411       g_free (execute_syntax_string (dw, generate_syntax (&fd)));
412       break;
413     case PSPPIRE_RESPONSE_PASTE:
414       g_free (paste_syntax_to_window (generate_syntax (&fd)));
415       break;
416     default:
417       break;
418     }
419
420   g_object_unref (fd.xml);
421 }
422
423
424 \f
425
426 static char *
427 generate_syntax (const struct factor *rd)
428 {
429   gchar *text;
430
431   GString *string = g_string_new ("FACTOR ");
432
433   g_string_append (string, "VARIABLES =  ");
434
435   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
436
437
438   g_string_append (string, "\n\t/CRITERIA = ");
439   if ( rd->extraction.explicit_nfactors )
440     g_string_append_printf (string, "FACTORS (%d)", rd->extraction.n_factors);
441   else
442     g_string_append_printf (string, "MINEIGEN (%g)", rd->extraction.mineigen);
443
444   /*
445     The CRITERIA = ITERATE subcommand is overloaded.
446      It applies to the next /ROTATION and/or EXTRACTION command whatever comes first.
447   */
448   g_string_append_printf (string, " ITERATE (%d)", rd->extraction.n_iterations);
449
450
451   g_string_append (string, "\n\t/EXTRACTION =");
452   if ( rd->extraction.paf)
453     g_string_append (string, "PAF");
454   else
455     g_string_append (string, "PC");
456
457
458
459
460   g_string_append (string, "\n\t/METHOD = ");
461   if ( rd->extraction.covariance )
462     g_string_append (string, "COVARIANCE");
463   else
464     g_string_append (string, "CORRELATION");
465
466
467
468   if ( rd->extraction.scree )
469     {
470       g_string_append (string, "\n\t/PLOT = ");
471       g_string_append (string, "EIGEN");
472     }
473
474   g_string_append (string, "\n\t/PRINT = ");
475   g_string_append (string, "INITIAL ");
476
477   if ( rd->extraction.unrotated )  
478     g_string_append (string, "EXTRACTION ");
479
480   if ( rd->rotation.rotated_solution )
481     g_string_append (string, "ROTATION");
482
483
484   /* The CRITERIA = ITERATE subcommand is overloaded.
485      It applies to the next /ROTATION and/or EXTRACTION command whatever comes first.
486   */
487   g_string_append_printf (string, "\n\t/CRITERIA = ITERATE (%d)",  rd->rotation.iterations  );
488
489   g_string_append (string, "\n\t/ROTATION = ");
490   g_string_append (string, rot_method_syntax[rd->rotation.method]);
491
492
493   g_string_append (string, ".\n");
494
495   text = string->str;
496
497   g_string_free (string, FALSE);
498
499   return text;
500 }