Enable reordering of variables by drag and drop
authorJohn Darrington <john@darrington.wattle.id.au>
Mon, 8 May 2017 08:49:39 +0000 (10:49 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Mon, 8 May 2017 08:49:39 +0000 (10:49 +0200)
src/ui/gui/psppire-data-sheet.c
src/ui/gui/psppire-variable-sheet.c

index f9800a501bab93b8b0ee19a29257b789c952962a..983fa68998fdd5c7d83df29691ecae1bac208e49 100644 (file)
@@ -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);
 }
index 7f3e6d5920f0a8356ea9d9fc3005b484b97cccea..7af764d7c9e6b94bfc6be8d99c38cbf3eed9dd2e 100644 (file)
@@ -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);
 }