1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 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-reliability.h"
21 
22 #include "psppire-var-view.h"
23 
24 #include "psppire-dialog.h"
25 #include "builder-wrapper.h"
26 
27 static void psppire_dialog_action_reliability_init            (PsppireDialogActionReliability      *act);
28 static void psppire_dialog_action_reliability_class_init      (PsppireDialogActionReliabilityClass *class);
29 
30 G_DEFINE_TYPE (PsppireDialogActionReliability, psppire_dialog_action_reliability, PSPPIRE_TYPE_DIALOG_ACTION);
31 
32 enum
33   {
34     ALPHA = 0,
35     SPLIT = 1
36   };
37 
38 static char *
generate_syntax(const PsppireDialogAction * act)39 generate_syntax (const PsppireDialogAction *act)
40 {
41   PsppireDialogActionReliability *rd = PSPPIRE_DIALOG_ACTION_RELIABILITY (act);
42   gchar *text;
43   GString *string = g_string_new ("RELIABILITY");
44 
45   g_string_append (string, "\n\t/VARIABLES=");
46   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
47 
48 
49   g_string_append (string, "\n\t/MODEL=");
50 
51   if (ALPHA == gtk_combo_box_get_active (GTK_COMBO_BOX (rd->model_combo)))
52     g_string_append (string, "ALPHA");
53   else
54     g_string_append_printf (string, "SPLIT (%d)",
55 			    gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (rd->split_spinbutton))
56 			);
57 
58   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->scale_if_item_deleted_checkbutton)))
59     g_string_append (string, "\n\t/SUMMARY = TOTAL");
60 
61   g_string_append (string, ".\n");
62 
63   text = string->str;
64 
65   g_string_free (string, FALSE);
66 
67   return text;
68 }
69 
70 
71 static gboolean
dialog_state_valid(gpointer user_data)72 dialog_state_valid (gpointer user_data)
73 {
74   PsppireDialogActionReliability *pda = user_data;
75   GtkTreeModel *liststore =
76     gtk_tree_view_get_model (GTK_TREE_VIEW (pda->variables));
77 
78   return (2 <= gtk_tree_model_iter_n_children (liststore, NULL));
79 }
80 
81 static void
update_split_control(PsppireDialogActionReliability * pda)82 update_split_control (PsppireDialogActionReliability *pda)
83 {
84   GtkTreeModel *liststore =
85     gtk_tree_view_get_model (GTK_TREE_VIEW (pda->variables));
86 
87   gint n_vars = gtk_tree_model_iter_n_children (liststore, NULL);
88 
89   gint sp = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (pda->split_spinbutton));
90 
91   if (sp >= n_vars)
92     gtk_spin_button_set_value (GTK_SPIN_BUTTON (pda->split_spinbutton), n_vars - 1);
93 
94   gtk_spin_button_set_range (GTK_SPIN_BUTTON (pda->split_spinbutton),
95 			     0, n_vars - 1);
96 
97   gtk_widget_set_sensitive (pda->split_point_hbox,
98 			    (SPLIT == gtk_combo_box_get_active (GTK_COMBO_BOX (pda->model_combo))));
99 }
100 
101 
102 static void
refresh(PsppireDialogAction * pda_)103 refresh (PsppireDialogAction *pda_)
104 {
105   PsppireDialogActionReliability *pda =
106     PSPPIRE_DIALOG_ACTION_RELIABILITY (pda_);
107   GtkTreeModel *liststore =
108     gtk_tree_view_get_model (GTK_TREE_VIEW (pda->variables));
109   gtk_list_store_clear (GTK_LIST_STORE (liststore));
110 
111   gtk_combo_box_set_active (GTK_COMBO_BOX (pda->model_combo), ALPHA);
112 
113   gtk_spin_button_set_value (GTK_SPIN_BUTTON (pda->split_spinbutton), 0);
114 
115   gtk_spin_button_set_range (GTK_SPIN_BUTTON (pda->split_spinbutton),
116 			     0, 0);
117 
118   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (pda->scale_if_item_deleted_checkbutton),
119 				FALSE);
120 }
121 
122 static GtkBuilder *
psppire_dialog_action_reliability_activate(PsppireDialogAction * a,GVariant * param)123 psppire_dialog_action_reliability_activate (PsppireDialogAction *a, GVariant *param)
124 {
125   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
126   PsppireDialogActionReliability *act = PSPPIRE_DIALOG_ACTION_RELIABILITY (a);
127   GtkTreeModel *liststore ;
128 
129   GtkBuilder *xml = builder_new ("reliability.ui");
130 
131   pda->dialog = get_widget_assert   (xml, "reliability-dialog");
132   pda->source = get_widget_assert   (xml, "dict-view");
133 
134   act->variables = get_widget_assert   (xml, "treeview2");
135 
136   act->split_point_hbox = get_widget_assert (xml, "split-point-hbox");
137 
138   act->variables = get_widget_assert   (xml, "treeview2");
139 
140   act->model_combo = get_widget_assert   (xml, "combobox1");
141   act->split_spinbutton = get_widget_assert (xml, "spinbutton1");
142 
143   liststore =
144     gtk_tree_view_get_model (GTK_TREE_VIEW (act->variables));
145 
146 
147   act->scale_if_item_deleted_checkbutton = get_widget_assert (xml, "totals-checkbutton");
148 
149   g_signal_connect_swapped (act->model_combo, "changed",
150 			    G_CALLBACK (update_split_control), pda);
151 
152 
153   g_signal_connect_swapped (liststore, "row-inserted",
154 			    G_CALLBACK (update_split_control), pda);
155   g_signal_connect_swapped (liststore, "row-deleted",
156 			    G_CALLBACK (update_split_control), pda);
157 
158 
159   psppire_dialog_action_set_refresh (pda, refresh);
160   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
161   return xml;
162 }
163 
164 static void
psppire_dialog_action_reliability_class_init(PsppireDialogActionReliabilityClass * class)165 psppire_dialog_action_reliability_class_init (PsppireDialogActionReliabilityClass *class)
166 {
167   PsppireDialogActionClass *pdac = PSPPIRE_DIALOG_ACTION_CLASS (class);
168   PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_reliability_activate;
169 
170   pdac->generate_syntax = generate_syntax;
171 }
172 
173 
174 static void
psppire_dialog_action_reliability_init(PsppireDialogActionReliability * act)175 psppire_dialog_action_reliability_init (PsppireDialogActionReliability *act)
176 {
177 }
178 
179