From: John Darrington Date: Mon, 5 Jun 2017 12:49:44 +0000 (+0200) Subject: Import Assistant: Mark lines beyond the declared maximum as insensitive X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0dffb4260d17aa199047cccf8423a5f92d633d6;p=pspp Import Assistant: Mark lines beyond the declared maximum as insensitive --- diff --git a/src/ui/gui/psppire-import-assistant.c b/src/ui/gui/psppire-import-assistant.c index 73879a1b51..f4f41efa64 100644 --- a/src/ui/gui/psppire-import-assistant.c +++ b/src/ui/gui/psppire-import-assistant.c @@ -612,7 +612,6 @@ chooser_page_enter (PsppireImportAssistant *ia, GtkWidget *page) static void chooser_page_leave (PsppireImportAssistant *ia, GtkWidget *page) { - g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__); 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); @@ -778,16 +777,14 @@ static void on_intro_amount_changed (PsppireImportAssistant *p) { gtk_widget_set_sensitive (p->n_cases_spin, - gtk_toggle_button_get_active ( - GTK_TOGGLE_BUTTON (p->n_cases_button))); + gtk_toggle_button_get_active + (GTK_TOGGLE_BUTTON (p->n_cases_button))); gtk_widget_set_sensitive (p->percent_spin, - gtk_toggle_button_get_active ( - GTK_TOGGLE_BUTTON (p->percent_button))); + gtk_toggle_button_get_active + (GTK_TOGGLE_BUTTON (p->percent_button))); } - - static void on_treeview_selection_change (PsppireImportAssistant *ia) { @@ -797,30 +794,45 @@ on_treeview_selection_change (PsppireImportAssistant *ia) GtkTreeIter iter; if (gtk_tree_selection_get_selected (selection, &model, &iter)) { + gint max_lines; int n; GtkTreePath *path = gtk_tree_model_get_path (model, &iter); gint *index = gtk_tree_path_get_indices (path); - n = *index; - gtk_tree_path_free (path); - - gtk_widget_set_sensitive (ia->variable_names_cb, n > 0); - - ia->delimiters_model - = psppire_delimited_text_new (GTK_TREE_MODEL (ia->text_file)); + g_object_get (model, "maximum-lines", &max_lines, NULL); + gtk_widget_set_sensitive (ia->variable_names_cb, + (n > 0 && n < max_lines)); + ia->delimiters_model = + psppire_delimited_text_new (GTK_TREE_MODEL (ia->text_file)); g_object_set (ia->delimiters_model, "first-line", n, NULL); - - g_print ("%s:%d DT %p first line %d\n", __FILE__, __LINE__, ia->delimiters_model, n); } } +static void +render_text_preview_line (GtkTreeViewColumn *tree_column, + GtkCellRenderer *cell, + GtkTreeModel *tree_model, + GtkTreeIter *iter, + gpointer data) +{ + /* + Set the text to a "insensitive" state if the row + is greater than what the user declared to be the maximum. + */ + PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (data); + GtkTreePath *path = gtk_tree_model_get_path (tree_model, iter); + gint *ii = gtk_tree_path_get_indices (path); + gint max_lines; + g_object_get (tree_model, "maximum-lines", &max_lines, NULL); + g_object_set (cell, "sensitive", (*ii < max_lines), NULL); + gtk_tree_path_free (path); +} /* Initializes IA's first_line substructure. */ static void first_line_page_create (PsppireImportAssistant *ia) { - 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", on_treeview_selection_change); @@ -840,24 +852,46 @@ first_line_page_create (PsppireImportAssistant *ia) GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes (_("Line"), renderer, "text", 0, NULL); + + gtk_tree_view_column_set_cell_data_func (column, renderer, render_text_preview_line, ia, 0); gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes (_("Text"), renderer, "text", 1, NULL); - gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column); + gtk_tree_view_column_set_cell_data_func (column, renderer, render_text_preview_line, ia, 0); - gtk_container_add (GTK_CONTAINER (scrolled_window), ia->first_line_tree_view); + gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column); g_signal_connect_swapped (ia->first_line_tree_view, "cursor-changed", - G_CALLBACK (on_treeview_selection_change), ia); + G_CALLBACK (on_treeview_selection_change), ia); + gtk_container_add (GTK_CONTAINER (scrolled_window), ia->first_line_tree_view); } + gtk_widget_show_all (scrolled_window); ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names"); - } - +static void +intro_on_leave (PsppireImportAssistant *ia) +{ + gint lc = 0; + g_object_get (ia->text_file, "line-count", &lc, NULL); + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button))) + { + gint max_lines = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)); + g_object_set (ia->text_file, "maximum-lines", max_lines, NULL); + } + else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button))) + { + gdouble percent = gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin)); + g_object_set (ia->text_file, "maximum-lines", (gint) (lc * percent / 100.0), NULL); + } + else + { + g_object_set (ia->text_file, "maximum-lines", lc, NULL); + } +} static void @@ -963,6 +997,7 @@ intro_page_create (PsppireImportAssistant *ia) G_CALLBACK (on_intro_amount_changed), ia); + g_object_set_data (G_OBJECT (w), "on-forward", intro_on_leave); g_object_set_data (G_OBJECT (w), "on-entering", intro_on_enter); g_object_set_data (G_OBJECT (w), "on-reset", reset_intro_page); } diff --git a/src/ui/gui/psppire-import-assistant.h b/src/ui/gui/psppire-import-assistant.h index 0610fad29b..4288982f62 100644 --- a/src/ui/gui/psppire-import-assistant.h +++ b/src/ui/gui/psppire-import-assistant.h @@ -113,7 +113,6 @@ struct _PsppireImportAssistant int response; PsppireTextFile *text_file; - GtkTreeModel *delimiters_model; struct sheet_spec_page *sheet_spec; diff --git a/src/ui/gui/psppire-text-file.c b/src/ui/gui/psppire-text-file.c index 9030896638..590455d90e 100644 --- a/src/ui/gui/psppire-text-file.c +++ b/src/ui/gui/psppire-text-file.c @@ -36,7 +36,9 @@ enum { PROP_0, PROP_FILE_NAME, - PROP_ENCODING + PROP_ENCODING, + PROP_MAXIMUM_LINES, + PROP_LINE_COUNT }; enum {MAX_LINE_LEN = 16384}; /* Max length of an acceptable line. */ @@ -124,7 +126,6 @@ read_lines (PsppireTextFile *tf) done: line_reader_close (reader); } - } static void @@ -137,6 +138,9 @@ psppire_text_file_set_property (GObject *object, switch (prop_id) { + case PROP_MAXIMUM_LINES: + tf->maximum_lines = g_value_get_int (value); + break; case PROP_FILE_NAME: tf->file_name = g_value_dup_string (value); read_lines (tf); @@ -163,6 +167,12 @@ psppire_text_file_get_property (GObject *object, switch (prop_id) { + case PROP_MAXIMUM_LINES: + g_value_set_int (value, text_file->maximum_lines); + break; + case PROP_LINE_COUNT: + g_value_set_int (value, text_file->line_cnt); + break; case PROP_FILE_NAME: g_value_set_string (value, text_file->file_name); break; @@ -184,7 +194,6 @@ static void psppire_text_file_dispose (GObject *object); static GObjectClass *parent_class = NULL; - static gboolean __tree_get_iter (GtkTreeModel *tree_model, GtkTreeIter *iter, @@ -426,6 +435,20 @@ psppire_text_file_class_init (PsppireTextFileClass *class) parent_class = g_type_class_peek_parent (class); object_class = (GObjectClass*) class; + GParamSpec *maximum_lines_spec = + g_param_spec_int ("maximum-lines", + "Maximum Lines", + P_("An upper limit on the number of lines to consider"), + 0, G_MAXINT, G_MAXINT, + G_PARAM_READWRITE); + + GParamSpec *line_count_spec = + g_param_spec_int ("line-count", + "Line Count", + P_("The number of lines in the file"), + 0, G_MAXINT, G_MAXINT, + G_PARAM_READABLE); + GParamSpec *file_name_spec = g_param_spec_string ("file-name", "File Name", @@ -443,6 +466,14 @@ psppire_text_file_class_init (PsppireTextFileClass *class) object_class->set_property = psppire_text_file_set_property; object_class->get_property = psppire_text_file_get_property; + g_object_class_install_property (object_class, + PROP_MAXIMUM_LINES, + maximum_lines_spec); + + g_object_class_install_property (object_class, + PROP_LINE_COUNT, + line_count_spec); + g_object_class_install_property (object_class, PROP_FILE_NAME, file_name_spec); @@ -455,7 +486,6 @@ psppire_text_file_class_init (PsppireTextFileClass *class) object_class->dispose = psppire_text_file_dispose; } - static void psppire_text_file_init (PsppireTextFile *text_file) { diff --git a/src/ui/gui/psppire-text-file.h b/src/ui/gui/psppire-text-file.h index 500acc82bd..486cefb4cc 100644 --- a/src/ui/gui/psppire-text-file.h +++ b/src/ui/gui/psppire-text-file.h @@ -57,6 +57,8 @@ struct _PsppireTextFile gchar *file_name; gchar *encoding; + gint maximum_lines; + /* The first several lines of the file. These copies which are UTF8 encoded, regardless of the file encoding. */ struct substring lines[MAX_PREVIEW_LINES];