Logistic Regression: Added options dialog box
[pspp] / src / ui / gui / psppire-dialog-action-logistic.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 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-logistic.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include "helper.h"
25 #include <ui/syntax-gen.h>
26 #include "psppire-var-view.h"
27
28 #include "psppire-dialog.h"
29 #include "builder-wrapper.h"
30 #include "checkbox-treeview.h"
31 #include "psppire-dict.h"
32 #include "libpspp/str.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 static void
40 psppire_dialog_action_logistic_class_init (PsppireDialogActionLogisticClass *class);
41
42 G_DEFINE_TYPE (PsppireDialogActionLogistic, psppire_dialog_action_logistic, PSPPIRE_TYPE_DIALOG_ACTION);
43
44 static void
45 set_sensitivity_from_toggle (GtkToggleButton *togglebutton,  gpointer data)
46 {
47   GtkWidget *w = data;
48   gboolean active = gtk_toggle_button_get_active (togglebutton);
49
50   gtk_widget_set_sensitive (w, active);
51 }
52
53
54 static gboolean
55 dialog_state_valid (gpointer data)
56 {
57   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (data);
58
59   const gchar *text = gtk_entry_get_text (GTK_ENTRY (rd->dep_var));
60
61   GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
62
63   GtkTreeIter notused;
64
65   return 0 != strcmp ("", text) &&
66     gtk_tree_model_get_iter_first (indep_vars, &notused);
67 }
68
69 static void
70 refresh (PsppireDialogAction *rd_)
71 {
72   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (rd_);
73
74   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
75   gtk_list_store_clear (GTK_LIST_STORE (liststore));
76
77   gtk_entry_set_text (GTK_ENTRY (rd->dep_var), "");
78 }
79
80
81 static void
82 on_opts_clicked (PsppireDialogActionLogistic *act)
83 {
84   int ret;
85
86   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), act->conf);
87   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->conf_entry), act->conf_level);
88   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->const_checkbox), act->constant);
89
90   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->cut_point_entry), act->cut_point);
91   gtk_spin_button_set_value (GTK_SPIN_BUTTON (act->iterations_entry), act->max_iterations);
92
93   
94   ret = psppire_dialog_run (PSPPIRE_DIALOG (act->opts_dialog));
95
96   if ( ret == PSPPIRE_RESPONSE_CONTINUE )
97     {
98       act->conf = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(act->conf_checkbox));
99       act->conf_level = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->conf_entry));
100       
101       act->constant = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(act->const_checkbox));
102
103       act->cut_point = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->cut_point_entry));
104       act->max_iterations = gtk_spin_button_get_value (GTK_SPIN_BUTTON (act->iterations_entry));
105     }
106 }
107
108
109 static void
110 psppire_dialog_action_logistic_activate (GtkAction *a)
111 {
112   PsppireDialogActionLogistic *act = PSPPIRE_DIALOG_ACTION_LOGISTIC (a);
113   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
114   GtkWidget *opts_button;
115
116   GtkBuilder *xml = builder_new ("logistic.ui");
117
118   pda->dialog = get_widget_assert   (xml, "logistic-dialog");
119   pda->source = get_widget_assert   (xml, "dict-view");
120   act->cut_point = 0.5;
121   act->max_iterations = 20;
122   act->constant = true;
123   act->conf = false;
124   act->conf_level = 95;
125
126   act->dep_var  = get_widget_assert   (xml, "dependent-entry");
127   act->indep_vars  = get_widget_assert   (xml, "indep-view");
128   act->opts_dialog = get_widget_assert (xml, "options-dialog");
129   act->conf_checkbox = get_widget_assert (xml, "checkbutton2");
130   act->conf_entry = get_widget_assert (xml, "spinbutton1");
131   act->const_checkbox = get_widget_assert (xml, "checkbutton1");
132
133   act->iterations_entry = get_widget_assert (xml, "spinbutton3");
134   act->cut_point_entry = get_widget_assert (xml, "spinbutton2");
135
136   opts_button = get_widget_assert (xml, "options-button");
137
138   g_signal_connect_swapped (opts_button, "clicked",
139                             G_CALLBACK (on_opts_clicked),  act);
140
141   g_signal_connect (act->conf_checkbox, "toggled",
142                     G_CALLBACK (set_sensitivity_from_toggle),  
143                     act->conf_entry);
144
145   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), TRUE);
146   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(act->conf_checkbox), FALSE);
147
148   g_object_unref (xml);
149
150   psppire_dialog_action_set_refresh (pda, refresh);
151
152   psppire_dialog_action_set_valid_predicate (pda,
153                                         dialog_state_valid);
154
155   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_logistic_parent_class)->activate)
156     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_logistic_parent_class)->activate (pda);
157 }
158
159
160
161 static char *
162 generate_syntax (PsppireDialogAction *a)
163 {
164   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (a);
165   gchar *text = NULL;
166
167   GString *string = g_string_new ("LOGISTIC REGRESSION ");
168
169   const gchar *dep = gtk_entry_get_text (GTK_ENTRY (rd->dep_var));
170
171   g_string_append (string, dep);
172
173   g_string_append (string, " WITH ");
174
175   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
176
177   g_string_append (string, "\n\t/CRITERIA =");
178
179   g_string_append_printf (string, " CUT(%g)", rd->cut_point);
180   g_string_append_printf (string, " ITERATE(%d)", rd->max_iterations);
181
182   if (rd->conf)
183     {
184       g_string_append_printf (string, "\n\t/PRINT = CI(%g)", rd->conf_level);
185     }
186
187   if (rd->constant) 
188     g_string_append (string, "\n\t/NOORIGIN");
189   else
190     g_string_append (string, "\n\t/ORIGIN");
191
192   g_string_append (string, ".\n");
193
194   text = string->str;
195
196   g_string_free (string, FALSE);
197
198   return text;
199 }
200
201 static void
202 psppire_dialog_action_logistic_class_init (PsppireDialogActionLogisticClass *class)
203 {
204   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
205
206   action_class->activate = psppire_dialog_action_logistic_activate;
207
208   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
209 }
210
211
212 static void
213 psppire_dialog_action_logistic_init (PsppireDialogActionLogistic *act)
214 {
215 }
216