app, libgimpwidgets, plug-ins: default increments for GimpScaleEntry.

Instead of setting always manually the step and page increments when
creating a GimpScaleEntry, let's just generate some common cases
automatically. Indeed the increments are rarely something you want to
care about. The algorithm used is:
- For a range under 1.0, use a hundredth and a tenth (typically a [0,
  1.0] range will step-increment of 0.01 and page-increment of 0.1).
- For small ranges (under 40), step-increment by 1, page-increment by 2.
- For bigger ranges, step-increment by 1, page-increment by 10.

For use cases when you absolutely want specific increment values, I add
the gimp_scale_entry_set_increments() function. It is much better to
have a small and understandable constructor call followed by
configuration calls (only when needed) rather than a constructor with a
crazy amount of parameters. Hence gimp_scale_entry_new() went from 17
arguments (absolutely unreadable calls) to now 5.
This commit is contained in:
Jehan 2020-10-30 11:58:05 +01:00
parent 1e81bdabb0
commit 0f05825a29
13 changed files with 107 additions and 122 deletions

View file

@ -313,7 +313,7 @@ palette_import_dialog_new (GimpContext *context)
/* The # of colors */
private->num_colors = gimp_scale_entry_new2 (_("N_umber of colors:"),
256, 2, 10000, 1, 10, 0);
256, 2, 10000, 0);
gimp_grid_attach_aligned (GTK_GRID (grid), -1, 1,
NULL, 0.0, 0.5,
private->num_colors, 3);
@ -325,8 +325,7 @@ palette_import_dialog_new (GimpContext *context)
private);
/* The columns */
private->columns = gimp_scale_entry_new2 (_("C_olumns:"),
16, 0, 64, 1, 8, 0);
private->columns = gimp_scale_entry_new2 (_("C_olumns:"), 16, 0, 64, 0);
gimp_grid_attach_aligned (GTK_GRID (grid), -1, 2,
NULL, 0.0, 0.5,
private->columns, 3);
@ -336,8 +335,7 @@ palette_import_dialog_new (GimpContext *context)
private);
/* The interval */
private->threshold = gimp_scale_entry_new2 (_("I_nterval:"),
1, 1, 128, 1, 8, 0);
private->threshold = gimp_scale_entry_new2 (_("I_nterval:"), 1, 1, 128, 0);
gimp_grid_attach_aligned (GTK_GRID (grid), -1, 3,
NULL, 0.0, 0.5,
private->threshold, 3);

View file

