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