39be5670b72e71d5fcb62fcce6f8e0f3b399d569
[pspp] / src / ui / gui / psppire-dialog-action-kmeans.c
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-kmeans.h"
21
22 #include "psppire-var-view.h"
23 #include <stdlib.h>
24 #include "psppire-dialog.h"
25 #include "builder-wrapper.h"
26
27 static void psppire_dialog_action_kmeans_init            (PsppireDialogActionKmeans      *act);
28 static void psppire_dialog_action_kmeans_class_init      (PsppireDialogActionKmeansClass *class);
29
30
31 G_DEFINE_TYPE (PsppireDialogActionKmeans, psppire_dialog_action_kmeans, PSPPIRE_TYPE_DIALOG_ACTION);
32
33 static char *
34 generate_syntax (PsppireDialogAction *act)
35 {
36   PsppireDialogActionKmeans *km = PSPPIRE_DIALOG_ACTION_KMEANS (act);
37   gchar *text;
38
39   GString *string = g_string_new ("QUICK CLUSTER ");
40
41   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (km->variables), 0, string);
42
43   g_string_append_printf (string, "\n\t/CRITERIA=CLUSTERS(%d)",
44                           atoi (gtk_entry_get_text (GTK_ENTRY (km->entry))));
45
46   g_string_append (string, ".\n");
47
48   text = string->str;
49
50   g_string_free (string, FALSE);
51
52   return text;
53 }
54
55
56
57 static gboolean
58 dialog_state_valid (gpointer user_data)
59 {
60   PsppireDialogActionKmeans *fd = user_data;
61
62   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
63
64   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
65     return FALSE;
66
67   if (atoi (gtk_entry_get_text (GTK_ENTRY (fd->entry))) < 2)
68     return FALSE;
69
70   return TRUE;
71 }
72
73 static void
74 refresh (PsppireDialogAction *fd_)
75 {
76   PsppireDialogActionKmeans *fd = PSPPIRE_DIALOG_ACTION_KMEANS (fd_);
77   GtkTreeModel *liststore =
78     gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
79   gtk_list_store_clear (GTK_LIST_STORE (liststore));
80
81   gtk_entry_set_text (GTK_ENTRY (fd->entry), "");
82 }
83
84 static void
85 psppire_dialog_action_kmeans_activate (GtkAction *a)
86 {
87   PsppireDialogActionKmeans *act = PSPPIRE_DIALOG_ACTION_KMEANS (a);
88   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
89
90   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
91   GtkBuilder *xml = g_hash_table_lookup (thing, a);
92   if (!xml)
93     {
94       xml = builder_new ("k-means.ui");
95       g_hash_table_insert (thing, a, xml);
96     }
97
98   pda->dialog = get_widget_assert   (xml, "k-means-dialog");
99   pda->source = get_widget_assert   (xml, "dict-view");
100
101   act->entry = get_widget_assert   (xml, "entry1");
102   act->variables = get_widget_assert (xml, "psppire-var-view1");
103
104   psppire_dialog_action_set_refresh (pda, refresh);
105   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
106
107   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_kmeans_parent_class)->activate)
108     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_kmeans_parent_class)->activate (pda);
109 }
110
111 static void
112 psppire_dialog_action_kmeans_class_init (PsppireDialogActionKmeansClass *class)
113 {
114   psppire_dialog_action_set_activation (class, psppire_dialog_action_kmeans_activate);
115   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
116 }
117
118
119 static void
120 psppire_dialog_action_kmeans_init (PsppireDialogActionKmeans *act)
121 {
122 }
123