plug-ins: forgotten Python plug-ins had to be ported to new Gimp.Procedure.new().

There was also one case of port to updated GimpResource API to get the name of a
palette.
This commit is contained in:
Jehan 2023-06-16 23:36:11 +02:00
parent 2b38a2df86
commit 3806b46fc5
5 changed files with 37 additions and 80 deletions

View File

@ -61,21 +61,13 @@ def format_text(text):
return ",".join(new_text)
def gradient_css_save(procedure, args, data):
if args.length() != 3:
error = 'Wrong parameters given'
return procedure.new_return_values(Gimp.PDBStatusType.CALLING_ERROR,
GLib.Error(error))
runmode = args.index(0)
gradient = args.index(1)
file = args.index(2)
config = procedure.create_config()
config.begin_run(None, Gimp.RunMode.INTERACTIVE, args)
def gradient_css_save(procedure, config, data):
runmode = config.get_property("run-mode")
if runmode == Gimp.RunMode.INTERACTIVE:
GimpUi.init('python-fu-gradient-save-as-css')
dialog = GimpUi.ProcedureDialog(procedure=procedure, config=config)
file = None
# Add gradient button
dialog.fill (["gradient"])
@ -137,20 +129,19 @@ def gradient_css_save(procedure, args, data):
if not dialog.run():
dialog.destroy()
config.end_run(Gimp.PDBStatusType.CANCEL)
return procedure.new_return_values(Gimp.PDBStatusType.CANCEL, GLib.Error())
else:
gradient = config.get_property("gradient")
file = Gio.file_new_for_path(file_entry.get_text())
#Save configs for non-connected UI element
config.set_property ("file", file)
dialog.destroy()
gradient = config.get_property("gradient")
file = config.get_property("file")
if file is None:
error = 'No file given'
config.end_run(Gimp.PDBStatusType.CALLING_ERROR)
return procedure.new_return_values(Gimp.PDBStatusType.CALLING_ERROR,
GLib.Error(error))
@ -187,10 +178,8 @@ def gradient_css_save(procedure, args, data):
flags=Gio.FileCreateFlags.REPLACE_DESTINATION,
cancellable=None)
if success:
config.end_run(Gimp.PDBStatusType.SUCCESS)
return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
else:
config.end_run(Gimp.PDBStatusType.EXECUTION_ERROR)
return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR,
GLib.Error('File saving failed: {}'.format(file.get_path())))
@ -225,7 +214,7 @@ class GradientsSaveAsCSS (Gimp.PlugIn):
procedure.set_documentation (_("Creates a new palette from a given gradient"),
_("Creates a new palette from a given gradient"),
name)
procedure.set_menu_label(_("Save as CSS..."))
procedure.set_menu_label(_("Save Gradient as CSS..."))
procedure.set_attribution("Joao S. O. Bueno",
"(c) GPL V3.0 or later",
"2011")

View File

@ -108,30 +108,20 @@ class PaletteOffset (Gimp.PlugIn):
return procedure
def run(self, procedure, args, data):
palette = None
amount = 1
def run(self, procedure, config, data):
runmode = config.get_property("run-mode")
palette = config.get_property("palette")
amount = config.get_property("amount")
# Get the parameters
if args.length() < 1:
error = 'No parameters given'
return procedure.new_return_values(Gimp.PDBStatusType.CALLING_ERROR,
GLib.Error(error))
runmode = args.index(0)
if args.length() > 1:
palette = args.index(1)
if palette is None:
palette = Gimp.context_get_palette()
config.set_property("palette", palette)
if not palette.is_valid():
error = f'Invalid palette ID: {palette.get_id()}'
return procedure.new_return_values(Gimp.PDBStatusType.CALLING_ERROR,
GLib.Error(error))
if args.length() > 2:
amount = args.index(2)
if runmode == Gimp.RunMode.INTERACTIVE:
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
@ -140,7 +130,7 @@ class PaletteOffset (Gimp.PlugIn):
use_header_bar = Gtk.Settings.get_default().get_property("gtk-dialogs-use-header")
dialog = GimpUi.Dialog(use_header_bar=use_header_bar,
title=_("Offset Palette..."))
title=_("Offset Palette..."))
dialog.add_button(_("_Cancel"), Gtk.ResponseType.CANCEL)
dialog.add_button(_("_OK"), Gtk.ResponseType.OK)
@ -162,10 +152,10 @@ class PaletteOffset (Gimp.PlugIn):
dialog.show()
if dialog.run() != Gtk.ResponseType.OK:
print ("Canceled")
return procedure.new_return_values(Gimp.PDBStatusType.CANCEL,
GLib.Error("Canceled"))
amount = self.get_property("amount")
config.set_property("amount", amount)
#If palette is read only, work on a copy:
editable = palette.is_editable()

View File

