From 99eeb94704d8518de75e9a69a708208af6fc33ab Mon Sep 17 00:00:00 2001 From: Alx Sa Date: Tue, 6 Feb 2024 13:28:40 +0000 Subject: [PATCH] tools: Add NDE option for Gradient tool This adds a new toggle that lets the user choose to apply a gradient as a non-destructive filter (off by default) --- app/tools/gimpgradientoptions.c | 21 ++++++++++++++++++++- app/tools/gimpgradientoptions.h | 1 + app/tools/gimpgradienttool.c | 23 +++++++++++++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/app/tools/gimpgradientoptions.c b/app/tools/gimpgradientoptions.c index a1a24fe609..e8ca35d3bf 100644 --- a/app/tools/gimpgradientoptions.c +++ b/app/tools/gimpgradientoptions.c @@ -51,7 +51,8 @@ enum PROP_SUPERSAMPLE_THRESHOLD, PROP_DITHER, PROP_INSTANT, - PROP_MODIFY_ACTIVE + PROP_MODIFY_ACTIVE, + PROP_APPLY_NON_DESTRUCTIVELY }; @@ -144,6 +145,14 @@ gimp_gradient_options_class_init (GimpGradientOptionsClass *klass) _("Modify the active gradient in-place"), FALSE, GIMP_PARAM_STATIC_STRINGS); + + GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_APPLY_NON_DESTRUCTIVELY, + "apply-non-destructively", + _("Apply non-destructively"), + _("If enabled, gradient will be applied as " + "a non-destructive filter"), + FALSE, + GIMP_PARAM_STATIC_STRINGS); } static void @@ -191,6 +200,9 @@ gimp_gradient_options_set_property (GObject *object, case PROP_MODIFY_ACTIVE: options->modify_active = g_value_get_boolean (value); break; + case PROP_APPLY_NON_DESTRUCTIVELY: + options->apply_non_destructively = g_value_get_boolean (value); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -238,6 +250,9 @@ gimp_gradient_options_get_property (GObject *object, case PROP_MODIFY_ACTIVE: g_value_set_boolean (value, options->modify_active); break; + case PROP_APPLY_NON_DESTRUCTIVELY: + g_value_set_boolean (value, options->apply_non_destructively); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -349,6 +364,10 @@ gimp_gradient_options_gui (GimpToolOptions *tool_options) options->instant_toggle = button; + /* the non-destructive filter toggle */ + button = gimp_prop_check_button_new (config, "apply-non-destructively", NULL); + gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); + /* the modify active toggle */ vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2); frame = gimp_prop_expanding_frame_new (config, "modify-active", NULL, diff --git a/app/tools/gimpgradientoptions.h b/app/tools/gimpgradientoptions.h index 0c07c0fd02..aedc593b42 100644 --- a/app/tools/gimpgradientoptions.h +++ b/app/tools/gimpgradientoptions.h @@ -49,6 +49,7 @@ struct _GimpGradientOptions gboolean instant; gboolean modify_active; + gboolean apply_non_destructively; /* options gui */ GtkWidget *instant_toggle; diff --git a/app/tools/gimpgradienttool.c b/app/tools/gimpgradienttool.c index 2f98807c87..64a6b226ab 100644 --- a/app/tools/gimpgradienttool.c +++ b/app/tools/gimpgradienttool.c @@ -43,6 +43,7 @@ #include "core/gimperror.h" #include "core/gimpgradient.h" #include "core/gimpimage.h" +#include "core/gimpimage-undo-push.h" #include "core/gimpprogress.h" #include "core/gimpprojection.h" @@ -757,7 +758,8 @@ gimp_gradient_tool_halt (GimpGradientTool *gradient_tool) static void gimp_gradient_tool_commit (GimpGradientTool *gradient_tool) { - GimpTool *tool = GIMP_TOOL (gradient_tool); + GimpTool *tool = GIMP_TOOL (gradient_tool); + GimpGradientOptions *options = GIMP_GRADIENT_TOOL_GET_OPTIONS (gradient_tool); if (gradient_tool->filter) { @@ -769,8 +771,25 @@ gimp_gradient_tool_commit (GimpGradientTool *gradient_tool) gimp_tool_control_push_preserve (tool->control, TRUE); - gimp_drawable_filter_commit (gradient_tool->filter, FALSE, + gimp_drawable_filter_commit (gradient_tool->filter, + options->apply_non_destructively, GIMP_PROGRESS (tool), FALSE); + + if (options->apply_non_destructively) + { + GimpDrawable *drawable = + gimp_drawable_filter_get_drawable (gradient_tool->filter); + + gimp_drawable_filter_layer_mask_freeze (gradient_tool->filter); + + gimp_image_undo_push_filter_add (gimp_display_get_image (tool->display), + _("Add filter"), + drawable, gradient_tool->filter); + } + + g_signal_handlers_disconnect_by_func (gradient_tool->filter, + gimp_gradient_tool_filter_flush, + gradient_tool); g_clear_object (&gradient_tool->filter); gimp_tool_control_pop_preserve (tool->control);