[gimp] app: make tool preset loading work



commit b676a1add071ec357a32de058cdff4854b3ed497
Author: Michael Natterer <mitch gimp org>
Date:   Sun Apr 11 15:43:29 2010 +0200

    app: make tool preset loading work
    
    - add "Gimp" property to GimpToolPreset
    - implement GimpConfigInterface::deserialize_property() and deserialize
      the "tool-options" locally so we can pass the required "gimp" construct
      property.

 app/core/gimptoolpreset-load.c |    4 +-
 app/core/gimptoolpreset.c      |  137 +++++++++++++++++++++++++++++++++++-----
 app/core/gimptoolpreset.h      |    1 +
 3 files changed, 125 insertions(+), 17 deletions(-)
---
diff --git a/app/core/gimptoolpreset-load.c b/app/core/gimptoolpreset-load.c
index fd3c060..79df8ef 100644
--- a/app/core/gimptoolpreset-load.c
+++ b/app/core/gimptoolpreset-load.c
@@ -40,7 +40,9 @@ gimp_tool_preset_load (GimpContext  *context,
   g_return_val_if_fail (g_path_is_absolute (filename), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  tool_preset = g_object_new (GIMP_TYPE_TOOL_PRESET, NULL);
+  tool_preset = g_object_new (GIMP_TYPE_TOOL_PRESET,
+                              "gimp", context->gimp,
+                              NULL);
 
   if (gimp_config_deserialize_file (GIMP_CONFIG (tool_preset),
                                     filename,
diff --git a/app/core/gimptoolpreset.c b/app/core/gimptoolpreset.c
index 3a05dbd..01fd93e 100644
--- a/app/core/gimptoolpreset.c
+++ b/app/core/gimptoolpreset.c
@@ -24,6 +24,7 @@
 
 #include "core-types.h"
 
+#include "gimp.h"
 #include "gimptoolinfo.h"
 #include "gimptooloptions.h"
 #include "gimptoolpreset.h"
@@ -37,29 +38,40 @@ enum
 {
   PROP_0,
   PROP_NAME,
+  PROP_GIMP,
   PROP_TOOL_OPTIONS
 };
 
 
-static void          gimp_tool_preset_finalize      (GObject      *object);
-static void          gimp_tool_preset_set_property  (GObject      *object,
-                                                     guint         property_id,
-                                                     const GValue *value,
-                                                     GParamSpec   *pspec);
-static void          gimp_tool_preset_get_property  (GObject      *object,
-                                                     guint         property_id,
-                                                     GValue       *value,
-                                                     GParamSpec   *pspec);
+static void        gimp_tool_preset_config_iface_init (GimpConfigInterface *iface);
+
+static void            gimp_tool_preset_finalize      (GObject      *object);
+static void            gimp_tool_preset_set_property  (GObject      *object,
+                                                       guint         property_id,
+                                                       const GValue *value,
+                                                       GParamSpec   *pspec);
+static void            gimp_tool_preset_get_property  (GObject      *object,
+                                                       guint         property_id,
+                                                       GValue       *value,
+                                                       GParamSpec   *pspec);
 static void
-       gimp_tool_preset_dispatch_properties_changed (GObject      *object,
-                                                     guint         n_pspecs,
-                                                     GParamSpec  **pspecs);
+         gimp_tool_preset_dispatch_properties_changed (GObject      *object,
+                                                       guint         n_pspecs,
+                                                       GParamSpec  **pspecs);
+
+static const gchar   * gimp_tool_preset_get_extension (GimpData     *data);
 
-static const gchar * gimp_tool_preset_get_extension (GimpData     *data);
+static gboolean gimp_tool_preset_deserialize_property (GimpConfig   *config,
+                                                       guint         property_id,
+                                                       GValue       *value,
+                                                       GParamSpec   *pspec,
+                                                       GScanner     *scanner,
+                                                       GTokenType   *expected);
 
 
-G_DEFINE_TYPE (GimpToolPreset, gimp_tool_preset,
-               GIMP_TYPE_DATA)
+G_DEFINE_TYPE_WITH_CODE (GimpToolPreset, gimp_tool_preset, GIMP_TYPE_DATA,
+                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONFIG,
+                                                gimp_tool_preset_config_iface_init))
 
 #define parent_class gimp_tool_preset_parent_class
 
@@ -83,6 +95,13 @@ gimp_tool_preset_class_init (GimpToolPresetClass *klass)
                                    "Unnamed",
                                    GIMP_PARAM_STATIC_STRINGS);
 
+  g_object_class_install_property (object_class, PROP_GIMP,
+                                   g_param_spec_object ("gimp",
+                                                        NULL, NULL,
+                                                        GIMP_TYPE_GIMP,
+                                                        GIMP_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT_ONLY));
+
   GIMP_CONFIG_INSTALL_PROP_OBJECT (object_class, PROP_TOOL_OPTIONS,
                                    "tool-options", NULL,
                                    GIMP_TYPE_TOOL_OPTIONS,
@@ -90,6 +109,12 @@ gimp_tool_preset_class_init (GimpToolPresetClass *klass)
 }
 
 static void
+gimp_tool_preset_config_iface_init (GimpConfigInterface *iface)
+{
+  iface->deserialize_property = gimp_tool_preset_deserialize_property;
+}
+
+static void
 gimp_tool_preset_init (GimpToolPreset *tool_preset)
 {
   tool_preset->tool_options = NULL;
@@ -115,7 +140,7 @@ gimp_tool_preset_set_property (GObject      *object,
                                const GValue *value,
                                GParamSpec   *pspec)
 {
-  GimpToolPreset  *tool_preset = GIMP_TOOL_PRESET (object);
+  GimpToolPreset *tool_preset = GIMP_TOOL_PRESET (object);
 
   switch (property_id)
     {
@@ -124,6 +149,10 @@ gimp_tool_preset_set_property (GObject      *object,
                             g_value_get_string (value));
       break;
 
+    case PROP_GIMP:
+      tool_preset->gimp = g_value_get_object (value); /* don't ref */
+      break;
+
     case PROP_TOOL_OPTIONS:
       if (tool_preset->tool_options)
         g_object_unref (tool_preset->tool_options);
@@ -151,6 +180,10 @@ gimp_tool_preset_get_property (GObject    *object,
       g_value_set_string (value, gimp_object_get_name (tool_preset));
       break;
 
+    case PROP_GIMP:
+      g_value_set_object (value, tool_preset->gimp);
+      break;
+
     case PROP_TOOL_OPTIONS:
       g_value_set_object (value, tool_preset->tool_options);
       break;
@@ -187,6 +220,77 @@ gimp_tool_preset_get_extension (GimpData *data)
   return GIMP_TOOL_PRESET_FILE_EXTENSION;
 }
 
+static gboolean
+gimp_tool_preset_deserialize_property (GimpConfig *config,
+                                       guint       property_id,
+                                       GValue     *value,
+                                       GParamSpec *pspec,
+                                       GScanner   *scanner,
+                                       GTokenType *expected)
+{
+  GimpToolPreset *tool_preset = GIMP_TOOL_PRESET (config);
+
+  switch (property_id)
+    {
+    case PROP_TOOL_OPTIONS:
+      {
+        GObject *options;
+        gchar   *type_name;
+        GType    type;
+
+        if (! gimp_scanner_parse_string (scanner, &type_name))
+          {
+            *expected = G_TOKEN_STRING;
+            break;
+          }
+
+        type = g_type_from_name (type_name);
+
+        if (! type)
+          {
+            g_scanner_error (scanner,
+                             "unable to determine type of '%s'",
+                             type_name);
+            *expected = G_TOKEN_STRING;
+            g_free (type_name);
+            break;
+          }
+
+        if (! g_type_is_a (type, GIMP_TYPE_TOOL_OPTIONS))
+          {
+            g_scanner_error (scanner,
+                             "'%s' is not a subclass of GimpToolOptions",
+                             type_name);
+            *expected = G_TOKEN_STRING;
+            g_free (type_name);
+            break;
+          }
+
+        g_free (type_name);
+
+        options = g_object_new (type,
+                                "gimp", tool_preset->gimp,
+                                NULL);
+
+        if (! GIMP_CONFIG_GET_INTERFACE (options)->deserialize (GIMP_CONFIG (options),
+                                                                scanner, 1,
+                                                                NULL))
+          {
+            g_object_unref (options);
+            break;
+          }
+
+        g_value_take_object (value, options);
+      }
+      break;
+
+    default:
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
 
 /*  public functions  */
 
@@ -208,6 +312,7 @@ gimp_tool_preset_new (GimpContext *context,
   return g_object_new (GIMP_TYPE_TOOL_PRESET,
                        "name",         name,
                        "stock-id",     stock_id,
+                       "gimp",         context->gimp,
                        "tool-options", tool_info->tool_options,
                        NULL);
 }
diff --git a/app/core/gimptoolpreset.h b/app/core/gimptoolpreset.h
index 905b201..43632fb 100644
--- a/app/core/gimptoolpreset.h
+++ b/app/core/gimptoolpreset.h
@@ -36,6 +36,7 @@ struct _GimpToolPreset
 {
   GimpData         parent_instance;
 
+  Gimp            *gimp;
   GimpToolOptions *tool_options;
 };
 



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