1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "ui/gui/text-data-import-dialog.h"
23 #include <gtk-contrib/psppire-sheet.h>
29 #include "data/data-in.h"
30 #include "data/data-out.h"
31 #include "data/format-guesser.h"
32 #include "data/value-labels.h"
33 #include "language/data-io/data-parser.h"
34 #include "language/lexer/lexer.h"
35 #include "libpspp/assertion.h"
36 #include "libpspp/i18n.h"
37 #include "libpspp/line-reader.h"
38 #include "libpspp/message.h"
39 #include "ui/gui/checkbox-treeview.h"
40 #include "ui/gui/dialog-common.h"
41 #include "ui/gui/executor.h"
42 #include "ui/gui/helper.h"
43 #include "ui/gui/builder-wrapper.h"
44 #include "ui/gui/psppire-data-window.h"
45 #include "ui/gui/psppire-dialog.h"
46 #include "ui/gui/psppire-encoding-selector.h"
47 #include "ui/gui/psppire-empty-list-store.h"
48 #include "ui/gui/psppire-var-sheet.h"
49 #include "ui/gui/psppire-var-store.h"
50 #include "ui/gui/psppire-scanf.h"
51 #include "ui/syntax-gen.h"
54 #include "gl/intprops.h"
55 #include "gl/xalloc.h"
58 #define _(msgid) gettext (msgid)
59 #define N_(msgid) msgid
61 struct import_assistant;
63 static void apply_dict (const struct dictionary *, struct string *);
64 static char *generate_syntax (const struct import_assistant *);
66 static void add_line_number_column (const struct import_assistant *,
69 /* Pops up the Text Data Import assistant. */
71 text_data_import_assistant (PsppireDataWindow *dw)
73 GtkWindow *parent_window = GTK_WINDOW (dw);
74 struct import_assistant *ia = init_assistant (parent_window);
75 GtkBuilder *builder = ia->asst.builder;
76 struct sheet_spec_page *ssp ;
78 if (!init_file (ia, parent_window))
86 add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"),
87 GTK_ASSISTANT_PAGE_INTRO);
89 add_page_to_assistant (ia, get_widget_assert (builder, "Formats"),
90 GTK_ASSISTANT_PAGE_CONFIRM);
93 gtk_widget_show_all (GTK_WIDGET (ia->asst.assistant));
95 ia->asst.main_loop = g_main_loop_new (NULL, false);
96 g_main_loop_run (ia->asst.main_loop);
97 g_main_loop_unref (ia->asst.main_loop);
99 switch (ia->asst.response)
101 case GTK_RESPONSE_APPLY:
102 free (execute_syntax_string (dw, generate_syntax (ia)));
104 case PSPPIRE_RESPONSE_PASTE:
105 free (paste_syntax_to_window (generate_syntax (ia)));
111 destroy_formats_page (ia);
112 destroy_separators_page (ia);
114 destroy_assistant (ia);
119 /* Emits PSPP syntax to S that applies the dictionary attributes
120 (such as missing values and value labels) of the variables in
123 apply_dict (const struct dictionary *dict, struct string *s)
125 size_t var_cnt = dict_get_var_cnt (dict);
128 for (i = 0; i < var_cnt; i++)
130 struct variable *var = dict_get_var (dict, i);
131 const char *name = var_get_name (var);
132 enum val_type type = var_get_type (var);
133 int width = var_get_width (var);
134 enum measure measure = var_get_measure (var);
135 enum alignment alignment = var_get_alignment (var);
136 const struct fmt_spec *format = var_get_print_format (var);
138 if (var_has_missing_values (var))
140 const struct missing_values *mv = var_get_missing_values (var);
143 syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
144 for (j = 0; j < mv_n_values (mv); j++)
147 ds_put_cstr (s, ", ");
148 syntax_gen_value (s, mv_get_value (mv, j), width, format);
151 if (mv_has_range (mv))
154 if (mv_has_value (mv))
155 ds_put_cstr (s, ", ");
156 mv_get_range (mv, &low, &high);
157 syntax_gen_num_range (s, low, high, format);
159 ds_put_cstr (s, ").\n");
161 if (var_has_value_labels (var))
163 const struct val_labs *vls = var_get_value_labels (var);
164 const struct val_lab **labels = val_labs_sorted (vls);
165 size_t n_labels = val_labs_count (vls);
168 syntax_gen_pspp (s, "VALUE LABELS %ss", name);
169 for (i = 0; i < n_labels; i++)
171 const struct val_lab *vl = labels[i];
172 ds_put_cstr (s, "\n ");
173 syntax_gen_value (s, &vl->value, width, format);
174 ds_put_byte (s, ' ');
175 syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
178 ds_put_cstr (s, ".\n");
180 if (var_has_label (var))
181 syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
182 name, var_get_label (var));
183 if (measure != var_default_measure (type))
184 syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
186 (measure == MEASURE_NOMINAL ? "NOMINAL"
187 : measure == MEASURE_ORDINAL ? "ORDINAL"
189 if (alignment != var_default_alignment (type))
190 syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
192 (alignment == ALIGN_LEFT ? "LEFT"
193 : alignment == ALIGN_CENTRE ? "CENTER"
195 if (var_get_display_width (var) != var_default_display_width (width))
196 syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
197 name, var_get_display_width (var));
201 /* Generates and returns PSPP syntax to execute the import
202 operation described by IA. The caller must free the syntax
205 generate_syntax (const struct import_assistant *ia)
207 struct string s = DS_EMPTY_INITIALIZER;
210 if (ssp->spreadsheet == NULL)
219 if (ia->file.encoding && strcmp (ia->file.encoding, "Auto"))
220 syntax_gen_pspp (&s, " /ENCODING=%sq\n", ia->file.encoding);
222 intro_append_syntax (ia->intro, &s);
225 " /ARRANGEMENT=DELIMITED\n"
227 if (ia->first_line->skip_lines > 0)
228 ds_put_format (&s, " /FIRSTCASE=%d\n", ia->first_line->skip_lines + 1);
229 ds_put_cstr (&s, " /DELIMITERS=\"");
230 if (ds_find_byte (&ia->separators->separators, '\t') != SIZE_MAX)
231 ds_put_cstr (&s, "\\t");
232 if (ds_find_byte (&ia->separators->separators, '\\') != SIZE_MAX)
233 ds_put_cstr (&s, "\\\\");
234 for (i = 0; i < ds_length (&ia->separators->separators); i++)
236 char c = ds_at (&ia->separators->separators, i);
238 ds_put_cstr (&s, "\"\"");
239 else if (c != '\t' && c != '\\')
242 ds_put_cstr (&s, "\"\n");
243 if (!ds_is_empty (&ia->separators->quotes))
244 syntax_gen_pspp (&s, " /QUALIFIER=%sq\n", ds_cstr (&ia->separators->quotes));
245 if (!ds_is_empty (&ia->separators->quotes) && ia->separators->escape)
246 ds_put_cstr (&s, " /ESCAPE\n");
247 ds_put_cstr (&s, " /VARIABLES=\n");
249 var_cnt = dict_get_var_cnt (ia->formats->dict);
250 for (i = 0; i < var_cnt; i++)
252 struct variable *var = dict_get_var (ia->formats->dict, i);
253 char format_string[FMT_STRING_LEN_MAX + 1];
254 fmt_to_string (var_get_print_format (var), format_string);
255 ds_put_format (&s, " %s %s%s\n",
256 var_get_name (var), format_string,
257 i == var_cnt - 1 ? "." : "");
260 apply_dict (ia->formats->dict, &s);
265 const struct sheet_spec_page *ssp = ia->sheet_spec;
273 (ssp->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
275 ssp->opts.sheet_index,
276 ssp->sri.read_names ? "ON" : "OFF");
279 if ( ssp->opts.cell_range)
282 "\n /CELLRANGE=RANGE %sq",
283 ssp->opts.cell_range);
288 "\n /CELLRANGE=FULL");
292 syntax_gen_pspp (&s, ".");
301 static void render_input_cell (GtkTreeViewColumn *tree_column,
302 GtkCellRenderer *cell,
303 GtkTreeModel *model, GtkTreeIter *iter,
306 static gboolean on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
307 gboolean keyboard_mode UNUSED,
309 struct import_assistant *);
313 /* Called to render one of the cells in the fields preview tree
316 render_input_cell (GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
317 GtkTreeModel *model, GtkTreeIter *iter,
320 struct import_assistant *ia = ia_;
321 struct substring field;
325 column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
327 row = empty_list_store_iter_to_row (iter) + ia->skip_lines;
328 field = ia->columns[column].contents[row];
329 if (field.string != NULL)
332 g_value_init (&text, G_TYPE_STRING);
333 g_value_take_string (&text, ss_xstrdup (field));
334 g_object_set_property (G_OBJECT (cell), "text", &text);
335 g_value_unset (&text);
336 g_object_set (cell, "background-set", FALSE, (void *) NULL);
342 "background-set", TRUE,
347 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
348 const struct import_assistant *ia,
349 size_t *row, size_t *column);
352 /* Called to render a tooltip on one of the cells in the fields
353 preview tree view. */
355 on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
356 gboolean keyboard_mode UNUSED,
357 GtkTooltip *tooltip, struct import_assistant *ia)
361 if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
364 if (ia->columns[column].contents[row].string != NULL)
367 gtk_tooltip_set_text (tooltip,
368 _("This input line has too few separators "
369 "to fill in this field."));
374 /* Parses the contents of the field at (ROW,COLUMN) according to
375 its variable format. If OUTPUTP is non-null, then *OUTPUTP
376 receives the formatted output for that field (which must be
377 freed with free). If TOOLTIPP is non-null, then *TOOLTIPP
378 receives a message suitable for use in a tooltip, if one is
379 needed, or a null pointer otherwise. Returns true if a
380 tooltip message is needed, otherwise false. */
382 parse_field (struct import_assistant *ia,
383 size_t row, size_t column,
384 char **outputp, char **tooltipp)
386 const struct fmt_spec *in;
391 struct substring field = ia->columns[column].contents[row];
392 struct variable *var = dict_get_var (ia->dict, column);
395 value_init (&val, var_get_width (var));
396 in = var_get_print_format (var);
397 out = fmt_for_output_from_input (in);
399 if (field.string != NULL)
403 error = data_in (field, "UTF-8", in->type, &val, var_get_width (var),
404 dict_get_encoding (ia->dict));
407 tooltip = xasprintf (_("Cannot parse field content `%.*s' as "
409 (int) field.length, field.string,
410 fmt_name (in->type), error);
416 tooltip = xstrdup (_("This input line has too few separators "
417 "to fill in this field."));
418 value_set_missing (&val, var_get_width (var));
422 *outputp = data_out (&val, dict_get_encoding (ia->dict), &out);
424 value_destroy (&val, var_get_width (var));
426 ok = tooltip == NULL;
427 if (tooltipp != NULL)
434 /* Called to render one of the cells in the data preview tree
437 render_output_cell (GtkTreeViewColumn *tree_column,
438 GtkCellRenderer *cell,
443 struct import_assistant *ia = ia_;
445 GValue gvalue = { 0, };
448 ok = parse_field (ia,
449 (empty_list_store_iter_to_row (iter)
451 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
455 g_value_init (&gvalue, G_TYPE_STRING);
456 g_value_take_string (&gvalue, output);
457 g_object_set_property (G_OBJECT (cell), "text", &gvalue);
458 g_value_unset (&gvalue);
461 g_object_set (cell, "background-set", FALSE, (void *) NULL);
465 "background-set", TRUE,
469 /* Called to render a tooltip for one of the cells in the data
470 preview tree view. */
472 on_query_output_tooltip (GtkWidget *widget, gint wx, gint wy,
473 gboolean keyboard_mode UNUSED,
474 GtkTooltip *tooltip, struct import_assistant *ia)
479 if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
482 if (parse_field (ia, row, column, NULL, &text))
485 gtk_tooltip_set_text (tooltip, text);
490 /* Utility functions used by multiple pages of the assistant. */
493 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
494 const struct import_assistant *ia,
495 size_t *row, size_t *column)
497 GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
501 GtkTreeViewColumn *tree_column;
502 GtkTreeModel *tree_model;
505 /* Check that WIDGET is really visible on the screen before we
506 do anything else. This is a bug fix for a sticky situation:
507 when text_data_import_assistant() returns, it frees the data
508 necessary to compose the tool tip message, but there may be
509 a tool tip under preparation at that point (even if there is
510 no visible tool tip) that will call back into us a little
511 bit later. Perhaps the correct solution to this problem is
512 to make the data related to the tool tips part of a GObject
513 that only gets destroyed when all references are released,
514 but this solution appears to be effective too. */
515 if (!gtk_widget_get_mapped (widget))
518 gtk_tree_view_convert_widget_to_bin_window_coords (tree_view,
520 if (!gtk_tree_view_get_path_at_pos (tree_view, bx, by,
521 &path, &tree_column, NULL, NULL))
524 *column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
527 tree_model = gtk_tree_view_get_model (tree_view);
528 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
529 gtk_tree_path_free (path);
533 *row = empty_list_store_iter_to_row (&iter) + ia->skip_lines;
538 make_tree_view (const struct import_assistant *ia,
540 GtkTreeView **tree_view)
544 *tree_view = GTK_TREE_VIEW (gtk_tree_view_new ());
545 model = GTK_TREE_MODEL (psppire_empty_list_store_new (
546 ia->file.line_cnt - first_line));
547 g_object_set_data (G_OBJECT (model), "lines", ia->file.lines + first_line);
548 g_object_set_data (G_OBJECT (model), "first-line",
549 GINT_TO_POINTER (first_line));
550 gtk_tree_view_set_model (*tree_view, model);
551 g_object_unref (model);
553 add_line_number_column (ia, *tree_view);
557 render_line_number (GtkTreeViewColumn *tree_column,
558 GtkCellRenderer *cell,
559 GtkTreeModel *tree_model,
563 gint row = empty_list_store_iter_to_row (iter);
564 char s[INT_BUFSIZE_BOUND (int)];
567 first_line = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model),
569 sprintf (s, "%d", first_line + row);
570 g_object_set (cell, "text", s, NULL);
574 add_line_number_column (const struct import_assistant *ia,
575 GtkTreeView *treeview)
577 GtkTreeViewColumn *column;
579 column = gtk_tree_view_column_new_with_attributes (
580 _("Line"), ia->asst.prop_renderer, (void *) NULL);
581 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
582 gtk_tree_view_column_set_fixed_width (
583 column, get_monospace_width (treeview, ia->asst.prop_renderer, 5));
584 gtk_tree_view_column_set_resizable (column, TRUE);
585 gtk_tree_view_column_set_cell_data_func (column, ia->asst.prop_renderer,
586 render_line_number, NULL, NULL);
587 gtk_tree_view_append_column (treeview, column);
591 get_monospace_width (GtkTreeView *treeview, GtkCellRenderer *renderer,
598 ds_put_byte_multiple (&s, '0', char_cnt);
599 ds_put_byte (&s, ' ');
600 width = get_string_width (treeview, renderer, ds_cstr (&s));
607 get_string_width (GtkTreeView *treeview, GtkCellRenderer *renderer,
611 g_object_set (G_OBJECT (renderer), "text", string, (void *) NULL);
612 gtk_cell_renderer_get_size (renderer, GTK_WIDGET (treeview),
613 NULL, NULL, NULL, &width, NULL);
618 make_data_column (struct import_assistant *ia, GtkTreeView *tree_view,
619 bool input, gint dict_idx)
621 struct variable *var = NULL;
622 struct column *column = NULL;
624 gint content_width, header_width;
625 GtkTreeViewColumn *tree_column;
629 column = &ia->columns[dict_idx];
631 var = dict_get_var (ia->dict, dict_idx);
633 name = escape_underscores (input ? column->name : var_get_name (var));
634 char_cnt = input ? column->width : var_get_print_format (var)->w;
635 content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
637 header_width = get_string_width (tree_view, ia->asst.prop_renderer,
640 tree_column = gtk_tree_view_column_new ();
641 g_object_set_data (G_OBJECT (tree_column), "column-number",
642 GINT_TO_POINTER (dict_idx));
643 gtk_tree_view_column_set_title (tree_column, name);
644 gtk_tree_view_column_pack_start (tree_column, ia->asst.fixed_renderer,
646 gtk_tree_view_column_set_cell_data_func (
647 tree_column, ia->asst.fixed_renderer,
648 input ? render_input_cell : render_output_cell, ia, NULL);
649 gtk_tree_view_column_set_sizing (tree_column, GTK_TREE_VIEW_COLUMN_FIXED);
650 gtk_tree_view_column_set_fixed_width (tree_column, MAX (content_width,
659 create_data_tree_view (bool input, GtkContainer *parent,
660 struct import_assistant *ia)
662 GtkTreeView *tree_view;
665 make_tree_view (ia, ia->skip_lines, &tree_view);
666 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (tree_view),
669 for (i = 0; i < ia->column_cnt; i++)
670 gtk_tree_view_append_column (tree_view,
671 make_data_column (ia, tree_view, input, i));
673 g_object_set (G_OBJECT (tree_view), "has-tooltip", TRUE, (void *) NULL);
674 g_signal_connect (tree_view, "query-tooltip",
675 G_CALLBACK (input ? on_query_input_tooltip
676 : on_query_output_tooltip), ia);
677 gtk_tree_view_set_fixed_height_mode (tree_view, true);
679 gtk_container_add (parent, GTK_WIDGET (tree_view));
680 gtk_widget_show (GTK_WIDGET (tree_view));
685 /* Increments the "watch cursor" level, setting the cursor for
686 the assistant window to a watch face to indicate to the user
687 that the ongoing operation may take some time. */
689 push_watch_cursor (struct import_assistant *ia)
691 if (++ia->asst.watch_cursor == 1)
693 GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
694 GdkDisplay *display = gtk_widget_get_display (widget);
695 GdkCursor *cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
696 gdk_window_set_cursor (widget->window, cursor);
697 gdk_cursor_unref (cursor);
698 gdk_display_flush (display);
702 /* Decrements the "watch cursor" level. If the level reaches
703 zero, the cursor is reset to its default shape. */
705 pop_watch_cursor (struct import_assistant *ia)
707 if (--ia->asst.watch_cursor == 0)
709 GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
710 gdk_window_set_cursor (widget->window, NULL);