[gimp/alxsa-soft-proofing-menu] libgimpwidgets: Set combobox with GimpColorProfile



commit c0adaf4f2a7d36a4acf2b1e0dd531476f66ac2f3
Author: Alx Sa <cmyk student gmail com>
Date:   Tue Aug 9 20:29:16 2022 +0000

    libgimpwidgets: Set combobox with GimpColorProfile
    
    This adds a new public API to set a GimpColorProfileComboBox's active
    option using a GimpColorProfile instead of a file.

 app/display/gimpstatusbar.c                    |  3 +
 libgimpwidgets/gimpcolorprofilecombobox.c      | 29 ++++++++++
 libgimpwidgets/gimpcolorprofilecombobox.h      | 26 +++++----
 libgimpwidgets/gimpcolorprofilestore-private.h |  5 ++
 libgimpwidgets/gimpcolorprofilestore.c         | 79 ++++++++++++++++++++++++++
 5 files changed, 130 insertions(+), 12 deletions(-)
---
diff --git a/app/display/gimpstatusbar.c b/app/display/gimpstatusbar.c
index 471aaa2081..644c8d9b11 100644
--- a/app/display/gimpstatusbar.c
+++ b/app/display/gimpstatusbar.c
@@ -2031,6 +2031,9 @@ gimp_statusbar_shell_image_simulation_profile_changed (GimpImage     *image,
                           profile_label);
   gtk_label_set_markup (GTK_LABEL (statusbar->profile_label), text);
   g_free (text);
+
+  gimp_color_profile_combo_box_set_active_profile (GIMP_COLOR_PROFILE_COMBO_BOX (statusbar->profile_combo),
+                                                   simulation_profile);
 }
 
 static gboolean
diff --git a/libgimpwidgets/gimpcolorprofilecombobox.c b/libgimpwidgets/gimpcolorprofilecombobox.c
index 77b85bed6c..dbd82aa7af 100644
--- a/libgimpwidgets/gimpcolorprofilecombobox.c
+++ b/libgimpwidgets/gimpcolorprofilecombobox.c
@@ -443,6 +443,35 @@ gimp_color_profile_combo_box_set_active_file (GimpColorProfileComboBox *combo,
     g_object_unref (profile);
 }
 