@ -444,21 +444,9 @@ class PaletteSort (Gimp.PlugIn):
return procedure
def run(self, procedure, args, data):
config = procedure.create_config()
config.begin_run(None, Gimp.RunMode.INTERACTIVE, args)
run_mode = args.index(0)
palette = args.index(1)
selection = args.index(2)
slice_expr = args.index(3)
channel1 = args.index(4)
ascending1 = args.index(5)
channel2 = args.index(6)
ascending2 = args.index(7)
quantize = args.index(8)
pchannel = args.index(9)
pquantize = args.index(10)
def run(self, procedure, config, data):
run_mode = config.get_property("run-mode")
palette = config.get_property("palette")
if palette is None or not palette.is_valid():
if palette is not None:
@ -466,6 +454,7 @@ class PaletteSort (Gimp.PlugIn):
sys.stderr.write('This should not happen. Please report to GIMP project.\n')
sys.stderr.write('Falling back to context palette instead.\n')
palette = Gimp.context_get_palette()
config.set_property("palette", palette)
if not palette.is_valid():
palette_name = palette.get_id()
@ -489,22 +478,20 @@ class PaletteSort (Gimp.PlugIn):
if not dialog.run():
dialog.destroy()
config.end_run(Gimp.PDBStatusType.CANCEL)
return procedure.new_return_values(Gimp.PDBStatusType.CANCEL, GLib.Error())
else:
palette = config.get_property("palette")
selection = config.get_property("selections")
slice_expr = config.get_property ("slice_expr")
channel1 = config.get_property("channel1")
ascending1 = config.get_property ("ascending1")
channel2 = config.get_property("channel2")
ascending2 = config.get_property ("ascending2")
quantize = config.get_property ("quantize")
pchannel = config.get_property("pchannel")
pquantize = config.get_property ("pquantize")
dialog.destroy()
dialog.destroy()
palette = config.get_property("palette")
selection = config.get_property("selections")
slice_expr = config.get_property ("slice_expr")
channel1 = config.get_property("channel1")
ascending1 = config.get_property ("ascending1")
channel2 = config.get_property("channel2")
ascending2 = config.get_property ("ascending2")
quantize = config.get_property ("quantize")
pchannel = config.get_property("pchannel")
pquantize = config.get_property ("pquantize")
try:
new_palette = palette_sort(palette, selection, slice_expr, channel1, ascending1,
channel2, ascending2, quantize, pchannel, pquantize)
@ -512,13 +499,10 @@ class PaletteSort (Gimp.PlugIn):
return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR,
GLib.Error(str(err)))
config.end_run(Gimp.PDBStatusType.SUCCESS)
return_val = procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
value = GObject.Value(Gimp.Palette, new_palette)
return_val.remove(1)
return_val.insert(1, value)
return return_val
Gimp.main(PaletteSort.__gtype__, sys.argv)

View File

@ -33,7 +33,7 @@ def make_gradient(palette, num_segments, num_colors):
# name the gradient same as the source palette
# For now, the name of a resource is the same as the ID
palette_name = palette.get_id()
palette_name = palette.get_name()
gradient = Gimp.Gradient.new(palette_name)
# assert gradient is valid but is has only one segment
assert gradient.get_number_of_segments() == 1
@ -56,13 +56,9 @@ def make_gradient(palette, num_segments, num_colors):
Gimp.context_set_gradient(gradient)
return gradient
def run(procedure, args, data):
config = procedure.create_config()
config.begin_run(None, Gimp.RunMode.INTERACTIVE, args)
def run(procedure, config, data):
# Get the parameters
run_mode = args.index(0)
palette = args.index(1)
run_mode = config.get_property("run-mode")
if run_mode == Gimp.RunMode.INTERACTIVE:
GimpUi.init(procedure.get_name())
@ -73,19 +69,18 @@ def run(procedure, args, data):
if not dialog.run():
dialog.destroy()
config.end_run(Gimp.PDBStatusType.CANCEL)
return procedure.new_return_values(Gimp.PDBStatusType.CANCEL, GLib.Error())
else:
palette = config.get_property("palette")
dialog.destroy()
dialog.destroy()
palette = config.get_property("palette")
if palette is None or not palette.is_valid():
if palette is not None:
sys.stderr.write(f'Invalid palette id: {palette.get_id()}\n')
sys.stderr.write('This should not happen. Please report to GIMP project.\n')
sys.stderr.write('Falling back to context palette instead.\n')
palette = Gimp.context_get_palette()
config.set_property("palette", palette)
num_colors = palette.get_color_count()
@ -95,7 +90,6 @@ def run(procedure, args, data):
num_segments = num_colors
gradient = make_gradient(palette, num_segments, num_colors)
config.end_run(Gimp.PDBStatusType.SUCCESS)
# XXX: for the error parameter, we want to return None.
# Unfortunately even though the argument is (nullable), pygobject
# looks like it may have a bug. So workaround is to just set a
@ -132,7 +126,7 @@ class PaletteToGradient (Gimp.PlugIn):
## Parameter: palette ##
@GObject.Property(type=Gimp.Palette,
default=None,
nick= _("Palette"))
nick= _("_Palette"))
def palette(self):
'''Palette or None for the currently selected palette'''
return self.palette

View File

@ -41,7 +41,7 @@ PROC_NAME = 'python-fu-console'
RESPONSE_BROWSE, RESPONSE_CLEAR, RESPONSE_SAVE = range(3)
def run(procedure, args, data):
def run(procedure, config, data):
GimpUi.init ("python-console.py")
namespace = {'__builtins__': __builtins__,