1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 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 "rank-dialog.h"
24 #include <ui/gui/psppire-data-window.h>
25 #include <ui/gui/dialog-common.h>
26 #include <ui/gui/dict-display.h>
27 #include <ui/gui/helper.h>
28 #include <ui/gui/psppire-dialog.h>
29 #include <ui/gui/psppire-var-store.h>
30 #include <ui/gui/psppire-var-view.h>
34 #define _(msgid) gettext (msgid)
35 #define N_(msgid) msgid
58 GtkWidget *group_vars;
61 GtkToggleButton *ascending_togglebutton;
62 GtkToggleButton *summary_togglebutton;
65 /* Types subdialog widgets */
67 GtkWidget *types_dialog;
68 GtkWidget *ntiles_entry;
70 GtkToggleButton *func_button[n_RANK_FUNCS];
71 GtkWidget *formula_box;
73 GtkToggleButton *blom;
74 GtkToggleButton *tukey;
75 GtkToggleButton *rankit;
78 /* Ties subdialog widgets */
80 PsppireDialog *ties_dialog;
81 GtkToggleButton *mean;
83 GtkToggleButton *high;
84 GtkToggleButton *condense;
88 refresh (PsppireDialog *dialog, struct rank_dialog *rd)
90 GtkTreeModel *liststore;
92 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->rank_vars));
93 gtk_list_store_clear (GTK_LIST_STORE (liststore));
95 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
96 gtk_list_store_clear (GTK_LIST_STORE (liststore));
98 gtk_toggle_button_set_active (rd->ascending_togglebutton, TRUE);
99 gtk_toggle_button_set_active (rd->summary_togglebutton, FALSE);
103 generate_syntax (const struct rank_dialog *rd)
107 GtkTreeModel *gs = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
111 GString *str = g_string_new ("RANK VARIABLES=");
113 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->rank_vars), 0, str);
115 g_string_append_printf (str, " (%c)",
116 gtk_toggle_button_get_active (rd->ascending_togglebutton)
119 if ( gtk_tree_model_get_iter_first (gs, ¬used) )
121 g_string_append (str, "\n\tBY ");
123 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->group_vars), 0, str);
126 g_string_append (str, "\n\t/PRINT = ");
127 if (gtk_toggle_button_get_active (rd->summary_togglebutton))
128 g_string_append (str, "YES");
130 g_string_append (str, "NO");
133 if (gtk_toggle_button_get_active (rd->func_button [RANK]))
134 g_string_append (str, "\n\t/RANK");
135 if (gtk_toggle_button_get_active (rd->func_button [NORMAL]))
136 g_string_append (str, "\n\t/NORMAL");
137 if (gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
138 g_string_append (str, "\n\t/PROPORTION");
139 if (gtk_toggle_button_get_active (rd->func_button [PERCENT]))
140 g_string_append (str, "\n\t/PERCENT");
141 if (gtk_toggle_button_get_active (rd->func_button [RFRACTION]))
142 g_string_append (str, "\n\t/RFRACTION");
143 if (gtk_toggle_button_get_active (rd->func_button [N]))
144 g_string_append (str, "\n\t/N");
145 if (gtk_toggle_button_get_active (rd->func_button [SAVAGE]))
146 g_string_append (str, "\n\t/SAVAGE");
147 if (gtk_toggle_button_get_active (rd->func_button [NTILES]))
149 gint n = gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->ntiles_entry));
150 g_string_append_printf (str, "\n\t/NTILES(%d)", n);
154 if (gtk_toggle_button_get_active (rd->func_button [NORMAL])
156 gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
158 g_string_append (str, "\n\t/FRACTION=");
160 if ( gtk_toggle_button_get_active (rd->blom))
161 g_string_append (str, "BLOM");
162 else if ( gtk_toggle_button_get_active (rd->tukey))
163 g_string_append (str, "TUKEY");
164 else if ( gtk_toggle_button_get_active (rd->rankit))
165 g_string_append (str, "RANKIT");
166 else if ( gtk_toggle_button_get_active (rd->vw))
167 g_string_append (str, "VW");
170 g_string_append (str, "\n\t/TIES=");
171 if ( gtk_toggle_button_get_active (rd->mean))
172 g_string_append (str, "MEAN");
173 else if ( gtk_toggle_button_get_active (rd->low))
174 g_string_append (str, "LOW");
175 else if ( gtk_toggle_button_get_active (rd->high))
176 g_string_append (str, "HIGH");
177 else if ( gtk_toggle_button_get_active (rd->condense))
178 g_string_append (str, "CONDENSE");
181 g_string_append (str, ".");
185 g_string_free (str, FALSE);
190 /* Dialog is valid iff at least one variable has been selected */
192 dialog_state_valid (gpointer data)
194 struct rank_dialog *rd = data;
196 GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->rank_vars));
200 return gtk_tree_model_get_iter_first (vars, ¬used);
203 static void on_ntiles_toggle (GtkToggleButton *, gpointer);
204 static void run_types_dialog (GtkButton *, gpointer);
205 static void run_ties_dialog (GtkButton *, gpointer );
208 set_sensitivity (struct rank_dialog *rd)
210 gboolean sens = gtk_toggle_button_get_active
211 (GTK_TOGGLE_BUTTON (rd->func_button[PROPORTION]))
213 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->func_button[NORMAL]));
215 gtk_widget_set_sensitive (rd->formula_box, sens);
218 /* Pops up the Rank dialog box */
220 rank_dialog (PsppireDataWindow *de)
224 struct rank_dialog rd;
226 GtkBuilder * builder = builder_new ("rank.ui");
228 GtkWidget *vars = get_widget_assert (builder, "dict-treeview");
230 GtkWidget *types_button = get_widget_assert (builder, "button1");
231 GtkWidget *ties_button = get_widget_assert (builder, "button2");
233 PsppireVarStore *vs = NULL;
235 g_object_get (de->data_editor, "var-store", &vs, NULL);
237 g_object_get (vs, "dictionary", &rd.dict, NULL);
238 rd.rank_vars = get_widget_assert (builder, "variables-treeview");
239 rd.group_vars = get_widget_assert (builder, "group-vars-treeview");
240 rd.dialog = get_widget_assert (builder, "rank-dialog");
241 rd.ascending_togglebutton =
242 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "radiobutton1"));
244 rd.summary_togglebutton =
245 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "summary-checkbutton"));
247 rd.types_dialog = get_widget_assert (builder, "rank-types-dialog");
250 rd.ntiles_entry = get_widget_assert (builder, "ntiles-entry");
252 rd.func_button[RANK] =
253 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rank-checkbutton"));
255 rd.func_button[SAVAGE] =
256 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "savage-checkbutton"));
258 rd.func_button[RFRACTION] =
259 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rfrac-checkbutton"));
261 rd.func_button[PERCENT] =
262 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "percent-checkbutton"));
265 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "sum-checkbutton"));
267 rd.func_button[NTILES] =
268 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "ntiles-checkbutton"));
270 rd.func_button[PROPORTION] =
271 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "prop-checkbutton"));
273 rd.func_button[NORMAL] =
274 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "normal-checkbutton"));
276 rd.formula_box = get_widget_assert (builder, "formula-frame");
278 rd.blom = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "blom-button"));
279 rd.tukey = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "tukey-button"));
280 rd.rankit = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rankit-button"));
281 rd.vw = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "vw-button"));
284 rd.ties_dialog = PSPPIRE_DIALOG (get_widget_assert (builder, "ties-dialog"));
286 rd.mean = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "mean-button"));
287 rd.low = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "low-button"));
288 rd.high = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "high-button"));
289 rd.condense = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "condense-button"));
291 g_signal_connect_swapped (rd.func_button[PROPORTION], "toggled",
292 G_CALLBACK (set_sensitivity),
295 g_signal_connect_swapped (rd.func_button[NORMAL], "toggled",
296 G_CALLBACK (set_sensitivity),
299 g_signal_connect (rd.func_button[NTILES], "toggled",
300 G_CALLBACK (on_ntiles_toggle),
303 gtk_window_set_transient_for (GTK_WINDOW (rd.dialog), GTK_WINDOW (de));
305 g_object_set (vars, "model", rd.dict, NULL);
307 g_signal_connect (types_button, "clicked",
308 G_CALLBACK (run_types_dialog), &rd);
310 g_signal_connect (ties_button, "clicked",
311 G_CALLBACK (run_ties_dialog), &rd);
313 g_signal_connect (rd.dialog, "refresh", G_CALLBACK (refresh), &rd);
315 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (rd.dialog),
316 dialog_state_valid, &rd);
318 response = psppire_dialog_run (PSPPIRE_DIALOG (rd.dialog));
323 case GTK_RESPONSE_OK:
324 g_free (execute_syntax_string (de, generate_syntax (&rd)));
326 case PSPPIRE_RESPONSE_PASTE:
327 g_free (paste_syntax_to_window (generate_syntax (&rd)));
333 g_object_unref (builder);
338 types_dialog_reset (struct rank_dialog *rd)
342 for (i = 0 ; i < n_RANK_FUNCS ; ++i )
343 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->func_button [i]),
346 gtk_widget_set_sensitive (rd->ntiles_entry, FALSE);
348 gtk_widget_set_sensitive (rd->formula_box, FALSE);
354 run_types_dialog (GtkButton *b, gpointer data)
356 struct rank_dialog *rd = data;
359 gtk_window_set_transient_for (GTK_WINDOW (rd->types_dialog),
360 GTK_WINDOW (rd->dialog));
362 types_dialog_reset (rd);
364 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->types_dialog));
368 run_ties_dialog (GtkButton *b, gpointer data)
370 struct rank_dialog *rd = data;
373 gtk_window_set_transient_for (GTK_WINDOW (rd->ties_dialog),
374 GTK_WINDOW (rd->dialog));
377 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->ties_dialog));
382 on_ntiles_toggle (GtkToggleButton *toggle_button, gpointer data)
385 gboolean active = gtk_toggle_button_get_active (toggle_button);
386 gtk_widget_set_sensitive (w, active);