From a64af5a8cec74642ccb1d7adc71678e29413f78d Mon Sep 17 00:00:00 2001 From: John Darrington Date: Mon, 8 May 2017 10:49:39 +0200 Subject: [PATCH] Enable reordering of variables by drag and drop --- src/ui/gui/psppire-data-sheet.c | 27 ++++++++++++++++++++++++++- src/ui/gui/psppire-variable-sheet.c | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/ui/gui/psppire-data-sheet.c b/src/ui/gui/psppire-data-sheet.c index f9800a501b..983fa68998 100644 --- a/src/ui/gui/psppire-data-sheet.c +++ b/src/ui/gui/psppire-data-sheet.c @@ -330,7 +330,7 @@ static gboolean dispose_has_run = FALSE; static void psppire_data_sheet_dispose (GObject *obj) { - PsppireDataSheet *sheet = PSPPIRE_DATA_SHEET (obj); + // PsppireDataSheet *sheet = PSPPIRE_DATA_SHEET (obj); if (dispose_has_run) return; @@ -372,6 +372,29 @@ set_dictionary (PsppireDataSheet *sheet) g_object_set (sheet, "hmodel", store->dict, NULL); } +static void +move_variable (PsppireDataSheet *sheet, gint from, gint to, gpointer ud) +{ + PsppireDataStore *data_store = NULL; + g_object_get (sheet, "data-model", &data_store, NULL); + + if (data_store == NULL) + return; + + PsppireDict *dict = data_store->dict; + struct variable *var = psppire_dict_get_variable (dict, from); + + if (var == NULL) + return; + gint new_pos = to; + /* The index refers to the final position, so if the source + is less than the destination, then we must subtract 1, to + account for the position vacated by the source */ + if (from < to) + new_pos--; + dict_reorder_var (dict->dict, var, new_pos); +} + static void psppire_data_sheet_init (PsppireDataSheet *sheet) { @@ -395,4 +418,6 @@ psppire_data_sheet_init (PsppireDataSheet *sheet) g_signal_connect (sheet, "notify::data-model", G_CALLBACK (set_dictionary), NULL); + + g_signal_connect (sheet, "column-moved", G_CALLBACK (move_variable), NULL); } diff --git a/src/ui/gui/psppire-variable-sheet.c b/src/ui/gui/psppire-variable-sheet.c index 7f3e6d5920..7af764d7c9 100644 --- a/src/ui/gui/psppire-variable-sheet.c +++ b/src/ui/gui/psppire-variable-sheet.c @@ -463,6 +463,28 @@ psppire_variable_sheet_new (void) return GTK_WIDGET (obj); } +static void +move_variable (PsppireVariableSheet *sheet, gint from, gint to, gpointer ud) +{ + PsppireDict *dict = NULL; + g_object_get (sheet, "data-model", &dict, NULL); + + if (dict == NULL) + return; + + struct variable *var = psppire_dict_get_variable (dict, from); + + if (var == NULL) + return; + gint new_pos = to; + /* The index refers to the final position, so if the source + is less than the destination, then we must subtract 1, to + account for the position vacated by the source */ + if (from < to) + new_pos--; + dict_reorder_var (dict->dict, var, new_pos); +} + static void psppire_variable_sheet_init (PsppireVariableSheet *sheet) { @@ -494,4 +516,7 @@ psppire_variable_sheet_init (PsppireVariableSheet *sheet) g_signal_connect_swapped (sheet, "value-changed", G_CALLBACK (change_var_property), sheet); + + g_signal_connect (sheet, "row-moved", + G_CALLBACK (move_variable), NULL); } -- 2.30.2