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;
60 static const struct extraction_parameters default_extraction_parameters = {1.0, 0, 25, FALSE, TRUE, FALSE, TRUE, FALSE};
68 PsppireDataWindow *de ;
70 /* The Extraction subdialog */
71 GtkWidget *extraction_dialog;
75 GtkWidget *iterations;
77 GtkWidget *nfactors_toggle;
78 GtkWidget *mineigen_toggle;
80 GtkWidget *covariance_toggle;
81 GtkWidget *correlation_toggle;
83 GtkWidget *scree_button;
84 GtkWidget *unrotated_button;
86 GtkWidget *extraction_combo;
88 struct extraction_parameters extraction;
92 load_extraction_parameters (struct factor *fd, const struct extraction_parameters *p)
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);
98 if (p->explicit_nfactors)
99 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->nfactors_toggle), TRUE);
101 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->mineigen_toggle), TRUE);
104 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->covariance_toggle), TRUE);
106 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->correlation_toggle), TRUE);
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);
113 gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 1);
115 gtk_combo_box_set_active (GTK_COMBO_BOX (fd->extraction_combo), 0);
120 set_extraction_parameters (struct extraction_parameters *p, const struct factor *fd)
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));
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));
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));
132 p->paf = (gtk_combo_box_get_active (GTK_COMBO_BOX (fd->extraction_combo)) == 1);
135 static void run_extractions_subdialog (struct factor *fd)
137 struct extraction_parameters *ex = &fd->extraction;
139 gint response = psppire_dialog_run (PSPPIRE_DIALOG (fd->extraction_dialog));
141 if ( response == PSPPIRE_RESPONSE_CONTINUE )
143 /* Set the parameters from their respective widgets */
144 set_extraction_parameters (ex, fd);
148 /* Cancelled. Reset the widgets to their old state */
149 load_extraction_parameters (fd, ex);
154 static char * generate_syntax (const struct factor *rd);
158 refresh (struct factor *fd)
160 GtkTreeModel *liststore =
161 gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
162 gtk_list_store_clear (GTK_LIST_STORE (liststore));
164 load_extraction_parameters (fd, &default_extraction_parameters);
169 dialog_state_valid (gpointer data)
171 struct factor *fd = data;
173 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
175 if (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
182 on_show (struct factor *fd, GtkWidget *dialog)
184 GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
186 gint n_vars = gtk_tree_model_iter_n_children (liststore, NULL);
188 gtk_spin_button_set_range (GTK_SPIN_BUTTON (fd->n_factors), 1, n_vars - 1);
193 on_extract_toggle (GtkToggleButton *button, struct factor *f)
195 gboolean active = gtk_toggle_button_get_active (button);
197 gtk_widget_set_sensitive (GTK_WIDGET (f->n_factors), active);
198 gtk_widget_set_sensitive (GTK_WIDGET (f->mineigen), ! active);
201 /* Pops up the Factor dialog box */
203 factor_dialog (PsppireDataWindow *dw)
212 GtkWidget *extraction_button ;
214 fd.xml = builder_new ("factor.ui");
216 fd.extraction = default_extraction_parameters;
218 dialog = get_widget_assert (fd.xml, "factor-dialog");
219 source = get_widget_assert (fd.xml, "dict-view");
220 extraction_button = get_widget_assert (fd.xml, "button-extraction");
222 fd.extraction_dialog = get_widget_assert (fd.xml, "extractions-dialog");
226 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &fd);
229 GtkWidget *hbox = get_widget_assert (fd.xml, "hbox6");
230 GtkWidget *eigenvalue_extraction = widget_scanf (_("Eigenvalues over %4.2f times the mean eigenvalue"), &fd.mineigen);
232 fd.nfactors_toggle = get_widget_assert (fd.xml, "nfactors-radiobutton");
233 fd.mineigen_toggle = get_widget_assert (fd.xml, "mineigen-radiobutton");
234 fd.n_factors = get_widget_assert (fd.xml, "spinbutton-nfactors");
235 fd.iterations = get_widget_assert (fd.xml, "spinbutton-iterations");
236 fd.covariance_toggle = get_widget_assert (fd.xml, "covariance-radiobutton");
237 fd.correlation_toggle = get_widget_assert (fd.xml, "correlations-radiobutton");
239 fd.scree_button = get_widget_assert (fd.xml, "scree-button");
240 fd.unrotated_button = get_widget_assert (fd.xml, "unrotated-button");
241 fd.extraction_combo = get_widget_assert (fd.xml, "combobox1");
243 gtk_container_add (GTK_CONTAINER (hbox), eigenvalue_extraction);
245 g_signal_connect (fd.nfactors_toggle, "toggled", G_CALLBACK (on_extract_toggle), &fd);
247 gtk_widget_show_all (eigenvalue_extraction);
250 g_signal_connect_swapped (extraction_button, "clicked", G_CALLBACK (run_extractions_subdialog), &fd);
252 g_signal_connect_swapped (fd.extraction_dialog, "show", G_CALLBACK (on_show), &fd);
254 fd.variables = get_widget_assert (fd.xml, "psppire-var-view1");
256 g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
258 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
259 gtk_window_set_transient_for (GTK_WINDOW (fd.extraction_dialog), GTK_WINDOW (fd.de));
261 g_object_get (vs, "dictionary", &fd.dict, NULL);
262 g_object_set (source, "model", fd.dict, NULL);
265 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
266 dialog_state_valid, &fd);
268 psppire_selector_set_allow (PSPPIRE_SELECTOR (get_widget_assert (fd.xml, "dep-selector")),
271 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
275 case GTK_RESPONSE_OK:
277 gchar *syntax = generate_syntax (&fd);
279 struct getl_interface *sss = create_syntax_string_source (syntax);
280 execute_syntax (sss);
285 case PSPPIRE_RESPONSE_PASTE:
287 gchar *syntax = generate_syntax (&fd);
288 paste_syntax_in_new_window (syntax);
297 g_object_unref (fd.xml);
304 generate_syntax (const struct factor *rd)
308 GString *string = g_string_new ("FACTOR ");
310 g_string_append (string, "VARIABLES = ");
312 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
314 g_string_append (string, "\n\t/EXTRACTION =");
315 if ( rd->extraction.paf)
316 g_string_append (string, "PAF");
318 g_string_append (string, "PC");
321 g_string_append (string, "\n\t/METHOD = ");
322 if ( rd->extraction.covariance )
323 g_string_append (string, "COVARIANCE");
325 g_string_append (string, "CORRELATION");
328 g_string_append (string, "\n\t/CRITERIA = ");
329 if ( rd->extraction.explicit_nfactors )
330 g_string_append_printf (string, "FACTORS (%d)", rd->extraction.n_factors);
332 g_string_append_printf (string, "MINEIGEN (%g)", rd->extraction.mineigen);
335 if ( rd->extraction.scree )
337 g_string_append (string, "\n\t/PLOT = ");
338 g_string_append (string, "EIGEN");
341 g_string_append (string, "\n\t/PRINT = ");
342 g_string_append (string, "INITIAL ");
343 if ( rd->extraction.unrotated )
344 g_string_append (string, "EXTRACTION ");
347 g_string_append (string, ".\n");
351 g_string_free (string, FALSE);