[gimp/wip/Jehan/classy-GIMP: 32/65] libgimp: create and use gimp_image_new_by_id().



commit cb110e5b139420fbea4aee17f45a1f87c872f815
Author: Jehan <jehan girinstud io>
Date:   Mon Aug 12 11:18:05 2019 +0200

    libgimp: create and use gimp_image_new_by_id().
    
    Simpler than using g_object_new() in a bunch of places.

 libgimp/gimpedit_pdb.c      |  4 ++--
 libgimp/gimpfileops_pdb.c   |  2 +-
 libgimp/gimpimage.c         | 24 ++++++++++++++++++++++++
 libgimp/gimpimage.h         |  1 +
 libgimp/gimpimage_pdb.c     |  6 +++---
 libgimp/gimpimagecombobox.c |  4 ++--
 libgimp/gimpitem_pdb.c      |  2 +-
 libgimp/gimpitemcombobox.c  |  2 +-
 libgimp/gimpparamspecs.c    |  2 +-
 pdb/pdb.pl                  |  2 +-
 10 files changed, 37 insertions(+), 12 deletions(-)
---
diff --git a/libgimp/gimpedit_pdb.c b/libgimp/gimpedit_pdb.c
index a7bf1f89ab..7b59d6bb94 100644
--- a/libgimp/gimpedit_pdb.c
+++ b/libgimp/gimpedit_pdb.c
@@ -263,7 +263,7 @@ gimp_edit_paste_as_new_image (void)
   gimp_value_array_unref (args);
 
   if (g_value_get_enum (gimp_value_array_index (return_vals, 0)) == GIMP_PDB_SUCCESS)
-    image = g_object_new (GIMP_TYPE_IMAGE, "id", gimp_value_array_index (return_vals, 1), NULL);
+    image = gimp_image_new_by_id (g_value_get_int (gimp_value_array_index (return_vals, 1)));
 
   gimp_value_array_unref (return_vals);
 
@@ -500,7 +500,7 @@ gimp_edit_named_paste_as_new_image (const gchar *buffer_name)
   gimp_value_array_unref (args);
 
   if (g_value_get_enum (gimp_value_array_index (return_vals, 0)) == GIMP_PDB_SUCCESS)
-    image = g_object_new (GIMP_TYPE_IMAGE, "id", gimp_value_array_index (return_vals, 1), NULL);
+    image = gimp_image_new_by_id (g_value_get_int (gimp_value_array_index (return_vals, 1)));
 
   gimp_value_array_unref (return_vals);
 
diff --git a/libgimp/gimpfileops_pdb.c b/libgimp/gimpfileops_pdb.c
index 3bbfde83da..7d3e1ea1da 100644
--- a/libgimp/gimpfileops_pdb.c
+++ b/libgimp/gimpfileops_pdb.c
@@ -78,7 +78,7 @@ gimp_file_load (GimpRunMode  run_mode,
   gimp_value_array_unref (args);
 
   if (g_value_get_enum (gimp_value_array_index (return_vals, 0)) == GIMP_PDB_SUCCESS)
-    image = g_object_new (GIMP_TYPE_IMAGE, "id", gimp_value_array_index (return_vals, 1), NULL);
+    image = gimp_image_new_by_id (g_value_get_int (gimp_value_array_index (return_vals, 1)));
 
   gimp_value_array_unref (return_vals);
 
diff --git a/libgimp/gimpimage.c b/libgimp/gimpimage.c
index 20c39693ee..696a0f10cf 100644
--- a/libgimp/gimpimage.c
+++ b/libgimp/gimpimage.c
@@ -134,6 +134,30 @@ gimp_image_get_id (GimpImage *image)
   return image->priv->id;
 }
 
