PsppireDictView: Change "dictionary" property to "model".
[pspp-builds.git] / 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
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 };
46
47
48 static char * generate_syntax (const struct reliability *rd);
49
50
51 static void
52 on_vars_changed (struct reliability *rd)
53 {
54   GtkTreeModel *tm =
55     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
56
57   gint n_vars = gtk_tree_model_iter_n_children (tm, NULL);
58
59   gint current_value =
60     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (rd->split_spinbutton));
61
62   gint new_value = current_value;
63
64   gtk_spin_button_set_range (GTK_SPIN_BUTTON (rd->split_spinbutton),
65                              0, n_vars - 1);
66
67   if ( current_value > n_vars - 1)
68     new_value = n_vars - 1;
69
70   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rd->split_spinbutton),
71                              new_value);
72 }
73
74 static void
75 on_method_change (struct reliability *rd)
76 {
77   gtk_widget_set_sensitive (rd->split_point_hbox,
78                             ( 1 == gtk_combo_box_get_active (GTK_COMBO_BOX (rd->model_combo))));
79
80 }
81
82 static void
83 refresh (PsppireDialog *dialog, struct reliability *rd)
84 {
85   GtkTreeModel *liststore =
86     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
87   gtk_list_store_clear (GTK_LIST_STORE (liststore));
88
89   gtk_combo_box_set_active (GTK_COMBO_BOX (rd->model_combo), 0);
90
91   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rd->split_spinbutton), 0);
92
93   gtk_spin_button_set_range (GTK_SPIN_BUTTON (rd->split_spinbutton),
94                              0, 0);
95 }
96
97
98 static gboolean
99 dialog_state_valid (gpointer data)
100 {
101   struct reliability *rd = data;
102
103   GtkTreeModel *liststore =
104     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
105
106   return (2 <= gtk_tree_model_iter_n_children (liststore, NULL));
107 }
108
109
110 /* Pops up the Reliability dialog box */
111 void
112 reliability_dialog (GObject *o, gpointer data)
113 {
114   struct reliability rd;
115   gint response;
116
117   GtkBuilder *xml = builder_new ("reliability.ui");
118   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
119   PsppireVarStore *vs;
120
121   GtkWidget *dialog = get_widget_assert   (xml, "reliability-dialog");
122   GtkWidget *source = get_widget_assert   (xml, "dict-view");
123
124   GtkWidget *selector = get_widget_assert (xml, "psppire-selector1");
125
126   rd.split_point_hbox = get_widget_assert (xml, "split-point-hbox");
127
128   rd.variables = get_widget_assert   (xml, "treeview2");
129
130   rd.model_combo = get_widget_assert   (xml, "combobox1");
131   rd.split_spinbutton = get_widget_assert (xml, "spinbutton1");
132
133   g_signal_connect_swapped (rd.model_combo, "changed",
134                             G_CALLBACK (on_method_change), &rd);
135
136   g_object_get (de->data_editor, "var-store", &vs, NULL);
137
138   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
139
140   g_object_get (vs, "dictionary", &rd.dict, NULL);
141   g_object_set (source, "model", rd.dict, NULL);
142
143   set_dest_model (GTK_TREE_VIEW (rd.variables), rd.dict);
144
145   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
146                                  insert_source_row_into_tree_view,
147                                  NULL,
148                                  NULL);
149
150   {
151     GtkTreeModel *tm =
152       gtk_tree_view_get_model (GTK_TREE_VIEW (rd.variables));
153
154
155     g_signal_connect_swapped (tm, "row-inserted",
156                       G_CALLBACK (on_vars_changed), &rd);
157
158     g_signal_connect_swapped (tm, "row-deleted",
159                       G_CALLBACK (on_vars_changed), &rd);
160   }
161
162   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &rd);
163
164   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
165                                       dialog_state_valid, &rd);
166
167   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
168
169
170   switch (response)
171     {
172     case GTK_RESPONSE_OK:
173       {
174         gchar *syntax = generate_syntax (&rd);
175
176         struct getl_interface *sss = create_syntax_string_source (syntax);
177         execute_syntax (sss);
178
179         g_free (syntax);
180       }
181       break;
182     case PSPPIRE_RESPONSE_PASTE:
183       {
184         gchar *syntax = generate_syntax (&rd);
185         paste_syntax_in_new_window (syntax);
186
187         g_free (syntax);
188       }
189       break;
190     default:
191       break;
192     }
193
194   g_object_unref (xml);
195 }
196
197
198 \f
199
200 static char *
201 generate_syntax (const struct reliability *rd)
202 {
203   gchar *text;
204   GString *string = g_string_new ("RELIABILITY");
205
206   g_string_append (string, "\n\t/VARIABLES=");
207   append_variable_names (string, rd->dict, GTK_TREE_VIEW (rd->variables), 0);
208
209
210   g_string_append (string, "\n\t/MODEL=");
211
212   if ( 0 == gtk_combo_box_get_active (GTK_COMBO_BOX (rd->model_combo)))
213     g_string_append (string, "ALPHA");
214   else
215     g_string_append_printf (string, "SPLIT (%d)",
216                             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (rd->split_spinbutton))
217                             );
218
219   g_string_append (string, ".\n");
220
221   text = string->str;
222
223   g_string_free (string, FALSE);
224
225   return text;
226 }