[PJSR] Any access to the history

bitli

Well-known member
In thew View object there is a property named historyIndex. However I have not found any way to access the history of a view, neither in PJSR nor in PCL.
Is there a way to programmatically iterate/extract the history of a view?
-- bitli
 
The following properties allow you to manage view process histories and initial states:

Code:
[b]ProcessContainer View.initialProcessing[/b] (read-only)

This property stores the view's initial state. The initial state contains the ordered set of process instances that were executed when the view was created. By default (for example, for images read from disk files) this property is an empty process container.

Code:
[b]ProcessContainer View.processing[/b] (read-only)

The view's process history. This is the set of process instances that have been executed on this view.

Code:
[b]uint View.historyIndex[/b]

The index of the view's current state on its process history. Valid values range from zero to
Code:
View.processing.length
. By changing the value of this property you can define the view's current state. For example:

Code:
   var v = View.viewById( "test" );
   --v.historyIndex; // this performs an "undo" operation
   ++v.historyIndex; // this performs a "redo" operation
   v.historyIndex = 0; // this performs an "undo all" operation
   v.historyIndex = v.processing.length; // this performs a "redo all" operation
   v.historyIndex = 3; // a view's process history allows for random access

Keep in mind that changing this property may involve read/write operations on image swap files, which can be very large disk files (perhaps gigabytes). Your script should not perform these operations unless they are strictly necessary.

Code:
[b]Boolean View.canGoBackward[/b] (read-only)

Returns true if this view allows you to perform an undo operation. This happens when
Code:
View.historyIndex
is greater than zero.

Code:
[b]Boolean View.canGoForward[/b] (read-only)

Returns true if this view allows you to perform a redo operation. This happens when
Code:
View.historyIndex
is less than
Code:
View.processing.length
.
 
Well, almost.
I cannot find how to find the name or view of the mask attached to a step (there are methods to set or invert the mask, but I did not see how to find it, other than extracting the source and parsing it).
Did I miss something?
I would like to explore the process history of all views to find where a mask is used, I sometime cannot delete an image because it is used as a mask, but I can't find out where.  There are also other use on analyzing the history.
Thanks,
-- bitli
 
Here is a workaround using parsing of the xml representation of the processes.
This small script list the images referencing a mask, may be useful if you do not remember where a mask is used (if at all).


Code:
"use strict";
// ShowMaskUsage.js
// List the views referencing a mask.
// Select the mask, execute the script, the referencing views will be logged on the console
// This is somewhat hacky and may not be 100% right, but proved useful.

#define DEBUG false

// Search masks refered to in a process container
var searchMask = /mask id="([^"]+)"/;

function isMaskReferencedInProcessContainer(maskId, pc) {
   if (pc == null) return false;
   for (var i=0; i<pc.length; i++) {
      var p = pc.at(i);
      var source = p.toSource("XPSM 1.0");
      var lines = source.split("\n");
      // if(DEBUG) Console.writeln("DEBUG - process "  + source.replace("<", "&lt;","g").replace(">","&gt;","g"));
      for (var j=0; j<lines.length;j++) {
         var line = lines[j];
         var searchResult = searchMask.exec(line);
         if(searchResult) {
            var maskIdInHistory = searchResult[1];
            if (DEBUG) Console.writeln("DEBUG -entry " +  i + ", line " + j + ", mask: " + maskIdInHistory);
            if (maskIdInHistory === maskId) return true;
         };
      }
   }
   // None found
   return false;
}

// { maskId -> {viewId - > count,...}, ... }
var maskUsage = {};
function addMaskReferences(name, maskIds) {
   for (var i=0; i<maskIds.length;i++) {
      var maskId = maskIds[i];
      if (maskUsage.hasOwnProperty(maskId)) {
         var ref = maskUsage[maskId];
         if (ref.hasOwnProperty(name)) {
            ref[name] = ref[name] + 1;
         } else {
            ref[name] = 1;
         }
       } else {
         maskUsage[maskId] = new Object;
         maskUsage[maskId][name]=1;
      }
   }
}


function main()
{
   Console.show();

   Console.writeln("ShowMaskUsage - list which views references the current view as a mask");
   var aw = ImageWindow.activeWindow;
   var cv = aw.mainView;
   if (! aw.isValidView(cv)) { Console.writeln("ERROR - No active view"); return 0; }
   var maskId = cv.id;

   if (aw.hasMaskReferences) {
      Console.writeln("View '" + maskId + "' is used as a mask");
   } else {
      Console.writeln("View '" + maskId + "' is not used as a mask");
      return 0;
   }

   // Look up in all windows except current one
   var windows = ImageWindow.windows;
   var nReferences = 0;
   if (DEBUG) Console.writeln("DEBUG - windows.length '" + windows.length);
   for (var i=0; i<windows.length; i++) {
      var w = windows[i];
      var v = w.mainView;
      
      if (DEBUG) Console.writeln("DEBUG - # '" + i + " view '" + v.id + "' cv.id '"+ cv.id + "'");
      if (v.id == cv.id) continue;

      var isReferencingMask = false;
      isReferencingMask |= isMaskReferencedInProcessContainer(maskId, v.initialProcessing);
       isReferencingMask |= isMaskReferencedInProcessContainer(maskId, v.processing);

      // Also check the  current mask
      if (DEBUG) Console.writeln("DEBUG - view '" + v.id + "' " + w.mask);
      if (typeof w.mask !== "undefined") {
      if (DEBUG) Console.writeln("DEBUG - view '" + v.id + "' has mask '" + w.mask.mainView.id + "'");
         if (w.mask.mainView.id === maskId) isReferencingMask = true;
      }

      if (isReferencingMask) {
         Console.writeln("   referenced by view '" + v.id + "'");
         nReferences ++;
      }

   }

   if (nReferences>0) {
      Console.writeln("ShowMaskUsage found " + nReferences + " references to the view '" + cv.id + "'");
   } else {
      Console.writeln("ShowMaskUsage found no reference to the view '" + cv.id + "'");
   }
   return nReferences;

}

main();

-- bitli
 
Back
Top