+/**
+ * gimp_image_new_by_id:
+ * @image_id: The image id.
+ *
+ * Returns: (nullable) (transfer full): a #GimpImage for @image_id or
+ *          %NULL if @image_id does not represent a valid image.
+ *
+ * Since: 3.0
+ **/
+GimpImage *
+gimp_image_new_by_id (gint32 image_id)
+{
+  GimpImage *image;
+
+  image = g_object_new (GIMP_TYPE_IMAGE,
+                        "id", image_id,
+                        NULL);
+
+  if (! gimp_image_is_valid (image))
+    g_clear_object (&image);
+
+  return image;
+}
+
 /**
  * gimp_image_get_colormap:
  * @image:      The image.
diff --git a/libgimp/gimpimage.h b/libgimp/gimpimage.h
index f10f2e0ed8..ed19c8f073 100644
--- a/libgimp/gimpimage.h
+++ b/libgimp/gimpimage.h
@@ -66,6 +66,7 @@ struct _GimpImageClass
 GType          gimp_image_get_type           (void) G_GNUC_CONST;
 
 gint32         gimp_image_get_id             (GimpImage    *image);
+GimpImage    * gimp_image_new_by_id          (gint32        image_id);
 
 guchar       * gimp_image_get_colormap       (GimpImage    *image,
                                               gint         *num_colors);
diff --git a/libgimp/gimpimage_pdb.c b/libgimp/gimpimage_pdb.c
index aaa2899284..3b9dc85723 100644
--- a/libgimp/gimpimage_pdb.c
+++ b/libgimp/gimpimage_pdb.c
@@ -169,7 +169,7 @@ gimp_image_new (gint              width,
   gimp_value_array_unref (args);
 
   if (g_value_get_enum (gimp_value_array_index (return_vals, 0)) == GIMP_PDB_SUCCESS)
-    image = g_object_new (GIMP_TYPE_IMAGE, "id", gimp_value_array_index (return_vals, 1), NULL);
+    image = gimp_image_new_by_id (g_value_get_int (gimp_value_array_index (return_vals, 1)));
 
   gimp_value_array_unref (return_vals);
 
@@ -223,7 +223,7 @@ gimp_image_new_with_precision (gint              width,
   gimp_value_array_unref (args);
 
   if (g_value_get_enum (gimp_value_array_index (return_vals, 0)) == GIMP_PDB_SUCCESS)
-    image = g_object_new (GIMP_TYPE_IMAGE, "id", gimp_value_array_index (return_vals, 1), NULL);
+    image = gimp_image_new_by_id (g_value_get_int (gimp_value_array_index (return_vals, 1)));
 
   gimp_value_array_unref (return_vals);
 
@@ -263,7 +263,7 @@ gimp_image_duplicate (GimpImage *image)
   gimp_value_array_unref (args);
 
   if (g_value_get_enum (gimp_value_array_index (return_vals, 0)) == GIMP_PDB_SUCCESS)
-    new_image = g_object_new (GIMP_TYPE_IMAGE, "id", gimp_value_array_index (return_vals, 1), NULL);
+    new_image = gimp_image_new_by_id (g_value_get_int (gimp_value_array_index (return_vals, 1)));
 
   gimp_value_array_unref (return_vals);
 
diff --git a/libgimp/gimpimagecombobox.c b/libgimp/gimpimagecombobox.c
index de6ee5ad97..f6c339ff4a 100644
--- a/libgimp/gimpimagecombobox.c
+++ b/libgimp/gimpimagecombobox.c
@@ -209,7 +209,7 @@ gimp_image_combo_box_model_add (GtkListStore            *store,
     {
       GimpImage *image;
 
-      image = g_object_new (GIMP_TYPE_IMAGE, "id", images[i], NULL);
+      image = gimp_image_new_by_id (images[i]);
       if (! constraint || (* constraint) (image, data))
         {
           gchar     *image_name = gimp_image_get_name (image);
@@ -286,7 +286,7 @@ gimp_image_combo_box_changed (GimpImageComboBox *combo_box)
     {
       GimpImage *image;
 
-      image = g_object_new (GIMP_TYPE_IMAGE, "id", image_ID, NULL);
+      image = gimp_image_new_by_id (image_ID);
       if (! gimp_image_is_valid (image))
         {
           GtkTreeModel *model;
diff --git a/libgimp/gimpitem_pdb.c b/libgimp/gimpitem_pdb.c
index 0b456a5d98..5fbbedcaff 100644
--- a/libgimp/gimpitem_pdb.c
+++ b/libgimp/gimpitem_pdb.c
@@ -110,7 +110,7 @@ gimp_item_get_image (gint32 item_ID)
   gimp_value_array_unref (args);
 
   if (g_value_get_enum (gimp_value_array_index (return_vals, 0)) == GIMP_PDB_SUCCESS)
-    image = g_object_new (GIMP_TYPE_IMAGE, "id", gimp_value_array_index (return_vals, 1), NULL);
+    image = gimp_image_new_by_id (g_value_get_int (gimp_value_array_index (return_vals, 1)));
 
   gimp_value_array_unref (return_vals);
 
diff --git a/libgimp/gimpitemcombobox.c b/libgimp/gimpitemcombobox.c
index 413ceb54f7..d7c5021c0d 100644
--- a/libgimp/gimpitemcombobox.c
+++ b/libgimp/gimpitemcombobox.c
@@ -401,7 +401,7 @@ gimp_item_combo_box_populate (GimpIntComboBox *combo_box)
       gint32    *items;
       gint       num_items;
 
-      image = g_object_new (GIMP_TYPE_IMAGE, "id", images[i], NULL);
+      image = gimp_image_new_by_id (images[i]);
 
       if (GIMP_IS_DRAWABLE_COMBO_BOX (combo_box) ||
           GIMP_IS_LAYER_COMBO_BOX (combo_box))
diff --git a/libgimp/gimpparamspecs.c b/libgimp/gimpparamspecs.c
index 63a4239f50..f5e3062511 100644
--- a/libgimp/gimpparamspecs.c
+++ b/libgimp/gimpparamspecs.c
@@ -122,7 +122,7 @@ gimp_param_image_id_validate (GParamSpec *pspec,
   if (ispec->none_ok && (image_id == 0 || image_id == -1))
     return FALSE;
 
-  image = g_object_new (GIMP_TYPE_IMAGE, "id", image_id, NULL);
+  image = gimp_image_new_by_id (image_id);
   if (! gimp_image_is_valid (image))
     {
       value->data[0].v_int = -1;
diff --git a/pdb/pdb.pl b/pdb/pdb.pl
index 947fb92045..03fcfeb84c 100644
--- a/pdb/pdb.pl
+++ b/pdb/pdb.pl
@@ -166,7 +166,7 @@ package Gimp::CodeGen::pdb;
                     init_value      => 'NULL',
                     out_annotate    => '(transfer full)',
                     get_value_func  => '$var = gimp_value_get_image ($value, gimp)',
-                    dup_value_func  => '$var = g_object_new (GIMP_TYPE_IMAGE, \"id\", $value, NULL)',
+                    dup_value_func  => '$var = gimp_image_new_by_id (g_value_get_int ($value))',
                     set_value_func  => 'gimp_value_set_image_id ($value, gimp_image_get_id ($var))',
                     take_value_func => 'gimp_value_set_image ($value, $var)',
                     convert_func    => 'gimp_image_get_id ($var)',


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