[gimp] libgimpwidgets: improved gimp_prop_scale_entry_new(), new function…



commit 2ea5dec56ec0ec00cd1351bd78eaf0b2aaeb2cb2
Author: Jehan <jehan girinstud io>
Date:   Wed Nov 25 02:19:39 2020 +0100

    libgimpwidgets: improved gimp_prop_scale_entry_new(), new function…
    
    … gimp_label_spin_set_digits() and deleted gimp_prop_opacity_entry_new()
    
    - The "digits" argument for the number of decimal places in
      gimp_prop_scale_entry_new() is now mostly useless since GimpLabelSpin
      (hence GimpScaleEntry too) got a nice estimation algorithm of sensible
      values.
    - Add gimp_label_spin_set_digits() function to manually set the digits
      property when we don't like the estimated value.
    - Also add a new "factor" argument to gimp_prop_scale_entry_new(). Its
      role is to allow a GimpScaleEntry showing a factored range, typically
      a [0, 100] range for some types of [0, 1] properties.
    - Remove gimp_prop_opacity_entry_new() which was basically a
      special-case of gimp_prop_scale_entry_new() and which can now be
      easily reproduced by simply set factor=100.0.
    - Update all usage of gimp_prop_scale_entry_new() in app/ and plug-ins/
      with updated arguments. It is interesting to note that all existing
      usage were either integers (digits=1) or when double, the estimated
      decimal places are the same as the ones which were manually set (hence
      showing the generic estimation is not too bad). So the new function
      gimp_label_spin_set_digits() was not even needed once in current code.

 app/widgets/gimpstrokeeditor.c        |   2 +-
 libgimpwidgets/gimplabelspin.c        |  21 ++++
 libgimpwidgets/gimplabelspin.h        |   2 +
 libgimpwidgets/gimppropwidgets.c      | 185 +++++++++++++---------------------
 libgimpwidgets/gimppropwidgets.h      |   8 +-
 libgimpwidgets/gimpwidgets.def        |   2 +-
 plug-ins/common/despeckle.c           |   6 +-
 plug-ins/common/file-csource.c        |   2 +-
 plug-ins/common/file-gbr.c            |   2 +-
 plug-ins/common/file-heif.c           |   2 +-
 plug-ins/common/file-xpm.c            |   2 +-
 plug-ins/file-webp/file-webp-dialog.c |   4 +-
 12 files changed, 106 insertions(+), 132 deletions(-)
---
diff --git a/app/widgets/gimpstrokeeditor.c b/app/widgets/gimpstrokeeditor.c
index b37b6b692a..bd7878d2cb 100644
--- a/app/widgets/gimpstrokeeditor.c
+++ b/app/widgets/gimpstrokeeditor.c
@@ -166,7 +166,7 @@ gimp_stroke_editor_constructed (GObject *object)
                             box, 2);
 
   scale = gimp_prop_scale_entry_new (G_OBJECT (options), "miter-limit",
-                                     NULL, 1, FALSE, 0.0, 0.0);
+                                     NULL, 1.0, FALSE, 0.0, 0.0);
   gtk_widget_hide (gimp_labeled_get_label (GIMP_LABELED (scale)));
   gimp_grid_attach_aligned (GTK_GRID (grid), 0, row++,
                             _("_Miter limit:"),
diff --git a/libgimpwidgets/gimplabelspin.c b/libgimpwidgets/gimplabelspin.c
index 3c779fa1e4..33ed62387c 100644
--- a/libgimpwidgets/gimplabelspin.c
+++ b/libgimpwidgets/gimplabelspin.c
@@ -479,6 +479,27 @@ gimp_label_spin_set_increments (GimpLabelSpin *spin,
                 NULL);
 }
 
