fc0bb7be4469cbff46f6acab4954687f65ef33e0
[pspp] / src / ui / gui / psppire-dialog-action-roc.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-roc.h"
21
22 #include "dialog-common.h"
23 #include <ui/syntax-gen.h>
24 #include "psppire-var-view.h"
25
26 #include "psppire-dialog.h"
27 #include "builder-wrapper.h"
28
29 #include "psppire-dict.h"
30 #include "libpspp/str.h"
31
32 static void
33 psppire_dialog_action_roc_class_init (PsppireDialogActionRocClass *class);
34
35 G_DEFINE_TYPE (PsppireDialogActionRoc, psppire_dialog_action_roc, PSPPIRE_TYPE_DIALOG_ACTION);
36
37 static gboolean
38 dialog_state_valid (gpointer data)
39 {
40   PsppireDialogActionRoc *rd = data;
41   const gchar *text;
42
43   GtkTreeModel *liststore =
44     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->test_variables));
45
46   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
47     return FALSE;
48
49   
50   text = gtk_entry_get_text (GTK_ENTRY (rd->state_variable));
51   if ( 0 == strcmp ("", text))
52     return FALSE;
53
54
55   text = gtk_entry_get_text (GTK_ENTRY (rd->state_value));
56   if ( 0 == strcmp ("", text))
57     return FALSE;
58
59
60   return TRUE;
61 }
62
63 static void
64 on_curve_button_toggle (GtkCheckButton *curve, PsppireDialogActionRoc *rd)
65 {
66   if ( !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (curve)))
67     {
68       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->reference)))
69         g_object_set (rd->reference, "inconsistent", TRUE, NULL);
70       g_object_set (rd->reference, "sensitive", FALSE, NULL);
71     }
72   else 
73     {
74       g_object_set (rd->reference, "inconsistent", FALSE, NULL);
75       g_object_set (rd->reference, "sensitive", TRUE, NULL);
76     }
77 }
78
79 static void
80 refresh (PsppireDialogAction *rd_)
81 {
82   PsppireDialogActionRoc *rd = PSPPIRE_DIALOG_ACTION_ROC (rd_);
83   GtkTreeModel *liststore =
84     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->test_variables));
85   gtk_list_store_clear (GTK_LIST_STORE (liststore));
86
87   gtk_entry_set_text (GTK_ENTRY (rd->state_variable), "");
88   gtk_entry_set_text (GTK_ENTRY (rd->state_value), "");
89
90   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->curve),          TRUE);
91   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->reference),      FALSE);
92   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->standard_error), FALSE);
93   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->coordinates),    FALSE);
94 }
95
96 static void
97 psppire_dialog_action_roc_activate (GtkAction *a)
98 {
99   PsppireDialogActionRoc *act = PSPPIRE_DIALOG_ACTION_ROC (a);
100   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
101
102   GtkBuilder *xml = builder_new ("roc.ui");
103   pda->dialog = get_widget_assert   (xml, "roc-dialog");
104   pda->source = get_widget_assert   (xml, "dict-view");
105   pda->source = get_widget_assert   (xml, "dict-view");
106
107   act->test_variables    = get_widget_assert   (xml, "psppire-var-view1");
108   act->state_variable    = get_widget_assert   (xml, "entry1");
109   act->state_value       = get_widget_assert   (xml, "entry2");
110
111   act->curve          = get_widget_assert   (xml, "curve");
112   act->reference      = get_widget_assert   (xml, "reference-line");
113   act->standard_error = get_widget_assert   (xml, "standard-error");
114   act->coordinates    = get_widget_assert   (xml, "co-ordinates");
115
116   g_object_unref (xml);
117
118   g_signal_connect (act->curve, "toggled",
119                     G_CALLBACK (on_curve_button_toggle), act);
120
121   psppire_dialog_action_set_refresh (pda, refresh);
122
123   psppire_dialog_action_set_valid_predicate (pda,
124                                         dialog_state_valid);
125
126   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_roc_parent_class)->activate)
127     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_roc_parent_class)->activate (pda);
128 }
129
130
131
132 static char *
133 generate_syntax (PsppireDialogAction *a)
134 {
135   PsppireDialogActionRoc *rd = PSPPIRE_DIALOG_ACTION_ROC (a);
136   gchar *text;
137   const gchar *var_name = gtk_entry_get_text (GTK_ENTRY (rd->state_variable));
138   GString *string = g_string_new ("ROC");
139
140   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->test_variables), 0, string);
141
142   g_string_append (string, " BY ");
143
144   g_string_append (string, var_name);
145
146   g_string_append (string, " (");
147   {
148     const gchar *value = gtk_entry_get_text (GTK_ENTRY (rd->state_value));
149
150     const struct variable *var = psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION(rd)->dict, var_name);
151
152     g_return_val_if_fail (var, NULL);
153
154     if ( var_is_alpha (var))
155       {
156         struct string str;
157         ds_init_empty (&str);
158         syntax_gen_string (&str, ss_cstr (value));
159         g_string_append (string, ds_cstr (&str));
160         ds_destroy (&str);
161       }
162     else
163       g_string_append (string, value);
164   }
165   g_string_append (string, ")");
166
167
168   /* The /PLOT subcommand */
169   g_string_append (string, "\n\t/PLOT ");
170   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->curve)))
171     {
172       g_string_append (string, "CURVE");
173       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->reference)))
174         g_string_append (string, " (REFERENCE)");
175     }
176   else
177     g_string_append (string, "NONE");
178
179
180   /* The /PRINT subcommand */
181   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->standard_error)) ||
182        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->coordinates)) )
183     {
184       g_string_append (string, "\n\t/PRINT");
185
186       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->standard_error)))
187         g_string_append (string, " SE");
188
189       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->coordinates)))
190         g_string_append (string, " COORDINATES");
191     }
192
193   g_string_append (string, ".\n");
194
195   text = string->str;
196
197   g_string_free (string, FALSE);
198
199   return text;
200 }
201
202 static void
203 psppire_dialog_action_roc_class_init (PsppireDialogActionRocClass *class)
204 {
205   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
206
207   action_class->activate = psppire_dialog_action_roc_activate;
208
209   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
210 }
211
212
213 static void
214 psppire_dialog_action_roc_init (PsppireDialogActionRoc *act)
215 {
216 }
217