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