Converted the correlations dialog to a PsppireDialogAction object
[pspp-builds.git] / src / ui / gui / k-means-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011, 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 #include <config.h>
18
19 #include "dialog-common.h"
20 #include <ui/syntax-gen.h>
21 #include <libpspp/str.h>
22
23 #include "k-means-dialog.h"
24 #include "psppire-selector.h"
25 #include "psppire-dictview.h"
26 #include "psppire-dialog.h"
27
28 #include "psppire-data-window.h"
29 #include "psppire-var-view.h"
30
31 #include "executor.h"
32 #include "helper.h"
33 #include "builder-wrapper.h"
34
35 #include <gtk/gtk.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41
42
43
44 struct k_means
45 {
46   GtkBuilder *xml;
47   PsppireDict *dict;
48
49   GtkWidget *variables;
50   PsppireDataWindow *de ;
51
52   GtkWidget *entry;
53 };
54
55 static char * generate_syntax (const struct k_means *rd);
56
57
58 static void
59 refresh (struct k_means *fd)
60 {
61   GtkTreeModel *liststore =
62     gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
63   gtk_list_store_clear (GTK_LIST_STORE (liststore));
64
65   gtk_entry_set_text (GTK_ENTRY (fd->entry), "");
66 }
67
68
69 static gboolean
70 dialog_state_valid (gpointer data)
71 {
72   struct k_means *fd = data;
73
74   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
75
76   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
77     return FALSE;
78
79   if (atoi (gtk_entry_get_text (GTK_ENTRY (fd->entry))) < 2)
80     return FALSE;
81
82   return TRUE;
83 }
84
85
86 /* Pops up the K_Means dialog box */
87 void
88 k_means_dialog (PsppireDataWindow *dw)
89 {
90   struct k_means fd;
91   gint response;
92
93   PsppireVarStore *vs;
94
95   GtkWidget *dialog ;
96   GtkWidget *source ;
97
98   fd.xml = builder_new ("k-means.ui");
99
100   dialog = get_widget_assert   (fd.xml, "k-means-dialog");
101   source = get_widget_assert   (fd.xml, "dict-view");
102
103   fd.entry = get_widget_assert   (fd.xml, "entry1");
104
105   fd.de = dw;
106
107   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &fd);
108
109
110   fd.variables = get_widget_assert   (fd.xml, "psppire-var-view1");
111
112   g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
113
114   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
115
116   g_object_get (vs, "dictionary", &fd.dict, NULL);
117   g_object_set (source, "model", fd.dict,
118                 "predicate", var_is_numeric,
119                 NULL);
120
121   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
122                                       dialog_state_valid, &fd);
123
124   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
125
126   switch (response)
127     {
128     case GTK_RESPONSE_OK:
129       g_free (execute_syntax_string (dw, generate_syntax (&fd)));
130       break;
131     case PSPPIRE_RESPONSE_PASTE:
132       g_free (paste_syntax_to_window (generate_syntax (&fd)));
133       break;
134     default:
135       break;
136     }
137
138   g_object_unref (fd.xml);
139 }
140
141
142 \f
143
144 static char *
145 generate_syntax (const struct k_means *km)
146 {
147   gchar *text;
148
149   GString *string = g_string_new ("QUICK CLUSTER ");
150
151   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (km->variables), 0, string);
152
153   g_string_append_printf (string, "\n\t/CRITERIA=CLUSTERS(%d)",
154                           atoi (gtk_entry_get_text (GTK_ENTRY (km->entry))));
155
156   g_string_append (string, ".\n");
157
158   text = string->str;
159
160   g_string_free (string, FALSE);
161
162   return text;
163 }