+/**
+ * gimp_label_spin_set_digits:
+ * @spin: the #GimpLabelSpin.
+ * @digits: the number of decimal places to display.
+ *
+ * Set the number of decimal place to display in the @spin's entry.
+ * If @digits is -1, then it will also be estimated based on @spin's
+ * range. Digits estimation will always be at least 1, so if you want to
+ * show integer values only, set 0 explicitly.
+ */
+void
+gimp_label_spin_set_digits (GimpLabelSpin *spin,
+                            gint           digits)
+{
+  g_return_if_fail (GIMP_IS_LABEL_SPIN (spin));
+
+  g_object_set (spin,
+                "digits", digits,
+                NULL);
+}
+
 /**
  * gimp_label_spin_get_spin_button:
  * @spin: The #GimpLabelSpin
diff --git a/libgimpwidgets/gimplabelspin.h b/libgimpwidgets/gimplabelspin.h
index 62f672511c..b39995a2e6 100644
--- a/libgimpwidgets/gimplabelspin.h
+++ b/libgimpwidgets/gimplabelspin.h
@@ -64,6 +64,8 @@ gdouble      gimp_label_spin_get_value       (GimpLabelSpin *spin);
 void         gimp_label_spin_set_increments  (GimpLabelSpin *spin,
                                               gdouble        step,
                                               gdouble        page);
+void         gimp_label_spin_set_digits      (GimpLabelSpin *spin,
+                                              gint           digits);
 
 GtkWidget  * gimp_label_spin_get_spin_button (GimpLabelSpin *spin);
 
diff --git a/libgimpwidgets/gimppropwidgets.c b/libgimpwidgets/gimppropwidgets.c
index 27c59b261e..f92b4f0152 100644
--- a/libgimpwidgets/gimppropwidgets.c
+++ b/libgimpwidgets/gimppropwidgets.c
@@ -74,6 +74,15 @@ static void         connect_notify     (GObject     *config,
                                         GCallback    callback,
                                         gpointer     callback_data);
 
+static gboolean gimp_prop_widget_double_to_factor   (GBinding     *binding,
+                                                     const GValue *from_value,
+                                                     GValue       *to_value,
+                                                     gpointer      user_data);
+static gboolean gimp_prop_widget_double_from_factor (GBinding     *binding,
+                                                     const GValue *from_value,
+                                                     GValue       *to_value,
+                                                     gpointer      user_data);
+
 
 /******************/
 /*  check button  */
