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) (void (*)(void)) gtk_tree_path_free, NULL);
202 on_entry_change (PsppireDialogActionAutorecode *rd)
204 gboolean valid = TRUE;
205 const char *text = gtk_entry_get_text (GTK_ENTRY (rd->new_name_entry));
207 if (0 == strcmp ("", text))
209 else if (psppire_dict_lookup_var (PSPPIRE_DIALOG_ACTION (rd)->dict, text))
216 g_hash_table_iter_init (&iter, rd->varmap);
217 while (g_hash_table_iter_next (&iter, &key, &value))
219 struct nlp *nlp = value;
221 if (0 == strcmp (nlp->name, text))
229 gtk_widget_set_sensitive (rd->change_button, valid);
233 /* Callback which gets called when a new row is selected
234 in the variable treeview.
235 It sets the name and label entry widgets to reflect the
236 currently selected row.
239 on_selection_change (GtkTreeSelection *selection, gpointer data)
241 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (data);
243 GtkTreeModel *model = psppire_var_view_get_current_model (PSPPIRE_VAR_VIEW (rd->var_view));
245 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
247 if (rows && !rows->next)
249 /* Exactly one row is selected */
251 struct variable *var;
255 gtk_widget_set_sensitive (rd->new_name_entry, TRUE);
256 gtk_widget_set_sensitive (rd->change_button, TRUE);
259 ok = gtk_tree_model_get_iter (model, &iter, (GtkTreePath*) rows->data);
260 g_return_if_fail (ok);
262 gtk_tree_model_get (model, &iter, 0, &var, -1);
264 nlp = g_hash_table_lookup (rd->varmap, var);
267 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), nlp->name ? nlp->name : "");
269 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
273 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
274 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
275 gtk_widget_set_sensitive (rd->change_button, FALSE);
278 g_list_foreach (rows, (GFunc) (void (*)(void)) gtk_tree_path_free, NULL);
285 render_new_var_name (GtkTreeViewColumn *tree_column,
286 GtkCellRenderer *cell,
287 GtkTreeModel *tree_model,
291 struct nlp *nlp = NULL;
293 PsppireDialogActionAutorecode *rd = PSPPIRE_DIALOG_ACTION_AUTORECODE (data);
297 struct variable *var = NULL;
299 gtk_tree_model_get (tree_model, iter,
303 nlp = g_hash_table_lookup (rd->varmap, var);
306 g_object_set (cell, "text", nlp->name, NULL);
308 g_object_set (cell, "text", "", NULL);
313 psppire_dialog_action_autorecode_activate (PsppireDialogAction *a, GVariant *param)
315 PsppireDialogActionAutorecode *act = PSPPIRE_DIALOG_ACTION_AUTORECODE (a);
316 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
318 GtkBuilder *xml = builder_new ("autorecode.ui");
320 pda->dialog = get_widget_assert (xml, "autorecode-dialog");
321 pda->source = get_widget_assert (xml, "dict-view");
323 act->var_view = get_widget_assert (xml, "var-view");
325 act->new_name_entry = get_widget_assert (xml, "entry1");
326 act->change_button = get_widget_assert (xml, "button1");
327 act->ascending = get_widget_assert (xml, "radiobutton1");
328 act->group = get_widget_assert (xml, "checkbutton1");
329 act->blank = get_widget_assert (xml, "checkbutton2");
332 GtkTreeSelection *sel;
334 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
336 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes (_("New"),
341 gtk_tree_view_column_set_cell_data_func (col, renderer,
345 gtk_tree_view_append_column (GTK_TREE_VIEW (act->var_view), col);
348 col = gtk_tree_view_get_column (GTK_TREE_VIEW (act->var_view), 0);
350 g_object_set (col, "title", _("Old"), NULL);
352 g_object_set (act->var_view, "headers-visible", TRUE, NULL);
354 act->varmap = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, nlp_destroy);
357 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (act->var_view));
360 g_signal_connect (sel, "changed",
361 G_CALLBACK (on_selection_change), act);
363 g_signal_connect (act->change_button, "clicked",
364 G_CALLBACK (on_change_clicked), act);
366 g_signal_connect_swapped (act->new_name_entry, "changed",
367 G_CALLBACK (on_entry_change), act);
371 psppire_dialog_action_set_refresh (pda, refresh);
372 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
378 psppire_dialog_action_autorecode_class_init (PsppireDialogActionAutorecodeClass *class)
380 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_autorecode_activate;
381 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
386 psppire_dialog_action_autorecode_init (PsppireDialogActionAutorecode *act)