+/**
+ * gimp_color_profile_combo_box_set_active_profile:
+ * @combo:   a #GimpColorProfileComboBox
+ * @profile: a #GimpColorProfile to set
+ *
+ * Selects a color profile from the @combo and makes it the active
+ * item.
+ *
+ * Since: 3.0
+ **/
+void
+gimp_color_profile_combo_box_set_active_profile (GimpColorProfileComboBox *combo,
+                                                 GimpColorProfile         *profile)
+{
+  GtkTreeModel     *model;
+  GtkTreeIter       iter;
+
+  g_return_if_fail (GIMP_IS_COLOR_PROFILE_COMBO_BOX (combo));
+  g_return_if_fail (profile == NULL || GIMP_IS_COLOR_PROFILE (profile));
+
+  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
+
+  if (_gimp_color_profile_store_history_find_profile (GIMP_COLOR_PROFILE_STORE (model),
+                                                      profile, &iter))
+    {
+      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
+    }
+}
+
 /**
  * gimp_color_profile_combo_box_get_active_file:
  * @combo: a #GimpColorProfileComboBox
diff --git a/libgimpwidgets/gimpcolorprofilecombobox.h b/libgimpwidgets/gimpcolorprofilecombobox.h
index 86f041dc52..93096c6914 100644
--- a/libgimpwidgets/gimpcolorprofilecombobox.h
+++ b/libgimpwidgets/gimpcolorprofilecombobox.h
@@ -62,22 +62,24 @@ struct _GimpColorProfileComboBoxClass
 };
 
 
-GType       gimp_color_profile_combo_box_get_type         (void) G_GNUC_CONST;
+GType       gimp_color_profile_combo_box_get_type           (void) G_GNUC_CONST;
 
-GtkWidget * gimp_color_profile_combo_box_new              (GtkWidget    *dialog,
-                                                           GFile        *history);
-GtkWidget * gimp_color_profile_combo_box_new_with_model   (GtkWidget    *dialog,
-                                                           GtkTreeModel *model);
+GtkWidget * gimp_color_profile_combo_box_new                (GtkWidget    *dialog,
+                                                             GFile        *history);
+GtkWidget * gimp_color_profile_combo_box_new_with_model     (GtkWidget    *dialog,
+                                                             GtkTreeModel *model);
 
-void        gimp_color_profile_combo_box_add_file         (GimpColorProfileComboBox *combo,
-                                                           GFile                    *file,
-                                                           const gchar              *label);
+void        gimp_color_profile_combo_box_add_file           (GimpColorProfileComboBox *combo,
+                                                             GFile                    *file,
+                                                             const gchar              *label);
 
-void        gimp_color_profile_combo_box_set_active_file  (GimpColorProfileComboBox *combo,
-                                                           GFile                    *file,
-                                                           const gchar              *label);
+void        gimp_color_profile_combo_box_set_active_file    (GimpColorProfileComboBox *combo,
+                                                             GFile                    *file,
+                                                             const gchar              *label);
+void        gimp_color_profile_combo_box_set_active_profile (GimpColorProfileComboBox *combo,
+                                                             GimpColorProfile         *profile);
 
-GFile *     gimp_color_profile_combo_box_get_active_file  (GimpColorProfileComboBox *combo);
+GFile *     gimp_color_profile_combo_box_get_active_file    (GimpColorProfileComboBox *combo);
 
 
 G_END_DECLS
diff --git a/libgimpwidgets/gimpcolorprofilestore-private.h b/libgimpwidgets/gimpcolorprofilestore-private.h
index 98de84d3f9..e65f941285 100644
--- a/libgimpwidgets/gimpcolorprofilestore-private.h
+++ b/libgimpwidgets/gimpcolorprofilestore-private.h
@@ -45,6 +45,11 @@ G_GNUC_INTERNAL gboolean  _gimp_color_profile_store_history_add     (GimpColorPr
                                                                      const gchar           *label,
                                                                      GtkTreeIter           *iter);
 
+G_GNUC_INTERNAL gboolean  _gimp_color_profile_store_history_find_profile
+                                                                    (GimpColorProfileStore *store,
+                                                                     GimpColorProfile      *profile,
+                                                                     GtkTreeIter           *iter);
+
 G_GNUC_INTERNAL void      _gimp_color_profile_store_history_reorder (GimpColorProfileStore *store,
                                                                      GtkTreeIter           *iter);
 
diff --git a/libgimpwidgets/gimpcolorprofilestore.c b/libgimpwidgets/gimpcolorprofilestore.c
index 6367de3cc5..8f25b86ede 100644
--- a/libgimpwidgets/gimpcolorprofilestore.c
+++ b/libgimpwidgets/gimpcolorprofilestore.c
@@ -24,8 +24,10 @@
 #include <string.h>
 
 #include <gtk/gtk.h>
+#include <gegl.h>
 
 #include "libgimpbase/gimpbase.h"
+#include "libgimpcolor/gimpcolor.h"
 #include "libgimpconfig/gimpconfig.h"
 
 #include "gimpwidgetstypes.h"
@@ -390,6 +392,83 @@ _gimp_color_profile_store_history_add (GimpColorProfileStore *store,
   return iter_valid;
 }
 
+/**
+ * _gimp_color_profile_store_history_find_profile:
+ * @store:   a #GimpColorProfileStore
+ * @profile: a #GimpColorProfile to find (or %NULL)
+ * @iter:    a #GtkTreeIter
+ *
+ * Returns: %TRUE if the iter is valid and pointing to the item
+ *
+ * Since: 3.0
+ **/
+gboolean
+_gimp_color_profile_store_history_find_profile (GimpColorProfileStore *store,
+                                                GimpColorProfile      *profile,
+                                                GtkTreeIter           *iter)
+{
+  GtkTreeModel *model;
+  gboolean      iter_valid;
+  gint          max = -1;
+
+  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE_STORE (store), FALSE);
+  g_return_val_if_fail (iter != NULL, FALSE);
+
+  model = GTK_TREE_MODEL (store);
+
+  for (iter_valid = gtk_tree_model_get_iter_first (model, iter);
+       iter_valid;
+       iter_valid = gtk_tree_model_iter_next (model, iter))
+    {
+      gint             type;
+      gint             index;
+      GFile            *this;
+      GimpColorProfile *combo_profile = NULL;
+
+      gtk_tree_model_get (model, iter,
+                          GIMP_COLOR_PROFILE_STORE_ITEM_TYPE, &type,
+                          GIMP_COLOR_PROFILE_STORE_INDEX,     &index,
+                          -1);
+
+      if (type != GIMP_COLOR_PROFILE_STORE_ITEM_FILE)
+        continue;
+
+      if (index > max)
+        max = index;
+
+      /*  check if we found a filename match  */
+      gtk_tree_model_get (model, iter,
+                          GIMP_COLOR_PROFILE_STORE_FILE, &this,
+                          -1);
+
+      /* Convert file to GimpColorProfile */
+      if (this)
+        combo_profile = gimp_color_profile_new_from_file (this, NULL);
+
+      if ((combo_profile && profile &&
+           gimp_color_profile_is_equal  (profile, combo_profile)) ||
+          (! this && ! profile))
+        {
+          if (this)
+            g_object_unref (this);
+          if (combo_profile)
+            g_object_unref (combo_profile);
+
+          return TRUE;
+        }
+
+      if (this)
+        g_object_unref (this);
+      if (combo_profile)
+            g_object_unref (combo_profile);
+    }
+
+  if (! profile)
+    return FALSE;
+
+  return iter_valid;
+}
+
 /**
  * _gimp_color_profile_store_history_reorder
  * @store: a #GimpColorProfileStore


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