Added rudimentary version of the Logistic Regression Dialog
[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 gboolean
45 dialog_state_valid (gpointer data)
46 {
47   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (data);
48
49   const gchar *text = gtk_entry_get_text (GTK_ENTRY (rd->dep_var));
50
51   GtkTreeModel *indep_vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
52
53   GtkTreeIter notused;
54
55   return 0 != strcmp ("", text) &&
56     gtk_tree_model_get_iter_first (indep_vars, &notused);
57 }
58
59 static void
60 refresh (PsppireDialogAction *rd_)
61 {
62   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (rd_);
63
64   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->indep_vars));
65   gtk_list_store_clear (GTK_LIST_STORE (liststore));
66
67   gtk_entry_set_text (GTK_ENTRY (rd->dep_var), "");
68 }
69
70
71 static void
72 psppire_dialog_action_logistic_activate (GtkAction *a)
73 {
74   PsppireDialogActionLogistic *act = PSPPIRE_DIALOG_ACTION_LOGISTIC (a);
75   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
76
77   GtkBuilder *xml = builder_new ("logistic.ui");
78
79   pda->dialog = get_widget_assert   (xml, "logistic-dialog");
80   pda->source = get_widget_assert   (xml, "dict-view");
81
82   act->dep_var  = get_widget_assert   (xml, "dependent-entry");
83   act->indep_vars  = get_widget_assert   (xml, "indep-view");
84
85   g_object_unref (xml);
86
87   psppire_dialog_action_set_refresh (pda, refresh);
88
89   psppire_dialog_action_set_valid_predicate (pda,
90                                         dialog_state_valid);
91
92   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_logistic_parent_class)->activate)
93     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_logistic_parent_class)->activate (pda);
94 }
95
96
97
98 static char *
99 generate_syntax (PsppireDialogAction *a)
100 {
101   PsppireDialogActionLogistic *rd = PSPPIRE_DIALOG_ACTION_LOGISTIC (a);
102   gchar *text = NULL;
103
104   GString *string = g_string_new ("LOGISTIC REGRESSION ");
105
106   const gchar *dep = gtk_entry_get_text (GTK_ENTRY (rd->dep_var));
107
108   g_string_append (string, dep);
109
110   g_string_append (string, " WITH ");
111
112   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->indep_vars), 0, string);
113
114   g_string_append (string, ".\n");
115
116   text = string->str;
117
118   g_string_free (string, FALSE);
119
120   return text;
121 }
122
123 static void
124 psppire_dialog_action_logistic_class_init (PsppireDialogActionLogisticClass *class)
125 {
126   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
127
128   action_class->activate = psppire_dialog_action_logistic_activate;
129
130   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
131 }
132
133
134 static void
135 psppire_dialog_action_logistic_init (PsppireDialogActionLogistic *act)
136 {
137 }
138