Improve the conversion to and from cell values
authorJohn Darrington <john@darrington.wattle.id.au>
Sat, 13 Aug 2016 18:15:42 +0000 (20:15 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 13 Aug 2016 18:15:42 +0000 (20:15 +0200)
src/ui/gui/psppire-data-editor.c
src/ui/gui/psppire-data-store.c

index 80bf587695598398093df776f37dcaa401497188..c108de556363f6cd6f4a4262186d3f9330fb6f8e 100644 (file)
 #include "ui/gui/psppire-conf.h"
 #include "ui/gui/psppire-var-sheet-header.h"
 
+#include "value-variant.h"
+
+
 #include "ui/gui/efficient-sheet/jmd-sheet.h"
+#include "ui/gui/efficient-sheet/jmd-sheet-body.h"
 
 #include <gettext.h>
 #define _(msgid) gettext (msgid)
@@ -246,12 +250,14 @@ change_data_value (PsppireDataStore *store, gint col, gint row, GValue *value)
     return;
 
   union value v;
-  value_init (&v, var_get_width (var));
-  v.f = g_value_get_double (value);
+
+  GVariant *vrnt = g_value_get_variant (value);
+  
+  value_variant_get (&v, vrnt);
   
   psppire_data_store_set_value (store, row, var, &v);
 
-  value_destroy (&v, var_get_width (var));
+  value_destroy_from_variant (&v, vrnt);
 }
 
 static void
@@ -512,6 +518,10 @@ on_data_selection_change (PsppireDataEditor *de, JmdRange *sel)
 
 static void set_font_recursively (GtkWidget *w, gpointer data);
 
+gchar *myconvfunc (GtkTreeModel *m, gint col, gint row, const GValue *v);
+void myreversefunc (GtkTreeModel *model, gint col, gint row, const gchar *in, GValue *out);
+
+
 static void
 psppire_data_editor_init (PsppireDataEditor *de)
 {
@@ -536,7 +546,11 @@ psppire_data_editor_init (PsppireDataEditor *de)
   gtk_box_pack_start (GTK_BOX (hbox), de->datum_entry, TRUE, TRUE, 0);
 
   de->split = FALSE;
-  de->data_sheet = g_object_new (JMD_TYPE_SHEET, NULL);
+  de->data_sheet = jmd_sheet_new ();
+  jmd_sheet_body_set_conversion_func
+    (JMD_SHEET_BODY (JMD_SHEET(de->data_sheet)->selected_body),
+     myconvfunc, myreversefunc);
+                                     
   GtkWidget *data_button = jmd_sheet_get_button (JMD_SHEET (de->data_sheet));
   gtk_button_set_label (GTK_BUTTON (data_button), _("Case"));
   de->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
index 40f033c392f443cd733cc51967fafc7e888413d7..1c934e0fb837a1a2be646c8ff7f520375807b054 100644 (file)
@@ -44,7 +44,7 @@
 #include "xalloc.h"
 #include "xmalloca.h"
 
-
+#include "value-variant.h"
 
 static void psppire_data_store_init            (PsppireDataStore      *data_store);
 static void psppire_data_store_class_init      (PsppireDataStoreClass *class);
@@ -130,7 +130,51 @@ __iter_nth_child (GtkTreeModel *tree_model,
   return TRUE;
 }
 
+void
+myreversefunc (GtkTreeModel *model, gint col, gint row,
+              const gchar *in, GValue *out)
+{
+  PsppireDataStore *store  = PSPPIRE_DATA_STORE (model);
+
+  const struct variable *variable = psppire_dict_get_variable (store->dict, col);
+  g_return_if_fail (variable);
+
+  const struct fmt_spec *fmt = var_get_print_format (variable);
+
+  int width = var_get_width (variable);
+
+  union value val;
+  value_init (&val, width);
+  char *xx = 
+    data_in (ss_cstr (in), psppire_dict_encoding (store->dict),
+            fmt->type, &val, width, "UTF-8");
+
+  GVariant *vrnt = value_variant_new (&val, width);
+  value_destroy (&val, width);
+
+  g_value_init (out, G_TYPE_VARIANT);
+  g_value_set_variant (out, vrnt);
+  free (xx);
+}
+
+gchar *
+myconvfunc (GtkTreeModel *model, gint col, gint row, const GValue *v)
+{
+  PsppireDataStore *store  = PSPPIRE_DATA_STORE (model);
 
+  const struct variable *variable = psppire_dict_get_variable (store->dict, col);
+  g_return_val_if_fail (variable, g_strdup ("???"));
+
+  GVariant *vrnt = g_value_get_variant (v);
+  union value val;
+  value_variant_get (&val, vrnt);
+
+  const struct fmt_spec *fmt = var_get_print_format (variable);
+  char *out =  data_out (&val, psppire_dict_encoding (store->dict),  fmt);
+  value_destroy_from_variant (&val, vrnt);
+
+  return out;
+}
 
 static void
 __get_value (GtkTreeModel *tree_model,
@@ -146,49 +190,28 @@ __get_value (GtkTreeModel *tree_model,
   if (NULL == variable)
     return;
 
-  if (var_is_numeric (variable))
-    g_value_init (value, G_TYPE_DOUBLE);
-  else
-    g_value_init (value, G_TYPE_STRING);
+  g_value_init (value, G_TYPE_VARIANT);
 
   gint row = GPOINTER_TO_INT (iter->user_data);
 
   struct ccase *cc = datasheet_get_row (store->datasheet, row);
-  
-  if (var_is_numeric (variable))
-    g_value_set_double (value, case_data_idx (cc, column)->f);
-  else
-    {
-      const gchar *ss = value_str (case_data_idx (cc, column),
-                            var_get_width (variable));
-      g_value_set_string (value, ss);
-    }
-  case_unref (cc);
-}
 
+  const union value *val = case_data_idx (cc, column);
 
-static GType
-__get_type (GtkTreeModel *tree_model,   gint idx)
-{
-  PsppireDataStore *store  = PSPPIRE_DATA_STORE (tree_model);
-
-  const struct variable *variable = psppire_dict_get_variable (store->dict, idx);
-
-  if (NULL == variable)
-    return 0;
+  GVariant *vv = value_variant_new (val, var_get_width (variable));
 
-  if (var_is_numeric (variable))
-    return G_TYPE_DOUBLE;
-
-  return G_TYPE_STRING;
+  g_value_set_variant (value, vv);
+  
+  case_unref (cc);
 }
 
+
 static void
 __tree_model_init (GtkTreeModelIface *iface)
 {
   iface->get_flags       = __tree_model_get_flags;
   iface->get_n_columns   = __tree_model_get_n_columns ;
-  iface->get_column_type = __get_type;
+  iface->get_column_type = NULL;
   iface->get_iter        = NULL; 
   iface->iter_next       = NULL; 
   iface->get_path        = NULL;