[gegl/meta-json: 10/18] meta-json: Can set initial properties from file



commit ecb41fb248e8937aeeb767828130e9a7e362345c
Author: Jon Nordby <jononor gmail com>
Date:   Sun Dec 28 20:22:25 2014 +0100

    meta-json: Can set initial properties from file

 operations/core/json.c |   67 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 65 insertions(+), 2 deletions(-)
---
diff --git a/operations/core/json.c b/operations/core/json.c
index 13e419c..a912308 100644
--- a/operations/core/json.c
+++ b/operations/core/json.c
@@ -63,7 +63,7 @@ json_op_get_type (void)
 }                                                                     
 */
 
-gchar *
+static gchar *
 component2geglop(const gchar *name) {
     gchar *dup = g_strdup(name);
     gchar *sep = g_strstr_len(dup, -1, "/");
@@ -74,6 +74,65 @@ component2geglop(const gchar *name) {
     return dup;
 }
 
+static gboolean
+gvalue_from_string(GValue *value, GType target_type, GValue *dest_value) {
+
+    // Custom conversion from string
+    if (!G_VALUE_HOLDS_STRING(value)) {
+        return FALSE;
+    }
+    const gchar *iip = g_value_get_string(value);
+    if (g_type_is_a(target_type, G_TYPE_DOUBLE)) {
+        gdouble d = g_ascii_strtod(iip, NULL);
+        g_value_set_double(dest_value, d);
+    } else if (g_type_is_a(target_type, G_TYPE_INT)) {
+        gint i = g_ascii_strtoll(iip, NULL, 10);
+        g_value_set_int(dest_value, i);
+    } else if (g_type_is_a(target_type, G_TYPE_INT64)) {
+        gint64 i = g_ascii_strtoll(iip, NULL, 10);
+        g_value_set_int64(dest_value, i);
+    } else if (g_type_is_a(target_type, GEGL_TYPE_COLOR)) {
+        GeglColor *color = gegl_color_new(iip);
+        if (!color || !GEGL_IS_COLOR(color)) {
+            return FALSE;
+        }
+        g_value_set_object(dest_value, color);
+    } else if (g_type_is_a(target_type, G_TYPE_ENUM)) {
+        GEnumClass *klass = (GEnumClass *)g_type_class_ref(target_type);
+        GEnumValue *val = g_enum_get_value_by_nick(klass, iip);
+        g_return_val_if_fail(val, FALSE);
+
+        g_value_set_enum(dest_value, val->value);
+        g_type_class_unref((gpointer)klass);
+    } else if (g_type_is_a(target_type, G_TYPE_BOOLEAN)) {
+        gboolean b = g_ascii_strcasecmp("true", iip) == 0;
+        g_value_set_boolean(dest_value, b);
+    } else {
+        return FALSE;
+    }
+    return TRUE;
+}
+
+gboolean
+set_prop(GeglNode *t, const gchar *port, GParamSpec *paramspec, GValue *value)  {
+    GType target_type = G_PARAM_SPEC_VALUE_TYPE(paramspec);
+    GValue dest_value = {0,};
+    g_value_init(&dest_value, target_type);
+
+    gboolean success = g_param_value_convert(paramspec, value, &dest_value, FALSE);
+    if (success) {
+        gegl_node_set_property(t, port, &dest_value);
+        return TRUE;
+    }
+
+    if (gvalue_from_string(value, target_type, &dest_value)) {
+        g_param_value_validate(paramspec, &dest_value);
+        gegl_node_set_property(t, port, &dest_value);
+        return TRUE;
+    }
+    return FALSE;
+}
+
 static void
 install_properties(JsonOpClass *json_op_class)
 {
@@ -216,7 +275,11 @@ attach (GeglOperation *operation)
           GValue value = G_VALUE_INIT;
           g_assert(JSON_NODE_HOLDS_VALUE(datanode));
           json_node_get_value(datanode, &value);
-          // TODO: set property value
+          GParamSpec *paramspec = gegl_node_find_property(tgt_node, tgt_port);
+
+          g_print("setting property value on %s,%s\n", tgt_proc, tgt_port);
+
+          set_prop(tgt_node, tgt_port, paramspec, &value);
           g_value_unset(&value);
       }
   }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]