Examine gui: remove var-is-numeric predicate from dict-view
[pspp] / src / ui / gui / psppire-dialog-action-examine.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 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-examine.h"
21
22 #include "psppire-var-view.h"
23 #include "psppire-dict.h"
24 #include "psppire-dialog.h"
25 #include "builder-wrapper.h"
26
27 #include "gettext.h"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31 static void psppire_dialog_action_examine_class_init      (PsppireDialogActionExamineClass *class);
32
33 G_DEFINE_TYPE (PsppireDialogActionExamine, psppire_dialog_action_examine, PSPPIRE_TYPE_DIALOG_ACTION);
34
35
36 #define     STAT_DESCRIPTIVES  0x01
37 #define     STAT_EXTREMES      0x02
38 #define     STAT_PERCENTILES   0x04
39
40 static void
41 run_stats_dialog (PsppireDialogActionExamine *ed)
42 {
43   gint response;
44
45   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->descriptives_button),
46                                 ed->stats & STAT_DESCRIPTIVES);
47
48   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->extremes_button),
49                                 ed->stats & STAT_EXTREMES);
50
51   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->percentiles_button),
52                                 ed->stats & STAT_PERCENTILES);
53
54   response = psppire_dialog_run (PSPPIRE_DIALOG (ed->stats_dialog));
55
56   if ( response == PSPPIRE_RESPONSE_CONTINUE )
57     {
58       ed->stats = 0;
59       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->descriptives_button) ))
60         ed->stats |= STAT_DESCRIPTIVES;
61
62       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->extremes_button) ))
63         ed->stats |= STAT_EXTREMES;
64
65       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->percentiles_button) ))
66         ed->stats |= STAT_PERCENTILES;
67     }
68 }
69
70 static void
71 run_opts_dialog (PsppireDialogActionExamine *ed)
72 {
73   gint response;
74
75   switch (ed->opts)
76     {
77     case OPT_LISTWISE:
78       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->listwise), TRUE);
79       break;
80     case OPT_PAIRWISE:
81       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->pairwise), TRUE);
82       break;
83     case OPT_REPORT:
84       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->report), TRUE);
85       break;
86     default:
87       g_assert_not_reached ();
88       break;
89     };
90
91   response = psppire_dialog_run (PSPPIRE_DIALOG (ed->opts_dialog));
92
93   if ( response == PSPPIRE_RESPONSE_CONTINUE )
94     {
95       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->listwise)))
96         ed->opts = OPT_LISTWISE;
97       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->pairwise)))
98         ed->opts = OPT_PAIRWISE;
99       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->report)))
100         ed->opts = OPT_REPORT;
101   }
102 }
103
104
105
106
107 static char *
108 generate_syntax (PsppireDialogAction *act)
109 {
110   PsppireDialogActionExamine *ed  = PSPPIRE_DIALOG_ACTION_EXAMINE (act);
111
112   const char *label;
113   gchar *text = NULL;
114   GString *str = g_string_new ("EXAMINE ");
115
116   g_string_append (str, "\n\t/VARIABLES=");
117   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (ed->variables), 0, str);
118
119   if ( 0  < gtk_tree_model_iter_n_children
120        (gtk_tree_view_get_model (GTK_TREE_VIEW (ed->factors)), NULL))
121     {
122       g_string_append (str, "\n\tBY ");
123       psppire_var_view_append_names (PSPPIRE_VAR_VIEW (ed->factors), 0, str);
124     }
125
126   label = gtk_entry_get_text (GTK_ENTRY (ed->id_var));
127   if ( 0 != strcmp (label, "") )
128     {
129       g_string_append (str, "\n\t/ID = ");
130       g_string_append (str, label);
131     }
132
133   if ( ed->stats & (STAT_DESCRIPTIVES | STAT_EXTREMES))
134     {
135       g_string_append (str, "\n\t/STATISTICS =");
136
137       if ( ed->stats & STAT_DESCRIPTIVES)
138         g_string_append (str, " DESCRIPTIVES");
139
140       if ( ed->stats & STAT_EXTREMES)
141         g_string_append (str, " EXTREME");
142     }
143
144   if ( ed->stats & STAT_PERCENTILES)
145     g_string_append (str, "\n\t/PERCENTILES");
146
147
148   g_string_append (str, "\n\t/MISSING=");
149   switch (ed->opts)
150     {
151     case OPT_REPORT:
152       g_string_append (str, "REPORT");
153       break;
154     case OPT_PAIRWISE:
155       g_string_append (str, "PAIRWISE");
156       break;
157     default:
158       g_string_append (str, "LISTWISE");
159       break;
160     };
161
162   g_string_append (str, ".");
163   text = str->str;
164
165   g_string_free (str, FALSE);
166
167   return text;
168 }
169
170 static gboolean
171 dialog_state_valid (PsppireDialogAction *da)
172 {
173   PsppireDialogActionExamine *pae  = PSPPIRE_DIALOG_ACTION_EXAMINE (da);
174   GtkTreeIter notused;
175   GtkTreeModel *vars =
176     gtk_tree_view_get_model (GTK_TREE_VIEW (pae->variables));
177
178   return gtk_tree_model_get_iter_first (vars, &notused);
179 }
180
181 static void
182 dialog_refresh (PsppireDialogAction *da)
183 {
184   PsppireDialogActionExamine *dae  = PSPPIRE_DIALOG_ACTION_EXAMINE (da);
185   GtkTreeModel *liststore = NULL;
186
187   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dae->variables));
188   gtk_list_store_clear (GTK_LIST_STORE (liststore));
189
190   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dae->factors));
191   gtk_list_store_clear (GTK_LIST_STORE (liststore));
192
193   gtk_entry_set_text (GTK_ENTRY (dae->id_var), "");
194   dae->stats = 0x00;
195   dae->opts = OPT_LISTWISE;
196 }
197
198 static void
199 psppire_dialog_action_examine_activate (GtkAction *a)
200 {
201   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
202   PsppireDialogActionExamine *act = PSPPIRE_DIALOG_ACTION_EXAMINE (a);
203
204   GtkBuilder *xml = builder_new ("examine.ui");
205
206   GtkWidget *stats_button = get_widget_assert (xml, "stats-button");
207   GtkWidget *opts_button = get_widget_assert (xml, "opts-button");
208
209   pda->dialog    = get_widget_assert   (xml, "examine-dialog");
210   pda->source    = get_widget_assert   (xml, "treeview1");
211   act->variables = get_widget_assert   (xml, "treeview2");
212   act->factors   = get_widget_assert   (xml, "treeview3");
213   act->id_var    = get_widget_assert   (xml, "entry1");
214
215   act->stats_dialog        = get_widget_assert (xml, "statistics-dialog");
216   act->descriptives_button = get_widget_assert (xml, "descriptives-button");
217   act->extremes_button     = get_widget_assert (xml, "extremes-button"); 
218   act->percentiles_button  = get_widget_assert (xml, "percentiles-button");
219
220   act->opts_dialog = get_widget_assert (xml, "options-dialog");
221   act->listwise    = get_widget_assert (xml, "radiobutton1");
222   act->pairwise    = get_widget_assert (xml, "radiobutton2");
223   act->report      = get_widget_assert (xml, "radiobutton3");
224
225   g_object_set (pda->source,
226                 "model", pda->dict,
227                 NULL);
228
229   psppire_dialog_action_set_valid_predicate (pda, (void *) dialog_state_valid);
230   psppire_dialog_action_set_refresh (pda, dialog_refresh);
231
232   g_signal_connect_swapped (stats_button, "clicked",
233                     G_CALLBACK (run_stats_dialog), act);
234
235   g_signal_connect_swapped (opts_button, "clicked",
236                             G_CALLBACK (run_opts_dialog), act);
237
238   PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_examine_parent_class)->activate (pda);
239 }
240
241 static void
242 psppire_dialog_action_examine_class_init (PsppireDialogActionExamineClass *class)
243 {
244   GTK_ACTION_CLASS (class)->activate = psppire_dialog_action_examine_activate;
245
246   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
247 }
248
249 static void
250 psppire_dialog_action_examine_init (PsppireDialogActionExamine *act)
251 {
252 }