1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009 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 <language/syntax-string-source.h>
21 #include <ui/syntax-gen.h>
22 #include <libpspp/str.h>
24 #include "factor-dialog.h"
25 #include "psppire-selector.h"
26 #include "psppire-dictview.h"
27 #include "psppire-dialog.h"
29 #include "psppire-data-window.h"
30 #include "psppire-var-view.h"
32 #include "widget-io.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
44 struct extraction_parameters
50 gboolean explicit_nfactors;
66 static const char *rot_method_syntax[] =
74 struct rotation_parameters
76 gboolean rotated_solution;
79 enum rotation_type method;
84 static const struct extraction_parameters default_extraction_parameters = {1.0, 0, 25, FALSE, TRUE, FALSE, TRUE, FALSE};
86 static const struct rotation_parameters default_rotation_parameters = {TRUE, 25, ROT_VARIMAX};
94 PsppireDataWindow *de ;
96 /* The Extraction subdialog */
97 GtkWidget *extraction_dialog;
98 GtkWidget *rotation_dialog;
100 GtkWidget *n_factors;
102 GtkWidget *extract_iterations;
104 GtkWidget *nfactors_toggle;
105 GtkWidget *mineigen_toggle;
107 GtkWidget *covariance_toggle;
108 GtkWidget *correlation_toggle;
110 GtkWidget *scree_button;
111 GtkWidget *unrotated_button;
113 GtkWidget *extraction_combo;
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;
125 struct extraction_parameters extraction;
126 struct rotation_parameters rotation;
130 load_rotation_parameters (struct factor *fd, const struct rotation_parameters *p)
132 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->display_rotated_solution),
133 p->rotated_solution);
135 gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->rotate_iterations),
141 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_none), TRUE);
144 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_varimax), TRUE);
147 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_quartimax), TRUE);
150 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->rotation_equimax), TRUE);
153 g_assert_not_reached ();
159 load_extraction_parameters (struct factor *fd, const struct extraction_parameters *p)
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);
164 gtk_spin_button_set_value (GTK_SPIN_BUTTON (fd->extract_iterations), p->n_iterations);
167 if (p->explicit_nfactors)
168 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->nfactors_toggle), TRUE);
170 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->mineigen_toggle), TRUE);
173 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->covariance_toggle), TRUE);
175 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->correlation_toggle), TRUE);
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);
182 gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 1);
184 gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 0);
189 set_rotation_parameters (const struct factor *fd, struct rotation_parameters *p)
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));
195 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_none)))
196 p->method = ROT_NONE;
198 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_varimax)))
199 p->method = ROT_VARIMAX;
201 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_quartimax)))
202 p->method = ROT_QUARTIMAX;
204 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->rotation_equimax)))
205 p->method = ROT_EQUIMAX;
209 set_extraction_parameters (const struct factor *fd, struct extraction_parameters *p)
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));
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));
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));
221 p->paf = (gtk_combo_box_get_active (GTK_COMBO_BOX (fd->extraction_combo)) == 1);
225 run_extractions_subdialog (struct factor *fd)
227 struct extraction_parameters *ex = &fd->extraction;
229 gint response = psppire_dialog_run (PSPPIRE_DIALOG (fd->extraction_dialog));
231 if ( response == PSPPIRE_RESPONSE_CONTINUE )
233 /* Set the parameters from their respective widgets */
234 set_extraction_parameters (fd, ex);
238 /* Cancelled. Reset the widgets to their old state */
239 load_extraction_parameters (fd, ex);
244 run_rotations_subdialog (struct factor *fd)
246 struct rotation_parameters *rot = &fd->rotation;
248 gint response = psppire_dialog_run (PSPPIRE_DIALOG (fd->rotation_dialog));
250 if ( response == PSPPIRE_RESPONSE_CONTINUE )
252 /* Set the parameters from their respective widgets */
253 set_rotation_parameters (fd, rot);
257 /* Cancelled. Reset the widgets to their old state */
258 load_rotation_parameters (fd, rot);
263 static char * generate_syntax (const struct factor *rd);
267 refresh (struct factor *fd)
269 GtkTreeModel *liststore =
270 gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
271 gtk_list_store_clear (GTK_LIST_STORE (liststore));
273 load_extraction_parameters (fd, &default_extraction_parameters);
274 load_rotation_parameters (fd, &default_rotation_parameters);
279 dialog_state_valid (gpointer data)
281 struct factor *fd = data;
283 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
285 if (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
292 on_show (struct factor *fd, GtkWidget *dialog)
294 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
296 gint n_vars = gtk_tree_model_iter_n_children (liststore, NULL);
298 gtk_spin_button_set_range (GTK_SPIN_BUTTON (fd->n_factors), 1, n_vars - 1);
303 on_extract_toggle (GtkToggleButton *button, struct factor *f)
305 gboolean active = gtk_toggle_button_get_active (button);
307 gtk_widget_set_sensitive (GTK_WIDGET (f->n_factors), active);
308 gtk_widget_set_sensitive (GTK_WIDGET (f->mineigen), ! active);
311 /* Pops up the Factor dialog box */
313 factor_dialog (PsppireDataWindow *dw)
322 GtkWidget *extraction_button ;
323 GtkWidget *rotation_button ;
325 fd.xml = builder_new ("factor.ui");
327 fd.extraction = default_extraction_parameters;
328 fd.rotation = default_rotation_parameters;
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");
335 fd.extraction_dialog = get_widget_assert (fd.xml, "extractions-dialog");
336 fd.rotation_dialog = get_widget_assert (fd.xml, "rotations-dialog");
340 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &fd);
343 GtkWidget *hbox = get_widget_assert (fd.xml, "hbox6");
344 GtkWidget *eigenvalue_extraction = widget_scanf (_("Eigenvalues over %4.2f times the mean eigenvalue"), &fd.mineigen);
346 fd.nfactors_toggle = get_widget_assert (fd.xml, "nfactors-radiobutton");
347 fd.mineigen_toggle = get_widget_assert (fd.xml, "mineigen-radiobutton");
348 fd.n_factors = get_widget_assert (fd.xml, "spinbutton-nfactors");
349 fd.extract_iterations = get_widget_assert (fd.xml, "spinbutton-extract-iterations");
350 fd.covariance_toggle = get_widget_assert (fd.xml, "covariance-radiobutton");
351 fd.correlation_toggle = get_widget_assert (fd.xml, "correlations-radiobutton");
353 fd.scree_button = get_widget_assert (fd.xml, "scree-button");
354 fd.unrotated_button = get_widget_assert (fd.xml, "unrotated-button");
355 fd.extraction_combo = get_widget_assert (fd.xml, "combobox1");
357 gtk_container_add (GTK_CONTAINER (hbox), eigenvalue_extraction);
359 g_signal_connect (fd.nfactors_toggle, "toggled", G_CALLBACK (on_extract_toggle), &fd);
361 gtk_widget_show_all (eigenvalue_extraction);
365 fd.rotate_iterations = get_widget_assert (fd.xml, "spinbutton-rot-iterations");
367 fd.display_rotated_solution = get_widget_assert (fd.xml, "checkbutton-rotated-solution");
369 fd.rotation_none = get_widget_assert (fd.xml, "radiobutton-none");
370 fd.rotation_varimax = get_widget_assert (fd.xml, "radiobutton-varimax");
371 fd.rotation_quartimax = get_widget_assert (fd.xml, "radiobutton-quartimax");
372 fd.rotation_equimax = get_widget_assert (fd.xml, "radiobutton-equimax");
375 g_signal_connect_swapped (extraction_button, "clicked", G_CALLBACK (run_extractions_subdialog), &fd);
376 g_signal_connect_swapped (rotation_button, "clicked", G_CALLBACK (run_rotations_subdialog), &fd);
378 g_signal_connect_swapped (fd.extraction_dialog, "show", G_CALLBACK (on_show), &fd);
380 fd.variables = get_widget_assert (fd.xml, "psppire-var-view1");
382 g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
384 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
385 gtk_window_set_transient_for (GTK_WINDOW (fd.extraction_dialog), GTK_WINDOW (fd.de));
386 gtk_window_set_transient_for (GTK_WINDOW (fd.rotation_dialog), GTK_WINDOW (fd.de));
388 g_object_get (vs, "dictionary", &fd.dict, NULL);
389 g_object_set (source, "model", fd.dict, NULL);
392 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
393 dialog_state_valid, &fd);
395 psppire_selector_set_allow (PSPPIRE_SELECTOR (get_widget_assert (fd.xml, "dep-selector")),
398 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
402 case GTK_RESPONSE_OK:
404 gchar *syntax = generate_syntax (&fd);
406 struct getl_interface *sss = create_syntax_string_source (syntax);
407 execute_syntax (sss);
412 case PSPPIRE_RESPONSE_PASTE:
414 gchar *syntax = generate_syntax (&fd);
415 paste_syntax_to_window (syntax);
424 g_object_unref (fd.xml);
431 generate_syntax (const struct factor *rd)
435 GString *string = g_string_new ("FACTOR ");
437 g_string_append (string, "VARIABLES = ");
439 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
442 g_string_append (string, "\n\t/CRITERIA = ");
443 if ( rd->extraction.explicit_nfactors )
444 g_string_append_printf (string, "FACTORS (%d)", rd->extraction.n_factors);
446 g_string_append_printf (string, "MINEIGEN (%g)", rd->extraction.mineigen);
449 The CRITERIA = ITERATE subcommand is overloaded.
450 It applies to the next /ROTATION and/or EXTRACTION command whatever comes first.
452 g_string_append_printf (string, " ITERATE (%d)", rd->extraction.n_iterations);
455 g_string_append (string, "\n\t/EXTRACTION =");
456 if ( rd->extraction.paf)
457 g_string_append (string, "PAF");
459 g_string_append (string, "PC");
464 g_string_append (string, "\n\t/METHOD = ");
465 if ( rd->extraction.covariance )
466 g_string_append (string, "COVARIANCE");
468 g_string_append (string, "CORRELATION");
472 if ( rd->extraction.scree )
474 g_string_append (string, "\n\t/PLOT = ");
475 g_string_append (string, "EIGEN");
478 g_string_append (string, "\n\t/PRINT = ");
479 g_string_append (string, "INITIAL ");
481 if ( rd->extraction.unrotated )
482 g_string_append (string, "EXTRACTION ");
484 if ( rd->rotation.rotated_solution )
485 g_string_append (string, "ROTATION");
488 /* The CRITERIA = ITERATE subcommand is overloaded.
489 It applies to the next /ROTATION and/or EXTRACTION command whatever comes first.
491 g_string_append_printf (string, "\n\t/CRITERIA = ITERATE (%d)", rd->rotation.iterations );
493 g_string_append (string, "\n\t/ROTATION = ");
494 g_string_append (string, rot_method_syntax[rd->rotation.method]);
497 g_string_append (string, ".\n");
501 g_string_free (string, FALSE);