Import Assistant: Mark lines beyond the declared maximum as insensitive
authorJohn Darrington <john@darrington.wattle.id.au>
Mon, 5 Jun 2017 12:49:44 +0000 (14:49 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Tue, 6 Jun 2017 05:58:29 +0000 (07:58 +0200)
src/ui/gui/psppire-import-assistant.c
src/ui/gui/psppire-import-assistant.h
src/ui/gui/psppire-text-file.c
src/ui/gui/psppire-text-file.h

index 73879a1b51eb8d496bb8ee19b3d168de92eea6db..f4f41efa64dee7b611f14a3d0aa3c3c5cd8ee99f 100644 (file)
@@ -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);
 }
index 0610fad29ba548d4712d2a9282698962276aa77e..4288982f62b6d11e268d73f648395de716ab3042 100644 (file)
@@ -113,7 +113,6 @@ struct _PsppireImportAssistant
   int response;
 
   PsppireTextFile *text_file;
-
   GtkTreeModel *delimiters_model;
 
   struct sheet_spec_page *sheet_spec;
index 9030896638d2ef2358a37626a36c7acb291353c4..590455d90e49151bc040dcb70d448fc90633a288 100644 (file)
@@ -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)
 {
index 500acc82bd0086d7d61b95f821d005df0d6b55f4..486cefb4cca9a4268418e20d5cd92b652212f356 100644 (file)
@@ -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];