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"
28 #include "data/data-in.h"
29 #include "data/data-out.h"
30 #include "data/format-guesser.h"
31 #include "data/value-labels.h"
32 #include "language/data-io/data-parser.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/line-reader.h"
37 #include "libpspp/message.h"
38 #include "ui/gui/checkbox-treeview.h"
39 #include "ui/gui/dialog-common.h"
40 #include "ui/gui/executor.h"
41 #include "ui/gui/helper.h"
42 #include "ui/gui/builder-wrapper.h"
43 #include "ui/gui/psppire-data-window.h"
44 #include "ui/gui/psppire-dialog.h"
45 #include "ui/gui/psppire-encoding-selector.h"
46 #include "ui/gui/psppire-empty-list-store.h"
47 #include "ui/gui/psppire-var-sheet.h"
48 #include "ui/gui/psppire-scanf.h"
51 #include "gl/intprops.h"
52 #include "gl/xalloc.h"
55 #define _(msgid) gettext (msgid)
56 #define N_(msgid) msgid
58 /* Page where the user verifies and adjusts input formats. */
62 PsppSheetView *data_tree_view;
63 PsppireDict *psppire_dict;
64 struct variable **modified_vars;
65 size_t modified_var_cnt;
68 /* The "formats" page of the assistant. */
70 static void on_variable_change (PsppireDict *dict, int idx,
71 struct import_assistant *);
72 static void clear_modified_vars (struct import_assistant *);
74 /* Initializes IA's formats substructure. */
77 formats_page_create (struct import_assistant *ia)
79 GtkBuilder *builder = ia->asst.builder;
80 struct formats_page *p = xzalloc (sizeof *p);
82 p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Formats"),
83 GTK_ASSISTANT_PAGE_CONFIRM);
85 p->data_tree_view = PSPP_SHEET_VIEW (get_widget_assert (builder, "data"));
86 p->modified_vars = NULL;
87 p->modified_var_cnt = 0;
92 /* Frees IA's formats substructure. */
94 destroy_formats_page (struct import_assistant *ia)
96 struct formats_page *p = ia->formats;
98 if (p->psppire_dict != NULL)
100 dict_destroy (p->psppire_dict->dict);
101 g_object_unref (p->psppire_dict);
103 clear_modified_vars (ia);
106 /* Called just before the formats page of the assistant is
109 prepare_formats_page (struct import_assistant *ia)
111 struct dictionary *dict;
112 PsppireDict *psppire_dict;
113 GtkBin *vars_scroller;
114 GtkWidget *old_var_sheet;
115 PsppireVarSheet *var_sheet;
116 struct formats_page *p = ia->formats;
117 struct fmt_guesser *fg;
118 unsigned long int number = 0;
121 push_watch_cursor (ia);
123 dict = dict_create (get_default_encoding ());
124 fg = fmt_guesser_create ();
125 for (column_idx = 0; column_idx < ia->column_cnt; column_idx++)
127 struct variable *modified_var =
128 (column_idx < p->modified_var_cnt ? p->modified_vars[column_idx] : NULL);
129 if (modified_var == NULL)
131 struct column *column = &ia->columns[column_idx];
132 struct variable *var;
133 struct fmt_spec format;
137 /* Choose variable name. */
138 name = dict_make_unique_var_name (dict, column->name, &number);
140 /* Choose variable format. */
141 fmt_guesser_clear (fg);
142 for (row = ia->skip_lines; row < ia->file.line_cnt; row++)
143 fmt_guesser_add (fg, column->contents[row]);
144 fmt_guesser_guess (fg, &format);
145 fmt_fix_input (&format);
147 /* Create variable. */
148 var = dict_create_var_assert (dict, name, fmt_var_width (&format));
149 var_set_both_formats (var, &format);
157 name = dict_make_unique_var_name (dict, var_get_name (modified_var),
159 dict_clone_var_as_assert (dict, modified_var, name);
163 fmt_guesser_destroy (fg);
165 psppire_dict = psppire_dict_new_from_dict (dict);
166 g_signal_connect (psppire_dict, "variable_changed",
167 G_CALLBACK (on_variable_change), ia);
169 ia->formats->psppire_dict = psppire_dict;
171 /* XXX: PsppireVarStore doesn't hold a reference to
172 psppire_dict for now, but it should. After it does, we
173 should g_object_ref the psppire_dict here, since we also
174 hold a reference via ia->formats->dict. */
175 var_sheet = PSPPIRE_VAR_SHEET (psppire_var_sheet_new ());
176 g_object_set (var_sheet,
177 "dictionary", psppire_dict,
178 "may-create-vars", FALSE,
179 "may-delete-vars", FALSE,
180 "format-use", FMT_FOR_INPUT,
181 "enable-grid-lines", PSPP_SHEET_VIEW_GRID_LINES_BOTH,
184 vars_scroller = GTK_BIN (get_widget_assert (ia->asst.builder, "vars-scroller"));
185 old_var_sheet = gtk_bin_get_child (vars_scroller);
186 if (old_var_sheet != NULL)
187 gtk_widget_destroy (old_var_sheet);
188 gtk_container_add (GTK_CONTAINER (vars_scroller), GTK_WIDGET (var_sheet));
189 gtk_widget_show (GTK_WIDGET (var_sheet));
191 gtk_widget_destroy (GTK_WIDGET (ia->formats->data_tree_view));
192 ia->formats->data_tree_view = create_data_tree_view (
194 GTK_CONTAINER (get_widget_assert (ia->asst.builder, "data-scroller")),
197 gtk_widget_show (ia->asst.paste_button);
199 pop_watch_cursor (ia);
202 /* Clears the set of user-modified variables from IA's formats
203 substructure. This discards user modifications to variable
204 formats, thereby causing formats to revert to their
207 clear_modified_vars (struct import_assistant *ia)
209 struct formats_page *p = ia->formats;
212 for (i = 0; i < p->modified_var_cnt; i++)
213 var_destroy (p->modified_vars[i]);
214 free (p->modified_vars);
215 p->modified_vars = NULL;
216 p->modified_var_cnt = 0;
219 /* Resets the formats page to its defaults, discarding user
222 reset_formats_page (struct import_assistant *ia)
224 clear_modified_vars (ia);
225 prepare_formats_page (ia);
230 /* Called when the user changes one of the variables in the
233 on_variable_change (PsppireDict *dict, int dict_idx,
234 struct import_assistant *ia)
236 struct formats_page *p = ia->formats;
237 PsppSheetView *tv = ia->formats->data_tree_view;
238 gint column_idx = dict_idx + 1;
240 push_watch_cursor (ia);
242 /* Remove previous column and replace with new column. */
243 pspp_sheet_view_remove_column (tv, pspp_sheet_view_get_column (tv, column_idx));
244 pspp_sheet_view_insert_column (tv, make_data_column (ia, tv, false, dict_idx),
247 /* Save a copy of the modified variable in modified_vars, so
248 that its attributes will be preserved if we back up to the
249 previous page with the Prev button and then come back
251 if (dict_idx >= p->modified_var_cnt)
254 p->modified_vars = xnrealloc (p->modified_vars, dict_idx + 1,
255 sizeof *p->modified_vars);
256 for (i = 0; i <= dict_idx; i++)
257 p->modified_vars[i] = NULL;
258 p->modified_var_cnt = dict_idx + 1;
260 if (p->modified_vars[dict_idx])
261 var_destroy (p->modified_vars[dict_idx]);
262 p->modified_vars[dict_idx]
263 = var_clone (psppire_dict_get_variable (dict, dict_idx));
265 pop_watch_cursor (ia);
272 formats_append_syntax (const struct import_assistant *ia, struct string *s)
276 ds_put_cstr (s, " /VARIABLES=\n");
278 var_cnt = dict_get_var_cnt (ia->dict);
279 for (i = 0; i < var_cnt; i++)
281 struct variable *var = dict_get_var (ia->dict, i);
282 char format_string[FMT_STRING_LEN_MAX + 1];
283 fmt_to_string (var_get_print_format (var), format_string);
284 ds_put_format (s, " %s %s%s\n",
285 var_get_name (var), format_string,
286 i == var_cnt - 1 ? "." : "");