1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2015 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/>. */
20 #include "psppire-dialog-action-autorecode.h"
22 #include "psppire-var-view.h"
24 #include "psppire-dialog.h"
25 #include "builder-wrapper.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
33 static void psppire_dialog_action_autorecode_init (PsppireDialogActionAutorecode *act);
34 static void psppire_dialog_action_autorecode_class_init (PsppireDialogActionAutorecodeClass *class);
37 G_DEFINE_TYPE (PsppireDialogActionAutorecode, psppire_dialog_action_autorecode, PSPPIRE_TYPE_DIALOG_ACTION);
41 dialog_state_valid (gpointer pda)
43 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (pda);
45 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->var_view));
46 const gint n_vars = gtk_tree_model_iter_n_children (model, NULL);
51 if (g_hash_table_size (rd->varmap) != n_vars)
58 refresh (PsppireDialogAction *pda)
60 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (pda);
62 GtkTreeModel *target_list = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->var_view));
64 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
65 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
66 gtk_widget_set_sensitive (rd->change_button, FALSE);
68 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->ascending), TRUE);
69 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->group), FALSE);
70 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->blank), FALSE);
73 g_hash_table_remove_all (rd->varmap);
75 gtk_list_store_clear (GTK_LIST_STORE (target_list));
88 nlp_create (const char *name, const char *label)
90 struct nlp *nlp = xmalloc (sizeof *nlp);
92 nlp->name = g_strdup (name);
96 if (label != NULL && 0 != strcmp ("", label))
97 nlp->label = g_strdup (label);
103 nlp_destroy (gpointer data)
105 struct nlp *nlp = data ;
116 generate_syntax (const PsppireDialogAction *act)
118 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (act);
124 GString *string = g_string_new ("AUTORECODE");
126 g_string_append (string, "\n\tVARIABLES =");
128 g_hash_table_iter_init (&iter, rd->varmap);
129 while (g_hash_table_iter_next (&iter, &key, &value))
131 struct variable *var = key;
132 g_string_append (string, " ");
133 g_string_append (string, var_get_name (var));
136 g_string_append (string, " INTO");
138 g_hash_table_iter_init (&iter, rd->varmap);
139 while (g_hash_table_iter_next (&iter, &key, &value))
141 struct nlp *nlp = value;
142 g_string_append (string, " ");
143 g_string_append (string, nlp->name);
146 if (! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->ascending)))
147 g_string_append (string, "\n\t/DESCENDING");
149 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->group)))
150 g_string_append (string, "\n\t/GROUP");
152 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->blank)))
153 g_string_append (string, "\n\t/BLANK = MISSING");
155 g_string_append (string, ".\n");
159 g_string_free (string, FALSE);
165 on_change_clicked (GObject *obj, gpointer data)
167 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (data);
168 struct variable *var = NULL;
170 GtkTreeModel *model = psppire_var_view_get_current_model (PSPPIRE_VAR_VIEW (rd->var_view));
172 GtkTreeSelection *selection =
173 gtk_tree_view_get_selection (GTK_TREE_VIEW (rd->var_view));
175 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
177 const gchar *dest_var_name =
178 gtk_entry_get_text (GTK_ENTRY (rd->new_name_entry));
180 if (NULL == rows || rows->next != NULL)
183 gtk_tree_model_get_iter (model, &iter, rows->data);
185 gtk_tree_model_get (model, &iter, 0, &var, -1);
187 g_hash_table_remove (rd->varmap, var);
189 nlp = nlp_create (dest_var_name, NULL);
191 g_hash_table_insert (rd->varmap, var, nlp);
193 gtk_tree_model_row_changed (model, rows->data, &iter);
196 g_list_foreach (rows, GFUNC_COMPAT_CAST (gtk_tree_path_free), NULL);
203 on_entry_change (PsppireDialogActionAutorecode *rd)
205 gboolean valid = TRUE;
206 const char *text = gtk_entry_get_text (GTK_ENTRY (rd->new_name_entry));
208 if (0 == strcmp ("", text))
210 else if (psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION (rd)->dict, text))
217 g_hash_table_iter_init (&iter, rd->varmap);
218 while (g_hash_table_iter_next (&iter, &key, &value))
220 struct nlp *nlp = value;
222 if (0 == strcmp (nlp->name, text))
230 gtk_widget_set_sensitive (rd->change_button, valid);
234 /* Callback which gets called when a new row is selected
235 in the variable treeview.
236 It sets the name and label entry widgets to reflect the
237 currently selected row.
240 on_selection_change (GtkTreeSelection *selection, gpointer data)
242 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (data);
244 GtkTreeModel *model = psppire_var_view_get_current_model (PSPPIRE_VAR_VIEW (rd->var_view));
246 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
248 if (rows && !rows->next)
250 /* Exactly one row is selected */
252 struct variable *var;
256 gtk_widget_set_sensitive (rd->new_name_entry, TRUE);
257 gtk_widget_set_sensitive (rd->change_button, TRUE);
260 ok = gtk_tree_model_get_iter (model, &iter, (GtkTreePath*) rows->data);
261 g_return_if_fail (ok);
263 gtk_tree_model_get (model, &iter, 0, &var, -1);
265 nlp = g_hash_table_lookup (rd->varmap, var);
268 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), nlp->name ? nlp->name : "");
270 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
274 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
275 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
276 gtk_widget_set_sensitive (rd->change_button, FALSE);
279 g_list_foreach (rows, GFUNC_COMPAT_CAST (gtk_tree_path_free), NULL);
286 render_new_var_name (GtkTreeViewColumn *tree_column,
287 GtkCellRenderer *cell,
288 GtkTreeModel *tree_model,
292 struct nlp *nlp = NULL;
294 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (data);
298 struct variable *var = NULL;
300 gtk_tree_model_get (tree_model, iter,
304 nlp = g_hash_table_lookup (rd->varmap, var);
307 g_object_set (cell, "text", nlp->name, NULL);
309 g_object_set (cell, "text", "", NULL);
314 psppire_dialog_action_autorecode_activate (PsppireDialogAction *a, GVariant *param)
316 PsppireDialogActionAutorecode *act = PSPPIRE_DIALOG_ACTION_AUTORECODE (a);
317 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
319 GtkBuilder *xml = builder_new ("autorecode.ui");
321 pda->dialog = get_widget_assert (xml, "autorecode-dialog");
322 pda->source = get_widget_assert (xml, "dict-view");
324 act->var_view = get_widget_assert (xml, "var-view");
326 act->new_name_entry = get_widget_assert (xml, "entry1");
327 act->change_button = get_widget_assert (xml, "button1");
328 act->ascending = get_widget_assert (xml, "radiobutton1");
329 act->group = get_widget_assert (xml, "checkbutton1");
330 act->blank = get_widget_assert (xml, "checkbutton2");
333 GtkTreeSelection *sel;
335 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
337 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes (_("New"),
342 gtk_tree_view_column_set_cell_data_func (col, renderer,
346 gtk_tree_view_append_column (GTK_TREE_VIEW (act->var_view), col);
349 col = gtk_tree_view_get_column (GTK_TREE_VIEW (act->var_view), 0);
351 g_object_set (col, "title", _("Old"), NULL);
353 g_object_set (act->var_view, "headers-visible", TRUE, NULL);
355 act->varmap = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, nlp_destroy);
358 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (act->var_view));
361 g_signal_connect (sel, "changed",
362 G_CALLBACK (on_selection_change), act);
364 g_signal_connect (act->change_button, "clicked",
365 G_CALLBACK (on_change_clicked), act);
367 g_signal_connect_swapped (act->new_name_entry, "changed",
368 G_CALLBACK (on_entry_change), act);
372 psppire_dialog_action_set_refresh (pda, refresh);
373 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
379 psppire_dialog_action_autorecode_class_init (PsppireDialogActionAutorecodeClass *class)
381 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_autorecode_activate;
382 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
387 psppire_dialog_action_autorecode_init (PsppireDialogActionAutorecode *act)