Author Topic: [PJSR] Any access to the history  (Read 4999 times)

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
[PJSR] Any access to the history
« on: 2014 January 04 08:00:59 »
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

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: [PJSR] Any access to the history
« Reply #1 on: 2014 January 05 12:28:10 »
The following properties allow you to manage view process histories and initial states:

ProcessContainer View.initialProcessing (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.

ProcessContainer View.processing (read-only)

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

uint View.historyIndex

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

   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.

Boolean View.canGoBackward (read-only)

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

Boolean View.canGoForward (read-only)

Returns true if this view allows you to perform a redo operation. This happens when View.historyIndex is less than View.processing.length.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: [PJSR] Any access to the history
« Reply #2 on: 2014 January 05 12:57:28 »
Thanks, very useful, just what I needed.
-- bitli

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: [PJSR] Any access to the history - STATUS partially solved
« Reply #3 on: 2014 January 06 10:25:58 »
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

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: [PJSR] Any access to the history
« Reply #4 on: 2014 January 09 13:17:30 »
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: [Select]
"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