From ad8b4178718bee79e042fa82f4e609b9f062100a Mon Sep 17 00:00:00 2001 From: Jehan Date: Fri, 30 Oct 2020 17:33:53 +0100 Subject: [PATCH] libgimpwidgets: better algorithm for GimpScaleEntry default increments. For very small ranges, just dividing by 10 and 100 is not very good. You could end up with weird step values. It is often better to use 10^(-x) values just below your range. I.e for a 0.5 range, a step of 0.1 and page of 0.01 are probably fine (better than 0.05 and 0.005). Of course as usual these are default values only and setting custom increments is possible through available API. Also fixing a small bug in gimp_scale_entry_set_increments() added in commit 0f05825a29. --- libgimpwidgets/gimpscaleentry.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/libgimpwidgets/gimpscaleentry.c b/libgimpwidgets/gimpscaleentry.c index d40f9d0fd2..12b4c2bb86 100644 --- a/libgimpwidgets/gimpscaleentry.c +++ b/libgimpwidgets/gimpscaleentry.c @@ -445,9 +445,31 @@ gimp_scale_entry_update_steps (GimpScaleEntry *entry) range = upper - lower; - if (range <= 1.0) + if (range > 0 && range <= 1.0) { - gimp_scale_entry_set_increments (entry, range / 10.0, range / 100.0); + gdouble places = 10.0; + gdouble step; + gdouble page; + + /* Compute some acceptable step and page increments always in the + * format `10**-X` where X is the rounded precision. + * So for instance: + * 0.8 will have increments 0.01 and 0.1. + * 0.3 will have increments 0.001 and 0.01. + * 0.06 will also have increments 0.001 and 0.01. + */ + while (range * places < 5.0) + places *= 10.0; + + + step = 0.1 / places; + page = 1.0 / places; + + gimp_scale_entry_set_increments (entry, step, page); + } + else if (range <= 2.0) + { + gimp_scale_entry_set_increments (entry, 0.01, 0.1); } else if (range <= 40.0) { @@ -917,7 +939,7 @@ gimp_scale_entry_set_increments (GimpScaleEntry *entry, gtk_adjustment_set_page_increment (gtk_range_get_adjustment (scale), page); gtk_adjustment_set_page_increment (gtk_spin_button_get_adjustment (spinbutton), page); - g_object_set (entry, + g_object_set (entry->priv->spinbutton, "climb-rate", step, NULL); }