@ -166,8 +166,7 @@ gimp_stroke_editor_constructed (GObject *object)
box, 2);
scale = gimp_prop_scale_entry_new (G_OBJECT (options), "miter-limit",
NULL, 1.0, 1.0, 1,
FALSE, 0.0, 0.0);
NULL, 1, FALSE, 0.0, 0.0);
gtk_widget_hide (gimp_scale_entry_get_label (GIMP_SCALE_ENTRY (scale)));
gimp_grid_attach_aligned (GTK_GRID (grid), 0, row++,
_("_Miter limit:"),

View file

@ -1465,8 +1465,6 @@ 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.
* @step_increment: Step size.
* @page_increment: Page size.
* @digits: Number of digits after decimal point to display. For
* integer properties, this will be ignored (always 0).
* @limit_scale: %FALSE if the range of possible values of the
@ -1489,8 +1487,6 @@ GtkWidget *
gimp_prop_scale_entry_new (GObject *config,
const gchar *property_name,
const gchar *label,
gdouble step_increment,
gdouble page_increment,
gint digits,
gboolean limit_scale,
gdouble lower_limit,
@ -1518,7 +1514,6 @@ gimp_prop_scale_entry_new (GObject *config,
label = g_param_spec_get_nick (param_spec);
widget = gimp_scale_entry_new2 (label, value, lower, upper,
step_increment, page_increment,
digits);
if (limit_scale)
{
@ -1621,8 +1616,7 @@ gimp_prop_opacity_entry_new (GObject *config,
g_return_val_if_fail (property_name != NULL, NULL);
widget = gimp_prop_scale_entry_new (config, property_name,
label, 0.01, 0.1, 1,
FALSE, 0.0, 0.0);
label, 1, FALSE, 0.0, 0.0);
if (widget)
{

View file

@ -123,8 +123,6 @@ GtkWidget * gimp_prop_hscale_new (GObject *config,
GtkWidget * gimp_prop_scale_entry_new (GObject *config,
const gchar *property_name,
const gchar *label,
gdouble step_increment,
gdouble page_increment,
gint digits,
gboolean limit_scale,
gdouble lower_limit,

View file

@ -55,8 +55,6 @@ enum
PROP_VALUE,
PROP_LOWER,
PROP_UPPER,
PROP_STEP_INCREMENT,
PROP_PAGE_INCREMENT,
PROP_DIGITS,
};
@ -83,6 +81,7 @@ static void gimp_scale_entry_get_property (GObject *object,
GParamSpec *pspec);
static void gimp_scale_entry_update_spin_width (GimpScaleEntry *entry);
static void gimp_scale_entry_update_steps (GimpScaleEntry *entry);
static gboolean gimp_scale_entry_linear_to_log (GBinding *binding,
const GValue *from_value,
@ -182,28 +181,6 @@ gimp_scale_entry_class_init (GimpScaleEntryClass *klass)
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
GIMP_PARAM_READWRITE));
/**
* GimpScaleEntry:step-increment:
*
* Since: 3.0
**/
g_object_class_install_property (object_class, PROP_STEP_INCREMENT,
g_param_spec_double ("step-increment", NULL,
"Step increment",
G_MINDOUBLE, G_MAXDOUBLE, 1.0,
GIMP_PARAM_READWRITE));
/**
* GimpScaleEntry:page-increment:
*
* Since: 3.0
**/
g_object_class_install_property (object_class, PROP_PAGE_INCREMENT,
g_param_spec_double ("page-increment", NULL,
"Page increment",
G_MINDOUBLE, G_MAXDOUBLE, 10.0,
GIMP_PARAM_READWRITE));
/**
* GimpScaleEntry:digits:
*
@ -336,6 +313,7 @@ gimp_scale_entry_set_property (GObject *object,
g_value_get_double (value));
gimp_scale_entry_update_spin_width (entry);
gimp_scale_entry_update_steps (entry);
}
break;
case PROP_UPPER:
@ -343,7 +321,7 @@ gimp_scale_entry_set_property (GObject *object,
GtkSpinButton *spinbutton;
GtkRange *scale;
g_return_if_fail (entry->priv->spinbutton);
g_return_if_fail (entry->priv->scale && entry->priv->spinbutton);
spinbutton = GTK_SPIN_BUTTON (entry->priv->spinbutton);
scale = GTK_RANGE (entry->priv->scale);
@ -353,40 +331,7 @@ gimp_scale_entry_set_property (GObject *object,
g_value_get_double (value));
gimp_scale_entry_update_spin_width (entry);
}
break;
case PROP_STEP_INCREMENT:
{
GtkSpinButton *spinbutton;
GtkAdjustment *adj;
g_return_if_fail (entry->priv->scale && entry->priv->spinbutton);
adj = gtk_range_get_adjustment (GTK_RANGE (entry->priv->scale));
gtk_adjustment_set_step_increment (adj, g_value_get_double (value));
spinbutton = GTK_SPIN_BUTTON (entry->priv->spinbutton);
if (adj != gtk_spin_button_get_adjustment (spinbutton))
gtk_adjustment_set_step_increment (gtk_spin_button_get_adjustment (spinbutton), g_value_get_double (value));
g_object_set (entry->priv->spinbutton,
"climb-rate", g_value_get_double (value),
NULL);
}
break;
case PROP_PAGE_INCREMENT:
{
GtkSpinButton *spinbutton;
GtkAdjustment *adj;
g_return_if_fail (entry->priv->scale && entry->priv->spinbutton);
adj = gtk_range_get_adjustment (GTK_RANGE (entry->priv->scale));
gtk_adjustment_set_page_increment (adj, g_value_get_double (value));
spinbutton = GTK_SPIN_BUTTON (entry->priv->spinbutton);
if (adj != gtk_spin_button_get_adjustment (spinbutton))
gtk_adjustment_set_page_increment (gtk_spin_button_get_adjustment (spinbutton), g_value_get_double (value));
gimp_scale_entry_update_steps (entry);
}
break;
case PROP_DIGITS:
@ -441,22 +386,6 @@ gimp_scale_entry_get_property (GObject *object,
g_value_set_double (value, gtk_adjustment_get_upper (adj));
}
break;
case PROP_STEP_INCREMENT:
{
GtkAdjustment *adj;
adj = gtk_spin_button_get_adjustment (spinbutton);
g_value_set_double (value, gtk_adjustment_get_step_increment (adj));
}
break;
case PROP_PAGE_INCREMENT:
{
GtkAdjustment *adj;
adj = gtk_spin_button_get_adjustment (spinbutton);
g_value_set_double (value, gtk_adjustment_get_page_increment (adj));
}
break;
case PROP_DIGITS:
g_value_set_uint (value, gtk_spin_button_get_digits (spinbutton));
break;
@ -475,6 +404,8 @@ gimp_scale_entry_update_spin_width (GimpScaleEntry *entry)
gdouble upper;
gint digits;
g_return_if_fail (GIMP_IS_SCALE_ENTRY (entry));
g_object_get (entry,
"lower", &lower,
"upper", &upper,
@ -496,6 +427,38 @@ gimp_scale_entry_update_spin_width (GimpScaleEntry *entry)
gtk_entry_set_width_chars (GTK_ENTRY (entry->priv->spinbutton), width);
}
static void
gimp_scale_entry_update_steps (GimpScaleEntry *entry)
{
gdouble lower;
gdouble upper;
gdouble range;
g_return_if_fail (GIMP_IS_SCALE_ENTRY (entry));
g_object_get (entry,
"lower", &lower,
"upper", &upper,
NULL);
g_return_if_fail (upper >= lower);
range = upper - lower;
if (range <= 1.0)
{
gimp_scale_entry_set_increments (entry, range / 10.0, range / 100.0);
}
else if (range <= 40.0)
{
gimp_scale_entry_set_increments (entry, 1.0, 2.0);
}
else
{
gimp_scale_entry_set_increments (entry, 1.0, 10.0);
}
}
static gboolean
gimp_scale_entry_linear_to_log (GBinding *binding,
const GValue *from_value,
@ -652,8 +615,6 @@ gimp_scale_entry_new_internal (gboolean color_scale,
* @value: The initial value.
* @lower: The lower boundary.
* @upper: The upper boundary.
* @step_increment: The step increment.
* @page_increment: The page increment.
* @digits: The number of decimal digits.
*
* This function creates a #GtkLabel, a #GtkHScale and a #GtkSpinButton and
@ -666,8 +627,6 @@ gimp_scale_entry_new2 (const gchar *text,
gdouble value,
gdouble lower,
gdouble upper,
gdouble step_increment,
gdouble page_increment,
guint digits)
{
GtkWidget *entry;
@ -677,8 +636,6 @@ gimp_scale_entry_new2 (const gchar *text,
"value", value,
"lower", lower,
"upper", upper,
"step-increment", step_increment,
"page-increment", page_increment,
"digits", digits,
NULL);
@ -776,6 +733,11 @@ gimp_scale_entry_get_scale (GimpScaleEntry *entry)
*
* If @limit_scale is %FALSE though, it would sync back both widgets
* range to the new values.
*
* Note that the step and page increments are updated when the range is
* updated according to some common usage algorithm which should work if
* you don't have very specific needs. If you want to customize the step
* increments yourself, you may call gimp_scale_entry_set_increments()
**/
void
gimp_scale_entry_set_range (GimpScaleEntry *entry,
@ -916,6 +878,50 @@ gimp_scale_entry_get_logarithmic (GimpScaleEntry *entry)
return entry->priv->logarithmic;
}
/**
* gimp_scale_entry_set_increments:
* @entry: the #GimpScaleEntry.
* @step: the step increment.
* @page: the page increment.
*
* Set the step and page increments for both the spin button and the
* scale. By default, these increment values are automatically computed
* depending on the range based on common usage. So you will likely not
* need to run this for most case. Yet if you want specific increments
* (which the widget cannot guess), you can call this function. If you
* want even more modularity and have different increments on each
* widget, use specific functions on gimp_scale_entry_get_spin_button()
* and gimp_scale_entry_get_scale().
*
* Note that there is no `get` function because it could not return
* shared values in case you edited each widget separately.
* Just use specific functions on the spin button and scale instead if
* you need to get existing increments.
*/
void
gimp_scale_entry_set_increments (GimpScaleEntry *entry,
gdouble step,
gdouble page)
{
GtkSpinButton *spinbutton;
GtkRange *scale;
g_return_if_fail (entry->priv->scale && entry->priv->spinbutton);
spinbutton = GTK_SPIN_BUTTON (entry->priv->spinbutton);
scale = GTK_RANGE (entry->priv->scale);
gtk_adjustment_set_step_increment (gtk_range_get_adjustment (scale), step);
gtk_adjustment_set_step_increment (gtk_spin_button_get_adjustment (spinbutton), step);
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,
"climb-rate", step,
NULL);
}
/**
* gimp_scale_entry_new:
* @grid: The #GtkGrid the widgets will be attached to.

View file

@ -68,8 +68,6 @@ GtkWidget * gimp_scale_entry_new2 (const gchar *text,
gdouble value,
gdouble lower,
gdouble upper,
gdouble step_increment,
gdouble page_increment,
guint digits);
gdouble gimp_scale_entry_get_value (GimpScaleEntry *entry);
@ -87,6 +85,10 @@ void gimp_scale_entry_set_logarithmic (GimpScaleEntry *entry,
gboolean logarithmic);
gboolean gimp_scale_entry_get_logarithmic (GimpScaleEntry *entry);
void gimp_scale_entry_set_increments (GimpScaleEntry *entry,
gdouble step,
gdouble page);
/**
* GIMP_SCALE_ENTRY_LABEL:
* @adj: The #GtkAdjustment returned by gimp_scale_entry_new().

View file

@ -459,8 +459,7 @@ despeckle_dialog (GimpProcedure *procedure,
scale = gimp_prop_scale_entry_new (config, "radius",
_("_Radius:"),
1, 5, 0,
FALSE, 0, 0);
0, FALSE, 0, 0);
gtk_grid_attach (GTK_GRID (grid), scale, 0, 0, 1, 1);
gtk_widget_show (scale);
@ -470,8 +469,7 @@ despeckle_dialog (GimpProcedure *procedure,
scale = gimp_prop_scale_entry_new (config, "black",
_("_Black level:"),
1, 8, 0,
FALSE, 0, 0);
0, FALSE, 0, 0);
gtk_grid_attach (GTK_GRID (grid), scale, 0, 1, 1, 1);
gtk_widget_show (scale);
@ -481,8 +479,7 @@ despeckle_dialog (GimpProcedure *procedure,
scale = gimp_prop_scale_entry_new (config, "white",
_("_White level:"),
1, 8, 0,
FALSE, 0, 0);
0, FALSE, 0, 0);
gtk_grid_attach (GTK_GRID (grid), scale, 0, 2, 1, 1);
gtk_widget_show (scale);

View file

@ -1035,9 +1035,8 @@ save_dialog (GimpProcedure *procedure,
/* Max Alpha Value
*/
scale = gimp_prop_scale_entry_new (config, "opacity",
_("Op_acity:"), 1, 10, 1,
FALSE, 0, 0);
scale = gimp_prop_scale_entry_new (config, "opacity", _("Op_acity:"),
1, FALSE, 0, 0);
gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 4);
gtk_widget_show (scale);
gtk_widget_show (dialog);

View file

@ -318,8 +318,7 @@ save_dialog (GimpProcedure *procedure,
entry, 2);
scale = gimp_prop_scale_entry_new (config, "spacing",
NULL, 1, 10, 0,
FALSE, 0, 0);
NULL, 0, FALSE, 0, 0);
gtk_widget_hide (gimp_scale_entry_get_label (GIMP_SCALE_ENTRY (scale)));
gimp_grid_attach_aligned (GTK_GRID (grid), 0, 1,
_("_Spacing:"), 0.0, 0.5, scale, 4);

View file

@ -2521,9 +2521,7 @@ save_dialog (GimpProcedure *procedure,
G_BINDING_INVERT_BOOLEAN);
scale = gimp_prop_scale_entry_new (config, "quality",
NULL,
1, 10, 0,
FALSE, 0, 0);
NULL, 0, FALSE, 0, 0);
gtk_widget_hide (gimp_scale_entry_get_label (GIMP_SCALE_ENTRY (scale)));
gimp_grid_attach_aligned (GTK_GRID (grid), 0, 1,
_("_Quality"),

View file

@ -2325,8 +2325,7 @@ save_dialog (GimpImage *image,
/* Compression level scale */
scale = gimp_prop_scale_entry_new (config, "compression",
NULL, 1, 1, 0,
FALSE, 0, 0);
NULL, 0, FALSE, 0, 0);
gtk_widget_hide (gimp_scale_entry_get_label (GIMP_SCALE_ENTRY (scale)));
gimp_grid_attach_aligned (GTK_GRID (grid), 0, 1,
_("Co_mpression level:"),

View file

@ -896,9 +896,7 @@ save_dialog (GimpProcedure *procedure,
GIMP_PROCEDURE_CONFIG (config),
_("Export Image as XPM"));
scale = gimp_prop_scale_entry_new (config, "threshold",
NULL, 1, 8, 0,
FALSE, 0, 0);
scale = gimp_prop_scale_entry_new (config, "threshold", NULL, 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);

View file

@ -123,16 +123,14 @@ save_dialog (GimpImage *image,
/* Create the slider for image quality */
quality_scale = gimp_prop_scale_entry_new (config, "quality",
_("Image _quality:"),
1.0, 10.0, 0,
FALSE, 0, 0);
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:"),
1.0, 10.0, 0,
FALSE, 0, 0);
0, FALSE, 0, 0);
gtk_grid_attach (GTK_GRID (grid), alpha_quality_scale, 0, row++, 3, 1);
gtk_widget_show (alpha_quality_scale);