plug-ins: start porting palette-to-gradient to new GimpPlugIn API.

Several aspects of the new API are kind of broken in the Python binding,
especially the arguments. So this first version has no arguments at all.
This also means it cannot be installed in "<Palettes>" menu for the time
being. This is work-in-progress and the missing parts will be
uncommented later when we figure out how to fix the problems.
This commit is contained in:
Jehan
2019-08-01 20:34:14 +02:00
parent ed86de48d2
commit 17667b7ddf

View File

@ -16,6 +16,9 @@
import gi import gi
gi.require_version('Gimp', '3.0') gi.require_version('Gimp', '3.0')
from gi.repository import Gimp from gi.repository import Gimp
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gio
import sys import sys
import gettext import gettext
@ -47,63 +50,83 @@ def make_gradient(palette, num_segments, num_colors):
Gimp.param_from_string(gradient)] Gimp.param_from_string(gradient)]
return len(retval), retval return len(retval), retval
def palette_to_gradient_repeating(palette): def palette_to_gradient(procedure, args, data):
# Localization
plug_in = procedure.get_plug_in()
plug_in.set_translation_domain ("gimp30-python",
Gio.file_new_for_path(Gimp.locale_directory()))
palette = Gimp.context_get_palette()
(_, num_colors) = Gimp.palette_get_info(palette) (_, num_colors) = Gimp.palette_get_info(palette)
num_segments = num_colors
return make_gradient(palette, num_segments, num_colors)
def palette_to_gradient(palette): if procedure.get_name() == 'python-fu-palette-to-gradient':
(_, num_colors) = Gimp.palette_get_info(palette) num_segments = num_colors - 1
num_segments = num_colors - 1 else: # 'python-fu-palette-to-gradient-repeating'
return make_gradient(palette, num_segments, num_colors) num_segments = num_colors
gradient = make_gradient(palette, num_segments, num_colors)
def run(name, n_params, params): # XXX: for the error parameter, we want to return None.
# run_mode = params[0].get_int32() # Unfortunately even though the argument is (nullable), pygobject
if name == 'python-fu-palette-to-gradient-repeating': # looks like it may have a bug. So workaround is to just set a
return palette_to_gradient_repeating(Gimp.context_get_palette()) # generic GLib.Error() since anyway the error won't be process with
else: # GIMP_PDB_SUCCESS status.
return palette_to_gradient(Gimp.context_get_palette()) # See pygobject#351
retval = procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
def query(): # TODO: uncomment when GParamSpec bug is fixed.
param = Gimp.ParamDef() #value = retval.index(1)
param.type = Gimp.PDBArgType.INT32 #value.set_string (gradient);
param.name = "run-mode" return retval
param.description = _("Run mode")
retval = Gimp.ParamDef() class PaletteToGradient (Gimp.PlugIn):
retval.type = Gimp.PDBArgType.STRING def do_query_procedures(self):
retval.name = "new-gradient" # XXX See pygobject#352 for the weird return value.
retval.description = _("Result") return ['python-fu-palette-to-gradient',
'python-fu-palette-to-gradient-repeating'], 2
Gimp.install_procedure( def do_create_procedure(self, name):
"python-fu-palette-to-gradient-repeating", procedure = Gimp.Procedure.new(self, name,
N_("Create a repeating gradient using colors from the palette"), Gimp.PDBProcType.PLUGIN,
"Create a new repeating gradient using colors from the palette.", palette_to_gradient, None)
"Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz", if name == 'python-fu-palette-to-gradient':
"Carol Spears", procedure.set_menu_label(N_("Palette to _Gradient"))
"2006", procedure.set_documentation(N_("Create a gradient using colors from the palette"),
N_("Palette to _Repeating Gradient"), "Create a new gradient using colors from the palette.",
"", "")
Gimp.PDBProcType.PLUGIN, elif name == 'python-fu-palette-to-gradient-repeating':
[ param ], procedure.set_menu_label(N_("Palette to _Repeating Gradient"))
[ retval ]) procedure.set_documentation(N_("Create a repeating gradient using colors from the palette"),
Gimp.plugin_menu_register("python-fu-palette-to-gradient-repeating", "<Palettes>") "Create a new repeating gradient using colors from the palette.",
"")
else:
procedure = None
Gimp.install_procedure( if procedure is not None:
"python-fu-palette-to-gradient", procedure.set_attribution("Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
N_("Create a gradient using colors from the palette"), "Carol Spears", "2006")
"Create a new gradient using colors from the palette.", returnspec = GObject.param_spec_string("new-gradient",
"Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz", "Name of the newly created gradient",
"Carol Spears", "Name of the newly created gradient",
"2006", None,
N_("Palette to _Gradient"), GObject.ParamFlags.READWRITE)
"", # Passing a GObjectParamSpec currently fails.
Gimp.PDBProcType.PLUGIN, # TODO: see pygobject#227
[ param ], #procedure.add_return_value(returnspec)
[ retval ])
Gimp.plugin_menu_register("python-fu-palette-to-gradient", "<Palettes>")
Gimp.plugin_domain_register("gimp30-python", Gimp.locale_directory())
info = Gimp.PlugInInfo () paramspec = GObject.param_spec_enum("run-mode",
info.set_callbacks (None, None, query, run) "Run mode",
Gimp.main_legacy (info, sys.argv) "The run mode",
Gimp.RunMode.__gtype__,
Gimp.RunMode.NONINTERACTIVE,
GObject.ParamFlags.READWRITE)
# TODO: see pygobject#227
#procedure.add_argument(paramspec)
# TODO: To be installed in '<Palettes>', we need a run mode
# parameter. Wait for the GParamSpec bug to be fixed before
# uncommenting.
#procedure.add_menu_path ('<Palettes>')
return procedure
Gimp.main(PaletteToGradient.__gtype__, sys.argv)