Convert kmeans dialog to a PsppireDialogAction
authorJohn Darrington <john@darrington.wattle.id.au>
Sun, 29 Jan 2012 10:57:50 +0000 (11:57 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Sun, 29 Jan 2012 10:57:50 +0000 (11:57 +0100)
src/ui/gui/automake.mk
src/ui/gui/data-editor.ui
src/ui/gui/k-means-dialog.c [deleted file]
src/ui/gui/k-means-dialog.h [deleted file]
src/ui/gui/psppire-data-window.c
src/ui/gui/psppire-dialog-action-kmeans.c [new file with mode: 0644]
src/ui/gui/psppire-dialog-action-kmeans.h [new file with mode: 0644]
src/ui/gui/widgets.c

index deafc52a5f606795695c04c013d6602c987cb350..c0624455139239e925085f5890efd4e79ab24d05 100644 (file)
@@ -184,8 +184,6 @@ src_ui_gui_psppire_SOURCES = \
        src/ui/gui/helper.h \
        src/ui/gui/k-related-dialog.c \
        src/ui/gui/k-related-dialog.h \
-       src/ui/gui/k-means-dialog.c \
-       src/ui/gui/k-means-dialog.h \
        src/ui/gui/ks-one-sample-dialog.c \
        src/ui/gui/ks-one-sample-dialog.h \
        src/ui/gui/main.c \
@@ -214,6 +212,8 @@ src_ui_gui_psppire_SOURCES = \
        src/ui/gui/psppire-dialog-action-correlation.h \
        src/ui/gui/psppire-dialog-action-descriptives.c \
        src/ui/gui/psppire-dialog-action-descriptives.h \
+       src/ui/gui/psppire-dialog-action-kmeans.c \
+       src/ui/gui/psppire-dialog-action-kmeans.h \
        src/ui/gui/psppire-dialog-action-var-info.c \
        src/ui/gui/psppire-dialog-action-var-info.h \
        src/ui/gui/psppire-dict.c \
index 8c77b9811b6f2c5a11fb07eea42bd567da61e980..f5f5405bbe2b965aa7dce094426969d4d888729e 100644 (file)
           </object>
         </child>
         <child>
-          <object class="GtkAction" id="k-means">
+          <object class="PsppireDialogActionKmeans" id="k-means">
             <property name="name">k-means</property>
+            <property name="manager">uimanager1</property>
             <property name="label" translatable="yes">_K-Means Cluster...</property>
           </object>
         </child>
diff --git a/src/ui/gui/k-means-dialog.c b/src/ui/gui/k-means-dialog.c
deleted file mode 100644 (file)
index 329602b..0000000
+++ /dev/null
@@ -1,163 +0,0 @@
-/* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2011, 2012  Free Software Foundation
-
-   This program is free software: you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation, either version 3 of the License, or
-   (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
-
-#include <config.h>
-
-#include "dialog-common.h"
-#include <ui/syntax-gen.h>
-#include <libpspp/str.h>
-
-#include "k-means-dialog.h"
-#include "psppire-selector.h"
-#include "psppire-dictview.h"
-#include "psppire-dialog.h"
-
-#include "psppire-data-window.h"
-#include "psppire-var-view.h"
-
-#include "executor.h"
-#include "helper.h"
-#include "builder-wrapper.h"
-
-#include <gtk/gtk.h>
-
-#include "gettext.h"
-#define _(msgid) gettext (msgid)
-#define N_(msgid) msgid
-
-
-
-
-struct k_means
-{
-  GtkBuilder *xml;
-  PsppireDict *dict;
-
-  GtkWidget *variables;
-  PsppireDataWindow *de ;
-
-  GtkWidget *entry;
-};
-
-static char * generate_syntax (const struct k_means *rd);
-
-
-static void
-refresh (struct k_means *fd)
-{
-  GtkTreeModel *liststore =
-    gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
-  gtk_list_store_clear (GTK_LIST_STORE (liststore));
-
-  gtk_entry_set_text (GTK_ENTRY (fd->entry), "");
-}
-
-
-static gboolean
-dialog_state_valid (gpointer data)
-{
-  struct k_means *fd = data;
-
-  GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
-
-  if  (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
-    return FALSE;
-
-  if (atoi (gtk_entry_get_text (GTK_ENTRY (fd->entry))) < 2)
-    return FALSE;
-
-  return TRUE;
-}
-
-
-/* Pops up the K_Means dialog box */
-void
-k_means_dialog (PsppireDataWindow *dw)
-{
-  struct k_means fd;
-  gint response;
-
-  PsppireVarStore *vs;
-
-  GtkWidget *dialog ;
-  GtkWidget *source ;
-
-  fd.xml = builder_new ("k-means.ui");
-
-  dialog = get_widget_assert   (fd.xml, "k-means-dialog");
-  source = get_widget_assert   (fd.xml, "dict-view");
-
-  fd.entry = get_widget_assert   (fd.xml, "entry1");
-
-  fd.de = dw;
-
-  g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &fd);
-
-
-  fd.variables = get_widget_assert   (fd.xml, "psppire-var-view1");
-
-  g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
-
-  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
-
-  g_object_get (vs, "dictionary", &fd.dict, NULL);
-  g_object_set (source, "model", fd.dict,
-               "predicate", var_is_numeric,
-               NULL);
-
-  psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
-                                     dialog_state_valid, &fd);
-
-  response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
-
-  switch (response)
-    {
-    case GTK_RESPONSE_OK:
-      g_free (execute_syntax_string (dw, generate_syntax (&fd)));
-      break;
-    case PSPPIRE_RESPONSE_PASTE:
-      g_free (paste_syntax_to_window (generate_syntax (&fd)));
-      break;
-    default:
-      break;
-    }
-
-  g_object_unref (fd.xml);
-}
-
-
-\f
-
-static char *
-generate_syntax (const struct k_means *km)
-{
-  gchar *text;
-
-  GString *string = g_string_new ("QUICK CLUSTER ");
-
-  psppire_var_view_append_names (PSPPIRE_VAR_VIEW (km->variables), 0, string);
-
-  g_string_append_printf (string, "\n\t/CRITERIA=CLUSTERS(%d)",
-                         atoi (gtk_entry_get_text (GTK_ENTRY (km->entry))));
-
-  g_string_append (string, ".\n");
-
-  text = string->str;
-
-  g_string_free (string, FALSE);
-
-  return text;
-}
diff --git a/src/ui/gui/k-means-dialog.h b/src/ui/gui/k-means-dialog.h
deleted file mode 100644 (file)
index 522acbc..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2011  Free Software Foundation
-
-   This program is free software: you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation, either version 3 of the License, or
-   (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
-
-#ifndef __K_MEANS_DIALOG_H
-#define __K_MEANS_DIALOG_H
-
-#include "psppire-data-window.h"
-
-void k_means_dialog (PsppireDataWindow * data);
-
-#endif
index 8ee4cafeda143f870380f4c397dcdd15ec68175e..6e57f5ba21f71acfc42bb97a638329e98321013c 100644 (file)
@@ -43,7 +43,6 @@
 #include "ui/gui/help-menu.h"
 #include "ui/gui/helper.h"
 #include "ui/gui/helper.h"
-#include "ui/gui/k-means-dialog.h"
 #include "ui/gui/k-related-dialog.h"
 #include "ui/gui/npar-two-sample-related.h"
 #include "ui/gui/oneway-anova-dialog.h"
@@ -1117,8 +1116,6 @@ psppire_data_window_finish_init (PsppireDataWindow *de,
 
   connect_action (de, "factor-analysis", G_CALLBACK (factor_dialog));
 
-  connect_action (de, "k-means", G_CALLBACK (k_means_dialog));
-
   connect_action (de, "chi-square", G_CALLBACK (chisquare_dialog));
   connect_action (de, "binomial", G_CALLBACK (binomial_dialog));
   connect_action (de, "runs", G_CALLBACK (runs_dialog));
diff --git a/src/ui/gui/psppire-dialog-action-kmeans.c b/src/ui/gui/psppire-dialog-action-kmeans.c
new file mode 100644 (file)
index 0000000..3656a5d
--- /dev/null
@@ -0,0 +1,120 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2012  Free Software Foundation
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+
+#include <config.h>
+
+#include "psppire-dialog-action-kmeans.h"
+
+#include "psppire-var-view.h"
+#include <stdlib.h>
+#include "psppire-dialog.h"
+#include "builder-wrapper.h"
+
+static void psppire_dialog_action_kmeans_init            (PsppireDialogActionKmeans      *act);
+static void psppire_dialog_action_kmeans_class_init      (PsppireDialogActionKmeansClass *class);
+
+
+G_DEFINE_TYPE (PsppireDialogActionKmeans, psppire_dialog_action_kmeans, PSPPIRE_TYPE_DIALOG_ACTION);
+
+static char *
+generate_syntax (PsppireDialogAction *act)
+{
+  PsppireDialogActionKmeans *km = PSPPIRE_DIALOG_ACTION_KMEANS (act);
+  gchar *text;
+
+  GString *string = g_string_new ("QUICK CLUSTER ");
+
+  psppire_var_view_append_names (PSPPIRE_VAR_VIEW (km->variables), 0, string);
+
+  g_string_append_printf (string, "\n\t/CRITERIA=CLUSTERS(%d)",
+                         atoi (gtk_entry_get_text (GTK_ENTRY (km->entry))));
+
+  g_string_append (string, ".\n");
+
+  text = string->str;
+
+  g_string_free (string, FALSE);
+
+  return text;
+}
+
+
+
+static gboolean
+dialog_state_valid (gpointer user_data)
+{
+  PsppireDialogActionKmeans *fd = user_data;
+
+  GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
+
+  if  (gtk_tree_model_iter_n_children (liststore, NULL) < 2)
+    return FALSE;
+
+  if (atoi (gtk_entry_get_text (GTK_ENTRY (fd->entry))) < 2)
+    return FALSE;
+
+  return TRUE;
+}
+
+static void
+refresh (PsppireDialogActionKmeans *fd)
+{
+  GtkTreeModel *liststore =
+    gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
+  gtk_list_store_clear (GTK_LIST_STORE (liststore));
+
+  gtk_entry_set_text (GTK_ENTRY (fd->entry), "");
+}
+
+static void
+psppire_dialog_action_kmeans_activate (GtkAction *a)
+{
+  PsppireDialogActionKmeans *act = PSPPIRE_DIALOG_ACTION_KMEANS (a);
+  PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
+  GtkBuilder *xml = builder_new ("k-means.ui");
+
+  pda->dialog = get_widget_assert   (xml, "k-means-dialog");
+  pda->source = get_widget_assert   (xml, "dict-view");
+
+  act->entry = get_widget_assert   (xml, "entry1");
+  act->variables = get_widget_assert (xml, "psppire-var-view1");
+
+  psppire_dialog_action_set_refresh (pda, refresh);
+
+  psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
+
+  g_object_unref (xml);
+
+  if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_kmeans_parent_class)->activate)
+    PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_kmeans_parent_class)->activate (pda);
+}
+
+static void
+psppire_dialog_action_kmeans_class_init (PsppireDialogActionKmeansClass *class)
+{
+  GtkActionClass *action_class = GTK_ACTION_CLASS (class);
+
+  action_class->activate = psppire_dialog_action_kmeans_activate;
+  PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
+}
+
+
+static void
+psppire_dialog_action_kmeans_init (PsppireDialogActionKmeans *act)
+{
+}
+
diff --git a/src/ui/gui/psppire-dialog-action-kmeans.h b/src/ui/gui/psppire-dialog-action-kmeans.h
new file mode 100644 (file)
index 0000000..3c602f2
--- /dev/null
@@ -0,0 +1,79 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2012  Free Software Foundation
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+
+#include <glib-object.h>
+#include <glib.h>
+
+#include "psppire-dialog-action.h"
+
+#ifndef __PSPPIRE_DIALOG_ACTION_KMEANS_H__
+#define __PSPPIRE_DIALOG_ACTION_KMEANS_H__
+
+G_BEGIN_DECLS
+
+
+#define PSPPIRE_TYPE_DIALOG_ACTION_KMEANS (psppire_dialog_action_kmeans_get_type ())
+
+#define PSPPIRE_DIALOG_ACTION_KMEANS(obj)      \
+                     (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+                                                 PSPPIRE_TYPE_DIALOG_ACTION_KMEANS, PsppireDialogActionKmeans))
+
+#define PSPPIRE_DIALOG_ACTION_KMEANS_CLASS(klass) \
+                     (G_TYPE_CHECK_CLASS_CAST ((klass), \
+                                PSPPIRE_TYPE_DIALOG_ACTION_KMEANS, \
+                                 PsppireDialogActionKmeansClass))
+
+
+#define PSPPIRE_IS_DIALOG_ACTION_KMEANS(obj) \
+                    (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PSPPIRE_TYPE_DIALOG_ACTION_KMEANS))
+
+#define PSPPIRE_IS_DIALOG_ACTION_KMEANS_CLASS(klass) \
+                     (G_TYPE_CHECK_CLASS_TYPE ((klass), PSPPIRE_TYPE_DIALOG_ACTION_KMEANS))
+
+
+#define PSPPIRE_DIALOG_ACTION_KMEANS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+                                  PSPPIRE_TYPE_DIALOG_ACTION_KMEANS, \
+                                  PsppireDialogActionKmeansClass))
+
+typedef struct _PsppireDialogActionKmeans       PsppireDialogActionKmeans;
+typedef struct _PsppireDialogActionKmeansClass  PsppireDialogActionKmeansClass;
+
+
+struct _PsppireDialogActionKmeans
+{
+  PsppireDialogAction parent;
+
+  /*< private >*/
+  gboolean dispose_has_run ;
+
+  /* Treeview containing the selected variables */
+  GtkWidget *variables;
+  GtkWidget *entry;
+};
+
+
+struct _PsppireDialogActionKmeansClass
+{
+  PsppireDialogActionClass parent_class;
+};
+
+
+GType psppire_dialog_action_kmeans_get_type (void) ;
+
+G_END_DECLS
+
+#endif /* __PSPPIRE_DIALOG_ACTION_KMEANS_H__ */
index 3a0e338a89b870a9d65e17fca41933da4b3ad37e..da5935cb7b7c528d77ce87b7efc93749ae4d60b3 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "psppire-dialog-action-correlation.h"
 #include "psppire-dialog-action-descriptives.h"
+#include "psppire-dialog-action-kmeans.h"
 #include "psppire-dialog-action-var-info.h"
 
 
@@ -36,5 +37,6 @@ preregister_widgets (void)
 
   psppire_dialog_action_correlation_get_type ();
   psppire_dialog_action_descriptives_get_type ();
+  psppire_dialog_action_kmeans_get_type ();
   psppire_dialog_action_var_info_get_type ();
 }