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