Reliability dialog: Add checkbox for summary subcommand
[pspp] / src / ui / gui / reliability-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009  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 #include <config.h>
18
19 #include "dialog-common.h"
20 #include <language/syntax-string-source.h>
21 #include "reliability-dialog.h"
22 #include "psppire-selector.h"
23 #include "psppire-dictview.h"
24 #include "psppire-dialog.h"
25
26 #include "psppire-data-window.h"
27 #include "psppire-var-view.h"
28
29 #include "executor.h"
30 #include "helper.h"
31
32 #include <gtk/gtk.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 struct reliability
40 {
41   PsppireDict *dict;
42   GtkWidget *model_combo;
43   GtkWidget *variables;
44   GtkWidget *split_point_hbox;
45   GtkWidget *split_spinbutton;
46   GtkWidget *scale_if_item_deleted_checkbutton;
47 };
48
49
50 static char * generate_syntax (const struct reliability *rd);
51
52
53 static void
54 on_vars_changed (struct reliability *rd)
55 {
56   GtkTreeModel *tm =
57     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
58
59   gint n_vars = gtk_tree_model_iter_n_children (tm, NULL);
60
61   gint current_value =
62     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (rd->split_spinbutton));
63
64   gint new_value = current_value;
65
66   gtk_spin_button_set_range (GTK_SPIN_BUTTON (rd->split_spinbutton),
67                              0, n_vars - 1);
68
69   if ( current_value > n_vars - 1)
70     new_value = n_vars - 1;
71
72   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rd->split_spinbutton),
73                              new_value);
74 }
75
76 static void
77 on_method_change (struct reliability *rd)
78 {
79   gtk_widget_set_sensitive (rd->split_point_hbox,
80                             ( 1 == gtk_combo_box_get_active (GTK_COMBO_BOX (rd->model_combo))));
81
82 }
83
84 static void
85 refresh (PsppireDialog *dialog, struct reliability *rd)
86 {
87   GtkTreeModel *liststore =
88     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
89   gtk_list_store_clear (GTK_LIST_STORE (liststore));
90
91   gtk_combo_box_set_active (GTK_COMBO_BOX (rd->model_combo), 0);
92
93   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rd->split_spinbutton), 0);
94
95   gtk_spin_button_set_range (GTK_SPIN_BUTTON (rd->split_spinbutton),
96                              0, 0);
97
98   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->scale_if_item_deleted_checkbutton),
99                                 FALSE);
100 }
101
102
103 static gboolean
104 dialog_state_valid (gpointer data)
105 {
106   struct reliability *rd = data;
107
108   GtkTreeModel *liststore =
109     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
110
111   return (2 <= gtk_tree_model_iter_n_children (liststore, NULL));
112 }
113
114
115 /* Pops up the Reliability dialog box */
116 void
117 reliability_dialog (PsppireDataWindow *de)
118 {
119   struct reliability rd;
120   gint response;
121
122   GtkBuilder *xml = builder_new ("reliability.ui");
123   PsppireVarStore *vs;
124
125   GtkWidget *dialog = get_widget_assert   (xml, "reliability-dialog");
126   GtkWidget *source = get_widget_assert   (xml, "dict-view");
127
128   rd.split_point_hbox = get_widget_assert (xml, "split-point-hbox");
129
130   rd.variables = get_widget_assert   (xml, "treeview2");
131
132   rd.model_combo = get_widget_assert   (xml, "combobox1");
133   rd.split_spinbutton = get_widget_assert (xml, "spinbutton1");
134
135   rd.scale_if_item_deleted_checkbutton = get_widget_assert (xml, "totals-checkbutton");
136
137   g_signal_connect_swapped (rd.model_combo, "changed",
138                             G_CALLBACK (on_method_change), &rd);
139
140   g_object_get (de->data_editor, "var-store", &vs, NULL);
141
142   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
143
144   g_object_get (vs, "dictionary", &rd.dict, NULL);
145   g_object_set (source, "model", rd.dict, NULL);
146
147   {
148     GtkTreeModel *tm =
149       gtk_tree_view_get_model (GTK_TREE_VIEW (rd.variables));
150
151
152     g_signal_connect_swapped (tm, "row-inserted",
153                       G_CALLBACK (on_vars_changed), &rd);
154
155     g_signal_connect_swapped (tm, "row-deleted",
156                       G_CALLBACK (on_vars_changed), &rd);
157   }
158
159   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &rd);
160
161   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
162                                       dialog_state_valid, &rd);
163
164   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
165
166
167   switch (response)
168     {
169     case GTK_RESPONSE_OK:
170       {
171         gchar *syntax = generate_syntax (&rd);
172
173         struct getl_interface *sss = create_syntax_string_source (syntax);
174         execute_syntax (sss);
175
176         g_free (syntax);
177       }
178       break;
179     case PSPPIRE_RESPONSE_PASTE:
180       {
181         gchar *syntax = generate_syntax (&rd);
182         paste_syntax_to_window (syntax);
183
184         g_free (syntax);
185       }
186       break;
187     default:
188       break;
189     }
190
191   g_object_unref (xml);
192 }
193
194
195 \f
196
197 static char *
198 generate_syntax (const struct reliability *rd)
199 {
200   gchar *text;
201   GString *string = g_string_new ("RELIABILITY");
202
203   g_string_append (string, "\n\t/VARIABLES=");
204   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
205
206
207   g_string_append (string, "\n\t/MODEL=");
208
209   if ( 0 == gtk_combo_box_get_active (GTK_COMBO_BOX (rd->model_combo)))
210     g_string_append (string, "ALPHA");
211   else
212     g_string_append_printf (string, "SPLIT (%d)",
213                             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (rd->split_spinbutton))
214                             );
215
216   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->scale_if_item_deleted_checkbutton)))
217     g_string_append (string, "\n\t/SUMMARY = TOTAL");
218
219   g_string_append (string, ".\n");
220
221   text = string->str;
222
223   g_string_free (string, FALSE);
224
225   return text;
226 }