@@ -1511,22 +1520,30 @@ gimp_prop_hscale_new (GObject     *config,
  * @label: (nullable): The text for the #GtkLabel which will appear left of
  *                     the #GtkHScale.
  * @property_name:  Name of integer or double property controlled by the scale.
- * @digits:         Number of digits after decimal point to display. For
- *                  integer properties, this will be ignored (always 0).
- *                  If set to -1, a reasonable value will be
- *                  approximated depending on @property_name's range.
+ * @factor:         Optional multiplier to convert @property_name's
+ *                  range into the #GimpScaleEntry's range. The common
+ *                  usage is to set 1.0.
  * @limit_scale:    %FALSE if the range of possible values of the
  *                  GtkHScale should be the same as of the GtkSpinButton.
  * @lower_limit:    The scale's lower boundary if @scale_limits is %TRUE.
  * @upper_limit:    The scale's upper boundary if @scale_limits is %TRUE.
  *
  * Creates a #GimpScaleEntry (slider and spin button) to set and display
- * the value of a specified int or double property.
- * See gimp_scale_entry_new() for more information.
+ * the value of a specified int or double property with sensible default
+ * settings depending on the range (decimal places, increments, etc.).
+ * These settings can be overridden by the relevant widget methods.
  *
  * If @label is %NULL, the @property_name's nick will be used as label
  * of the returned object.
  *
+ * If @factor is not 1.0, the widget's range will be computed based of
+ * @property_name's range multiplied by @factor. A typical usage would
+ * be to display a [0.0, 1.0] range as [0.0, 100.0] by setting 100.0 as
+ * @factor.
+ *
+ * See gimp_scale_entry_set_bounds() for more information on
+ * @limit_scale, @lower_limit and @upper_limit.
+ *
  * Returns: (transfer full): The newly allocated #GimpScaleEntry.
  *
  * Since: 2.4
@@ -1535,7 +1552,7 @@ GtkWidget *
 gimp_prop_scale_entry_new (GObject     *config,
                            const gchar *property_name,
                            const gchar *label,
-                           gint         digits,
+                           gdouble      factor,
                            gboolean     limit_scale,
                            gdouble      lower_limit,
                            gdouble      upper_limit)
@@ -1543,10 +1560,13 @@ gimp_prop_scale_entry_new (GObject     *config,
   GtkWidget   *widget;
   GParamSpec  *param_spec;
   const gchar *tooltip;
+  gdouble     *user_data;
   gdouble      value;
   gdouble      lower;
   gdouble      upper;
 
+  g_return_val_if_fail (factor != 0.0, NULL);
+
   param_spec = find_param_spec (config, property_name, G_STRFUNC);
   if (! param_spec)
     return NULL;
@@ -1555,121 +1575,29 @@ gimp_prop_scale_entry_new (GObject     *config,
                             param_spec, &value, &lower, &upper, G_STRFUNC))
     return NULL;
 
-  if (G_IS_PARAM_SPEC_INT (param_spec) || G_IS_PARAM_SPEC_UINT (param_spec))
-    digits = 0;
-
   if (! label)
     label = g_param_spec_get_nick (param_spec);
 
-  widget = gimp_scale_entry_new (label, value, lower, upper, digits);
+  widget = gimp_scale_entry_new (label, value, lower * factor, upper * factor, -1);
   if (limit_scale)
-    {
-      gimp_scale_entry_set_bounds (GIMP_SCALE_ENTRY (widget),
-                                   lower_limit, upper_limit,
-                                   FALSE);
-    }
+    gimp_scale_entry_set_bounds (GIMP_SCALE_ENTRY (widget),
+                                 lower_limit, upper_limit,
+                                 FALSE);
 
   tooltip = g_param_spec_get_blurb (param_spec);
   gimp_help_set_help_data (widget, tooltip, NULL);
 
-  g_object_bind_property (config, property_name,
-                          widget, "value",
-                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
-
-  return widget;
-}
-
-static void
-gimp_prop_widget_set_factor (GtkWidget     *widget,
-                             GtkAdjustment *adjustment,
-                             gdouble        factor,
-                             gdouble        step_increment,
-                             gdouble        page_increment,
-                             gint           digits)
-{
-  gdouble *factor_store;
-  gdouble  old_factor = 1.0;
-  gdouble  f;
-
-  g_return_if_fail (widget == NULL || GTK_IS_SPIN_BUTTON (widget));
-  g_return_if_fail (widget != NULL || GTK_IS_ADJUSTMENT (adjustment));
-  g_return_if_fail (factor != 0.0);
-  g_return_if_fail (digits >= 0);
-
-  if (! adjustment)
-    adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
-
-  g_return_if_fail (get_param_spec (G_OBJECT (adjustment)) != NULL);
-
-  factor_store = g_object_get_data (G_OBJECT (adjustment),
-                                    "gimp-prop-adjustment-factor");
-  if (factor_store)
-    {
-      old_factor = *factor_store;
-    }
-  else
-    {
-      factor_store = g_new (gdouble, 1);
-      g_object_set_data_full (G_OBJECT (adjustment),
-                              "gimp-prop-adjustment-factor",
-                              factor_store, (GDestroyNotify) g_free);
-    }
-
-  *factor_store = factor;
-
-  f = factor / old_factor;
-
-  if (step_increment <= 0)
-    step_increment = f * gtk_adjustment_get_step_increment (adjustment);
-
-  if (page_increment <= 0)
-    page_increment = f * gtk_adjustment_get_page_increment (adjustment);
-
-  gtk_adjustment_configure (adjustment,
-                            f * gtk_adjustment_get_value (adjustment),
-                            f * gtk_adjustment_get_lower (adjustment),
-                            f * gtk_adjustment_get_upper (adjustment),
-                            step_increment,
-                            page_increment,
-                            f * gtk_adjustment_get_page_size (adjustment));
-
-  gtk_spin_button_set_digits (GTK_SPIN_BUTTON (widget), digits);
-}
-
-/**
- * gimp_prop_opacity_entry_new:
- * @config:        Object to which property is attached.
- * @property_name: Name of double property controlled by the spin button.
- * @label:         The text for the #GtkLabel which will appear left of the
- *                 #GtkHScale.
- *
- * Creates a #libgimpwidgets-gimpscaleentry (slider and spin button)
- * to set and display the value of the specified double property,
- * which should represent an "opacity" variable with range 0 to 100.
- * See gimp_scale_entry_new() for more information.
- *
- * Returns: (transfer full): The #GtkSpinButton's #GtkAdjustment.
- *
- * Since: 2.4
- */
-GtkWidget *
-gimp_prop_opacity_entry_new (GObject     *config,
-                             const gchar *property_name,
-                             const gchar *label)
-{
-  GtkWidget *widget;
-
-  g_return_val_if_fail (G_IS_OBJECT (config), NULL);
-  g_return_val_if_fail (property_name != NULL, NULL);
-
-  widget = gimp_prop_scale_entry_new (config, property_name,
-                                      label, 1, FALSE, 0.0, 0.0);
-
-  if (widget)
-    {
-      gimp_prop_widget_set_factor (gimp_label_spin_get_spin_button (GIMP_LABEL_SPIN (widget)),
-                                   NULL, 100.0, 0.0, 0.0, 1);
-    }
+  user_data = g_new0 (gdouble, 1);
+  *user_data = factor;
+  /* With @factor == 1.0, this is equivalent to a
+   * g_object_bind_property().
+   */
+  g_object_bind_property_full (config, property_name,
+                               widget, "value",
+                               G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE,
+                               gimp_prop_widget_double_to_factor,
+                               gimp_prop_widget_double_from_factor,
+                               user_data, (GDestroyNotify) g_free);
 
   return widget;
 }
@@ -4280,3 +4208,32 @@ connect_notify (GObject     *config,
 
   g_free (notify_name);
 }
+
+
+static gboolean
+gimp_prop_widget_double_to_factor (GBinding     *binding,
+                                   const GValue *from_value,
+                                   GValue       *to_value,
+                                   gpointer      user_data)
+{
+  gdouble *factor = (gdouble*) user_data;
+  gdouble  val    = g_value_get_double (from_value);
+
+  g_value_set_double (to_value, val * (*factor));
+
+  return TRUE;
+}
+
+static gboolean
+gimp_prop_widget_double_from_factor (GBinding     *binding,
+                                     const GValue *from_value,
+                                     GValue       *to_value,
+                                     gpointer      user_data)
+{
+  gdouble *factor = (gdouble*) user_data;
+  gdouble val     = g_value_get_double (from_value);
+
+  g_value_set_double (to_value, val / (*factor));
+
+  return TRUE;
+}
diff --git a/libgimpwidgets/gimppropwidgets.h b/libgimpwidgets/gimppropwidgets.h
index 78adefde97..18fc441dd7 100644
--- a/libgimpwidgets/gimppropwidgets.h
+++ b/libgimpwidgets/gimppropwidgets.h
@@ -128,17 +128,11 @@ GtkWidget     * gimp_prop_hscale_new              (GObject      *config,
 GtkWidget     * gimp_prop_scale_entry_new         (GObject      *config,
                                                    const gchar  *property_name,
                                                    const gchar  *label,
-                                                   gint          digits,
+                                                   gdouble       factor,
                                                    gboolean      limit_scale,
                                                    gdouble       lower_limit,
                                                    gdouble       upper_limit);
 
-/*  special form of gimp_prop_scale_entry_new() for GParamDouble  */
-
-GtkWidget     * gimp_prop_opacity_entry_new       (GObject       *config,
-                                                   const gchar   *property_name,
-                                                   const gchar   *label);
-
 
 /*  GimpParamMemsize  */
 
diff --git a/libgimpwidgets/gimpwidgets.def b/libgimpwidgets/gimpwidgets.def
index e269a3cd17..87795d89af 100644
--- a/libgimpwidgets/gimpwidgets.def
+++ b/libgimpwidgets/gimpwidgets.def
@@ -234,6 +234,7 @@ EXPORTS
        gimp_label_spin_get_type
        gimp_label_spin_get_value
        gimp_label_spin_new
+       gimp_label_spin_set_digits
        gimp_label_spin_set_increments
        gimp_label_spin_set_value
        gimp_labeled_get_label
@@ -351,7 +352,6 @@ EXPORTS
        gimp_prop_label_new
        gimp_prop_label_spin_new
        gimp_prop_memsize_entry_new
-       gimp_prop_opacity_entry_new
        gimp_prop_path_editor_new
        gimp_prop_pointer_combo_box_new
        gimp_prop_scale_entry_new
diff --git a/plug-ins/common/despeckle.c b/plug-ins/common/despeckle.c
index 60c28e33d3..9034e5f352 100644
--- a/plug-ins/common/despeckle.c
+++ b/plug-ins/common/despeckle.c
@@ -459,7 +459,7 @@ despeckle_dialog (GimpProcedure *procedure,
 
   scale = gimp_prop_scale_entry_new (config, "radius",
                                      _("_Radius:"),
-                                     0, FALSE, 0, 0);
+                                     1.0, FALSE, 0, 0);
   gtk_grid_attach (GTK_GRID (grid), scale, 0, 0, 1, 1);
   gtk_widget_show (scale);
 
@@ -469,7 +469,7 @@ despeckle_dialog (GimpProcedure *procedure,
 
   scale = gimp_prop_scale_entry_new (config, "black",
                                      _("_Black level:"),
-                                     0, FALSE, 0, 0);
+                                     1.0, FALSE, 0, 0);
   gtk_grid_attach (GTK_GRID (grid), scale, 0, 1, 1, 1);
   gtk_widget_show (scale);
 
@@ -479,7 +479,7 @@ despeckle_dialog (GimpProcedure *procedure,
 
   scale = gimp_prop_scale_entry_new (config, "white",
                                      _("_White level:"),
-                                     0, FALSE, 0, 0);
+                                     1.0, FALSE, 0, 0);
   gtk_grid_attach (GTK_GRID (grid), scale, 0, 2, 1, 1);
   gtk_widget_show (scale);
 
diff --git a/plug-ins/common/file-csource.c b/plug-ins/common/file-csource.c
index 08a7e27966..43c0e07233 100644
--- a/plug-ins/common/file-csource.c
+++ b/plug-ins/common/file-csource.c
@@ -1036,7 +1036,7 @@ save_dialog (GimpProcedure *procedure,
   /* Max Alpha Value
    */
   scale = gimp_prop_scale_entry_new (config, "opacity", _("Op_acity:"),
-                                     1, FALSE, 0, 0);
+                                     1.0, FALSE, 0, 0);
   gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 4);
   gtk_widget_show (scale);
   gtk_widget_show (dialog);
diff --git a/plug-ins/common/file-gbr.c b/plug-ins/common/file-gbr.c
index 95545d89b2..01b3b2d0f9 100644
--- a/plug-ins/common/file-gbr.c
+++ b/plug-ins/common/file-gbr.c
@@ -318,7 +318,7 @@ save_dialog (GimpProcedure *procedure,
                             entry, 2);
 
   scale = gimp_prop_scale_entry_new (config, "spacing",
-                                     NULL, 0, FALSE, 0, 0);
+                                     NULL, 1.0, FALSE, 0, 0);
   gtk_widget_hide (gimp_labeled_get_label (GIMP_LABELED (scale)));
   gimp_grid_attach_aligned (GTK_GRID (grid), 0, 1,
                             _("_Spacing:"), 0.0, 0.5, scale, 4);
