Convert Rank Dialog to PsppireDialogAction
[pspp] / src / ui / gui / psppire-dialog-action-rank.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011, 2012  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
18 #include <config.h>
19
20 #include "psppire-dialog-action-rank.h"
21
22 #include "psppire-var-view.h"
23 #include "dialog-common.h"
24 #include "psppire-selector.h"
25 #include "psppire-dict.h"
26 #include "psppire-dialog.h"
27 #include "builder-wrapper.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 static void psppire_dialog_action_rank_class_init      (PsppireDialogActionRankClass *class);
34
35 G_DEFINE_TYPE (PsppireDialogActionRank, psppire_dialog_action_rank, PSPPIRE_TYPE_DIALOG_ACTION);
36
37
38 static char *
39 generate_syntax (PsppireDialogAction *act)
40 {
41   PsppireDialogActionRank *rd  = PSPPIRE_DIALOG_ACTION_RANK (act);
42
43   gchar *text = NULL;
44   GtkTreeModel *gs = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
45
46   GtkTreeIter notused;
47
48   GString *str = g_string_new ("RANK VARIABLES=");
49
50   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->rank_vars), 0, str);
51
52   g_string_append_printf (str, " (%c)",
53                    gtk_toggle_button_get_active (rd->ascending_togglebutton)
54                    ?'A':'D');
55
56   if (  gtk_tree_model_get_iter_first (gs, &notused) )
57     {
58       g_string_append (str, "\n\tBY ");
59
60       psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->group_vars),  0, str);
61     }
62
63   g_string_append (str, "\n\t/PRINT = ");
64   if (gtk_toggle_button_get_active (rd->summary_togglebutton))
65     g_string_append (str, "YES");
66   else
67     g_string_append (str, "NO");
68
69
70   if (gtk_toggle_button_get_active (rd->func_button [RANK]))
71     g_string_append (str, "\n\t/RANK");
72   if (gtk_toggle_button_get_active (rd->func_button [NORMAL]))
73     g_string_append (str, "\n\t/NORMAL");
74   if (gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
75     g_string_append (str, "\n\t/PROPORTION");
76   if (gtk_toggle_button_get_active (rd->func_button [PERCENT]))
77     g_string_append (str, "\n\t/PERCENT");
78   if (gtk_toggle_button_get_active (rd->func_button [RFRACTION]))
79     g_string_append (str, "\n\t/RFRACTION");
80   if (gtk_toggle_button_get_active (rd->func_button [N]))
81     g_string_append (str, "\n\t/N");
82   if (gtk_toggle_button_get_active (rd->func_button [SAVAGE]))
83     g_string_append (str, "\n\t/SAVAGE");
84   if (gtk_toggle_button_get_active (rd->func_button [NTILES]))
85     {
86       gint n = gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->ntiles_entry));
87       g_string_append_printf (str, "\n\t/NTILES(%d)", n);
88     }
89
90
91   if (gtk_toggle_button_get_active (rd->func_button [NORMAL])
92       ||
93       gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
94     {
95       g_string_append (str, "\n\t/FRACTION=");
96
97       if ( gtk_toggle_button_get_active (rd->blom))
98         g_string_append (str, "BLOM");
99       else if ( gtk_toggle_button_get_active (rd->tukey))
100         g_string_append (str, "TUKEY");
101       else if ( gtk_toggle_button_get_active (rd->rankit))
102         g_string_append (str, "RANKIT");
103       else if ( gtk_toggle_button_get_active (rd->vw))
104         g_string_append (str, "VW");
105     }
106
107   g_string_append (str, "\n\t/TIES=");
108   if ( gtk_toggle_button_get_active (rd->mean))
109     g_string_append (str, "MEAN");
110   else if ( gtk_toggle_button_get_active (rd->low))
111     g_string_append (str, "LOW");
112   else if ( gtk_toggle_button_get_active (rd->high))
113     g_string_append (str, "HIGH");
114   else if ( gtk_toggle_button_get_active (rd->condense))
115     g_string_append (str, "CONDENSE");
116
117
118   g_string_append (str, ".");
119   text = str->str;
120
121   g_string_free (str, FALSE);
122
123   return text;
124 }
125
126 static gboolean
127 dialog_state_valid (PsppireDialogAction *da)
128 {
129   PsppireDialogActionRank *dar  = PSPPIRE_DIALOG_ACTION_RANK (da);
130   GtkTreeIter notused;
131   GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (dar->rank_vars));
132
133   return gtk_tree_model_get_iter_first (vars, &notused);
134 }
135
136 static void
137 dialog_refresh (PsppireDialogAction *act)
138 {
139   PsppireDialogActionRank *dar  = PSPPIRE_DIALOG_ACTION_RANK (act);
140
141   GtkTreeModel *liststore;
142
143   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dar->rank_vars));
144   gtk_list_store_clear (GTK_LIST_STORE (liststore));
145
146   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dar->group_vars));
147   gtk_list_store_clear (GTK_LIST_STORE (liststore));
148
149   gtk_toggle_button_set_active (dar->ascending_togglebutton, TRUE);
150   gtk_toggle_button_set_active (dar->summary_togglebutton, FALSE);
151 }
152
153 static void
154 types_dialog_reset (PsppireDialogActionRank *rd)
155 {
156   gint i;
157
158   for (i = 0 ; i < n_RANK_FUNCS ; ++i )
159     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->func_button [i]),
160                                   FALSE);
161
162   gtk_widget_set_sensitive (rd->ntiles_entry, FALSE);
163
164   gtk_widget_set_sensitive (rd->formula_box, FALSE);
165 }
166
167 static void
168 run_types_dialog (GtkButton *b, PsppireDialogActionRank *dar)
169 {
170   PsppireDialogAction *act  = PSPPIRE_DIALOG_ACTION (dar);
171
172   gint response;
173
174   gtk_window_set_transient_for (GTK_WINDOW (dar->types_dialog),
175                                 GTK_WINDOW (act->dialog));
176
177   types_dialog_reset (dar);
178
179   response = psppire_dialog_run (PSPPIRE_DIALOG (dar->types_dialog));
180 }
181
182 static void
183 run_ties_dialog (GtkButton *b,  PsppireDialogActionRank *dar)
184 {
185   PsppireDialogAction *act  = PSPPIRE_DIALOG_ACTION (dar);
186
187   gint response;
188
189   gtk_window_set_transient_for (GTK_WINDOW (dar->ties_dialog),
190                                 GTK_WINDOW (act->dialog));
191
192   response = psppire_dialog_run (PSPPIRE_DIALOG (dar->ties_dialog));
193 }
194
195 static void
196 on_ntiles_toggle (GtkToggleButton *toggle_button, PsppireDialogActionRank *dar)
197 {
198   gboolean active = gtk_toggle_button_get_active (toggle_button);
199   gtk_widget_set_sensitive (GTK_WIDGET (dar), active);
200 }
201
202 static void
203 set_sensitivity (PsppireDialogActionRank *dar)
204 {
205   gboolean sens = gtk_toggle_button_get_active
206     (GTK_TOGGLE_BUTTON (dar->func_button[PROPORTION]))
207     ||
208     gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dar->func_button[NORMAL]));
209
210   gtk_widget_set_sensitive (dar->formula_box, sens);
211 }
212
213 static void
214 psppire_dialog_action_rank_activate (GtkAction *a)
215 {
216   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
217   PsppireDialogActionRank *act = PSPPIRE_DIALOG_ACTION_RANK (a);
218
219   GtkBuilder *xml = builder_new ("rank.ui");
220
221   GtkWidget *types_button = get_widget_assert (xml, "button1");
222   GtkWidget *ties_button = get_widget_assert (xml, "button2");
223
224   pda->dialog    = get_widget_assert   (xml, "rank-dialog");
225   pda->source    = get_widget_assert   (xml, "dict-treeview");
226   act->rank_vars = get_widget_assert   (xml, "variables-treeview");
227   act->group_vars =  get_widget_assert (xml, "group-vars-treeview");
228   act->ascending_togglebutton =
229     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
230
231   act->summary_togglebutton =
232     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "summary-checkbutton"));
233
234   act->types_dialog = get_widget_assert (xml, "rank-types-dialog");
235
236
237   act->ntiles_entry  = get_widget_assert (xml, "ntiles-entry");
238
239   act->func_button[RANK]    =
240     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rank-checkbutton"));
241
242   act->func_button[SAVAGE]  =
243     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "savage-checkbutton"));
244
245   act->func_button[RFRACTION] =
246     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rfrac-checkbutton"));
247
248   act->func_button[PERCENT] =
249     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "percent-checkbutton"));
250
251   act->func_button[N]       =
252     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "sum-checkbutton"));
253
254   act->func_button[NTILES] =
255     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ntiles-checkbutton"));
256
257   act->func_button[PROPORTION] =
258     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "prop-checkbutton"));
259
260   act->func_button[NORMAL] =
261     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "normal-checkbutton"));
262
263   act->formula_box = get_widget_assert (xml, "formula-frame");
264
265   act->blom = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "blom-button"));
266   act->tukey = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "tukey-button"));
267   act->rankit = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rankit-button"));
268   act->vw = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "vw-button"));
269
270   /* Ties dialog */
271   act->ties_dialog = PSPPIRE_DIALOG (get_widget_assert (xml, "ties-dialog"));
272
273   act->mean = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "mean-button"));
274   act->low = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "low-button"));
275   act->high = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "high-button"));
276   act->condense = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "condense-button"));
277
278   g_signal_connect_swapped (act->func_button[PROPORTION], "toggled",
279                             G_CALLBACK (set_sensitivity),
280                             act);
281
282   g_signal_connect_swapped (act->func_button[NORMAL], "toggled",
283                             G_CALLBACK (set_sensitivity),
284                             act);
285
286   g_signal_connect (types_button, "clicked",
287                     G_CALLBACK (run_types_dialog),  act);
288
289   g_signal_connect (ties_button, "clicked",
290                     G_CALLBACK (run_ties_dialog),  act);
291
292   g_signal_connect (act->func_button[NTILES], "toggled",
293                     G_CALLBACK (on_ntiles_toggle),
294                     act->ntiles_entry);
295
296   psppire_dialog_action_set_valid_predicate (pda, (void *) dialog_state_valid);
297   psppire_dialog_action_set_refresh (pda, dialog_refresh);
298
299   PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_rank_parent_class)->activate (pda);
300   
301   g_object_unref (xml);
302 }
303
304 static void
305 psppire_dialog_action_rank_class_init (PsppireDialogActionRankClass *class)
306 {
307   GTK_ACTION_CLASS (class)->activate = psppire_dialog_action_rank_activate;
308
309   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
310 }
311
312 static void
313 psppire_dialog_action_rank_init (PsppireDialogActionRank *act)
314 {
315 }