0d25f3e7ceeb4e67baca44e3411ab1029384ce38
[pspp] / src / ui / gui / psppire-dialog-action-1sks.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012, 2013  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-1sks.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_1sks_init            (PsppireDialogAction1sks      *act);
28 static void psppire_dialog_action_1sks_class_init      (PsppireDialogAction1sksClass *class);
29
30 G_DEFINE_TYPE (PsppireDialogAction1sks, psppire_dialog_action_1sks, PSPPIRE_TYPE_DIALOG_ACTION);
31
32
33 enum
34   {
35     CB_NORMAL,
36     CB_POISSON,
37     CB_UNIFORM,
38     CB_EXPONENTIAL
39   };
40
41 static void
42 append_fragment (GString *string, const gchar *dist, PsppireVarView *vv)
43 {
44   g_string_append (string, "\n\t/KOLMOGOROV-SMIRNOV");
45
46   g_string_append (string, " ( ");
47   g_string_append (string, dist);
48   g_string_append (string, " ) = ");
49
50   psppire_var_view_append_names (vv, 0, string);
51 }
52
53
54 static char *
55 generate_syntax (PsppireDialogAction *act)
56 {
57   PsppireDialogAction1sks *rd = PSPPIRE_DIALOG_ACTION_1SKS (act);
58   gchar *text;
59
60   GString *string = g_string_new ("NPAR TEST");
61
62   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_NORMAL])))
63     append_fragment (string, "NORMAL", PSPPIRE_VAR_VIEW (rd->variables));
64
65   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_UNIFORM])))
66     append_fragment (string, "UNIFORM", PSPPIRE_VAR_VIEW (rd->variables));
67
68   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_POISSON])))
69     append_fragment (string, "POISSON", PSPPIRE_VAR_VIEW (rd->variables));
70
71   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_EXPONENTIAL])))
72     append_fragment (string, "EXPONENTIAL", PSPPIRE_VAR_VIEW (rd->variables));
73
74   g_string_append (string, ".\n");
75
76   text = string->str;
77
78   g_string_free (string, FALSE);
79
80   return text;
81 }
82
83
84 static gboolean
85 dialog_state_valid (gpointer data)
86 {
87   int i;
88   PsppireDialogAction1sks *fd = PSPPIRE_DIALOG_ACTION_1SKS (data);
89
90   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
91
92   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
93     return FALSE;
94
95   for (i = 0; i < 4; ++i)
96     {
97       if ( TRUE == gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[i])))
98         break;
99     }
100   if ( i >= 4)
101     return FALSE;
102
103   return TRUE;
104 }
105
106 static void
107 refresh (PsppireDialogAction *rd_)
108 {
109   PsppireDialogAction1sks *fd = PSPPIRE_DIALOG_ACTION_1SKS (rd_);
110   int i;
111   GtkTreeModel *liststore =
112     gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
113   gtk_list_store_clear (GTK_LIST_STORE (liststore));
114
115   for (i = 0; i < 4; ++i)
116     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->cb[i]), FALSE);
117 }
118
119 static void
120 psppire_dialog_action_1sks_activate (GtkAction *a)
121 {
122   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
123   PsppireDialogAction1sks *act = PSPPIRE_DIALOG_ACTION_1SKS (a);
124
125   GHashTable *thing = psppire_dialog_action_get_pointer (pda);
126   GtkBuilder *xml = g_hash_table_lookup (thing, a);
127   if (!xml)
128     {
129       xml = builder_new ("ks-one-sample.ui");
130       g_hash_table_insert (thing, a, xml);
131     }
132
133   pda->dialog = get_widget_assert   (xml, "ks-one-sample-dialog");
134   pda->source = get_widget_assert   (xml, "dict-view");
135
136   act->variables = get_widget_assert   (xml, "psppire-var-view1");
137
138   act->cb[CB_NORMAL] = get_widget_assert (xml, "checkbutton-normal");
139   act->cb[CB_POISSON] = get_widget_assert (xml, "checkbutton-poisson");
140   act->cb[CB_UNIFORM] = get_widget_assert (xml, "checkbutton-uniform");
141   act->cb[CB_EXPONENTIAL] = get_widget_assert (xml, "checkbutton-exp");
142
143   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
144   psppire_dialog_action_set_refresh (pda, refresh);
145
146   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_1sks_parent_class)->activate)
147     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_1sks_parent_class)->activate (pda);
148 }
149
150 static void
151 psppire_dialog_action_1sks_class_init (PsppireDialogAction1sksClass *class)
152 {
153   psppire_dialog_action_set_activation (class, psppire_dialog_action_1sks_activate);
154   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
155 }
156
157
158 static void
159 psppire_dialog_action_1sks_init (PsppireDialogAction1sks *act)
160 {
161 }
162