1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011 Free Software Foundation
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.
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.
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/>. */
19 #include "dialog-common.h"
20 #include <ui/syntax-gen.h>
21 #include <libpspp/str.h>
23 #include "factor-dialog.h"
24 #include "psppire-selector.h"
25 #include "psppire-dictview.h"
26 #include "psppire-dialog.h"
28 #include "psppire-data-window.h"
29 #include "psppire-var-view.h"
31 #include "psppire-scanf.h"
39 #define _(msgid) gettext (msgid)
40 #define N_(msgid) msgid
43 struct extraction_parameters
49 gboolean explicit_nfactors;
65 static const char *rot_method_syntax[] =
73 struct rotation_parameters
75 gboolean rotated_solution;
78 enum rotation_type method;
83 static const struct extraction_parameters default_extraction_parameters = {1.0, 0, 25, FALSE, TRUE, FALSE, TRUE, FALSE};
85 static const struct rotation_parameters default_rotation_parameters = {TRUE, 25, ROT_VARIMAX};
93 PsppireDataWindow *de ;
95 /* The Extraction subdialog */
96 GtkWidget *extraction_dialog;
97 GtkWidget *rotation_dialog;
101 GtkWidget *extract_iterations;
103 GtkWidget *nfactors_toggle;
104 GtkWidget *mineigen_toggle;
106 GtkWidget *covariance_toggle;
107 GtkWidget *correlation_toggle;
109 GtkWidget *scree_button;
110 GtkWidget *unrotated_button;
112 GtkWidget *extraction_combo;
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;
124 struct extraction_parameters extraction;
125 struct rotation_parameters rotation;
129 load_rotation_parameters (struct factor *fd, const struct rotation_parameters *p)
131 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->display_rotated_solution),
132 p->rotated_solution);
134 gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->rotate_iterations),
140 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_none), TRUE);
143 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_varimax), TRUE);
146 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_quartimax), TRUE);
149 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_equimax), TRUE);
152 g_assert_not_reached ();
158 load_extraction_parameters (struct factor *fd, const struct extraction_parameters *p)
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);
163 gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->extract_iterations), p->n_iterations);
166 if (p->explicit_nfactors)
167 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->nfactors_toggle), TRUE);
169 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->mineigen_toggle), TRUE);
172 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->covariance_toggle), TRUE);
174 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->correlation_toggle), TRUE);
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);
181 gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 1);
183 gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 0);
188 set_rotation_parameters (const struct factor *fd, struct rotation_parameters *p)
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));
194 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_none)))
195 p->method = ROT_NONE;
197 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_varimax)))
198 p->method = ROT_VARIMAX;
200 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_quartimax)))
201 p->method = ROT_QUARTIMAX;
203 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_equimax)))
204 p->method = ROT_EQUIMAX;
208 set_extraction_parameters (const struct factor *fd, struct extraction_parameters *p)
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));
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));
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));
220 p->paf = (gtk_combo_box_get_active (GTK_COMBO_BOX (fd->extraction_combo)) == 1);
224 run_extractions_subdialog (struct factor *fd)
226 struct extraction_parameters *ex = &fd->extraction;
228 gint response = psppire_dialog_run (PSPPIRE_DIALOG (fd->extraction_dialog));
230 if ( response == PSPPIRE_RESPONSE_CONTINUE )
232 /* Set the parameters from their respective widgets */
233 set_extraction_parameters (fd, ex);
237 /* Cancelled. Reset the widgets to their old state */
238 load_extraction_parameters (fd, ex);
243 run_rotations_subdialog (struct factor *fd)
245 struct rotation_parameters *rot = &fd->rotation;
247 gint response = psppire_dialog_run (PSPPIRE_DIALOG (fd->rotation_dialog));
249 if ( response == PSPPIRE_RESPONSE_CONTINUE )
251 /* Set the parameters from their respective widgets */
252 set_rotation_parameters (fd, rot);
256 /* Cancelled. Reset the widgets to their old state */
257 load_rotation_parameters (fd, rot);
262 static char * generate_syntax (const struct factor *rd);
266 refresh (struct factor *fd)
268 GtkTreeModel *liststore =
269 gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
270 gtk_list_store_clear (GTK_LIST_STORE (liststore));
272 load_extraction_parameters (fd, &default_extraction_parameters);
273 load_rotation_parameters (fd, &default_rotation_parameters);
278 dialog_state_valid (gpointer data)
280 struct factor *fd = data;
282 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
284 if (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
291 on_show (struct factor *fd, GtkWidget *dialog)
293 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
295 gint n_vars = gtk_tree_model_iter_n_children (liststore, NULL);
297 gtk_spin_button_set_range (GTK_SPIN_BUTTON (fd->n_factors), 1, n_vars - 1);
302 on_extract_toggle (GtkToggleButton *button, struct factor *f)
304 gboolean active = gtk_toggle_button_get_active (button);
306 gtk_widget_set_sensitive (GTK_WIDGET (f->n_factors), active);
307 gtk_widget_set_sensitive (GTK_WIDGET (f->mineigen), ! active);
310 /* Pops up the Factor dialog box */
312 factor_dialog (PsppireDataWindow *dw)
321 GtkWidget *extraction_button ;
322 GtkWidget *rotation_button ;
324 fd.xml = builder_new ("factor.ui");
326 fd.extraction = default_extraction_parameters;
327 fd.rotation = default_rotation_parameters;
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");
334 fd.extraction_dialog = get_widget_assert (fd.xml, "extractions-dialog");
335 fd.rotation_dialog = get_widget_assert (fd.xml, "rotations-dialog");
339 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &fd);
342 GtkWidget *hbox = get_widget_assert (fd.xml, "hbox6");
343 GtkWidget *eigenvalue_extraction ;
345 fd.mineigen_toggle = get_widget_assert (fd.xml, "mineigen-radiobutton");
347 eigenvalue_extraction = psppire_scanf_new (_("_Eigenvalues over %4.2f times the mean eigenvalue"), &fd.mineigen);
349 g_object_set (eigenvalue_extraction,
350 "use-underline", TRUE,
351 "mnemonic-widget", fd.mineigen_toggle,
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");
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");
364 gtk_container_add (GTK_CONTAINER (hbox), eigenvalue_extraction);
366 g_signal_connect (fd.nfactors_toggle, "toggled", G_CALLBACK (on_extract_toggle), &fd);
368 gtk_widget_show_all (eigenvalue_extraction);
372 fd.rotate_iterations = get_widget_assert (fd.xml, "spinbutton-rot-iterations");
374 fd.display_rotated_solution = get_widget_assert (fd.xml, "checkbutton-rotated-solution");
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");
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);
385 g_signal_connect_swapped (fd.extraction_dialog, "show", G_CALLBACK (on_show), &fd);
387 fd.variables = get_widget_assert (fd.xml, "psppire-var-view1");
389 g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
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));
395 g_object_get (vs, "dictionary", &fd.dict, NULL);
396 g_object_set (source, "model", fd.dict, NULL);
399 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
400 dialog_state_valid, &fd);
402 psppire_selector_set_allow (PSPPIRE_SELECTOR (get_widget_assert (fd.xml, "dep-selector")),
405 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
409 case GTK_RESPONSE_OK:
410 g_free (execute_syntax_string (dw, generate_syntax (&fd)));
412 case PSPPIRE_RESPONSE_PASTE:
413 g_free (paste_syntax_to_window (generate_syntax (&fd)));
419 g_object_unref (fd.xml);
426 generate_syntax (const struct factor *rd)
430 GString *string = g_string_new ("FACTOR ");
432 g_string_append (string, "VARIABLES = ");
434 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
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);
441 g_string_append_printf (string, "MINEIGEN (%g)", rd->extraction.mineigen);
444 The CRITERIA = ITERATE subcommand is overloaded.
445 It applies to the next /ROTATION and/or EXTRACTION command whatever comes first.
447 g_string_append_printf (string, " ITERATE (%d)", rd->extraction.n_iterations);
450 g_string_append (string, "\n\t/EXTRACTION =");
451 if ( rd->extraction.paf)
452 g_string_append (string, "PAF");
454 g_string_append (string, "PC");
459 g_string_append (string, "\n\t/METHOD = ");
460 if ( rd->extraction.covariance )
461 g_string_append (string, "COVARIANCE");
463 g_string_append (string, "CORRELATION");
467 if ( rd->extraction.scree )
469 g_string_append (string, "\n\t/PLOT = ");
470 g_string_append (string, "EIGEN");
473 g_string_append (string, "\n\t/PRINT = ");
474 g_string_append (string, "INITIAL ");
476 if ( rd->extraction.unrotated )
477 g_string_append (string, "EXTRACTION ");
479 if ( rd->rotation.rotated_solution )
480 g_string_append (string, "ROTATION");
483 /* The CRITERIA = ITERATE subcommand is overloaded.
484 It applies to the next /ROTATION and/or EXTRACTION command whatever comes first.
486 g_string_append_printf (string, "\n\t/CRITERIA = ITERATE (%d)", rd->rotation.iterations );
488 g_string_append (string, "\n\t/ROTATION = ");
489 g_string_append (string, rot_method_syntax[rd->rotation.method]);
492 g_string_append (string, ".\n");
496 g_string_free (string, FALSE);