#include "data/ods-reader.h"
#include "data/spreadsheet-reader.h"
#include "data/value-labels.h"
+#include "data/casereader-provider.h"
#include "gl/intprops.h"
#include "psppire-spreadsheet-model.h"
#include "psppire-text-file.h"
#include "psppire-delimited-text.h"
+#include "psppire-data-sheet.h"
+#include "psppire-data-store.h"
+#include "psppire-dict.h"
+#include "psppire-variable-sheet.h"
#include "ui/syntax-gen.h"
if (ia->spreadsheet)
spreadsheet_unref (ia->spreadsheet);
- destroy_columns (ia);
+ // destroy_columns (ia);
ds_destroy (&ia->separators);
ds_destroy (&ia->quotes);
f < n_fields; f++)
{
GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
+
+ const gchar *title = NULL;
+
+ if (f == 0)
+ title = _("line");
+ else
+ {
+ if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
+ {
+ title =
+ psppire_delimited_text_get_header_title
+ (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), f - 1);
+ }
+ if (title == NULL)
+ title = _("var");
+ }
+
GtkTreeViewColumn *column =
- gtk_tree_view_column_new_with_attributes ("var", renderer,
+ gtk_tree_view_column_new_with_attributes (title,
+ renderer,
"text", f,
NULL);
g_object_set (column,
"resizable", TRUE,
"sizing", GTK_TREE_VIEW_COLUMN_AUTOSIZE,
NULL);
-
+
gtk_tree_view_append_column (GTK_TREE_VIEW (ia->fields_tree_view), column);
}
}
prepare_separators_page (PsppireImportAssistant *ia, GtkWidget *page)
{
gtk_tree_view_set_model (GTK_TREE_VIEW (ia->fields_tree_view), ia->delimiters_model);
-
+
g_signal_connect_swapped (ia->delimiters_model, "notify::delimiters",
G_CALLBACK (reset_tree_view_model), ia);
chooser_page_leave (PsppireImportAssistant *ia, GtkWidget *page)
{
g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
- gchar *file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (page));
+ g_free (ia->file_name);
+ ia->file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (page));
gchar *encoding = psppire_encoding_selector_get_encoding (ia->encoding_selector);
if (!ia->spreadsheet)
{
- ia->text_file = psppire_text_file_new (file_name, encoding);
+ ia->text_file = psppire_text_file_new (ia->file_name, encoding);
gtk_tree_view_set_model (GTK_TREE_VIEW (ia->first_line_tree_view),
GTK_TREE_MODEL (ia->text_file));
}
- g_free (file_name);
+
g_free (encoding);
}
ia->builder = builder_new ("text-data-import.ui");
ia->current_page = -1 ;
- ia->column_cnt = 0;
- ia->columns = NULL;
+ /* ia->column_cnt = 0; */
+ /* ia->columns = NULL; */
+
+ ia->file_name = NULL;
ia->spreadsheet = NULL;
ia->watch_cursor = 0;
GTK_TOGGLE_BUTTON (p->percent_button)));
}
-/* Sets the widgets to match IA's first_line substructure. */
-static void
-set_first_line (PsppireImportAssistant *ia)
-{
- GtkTreePath *path = gtk_tree_path_new_from_indices (ia->skip_lines, -1);
-
- gtk_tree_path_free (path);
-
- gtk_toggle_button_set_active (
- GTK_TOGGLE_BUTTON (ia->variable_names_cb),
- ia->variable_names);
- gtk_widget_set_sensitive (ia->variable_names_cb,
- ia->skip_lines > 0);
-}
#if SHEET_MERGE
#endif
static void
-on_cursor_change (GtkTreeView *treeview, gpointer user_data)
+on_treeview_selection_change (PsppireImportAssistant *ia)
{
- PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (user_data);
- GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);
+ GtkTreeSelection *selection =
+ gtk_tree_view_get_selection (GTK_TREE_VIEW (ia->first_line_tree_view));
GtkTreeModel *model = NULL;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
PsppireTextFile *tf = PSPPIRE_TEXT_FILE (model);
GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
gint *index = gtk_tree_path_get_indices (path);
-
+
n = *index;
-
+
gtk_tree_path_free (path);
- g_print ("%s:%d Setting first line to %d\n", __FILE__, __LINE__, n);
+ gtk_widget_set_sensitive (ia->variable_names_cb, n > 0);
- ia->delimiters_model = psppire_delimited_text_new (ia->text_file);
+ ia->delimiters_model
+ = psppire_delimited_text_new (GTK_TREE_MODEL (ia->text_file));
g_object_set (ia->delimiters_model, "first-line", n, NULL);
+
+ // ia->skip_lines = n;
}
}
g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
GtkWidget *w = get_widget_assert (ia->builder, "FirstLine");
- g_object_set_data (G_OBJECT (w), "on-entering", set_first_line);
+ g_object_set_data (G_OBJECT (w), "on-entering", on_treeview_selection_change);
add_page_to_assistant (ia, w,
GTK_ASSISTANT_PAGE_CONTENT, _("Select the First Line"));
gtk_container_add (GTK_CONTAINER (scrolled_window), ia->first_line_tree_view);
- g_signal_connect (ia->first_line_tree_view, "cursor-changed",
- G_CALLBACK (on_cursor_change), ia);
+ g_signal_connect_swapped (ia->first_line_tree_view, "cursor-changed",
+ G_CALLBACK (on_treeview_selection_change), ia);
}
gtk_widget_show_all (scrolled_window);
-
+ ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
#if SHEET_MERGE
- ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
pspp_sheet_selection_set_mode (
pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view)),
PSPP_SHEET_SELECTION_BROWSE);
g_signal_connect_swapped (ia->variable_names_cb, "toggled",
G_CALLBACK (set_first_line_options), ia);
-
g_object_set_data (G_OBJECT (w), "on-reset", reset_first_line_page);
#endif
}
struct substring *contents;
};
+#if SHEET_MERGE
static void
destroy_columns (PsppireImportAssistant *ia)
free (ia->columns);
}
-#if SHEET_MERGE
+
/* Called to render one of the cells in the fields preview tree
view. */
(void *) NULL);
}
-#endif
-
/* Parses the contents of the field at (ROW,COLUMN) according to
its variable format. If OUTPUTP is non-null, then *OUTPUTP
receives the formatted output for that field (which must be
return ok;
}
-#if SHEET_MERGE
-
/* Called to render one of the cells in the data preview tree
view. */
static void
static void
choose_column_names (PsppireImportAssistant *ia)
{
- struct dictionary *dict;
- unsigned long int generated_name_count = 0;
- struct column *col;
- size_t name_row;
-
- dict = dict_create (get_default_encoding ());
- name_row = ia->variable_names && ia->skip_lines ? ia->skip_lines : 0;
- for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
+ if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
{
- char *hint, *name;
-
- hint = name_row ? ss_xstrdup (col->contents[name_row - 1]) : NULL;
- name = dict_make_unique_var_name (dict, hint, &generated_name_count);
- free (hint);
+ int i;
+ unsigned long int generated_name_count = 0;
+ dict_clear (ia->dict);
+ for (i = 0; i < gtk_tree_model_get_n_columns (ia->delimiters_model) - 1; ++i)
+ {
+ const gchar *candidate_name =
+ psppire_delimited_text_get_header_title (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), i);
- col->name = name;
- dict_create_var_assert (dict, name, 0);
+ char *name = dict_make_unique_var_name (ia->dict, candidate_name, &generated_name_count);
+ dict_create_var_assert (ia->dict, name, 0);
+ free (name);
+ }
}
- dict_destroy (dict);
}
#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__);
+}
+
+
+static void
+foo (struct dictionary *dict, void *aux)
+{
+ PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
+ g_print ("%s:%d\n", __FILE__, __LINE__);
+
+ 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 = NULL;
+
+ g_object_get (ia->data_sheet, "data-model", &store, NULL);
+
+ psppire_data_store_set_reader (store, reader);
+}
+
/* Called just before the formats page of the assistant is
displayed. */
static void
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 ();
+
+ dict_set_change_callback (ia->dict, foo, ia);
+
+ 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);
}
static void
{
GtkBuilder *builder = ia->builder;
-
GtkWidget *w = get_widget_assert (builder, "Formats");
g_object_set_data (G_OBJECT (w), "on-entering", prepare_formats_page);
g_object_set_data (G_OBJECT (w), "on-reset", reset_formats_page);
+ GtkWidget *vars_scroller = get_widget_assert (builder, "vars-scroller");
+ if (ia->var_sheet == NULL)
+ {
+ ia->var_sheet = psppire_variable_sheet_new ();
+
+ gtk_container_add (GTK_CONTAINER (vars_scroller), ia->var_sheet);
+
+ ia->dict = dict_create (get_default_encoding ());
+
+ gtk_widget_show_all (vars_scroller);
+ }
+ GtkWidget *data_scroller = get_widget_assert (builder, "data-scroller");
+ if (ia->data_sheet == NULL)
+ {
+ ia->data_sheet = psppire_data_sheet_new ();
+
+ gtk_container_add (GTK_CONTAINER (data_scroller), ia->data_sheet);
+
+ gtk_widget_show_all (data_scroller);
+ }
+
add_page_to_assistant (ia, w,
GTK_ASSISTANT_PAGE_CONFIRM, _("Adjust Variable Formats"));
}
}
-
static void
first_line_append_syntax (const PsppireImportAssistant *ia, struct string *s)
{
- if (ia->skip_lines > 0)
- ds_put_format (s, " /FIRSTCASE=%d\n", ia->skip_lines + 1);
-}
+ gint first_case = 0;
+ g_object_get (ia->delimiters_model, "first-line", &first_case, NULL);
+ if (first_case > 0)
+ ds_put_format (s, " /FIRSTCASE=%d\n", first_case + 1);
+}
static void
intro_append_syntax (const PsppireImportAssistant *ia, struct string *s)
gchar *file_name = NULL;
gchar *encoding = NULL;
g_object_get (ia->text_file,
- "filename", &file_name,
+ "file-name", &file_name,
"encoding", &encoding,
NULL);