1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2010, 2011, 2012, 2013, 2014  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-chisquare.h"
21 
22 #include <float.h>
23 
24 #include "psppire-var-view.h"
25 
26 #include "psppire-dialog.h"
27 #include "builder-wrapper.h"
28 #include "psppire-acr.h"
29 #include "dialog-common.h"
30 
31 #include <libpspp/str.h>
32 
33 static void psppire_dialog_action_chisquare_init            (PsppireDialogActionChisquare      *act);
34 static void psppire_dialog_action_chisquare_class_init      (PsppireDialogActionChisquareClass *class);
35 
36 G_DEFINE_TYPE (PsppireDialogActionChisquare, psppire_dialog_action_chisquare, PSPPIRE_TYPE_DIALOG_ACTION);
37 
38 
39 static char *
generate_syntax(const PsppireDialogAction * act)40 generate_syntax (const PsppireDialogAction *act)
41 {
42   PsppireDialogActionChisquare *scd = PSPPIRE_DIALOG_ACTION_CHISQUARE (act);
43 
44   gchar *text;
45   struct string dss;
46 
47   ds_init_cstr (&dss, "NPAR TEST\n\t/CHISQUARE=");
48 
49   psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (scd->var_view), 0, &dss);
50 
51   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->range_button)))
52     {
53       ds_put_cstr (&dss, "(");
54 
55       ds_put_cstr (&dss,
56 		       gtk_entry_get_text (GTK_ENTRY (scd->value_lower)));
57 
58       ds_put_cstr (&dss, ", ");
59 
60       ds_put_cstr (&dss,
61 		       gtk_entry_get_text (GTK_ENTRY (scd->value_upper)));
62 
63       ds_put_cstr (&dss, ")");
64     }
65 
66   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->values_button)))
67     {
68       GtkListStore *ls = scd->expected_list;
69       GtkTreeIter iter;
70       gboolean ok;
71 
72       ds_put_cstr (&dss, "\n\t");
73       ds_put_cstr (&dss, "/EXPECTED = ");
74 
75 
76       for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
77 					       &iter);
78  	   ok;
79 	   ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
80 	{
81 	  gdouble v;
82 
83 	  gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
84 
85 	  ds_put_c_format (&dss, " %.*g", DBL_DIG + 1, v);
86 	}
87     }
88 
89   ds_put_cstr (&dss, ".\n");
90 
91   text = ds_steal_cstr (&dss);
92 
93   ds_destroy (&dss);
94 
95   return text;
96 }
97 
98 
99 static gboolean
dialog_state_valid(gpointer a)100 dialog_state_valid (gpointer a)
101 {
102   GtkTreeIter notused;
103   PsppireDialogActionChisquare *act = PSPPIRE_DIALOG_ACTION_CHISQUARE (a);
104 
105   GtkTreeModel *vars =
106     gtk_tree_view_get_model (GTK_TREE_VIEW (act->var_view));
107 
108   if (!gtk_tree_model_get_iter_first (vars, &notused))
109     return FALSE;
110 
111   return TRUE;
112 }
113 
114 static void
refresh(PsppireDialogAction * rd_)115 refresh (PsppireDialogAction *rd_)
116 {
117   PsppireDialogActionChisquare *csd = PSPPIRE_DIALOG_ACTION_CHISQUARE (rd_);
118 
119   GtkTreeModel *liststore =
120     gtk_tree_view_get_model (GTK_TREE_VIEW (csd->var_view));
121 
122   gtk_list_store_clear (GTK_LIST_STORE (liststore));
123 
124   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (csd->button1), TRUE);
125 
126   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (csd->button2), TRUE);
127   gtk_entry_set_text (GTK_ENTRY (csd->value_lower), "");
128   gtk_entry_set_text (GTK_ENTRY (csd->value_upper), "");
129 }
130 
131 static GtkBuilder *
psppire_dialog_action_chisquare_activate(PsppireDialogAction * a,GVariant * param)132 psppire_dialog_action_chisquare_activate (PsppireDialogAction *a, GVariant *param)
133 {
134   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
135   PsppireDialogActionChisquare *act = PSPPIRE_DIALOG_ACTION_CHISQUARE (a);
136 
137   GtkBuilder *xml = builder_new ("chi-square.ui");
138 
139   GtkWidget *range_table = get_widget_assert   (xml, "range-table");
140   GtkWidget *values_acr = get_widget_assert   (xml, "psppire-acr1");
141   GtkWidget *expected_value_entry =
142     get_widget_assert   (xml, "expected-value-entry");
143 
144   pda->dialog = get_widget_assert   (xml, "chisquare-dialog");
145   pda->source = get_widget_assert   (xml, "dict-view");
146 
147   act->expected_list = gtk_list_store_new (1, G_TYPE_DOUBLE);
148 
149   act->button1 = get_widget_assert   (xml, "radiobutton1");
150   act->button2 = get_widget_assert   (xml, "radiobutton3");
151   act->var_view = get_widget_assert   (xml, "variables-treeview");
152 
153   act->range_button = get_widget_assert   (xml, "radiobutton4");
154   act->value_lower = get_widget_assert   (xml, "entry1");
155   act->value_upper = get_widget_assert   (xml, "entry2");
156 
157   act->values_button = get_widget_assert   (xml, "radiobutton2");
158 
159   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
160   psppire_dialog_action_set_refresh (pda, refresh);
161 
162   g_signal_connect (act->range_button, "toggled",
163 		    G_CALLBACK (set_sensitivity_from_toggle),
164 		    range_table);
165 
166 
167   g_signal_connect (act->values_button, "toggled",
168 		    G_CALLBACK (set_sensitivity_from_toggle),
169 		    values_acr);
170 
171   g_signal_connect (act->values_button, "toggled",
172 		    G_CALLBACK (set_sensitivity_from_toggle),
173 		    expected_value_entry);
174 
175   psppire_acr_set_entry (PSPPIRE_ACR (values_acr),
176 			 GTK_ENTRY (expected_value_entry));
177 
178   psppire_acr_set_model(PSPPIRE_ACR (values_acr), act->expected_list);
179 
180   return xml;
181 }
182 
183 static void
psppire_dialog_action_chisquare_class_init(PsppireDialogActionChisquareClass * class)184 psppire_dialog_action_chisquare_class_init (PsppireDialogActionChisquareClass *class)
185 {
186   PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_chisquare_activate;
187   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
188 }
189 
190 
191 static void
psppire_dialog_action_chisquare_init(PsppireDialogActionChisquare * act)192 psppire_dialog_action_chisquare_init (PsppireDialogActionChisquare *act)
193 {
194 }
195 
196