1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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"
21 #include "page-intro.h"
22 #include "page-sheet-spec.h"
23 #include "page-first-line.h"
24 #include "page-separators.h"
25 #include "page-formats.h"
34 #include "data/data-in.h"
35 #include "data/data-out.h"
36 #include "data/format-guesser.h"
37 #include "data/value-labels.h"
38 #include "language/data-io/data-parser.h"
39 #include "language/lexer/lexer.h"
40 #include "libpspp/assertion.h"
41 #include "libpspp/i18n.h"
42 #include "libpspp/line-reader.h"
43 #include "libpspp/message.h"
44 #include "ui/gui/dialog-common.h"
45 #include "ui/gui/executor.h"
46 #include "ui/gui/helper.h"
47 #include "ui/gui/builder-wrapper.h"
48 #include "ui/gui/pspp-sheet-selection.h"
49 #include "ui/gui/pspp-sheet-view.h"
50 #include "ui/gui/psppire-data-window.h"
51 #include "ui/gui/psppire-dialog.h"
52 #include "ui/gui/psppire-encoding-selector.h"
53 #include "ui/gui/psppire-empty-list-store.h"
54 #include "ui/gui/psppire-var-sheet.h"
55 #include "ui/gui/psppire-scanf.h"
56 #include "ui/syntax-gen.h"
58 #include "gl/intprops.h"
59 #include "gl/xalloc.h"
62 #define _(msgid) gettext (msgid)
63 #define N_(msgid) msgid
65 struct import_assistant;
67 static void apply_dict (const struct dictionary *, struct string *);
68 static char *generate_syntax (const struct import_assistant *);
70 static void add_line_number_column (const struct import_assistant *,
73 /* Pops up the Text Data Import assistant. */
75 text_data_import_assistant (PsppireDataWindow *dw)
77 GtkWindow *parent_window = GTK_WINDOW (dw);
78 struct import_assistant *ia = init_assistant (parent_window);
79 struct sheet_spec_page *ssp ;
81 if (!init_file (ia, parent_window))
91 ia->sheet_spec = sheet_spec_page_create (ia);
95 ia->intro = intro_page_create (ia);
96 ia->first_line = first_line_page_create (ia);
97 ia->separators = separators_page_create (ia);
99 ia->formats = formats_page_create (ia);
101 gtk_widget_show_all (GTK_WIDGET (ia->asst.assistant));
103 ia->asst.main_loop = g_main_loop_new (NULL, false);
107 Instead of this block,
108 A simple g_main_loop_run (ia->asst.main_loop); should work here. But it seems to crash.
111 GMainContext *ctx = g_main_loop_get_context (ia->asst.main_loop);
112 ia->asst.loop_done = false;
113 while (! ia->asst.loop_done)
115 g_main_context_iteration (ctx, TRUE);
118 g_main_loop_unref (ia->asst.main_loop);
120 switch (ia->asst.response)
122 case GTK_RESPONSE_APPLY:
124 gchar *fn = g_path_get_basename (ia->file.file_name);
125 open_data_window (PSPPIRE_WINDOW (dw), fn, NULL, generate_syntax (ia));
129 case PSPPIRE_RESPONSE_PASTE:
130 free (paste_syntax_to_window (generate_syntax (ia)));
138 destroy_formats_page (ia);
139 destroy_separators_page (ia);
143 /* Emits PSPP syntax to S that applies the dictionary attributes
144 (such as missing values and value labels) of the variables in
147 apply_dict (const struct dictionary *dict, struct string *s)
149 size_t var_cnt = dict_get_var_cnt (dict);
152 for (i = 0; i < var_cnt; i++)
154 struct variable *var = dict_get_var (dict, i);
155 const char *name = var_get_name (var);
156 enum val_type type = var_get_type (var);
157 int width = var_get_width (var);
158 enum measure measure = var_get_measure (var);
159 enum var_role role = var_get_role (var);
160 enum alignment alignment = var_get_alignment (var);
161 const struct fmt_spec *format = var_get_print_format (var);
163 if (var_has_missing_values (var))
165 const struct missing_values *mv = var_get_missing_values (var);
168 syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
169 for (j = 0; j < mv_n_values (mv); j++)
172 ds_put_cstr (s, ", ");
173 syntax_gen_value (s, mv_get_value (mv, j), width, format);
176 if (mv_has_range (mv))
179 if (mv_has_value (mv))
180 ds_put_cstr (s, ", ");
181 mv_get_range (mv, &low, &high);
182 syntax_gen_num_range (s, low, high, format);
184 ds_put_cstr (s, ").\n");
186 if (var_has_value_labels (var))
188 const struct val_labs *vls = var_get_value_labels (var);
189 const struct val_lab **labels = val_labs_sorted (vls);
190 size_t n_labels = val_labs_count (vls);
193 syntax_gen_pspp (s, "VALUE LABELS %ss", name);
194 for (i = 0; i < n_labels; i++)
196 const struct val_lab *vl = labels[i];
197 ds_put_cstr (s, "\n ");
198 syntax_gen_value (s, &vl->value, width, format);
199 ds_put_byte (s, ' ');
200 syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
203 ds_put_cstr (s, ".\n");
205 if (var_has_label (var))
206 syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
207 name, var_get_label (var));
208 if (measure != var_default_measure (type))
209 syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
210 name, measure_to_syntax (measure));
211 if (role != ROLE_INPUT)
212 syntax_gen_pspp (s, "VARIABLE ROLE /%ss %ss.\n",
213 var_role_to_syntax (role), name);
214 if (alignment != var_default_alignment (type))
215 syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
216 name, alignment_to_syntax (alignment));
217 if (var_get_display_width (var) != var_default_display_width (width))
218 syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
219 name, var_get_display_width (var));
223 /* Generates and returns PSPP syntax to execute the import
224 operation described by IA. The caller must free the syntax
227 generate_syntax (const struct import_assistant *ia)
229 struct string s = DS_EMPTY_INITIALIZER;
231 if (ia->spreadsheet == NULL)
238 if (ia->file.encoding && strcmp (ia->file.encoding, "Auto"))
239 syntax_gen_pspp (&s, " /ENCODING=%sq\n", ia->file.encoding);
241 intro_append_syntax (ia->intro, &s);
245 " /ARRANGEMENT=DELIMITED\n"
248 first_line_append_syntax (ia, &s);
249 separators_append_syntax (ia, &s);
250 formats_append_syntax (ia, &s);
251 apply_dict (ia->dict, &s);
255 return sheet_spec_gen_syntax (ia);
263 static void render_input_cell (PsppSheetViewColumn *tree_column,
264 GtkCellRenderer *cell,
265 GtkTreeModel *model, GtkTreeIter *iter,
268 static gboolean on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
269 gboolean keyboard_mode UNUSED,
271 struct import_assistant *);
275 /* Called to render one of the cells in the fields preview tree
278 render_input_cell (PsppSheetViewColumn *tree_column, GtkCellRenderer *cell,
279 GtkTreeModel *model, GtkTreeIter *iter,
282 struct import_assistant *ia = ia_;
283 struct substring field;
287 column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
289 row = empty_list_store_iter_to_row (iter) + ia->skip_lines;
290 field = ia->columns[column].contents[row];
291 if (field.string != NULL)
294 g_value_init (&text, G_TYPE_STRING);
295 g_value_take_string (&text, ss_xstrdup (field));
296 g_object_set_property (G_OBJECT (cell), "text", &text);
297 g_value_unset (&text);
298 g_object_set (cell, "background-set", FALSE, (void *) NULL);
304 "background-set", TRUE,
309 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
310 const struct import_assistant *ia,
311 size_t *row, size_t *column);
314 /* Called to render a tooltip on one of the cells in the fields
315 preview tree view. */
317 on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
318 gboolean keyboard_mode UNUSED,
319 GtkTooltip *tooltip, struct import_assistant *ia)
323 if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
326 if (ia->columns[column].contents[row].string != NULL)
329 gtk_tooltip_set_text (tooltip,
330 _("This input line has too few separators "
331 "to fill in this field."));
336 /* Parses the contents of the field at (ROW,COLUMN) according to
337 its variable format. If OUTPUTP is non-null, then *OUTPUTP
338 receives the formatted output for that field (which must be
339 freed with free). If TOOLTIPP is non-null, then *TOOLTIPP
340 receives a message suitable for use in a tooltip, if one is
341 needed, or a null pointer otherwise. Returns true if a
342 tooltip message is needed, otherwise false. */
344 parse_field (struct import_assistant *ia,
345 size_t row, size_t column,
346 char **outputp, char **tooltipp)
348 const struct fmt_spec *in;
353 struct substring field = ia->columns[column].contents[row];
354 struct variable *var = dict_get_var (ia->dict, column);
357 value_init (&val, var_get_width (var));
358 in = var_get_print_format (var);
359 out = fmt_for_output_from_input (in);
361 if (field.string != NULL)
365 error = data_in (field, "UTF-8", in->type, &val, var_get_width (var),
366 dict_get_encoding (ia->dict));
369 tooltip = xasprintf (_("Cannot parse field content `%.*s' as "
371 (int) field.length, field.string,
372 fmt_name (in->type), error);
378 tooltip = xstrdup (_("This input line has too few separators "
379 "to fill in this field."));
380 value_set_missing (&val, var_get_width (var));
384 *outputp = data_out (&val, dict_get_encoding (ia->dict), &out);
386 value_destroy (&val, var_get_width (var));
388 ok = tooltip == NULL;
389 if (tooltipp != NULL)
396 /* Called to render one of the cells in the data preview tree
399 render_output_cell (PsppSheetViewColumn *tree_column,
400 GtkCellRenderer *cell,
405 struct import_assistant *ia = ia_;
407 GValue gvalue = { 0, };
410 ok = parse_field (ia,
411 (empty_list_store_iter_to_row (iter)
413 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
417 g_value_init (&gvalue, G_TYPE_STRING);
418 g_value_take_string (&gvalue, output);
419 g_object_set_property (G_OBJECT (cell), "text", &gvalue);
420 g_value_unset (&gvalue);
423 g_object_set (cell, "background-set", FALSE, (void *) NULL);
427 "background-set", TRUE,
431 /* Called to render a tooltip for one of the cells in the data
432 preview tree view. */
434 on_query_output_tooltip (GtkWidget *widget, gint wx, gint wy,
435 gboolean keyboard_mode UNUSED,
436 GtkTooltip *tooltip, struct import_assistant *ia)
441 if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
444 if (parse_field (ia, row, column, NULL, &text))
447 gtk_tooltip_set_text (tooltip, text);
452 /* Utility functions used by multiple pages of the assistant. */
455 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
456 const struct import_assistant *ia,
457 size_t *row, size_t *column)
459 PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
463 PsppSheetViewColumn *tree_column;
464 GtkTreeModel *tree_model;
467 /* Check that WIDGET is really visible on the screen before we
468 do anything else. This is a bug fix for a sticky situation:
469 when text_data_import_assistant() returns, it frees the data
470 necessary to compose the tool tip message, but there may be
471 a tool tip under preparation at that point (even if there is
472 no visible tool tip) that will call back into us a little
473 bit later. Perhaps the correct solution to this problem is
474 to make the data related to the tool tips part of a GObject
475 that only gets destroyed when all references are released,
476 but this solution appears to be effective too. */
477 if (!gtk_widget_get_mapped (widget))
480 pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
482 if (!pspp_sheet_view_get_path_at_pos (tree_view, bx, by,
483 &path, &tree_column, NULL, NULL))
486 *column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
489 tree_model = pspp_sheet_view_get_model (tree_view);
490 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
491 gtk_tree_path_free (path);
495 *row = empty_list_store_iter_to_row (&iter) + ia->skip_lines;
500 make_tree_view (const struct import_assistant *ia,
502 PsppSheetView **tree_view)
506 *tree_view = PSPP_SHEET_VIEW (pspp_sheet_view_new ());
507 pspp_sheet_view_set_grid_lines (*tree_view, PSPP_SHEET_VIEW_GRID_LINES_BOTH);
508 model = GTK_TREE_MODEL (psppire_empty_list_store_new (
509 ia->file.line_cnt - first_line));
510 g_object_set_data (G_OBJECT (model), "lines", ia->file.lines + first_line);
511 g_object_set_data (G_OBJECT (model), "first-line",
512 GINT_TO_POINTER (first_line));
513 pspp_sheet_view_set_model (*tree_view, model);
514 g_object_unref (model);
516 add_line_number_column (ia, *tree_view);
520 render_line_number (PsppSheetViewColumn *tree_column,
521 GtkCellRenderer *cell,
522 GtkTreeModel *tree_model,
526 gint row = empty_list_store_iter_to_row (iter);
527 char s[INT_BUFSIZE_BOUND (int)];
530 first_line = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model),
532 sprintf (s, "%d", first_line + row);
533 g_object_set (cell, "text", s, NULL);
537 add_line_number_column (const struct import_assistant *ia,
538 PsppSheetView *treeview)
540 PsppSheetViewColumn *column;
542 column = pspp_sheet_view_column_new_with_attributes (
543 _("Line"), ia->asst.prop_renderer, (void *) NULL);
544 pspp_sheet_view_column_set_fixed_width (
545 column, get_monospace_width (treeview, ia->asst.prop_renderer, 5));
546 pspp_sheet_view_column_set_resizable (column, TRUE);
547 pspp_sheet_view_column_set_cell_data_func (column, ia->asst.prop_renderer,
548 render_line_number, NULL, NULL);
549 pspp_sheet_view_append_column (treeview, column);
553 get_monospace_width (PsppSheetView *treeview, GtkCellRenderer *renderer,
560 ds_put_byte_multiple (&s, '0', char_cnt);
561 ds_put_byte (&s, ' ');
562 width = get_string_width (treeview, renderer, ds_cstr (&s));
569 get_string_width (PsppSheetView *treeview, GtkCellRenderer *renderer,
573 g_object_set (G_OBJECT (renderer), "text", string, (void *) NULL);
574 gtk_cell_renderer_get_preferred_width (renderer, GTK_WIDGET (treeview),
579 PsppSheetViewColumn *
580 make_data_column (struct import_assistant *ia, PsppSheetView *tree_view,
581 bool input, gint dict_idx)
583 struct variable *var = NULL;
584 struct column *column = NULL;
586 gint content_width, header_width;
587 PsppSheetViewColumn *tree_column;
591 column = &ia->columns[dict_idx];
593 var = dict_get_var (ia->dict, dict_idx);
595 name = escape_underscores (input ? column->name : var_get_name (var));
596 char_cnt = input ? column->width : var_get_print_format (var)->w;
597 content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
599 header_width = get_string_width (tree_view, ia->asst.prop_renderer,
602 tree_column = pspp_sheet_view_column_new ();
603 g_object_set_data (G_OBJECT (tree_column), "column-number",
604 GINT_TO_POINTER (dict_idx));
605 pspp_sheet_view_column_set_title (tree_column, name);
606 pspp_sheet_view_column_pack_start (tree_column, ia->asst.fixed_renderer,
608 pspp_sheet_view_column_set_cell_data_func (
609 tree_column, ia->asst.fixed_renderer,
610 input ? render_input_cell : render_output_cell, ia, NULL);
611 pspp_sheet_view_column_set_fixed_width (tree_column, MAX (content_width,
620 create_data_tree_view (bool input, GtkContainer *parent,
621 struct import_assistant *ia)
623 PsppSheetView *tree_view;
626 make_tree_view (ia, ia->skip_lines, &tree_view);
627 pspp_sheet_selection_set_mode (pspp_sheet_view_get_selection (tree_view),
628 PSPP_SHEET_SELECTION_NONE);
630 for (i = 0; i < ia->column_cnt; i++)
631 pspp_sheet_view_append_column (tree_view,
632 make_data_column (ia, tree_view, input, i));
634 g_object_set (G_OBJECT (tree_view), "has-tooltip", TRUE, (void *) NULL);
635 g_signal_connect (tree_view, "query-tooltip",
636 G_CALLBACK (input ? on_query_input_tooltip
637 : on_query_output_tooltip), ia);
640 gtk_container_add (parent, GTK_WIDGET (tree_view));
641 gtk_widget_show (GTK_WIDGET (tree_view));
646 /* Increments the "watch cursor" level, setting the cursor for
647 the assistant window to a watch face to indicate to the user
648 that the ongoing operation may take some time. */
650 push_watch_cursor (struct import_assistant *ia)
652 if (++ia->asst.watch_cursor == 1)
654 GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
655 GdkDisplay *display = gtk_widget_get_display (widget);
656 GdkCursor *cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
657 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
658 g_object_unref (cursor);
659 gdk_display_flush (display);
663 /* Decrements the "watch cursor" level. If the level reaches
664 zero, the cursor is reset to its default shape. */
666 pop_watch_cursor (struct import_assistant *ia)
668 if (--ia->asst.watch_cursor == 0)
670 GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
671 gdk_window_set_cursor (gtk_widget_get_window (widget), NULL);