How to make scripts undoable?

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

 



Olof and others mentioned here that it is important for the GIMP to
have a consistent interface.  One of the inconsistencies that should
be fixed before 1.2 is the fact that many scripts (mostly Script-Fu)
are not undoable.  Making these scripts undo-aware is a prerequisite
for moving them from the "Script-Fu" menu to the "Filters" menu or
some other location.  So I started thinking about this a bit...

How to make these scripts undo-aware?  The problem is not really
specific to Script-Fu, because I think that the same basic problem
applies to any script that calls GIMP internal functions and other
plugins through the PDB.  Leaving the undo enabled during PDB calls
would fill up the undo stack quickly.  Also, it is not possible to use
gimp-undo-push-group-start because there would be some conflicts if
all scripts use the same number for the undo group (UndoType).

So I tried to find a solution that does not consume too much memory
and still allows the user to undo any script as if it was a single
operation.  Here is some pseudo-code for a kludge that might work,
but I would like to hear if anybody has a better suggestion:

  if (gimp_image_get_undo (img) == TRUE)
    {
       new_img = gimp_channel_ops_duplicate (img);
       gimp_image_disable_undo (new_img);
    }
  else
    new_img = img;

  /* now do all the hard work on 'new_img' (undo is disabled) */

  if (new_img != img)
    {
      gimp_edit_cut (layer_in_new_img);
      gimp_edit_paste (layer_in_img);
      gimp_image_delete (new_img);
    }

The trick is to let the script do whatever it wants in a separate
image that has its undo disabled.  Then it will not consume much
memory, regardless of the number of operations that are done on the
image.  When the result is ready, the corresponding layers are copied
back to the original image.  That last step would then count as one
operation, so the user could undo the whole thing as with all other
commands.

Of course, if a script touches only one layer of the image, there is
no need to duplicate everyting: duplicating the current layer is
enough.  Also, if a script calls a second script, then that second
script will detect that the new image has already been created (undo
is disabled) so it will work on that image directly.

I think this solution could work, but this is not very elegant.  If
anyone can think of a better solution, I would be glad to hear it.
And if a good solution is found, then I volunteer for making most of
the Script-Fu scripts undo-aware, so that they can be moved to the
"Filters" sub-menu instead of being under a special "Script-Fu" menu.

-Raphael



[Index of Archives]     [Video For Linux]     [Photo]     [Yosemite News]     [gtk]     [GIMP for Windows]     [KDE]     [GEGL]     [Gimp's Home]     [Gimp on GUI]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux