Populate the data sheet in the import assistant
authorJohn Darrington <john@darrington.wattle.id.au>
Thu, 18 May 2017 17:14:43 +0000 (19:14 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Thu, 18 May 2017 17:14:43 +0000 (19:14 +0200)
src/ui/gui/psppire-delimited-text.c
src/ui/gui/psppire-import-assistant.c

index fec2f707723ff3971074f5cd699fa81ceac7974c..98f76b4301953f330f881d37b193111eb8893f52 100644 (file)
@@ -260,9 +260,13 @@ static gint
 __tree_model_iter_n_children (GtkTreeModel *tree_model,
                              GtkTreeIter *iter)
 {
-  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
+  PsppireDelimitedText *file  = PSPPIRE_DELIMITED_TEXT (tree_model);
+  //  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
   g_assert (iter == NULL);
-  return 0;
+
+  gint children = gtk_tree_model_iter_n_children (file->child, NULL);
+
+  return children - file->first_line;
 }
 
 static GtkTreeModelFlags
index df937ff7a3286dafcb225acf5baaadf80c1d79b7..6650b5d9cfb2d132467cd634f29c92f55f19cbb5 100644 (file)
@@ -32,6 +32,7 @@
 #include "data/ods-reader.h"
 #include "data/spreadsheet-reader.h"
 #include "data/value-labels.h"
+#include "data/casereader-provider.h"
 
 #include "gl/intprops.h"
 
@@ -1796,6 +1797,65 @@ on_variable_change (PsppireDict *dict, int dict_idx,
 #endif
 
 
+static struct casereader_random_class my_casereader_class;
+
+static struct ccase *
+my_read (struct casereader *reader, void *aux, casenumber idx)
+{
+  PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
+  GtkTreeModel *tm = GTK_TREE_MODEL (ia->delimiters_model);
+
+  GtkTreePath *tp = gtk_tree_path_new_from_indices (idx, -1);
+
+  const struct caseproto *proto = casereader_get_proto (reader);
+
+  GtkTreeIter iter;
+  struct ccase *c = NULL;
+  if (gtk_tree_model_get_iter (tm, &iter, tp))
+    {
+      c = case_create (proto);
+      int i;
+      for (i = 0 ; i < caseproto_get_n_widths (proto); ++i)
+       {
+         GValue value = {0};
+         gtk_tree_model_get_value (tm, &iter, i + 1, &value);
+
+         const struct variable *var = dict_get_var (ia->dict, i);
+
+         const gchar *ss = g_value_get_string (&value);
+
+         union value *v = case_data_rw (c, var);
+         char *xx = data_in (ss_cstr (ss),
+                             "UTF-8",
+                             var_get_write_format (var)->type,
+                             v, var_get_width (var), "UTF-8");
+
+         /* if (xx) */
+         /*   g_print ("%s:%d Err %s\n", __FILE__, __LINE__, xx); */
+         free (xx);
+         g_value_unset (&value);
+       }
+    }
+
+  gtk_tree_path_free (tp);
+
+  return c;
+}
+
+static void
+my_destroy (struct casereader *reader, void *aux)
+{
+  PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
+  g_print ("%s:%d\n", __FILE__, __LINE__);
+}
+
+static void
+my_advance (struct casereader *reader, void *aux, casenumber cnt)
+{
+  g_print ("%s:%d\n", __FILE__, __LINE__);
+}
+
+
 /* Called just before the formats page of the assistant is
    displayed. */
 static void
@@ -1804,7 +1864,27 @@ prepare_formats_page (PsppireImportAssistant *ia)
   PsppireDict *dict = psppire_dict_new_from_dict (ia->dict);
   g_object_set (ia->var_sheet, "data-model", dict, NULL);
 
+  my_casereader_class.read = my_read;
+  my_casereader_class.destroy = my_destroy;
+  my_casereader_class.advance = my_advance;
+
+  struct caseproto *proto = caseproto_create ();
+
+  int i;
+  for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
+    {
+      const struct variable *var = dict_get_var (ia->dict, i);
+      proto = caseproto_add_width (proto, var_get_width (var));
+    }
+
+  gint n_rows = gtk_tree_model_iter_n_children (ia->delimiters_model, NULL);
+
+  struct casereader *reader =
+    casereader_create_random (proto, n_rows, &my_casereader_class,  ia);
+
   PsppireDataStore *store = psppire_data_store_new (dict);
+  psppire_data_store_set_reader (store, reader);
+
   g_object_set (ia->data_sheet, "data-model", store, NULL);
 
   gtk_widget_show (ia->paste_button);