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"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
32 static void psppire_dialog_action_autorecode_init (PsppireDialogActionAutorecode *act);
33 static void psppire_dialog_action_autorecode_class_init (PsppireDialogActionAutorecodeClass *class);
36 G_DEFINE_TYPE (PsppireDialogActionAutorecode, psppire_dialog_action_autorecode, PSPPIRE_TYPE_DIALOG_ACTION);
40 dialog_state_valid (gpointer pda)
42 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (pda);
44 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->var_view));
45 const gint n_vars = gtk_tree_model_iter_n_children (model, NULL);
50 if (g_hash_table_size (rd->varmap) != n_vars)
57 refresh (PsppireDialogAction *pda)
59 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (pda);
61 GtkTreeModel *target_list = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->var_view));
63 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
64 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
65 gtk_widget_set_sensitive (rd->change_button, FALSE);
67 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->ascending), TRUE);
68 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->group), FALSE);
69 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->blank), FALSE);
72 g_hash_table_remove_all (rd->varmap);
74 gtk_list_store_clear (GTK_LIST_STORE (target_list));
87 nlp_create (const char *name, const char *label)
89 struct nlp *nlp = xmalloc (sizeof *nlp);
91 nlp->name = g_strdup (name);
95 if (label != NULL && 0 != strcmp ("", label))
96 nlp->label = g_strdup (label);
102 nlp_destroy (gpointer data)
104 struct nlp *nlp = data ;
115 generate_syntax (const PsppireDialogAction *act)
117 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (act);
123 GString *string = g_string_new ("AUTORECODE");
125 g_string_append (string, "\n\tVARIABLES =");
127 g_hash_table_iter_init (&iter, rd->varmap);
128 while (g_hash_table_iter_next (&iter, &key, &value))
130 struct variable *var = key;
131 g_string_append (string, " ");
132 g_string_append (string, var_get_name (var));
135 g_string_append (string, " INTO");
137 g_hash_table_iter_init (&iter, rd->varmap);
138 while (g_hash_table_iter_next (&iter, &key, &value))
140 struct nlp *nlp = value;
141 g_string_append (string, " ");
142 g_string_append (string, nlp->name);
145 if (! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->ascending)))
146 g_string_append (string, "\n\t/DESCENDING");
148 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->group)))
149 g_string_append (string, "\n\t/GROUP");
151 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->blank)))
152 g_string_append (string, "\n\t/BLANK = MISSING");
154 g_string_append (string, ".\n");
158 g_string_free (string, FALSE);
164 on_change_clicked (GObject *obj, gpointer data)
166 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (data);
167 struct variable *var = NULL;
169 GtkTreeModel *model = psppire_var_view_get_current_model (PSPPIRE_VAR_VIEW (rd->var_view));
171 GtkTreeSelection *selection =
172 gtk_tree_view_get_selection (GTK_TREE_VIEW (rd->var_view));
174 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
176 const gchar *dest_var_name =
177 gtk_entry_get_text (GTK_ENTRY (rd->new_name_entry));
179 if (NULL == rows || rows->next != NULL)
182 gtk_tree_model_get_iter (model, &iter, rows->data);
184 gtk_tree_model_get (model, &iter, 0, &var, -1);
186 g_hash_table_remove (rd->varmap, var);
188 nlp = nlp_create (dest_var_name, NULL);
190 g_hash_table_insert (rd->varmap, var, nlp);
192 gtk_tree_model_row_changed (model, rows->data, &iter);
195 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
201 on_entry_change (PsppireDialogActionAutorecode *rd)
203 gboolean valid = TRUE;
204 const char *text = gtk_entry_get_text (GTK_ENTRY (rd->new_name_entry));
206 if (0 == strcmp ("", text))
208 else if (psppire_dict_lookup_var (rd->dict, text))
215 g_hash_table_iter_init (&iter, rd->varmap);
216 while (g_hash_table_iter_next (&iter, &key, &value))
218 struct nlp *nlp = value;
220 if (0 == strcmp (nlp->name, text))
228 gtk_widget_set_sensitive (rd->change_button, valid);
232 /* Callback which gets called when a new row is selected
233 in the variable treeview.
234 It sets the name and label entry widgets to reflect the
235 currently selected row.
238 on_selection_change (GtkTreeSelection *selection, gpointer data)
240 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (data);
242 GtkTreeModel *model = psppire_var_view_get_current_model (PSPPIRE_VAR_VIEW (rd->var_view));
244 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
246 if (rows && !rows->next)
248 /* Exactly one row is selected */
250 struct variable *var;
254 gtk_widget_set_sensitive (rd->new_name_entry, TRUE);
255 gtk_widget_set_sensitive (rd->change_button, TRUE);
258 ok = gtk_tree_model_get_iter (model, &iter, (GtkTreePath*) rows->data);
259 g_return_if_fail (ok);
261 gtk_tree_model_get (model, &iter, 0, &var, -1);
263 nlp = g_hash_table_lookup (rd->varmap, var);
266 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), nlp->name ? nlp->name : "");
268 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
272 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
273 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
274 gtk_widget_set_sensitive (rd->change_button, FALSE);
277 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
284 render_new_var_name (GtkTreeViewColumn *tree_column,
285 GtkCellRenderer *cell,
286 GtkTreeModel *tree_model,
290 struct nlp *nlp = NULL;
292 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (data);
296 struct variable *var = NULL;
298 gtk_tree_model_get (tree_model, iter,
302 nlp = g_hash_table_lookup (rd->varmap, var);
305 g_object_set (cell, "text", nlp->name, NULL);
307 g_object_set (cell, "text", "", NULL);
312 psppire_dialog_action_autorecode_activate (PsppireDialogAction *a, GVariant *param)
314 PsppireDialogActionAutorecode *act = PSPPIRE_DIALOG_ACTION_AUTORECODE (a);
315 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
317 GtkBuilder *xml = builder_new ("autorecode.ui");
319 pda->dialog = get_widget_assert (xml, "autorecode-dialog");
320 pda->source = get_widget_assert (xml, "dict-view");
322 act->var_view = get_widget_assert (xml, "var-view");
324 act->new_name_entry = get_widget_assert (xml, "entry1");
325 act->change_button = get_widget_assert (xml, "button1");
326 act->ascending = get_widget_assert (xml, "radiobutton1");
327 act->group = get_widget_assert (xml, "checkbutton1");
328 act->blank = get_widget_assert (xml, "checkbutton2");
331 GtkTreeSelection *sel;
333 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
335 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes (_("New"),
340 gtk_tree_view_column_set_cell_data_func (col, renderer,
344 gtk_tree_view_append_column (GTK_TREE_VIEW (act->var_view), col);
347 col = gtk_tree_view_get_column (GTK_TREE_VIEW (act->var_view), 0);
349 g_object_set (col, "title", _("Old"), NULL);
351 g_object_set (act->var_view, "headers-visible", TRUE, NULL);
353 act->varmap = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, nlp_destroy);
356 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (act->var_view));
359 g_signal_connect (sel, "changed",
360 G_CALLBACK (on_selection_change), act);
362 g_signal_connect (act->change_button, "clicked",
363 G_CALLBACK (on_change_clicked), act);
365 g_signal_connect_swapped (act->new_name_entry, "changed",
366 G_CALLBACK (on_entry_change), act);
370 psppire_dialog_action_set_refresh (pda, refresh);
371 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
377 psppire_dialog_action_autorecode_class_init (PsppireDialogActionAutorecodeClass *class)
379 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_autorecode_activate;
380 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
385 psppire_dialog_action_autorecode_init (PsppireDialogActionAutorecode *act)