diff --git a/plug-ins/common/file-heif.c b/plug-ins/common/file-heif.c
index c651d33fbc..7ea0493308 100644
--- a/plug-ins/common/file-heif.c
+++ b/plug-ins/common/file-heif.c
@@ -2521,7 +2521,7 @@ save_dialog (GimpProcedure *procedure,
                           G_BINDING_INVERT_BOOLEAN);
 
   scale = gimp_prop_scale_entry_new (config, "quality",
-                                     NULL, 0, FALSE, 0, 0);
+                                     NULL, 1.0, FALSE, 0, 0);
   gtk_widget_hide (gimp_labeled_get_label (GIMP_LABELED (scale)));
   gimp_grid_attach_aligned (GTK_GRID (grid), 0, 1,
                             _("_Quality"),
diff --git a/plug-ins/common/file-xpm.c b/plug-ins/common/file-xpm.c
index e433d17e21..73d7d29b43 100644
--- a/plug-ins/common/file-xpm.c
+++ b/plug-ins/common/file-xpm.c
@@ -896,7 +896,7 @@ save_dialog (GimpProcedure *procedure,
                                       GIMP_PROCEDURE_CONFIG (config),
                                       _("Export Image as XPM"));
 
-  scale = gimp_prop_scale_entry_new (config, "threshold", NULL, 0, FALSE, 0, 0);
+  scale = gimp_prop_scale_entry_new (config, "threshold", NULL, 1.0, FALSE, 0, 0);
   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                       scale, TRUE, TRUE, 6);
   gtk_widget_show (scale);
diff --git a/plug-ins/file-webp/file-webp-dialog.c b/plug-ins/file-webp/file-webp-dialog.c
index 49a092777d..b459293350 100644
--- a/plug-ins/file-webp/file-webp-dialog.c
+++ b/plug-ins/file-webp/file-webp-dialog.c
@@ -123,14 +123,14 @@ save_dialog (GimpImage     *image,
   /* Create the slider for image quality */
   quality_scale = gimp_prop_scale_entry_new (config, "quality",
                                              _("Image _quality:"),
-                                             0, FALSE, 0, 0);
+                                             1.0, FALSE, 0, 0);
   gtk_grid_attach (GTK_GRID (grid), quality_scale, 0, row++, 3, 1);
   gtk_widget_show (quality_scale);
 
   /* Create the slider for alpha channel quality */
   alpha_quality_scale = gimp_prop_scale_entry_new (config, "alpha-quality",
                                                    _("Alpha q_uality:"),
-                                                   0, FALSE, 0, 0);
+                                                   1.0, FALSE, 0, 0);
   gtk_grid_attach (GTK_GRID (grid), alpha_quality_scale, 0, row++, 3, 1);
   gtk_widget_show (alpha_quality_scale);
 


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