[gimp] plug-ins: add interactive run mode for palette-offset.



commit dc8f9dd1687b046dc5392a723da1f8ccbdd773ce
Author: Jehan <jehan girinstud io>
Date:   Tue Aug 6 17:31:30 2019 +0200

    plug-ins: add interactive run mode for palette-offset.
    
    In interactive run-mode, the offset amount can be selected through a
    small dialog box.
    Only a WITH_LAST_VALS mode is still to be done.

 plug-ins/python/palette-offset.py | 139 ++++++++++++++++++++++++--------------
 1 file changed, 90 insertions(+), 49 deletions(-)
---
diff --git a/plug-ins/python/palette-offset.py b/plug-ins/python/palette-offset.py
index 5329da4cce..a24bbdc045 100644
--- a/plug-ins/python/palette-offset.py
+++ b/plug-ins/python/palette-offset.py
@@ -25,47 +25,6 @@ import gettext
 _ = gettext.gettext
 def N_(message): return message
 
-def run(procedure, args, data):
-    # Get the parameters
-    palette = None
-    if args.length() > 1:
-        palette = args.index(1)
-    if palette == '' or palette is None:
-        palette = Gimp.context_get_palette()
-    (exists, num_colors) = Gimp.palette_get_info(palette)
-    if not exists:
-        error = 'Unknown palette: {}'.format(palette)
-        return procedure.new_return_values(Gimp.PDBStatusType.CALLING_ERROR,
-                                           GLib.Error(error))
-    if args.length() > 2:
-        amount = args.index(2)
-    else:
-        amount = 1
-
-    #If palette is read only, work on a copy:
-    editable = Gimp.palette_is_editable(palette)
-    if not editable:
-        palette = Gimp.palette_duplicate (palette)
-
-    tmp_entry_array = []
-    for i in range (num_colors):
-        tmp_entry_array.append  ((Gimp.palette_entry_get_name (palette, i)[1],
-                                  Gimp.palette_entry_get_color (palette, i)[1]))
-    for i in range (num_colors):
-        target_index = i + amount
-        if target_index >= num_colors:
-            target_index -= num_colors
-        elif target_index < 0:
-            target_index += num_colors
-        Gimp.palette_entry_set_name (palette, target_index, tmp_entry_array[i][0])
-        Gimp.palette_entry_set_color (palette, target_index, tmp_entry_array[i][1])
-
-    retval = procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
-    value = GObject.Value(GObject.TYPE_STRING, palette)
-    retval.remove(1)
-    retval.insert(1, value)
-    return retval
-
 class PaletteOffset (Gimp.PlugIn):
     ## Parameter: run-mode ##
     @GObject.Property(type=Gimp.RunMode,
@@ -73,11 +32,11 @@ class PaletteOffset (Gimp.PlugIn):
                       nick="Run mode", blurb="The run mode")
     def run_mode(self):
         """Read-write integer property."""
-        return self.runmode
+        return self._run_mode
 
     @run_mode.setter
-    def run_mode(self, runmode):
-        self.runmode = runmode
+    def run_mode(self, run_mode):
+        self._run_mode = run_mode
 
     ## Parameter: palette ##
     @GObject.Property(type=str,
@@ -85,11 +44,11 @@ class PaletteOffset (Gimp.PlugIn):
                       nick= _("Palette"),
                       blurb= _("Palette"))
     def palette(self):
-        return self.palette
+        return self._palette
 
     @palette.setter
     def palette(self, palette):
-        self.palette = palette
+        self._palette = palette
 
     ## Parameter: amount ##
     @GObject.Property(type=int,
@@ -97,11 +56,11 @@ class PaletteOffset (Gimp.PlugIn):
                       nick= _("Off_set"),
                       blurb= _("Offset"))
     def amount(self):
-        return self.amount
+        return self._amount
 
     @amount.setter
     def amount(self, amount):
-        self.amount = amount
+        self._amount = amount
 
     ## Return: new-palette ##
     @GObject.Property(type=str,
@@ -126,7 +85,7 @@ class PaletteOffset (Gimp.PlugIn):
     def do_create_procedure(self, name):
         procedure = Gimp.Procedure.new(self, name,
                                        Gimp.PDBProcType.PLUGIN,
-                                       run, None)
+                                       self.run, None)
         if name == 'python-fu-palette-offset':
             procedure.set_menu_label(N_("_Offset Palette..."))
             procedure.set_documentation(N_("Offset the colors in a palette"),
@@ -145,4 +104,86 @@ class PaletteOffset (Gimp.PlugIn):
 
         return procedure
 
+    def run(self, procedure, args, data):
+        palette = None
+        amount  = 1
+
+        # 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 == '' or palette is None:
+            palette = Gimp.context_get_palette()
+        (exists, num_colors) = Gimp.palette_get_info(palette)
+        if not exists:
+            error = 'Unknown palette: {}'.format(palette)
+            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
+
+            Gimp.ui_init ("palette-offset.py", False)
+
+            use_header_bar = Gtk.Settings.get_default().get_property("gtk-dialogs-use-header")
+            dialog = Gimp.Dialog(use_header_bar=use_header_bar,
+                                 title=_("Offset Palette..."))
+
+            dialog.add_button("_Cancel", Gtk.ResponseType.CANCEL)
+            dialog.add_button("_OK", Gtk.ResponseType.OK)
+
+            box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
+                          homogeneous=False, spacing=12)
+            dialog.get_content_area().add(box)
+            box.show()
+
+            label = Gtk.Label.new_with_mnemonic("Off_set")
+            box.pack_start(label, False, False, 1)
+            label.show()
+
+            amount = self.set_property("amount", amount)
+            spin = Gimp.prop_spin_button_new(self, "amount", 1.0, 5.0, 0)
+            spin.set_activates_default(True)
+            box.pack_end(spin, False, False, 1)
+            spin.show()
+
+            dialog.show()
+            if dialog.run() != Gtk.ResponseType.OK:
+                return procedure.new_return_values(Gimp.PDBStatusType.CANCEL,
+                                                   GLib.Error())
+            amount = self.get_property("amount")
+
+        #If palette is read only, work on a copy:
+        editable = Gimp.palette_is_editable(palette)
+        if not editable:
+            palette = Gimp.palette_duplicate (palette)
+
+        tmp_entry_array = []
+        for i in range (num_colors):
+            tmp_entry_array.append  ((Gimp.palette_entry_get_name (palette, i)[1],
+                                      Gimp.palette_entry_get_color (palette, i)[1]))
+        for i in range (num_colors):
+            target_index = i + amount
+            if target_index >= num_colors:
+                target_index -= num_colors
+            elif target_index < 0:
+                target_index += num_colors
+            Gimp.palette_entry_set_name (palette, target_index, tmp_entry_array[i][0])
+            Gimp.palette_entry_set_color (palette, target_index, tmp_entry_array[i][1])
+
+        retval = procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
+        value = GObject.Value(GObject.TYPE_STRING, palette)
+        retval.remove(1)
+        retval.insert(1, value)
+        return retval
+
 Gimp.main(PaletteOffset.__gtype__, sys.argv)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]