Re: question about Windowmanager.toFront()

Posted by Kenneth Sloan-2 on
URL: http://imagej.273.s1.nabble.com/question-about-Windowmanager-toFront-tp5022379p5022387.html

A  'Thread.sleep(100)' may work NOW (on some machines), but it seems like a kludge.  Is there a better
way for a script to wait for/check for/receive a signal that an asynchronous operation has completed?

Can a script SET IJ.macrorunning explicitly?  That would also work (but then "macrorunning" is no longer quite
the right name).  

How about a generic "wait for all asynchronous operations to complete"?  I would like that better than "sleep(100) and hope that's long enough".

Is there a list, somewhere, of all asynchronous operations in IJ?

And finally...is this an issue for Java (not javascript) plugins?  That might explain some weird behavior I have seen
in the  past and managed to muddle through without really understanding them.  That is - what would you predict would be the behavior if the script were transliterated into Java and run (with, and without, the Thread.sleep)?

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.





> On Aug 21, 2019, at 10:00, Michael Schmid <[hidden email]> wrote:
>
> Hi Aryeh,
>
> it is clearly a timing issue.
> The following script works *sometimes* (using the "Evaluate Python" of the ImageJ Editor). If it works correctly, it takes the '._orig' to the foreground, and also the Window menu of the ImageJ panel shows it checked as foreground image.
> In a few cases, the "_orig" goes to the foreground, and WindowManager.getCurrentImage() reports it as foreground image at that moment, but 100 ms later, the second IJ.getImage() command shows the '_copy' as a front image.
> In that case, also the Window menu of the ImageJ main panel shows the "_copy" as foreground, and any successive operation such as "Gaussian Blur" also affects the "_copy".
>
> I guess that it takes some time to display the input image after opening it (which is an asynchronous operation), and when it gets displayed, it may become the foreground image.
>
> Adding a 'Thread.sleep(100)' after showing the input image seems to fix the problem.
>
>
> In macros, this does not happen because ImagePlus.show() checks for IJ.macrorunning, which is probably not set by running a Python script.
> This would mean that the problem can be also solved by using IJ.open(inputPath) or IJ.openImage(inputPath) instead of ImagePlus(inputPath).
> IJ.open/openImage sets IJ.macrorunning, so it should ensure that the image gets displayed before continuing.
>
> ---
> By the way, I had to comment out the import statements to make it run.
>
> ImageJ 1.52q40; Java 1.8.0_112 [64-bit]; Linux 4.4.0-159-generic; 46MB of 10000MB (<1%)
> ImageJ/plugins/Jython.jar is from Sept 2017
>
> ________________________________________________________________
> #import os
> #from os import sys, sep, path, makedirs
>
> def open_image():
>    """
>    opens an image, returns an imagePlus object and its name in that order
>    """
>    # Prompt user for the input image file.
>    print "open_image begin"
>    op = OpenDialog("Choose input image...", "")
>    print op.getFileName()
>
>    if op.getFileName() == None:
>        sys.exit('User canceled dialog')
>    # open selected image and prepare it for analysis
>
>    inputName = op.getFileName()
>    inputDirPath = op.getDirectory()
>    inputPath = inputDirPath + inputName
>
>    # Strip the suffix off of the input image name
>    if inputName[-4] == ".":
>        inputPrefix = inputName[:-4]    # assumes that a suffix exists
>    else:
>        inputPrefix = inputName
>
>    #opens image and returns it.
>    inputImp = ImagePlus(inputPath)
>
>    print "open_image finis"
>    return inputImp, inputPrefix, inputDirPath
>
> def setFrontWin(imp):
>    win = imp.getWindow()
>    if win == None:
>        imp.show()
>        win =  imp.getWindow()
>    print "toFront: ", win
>    IJ.selectWindow(imp.getID())
>    print "selected: ", WindowManager.getCurrentImage()
>    print "getImage: ", IJ.getImage()
>    Thread.sleep(100)
>    print "getImage: ", IJ.getImage()
>    return win
>
> fullInputImp, inputPrefix, inputDirPath = open_image()
> fullInputImp.show()
> print fullInputImp, inputPrefix, inputDirPath
>
> inputImp = fullInputImp.duplicate()
> inputImp.show()
> inputImp.setTitle(inputPrefix+"_copy")
> fullInputImp.setTitle(inputPrefix+"_orig")
> setFrontWin(fullInputImp) ________________________________________________________________
>
> Best,
>
> Michael
> ________________________________________________________________
> On 21.08.19 10:49, Aryeh Weiss wrote:
>> Hi Michael,
>> Thank you for your quick reply. I replaced the WindowManager.toFront() call with the IJ.selectWindow() call,.
>> As before, the simple test script that loops through the windows works, while the second test script that duplicates the input window does not.
>> Best regards
>> --aryeh
>> On 21/08/2019 11:36, Michael Schmid wrote:
>>> Hi Aryeh,
>>>
>>> a safer way to put an image to the foreground is
>>>   IJ.selectWindow(imp.getID())
>>>
>>> Otherwise you may need to introduce time delays. Displaying a window and bringing it to the foreground are asynchronous operations, which may be slower than the execution of your code.
>>>
>>> IJ.selectWindow has a loop with time delay to wait until the window is really in the foreground.
>>>
>>> Michael
>>> ________________________________________________________________
>>> On 21.08.19 10:13, Aryeh Weiss wrote:
>>> > I wish to select a window to be the front window in my display.  to do this I have function called setFrontWin() that checks if the image is displayed, shows it if needed, and then moves it to the front. I tested setFrontWin() using the script below, that loops through all of the open windows and moves them to the front. This works fine.
>>> >
>>> > ################################
>>> > def setFrontWin(imp):
>>> >      win = WindowManager.getWindow(imp.getTitle())
>>> >      if win == None:
>>> >          imp.show()
>>> >          win = WindowManager.getWindow(imp.getTitle())
>>> >      win.toFront()
>>> >      return win
>>> >
>>> > for w in WindowManager.getImageTitles():
>>> >      imp = WindowManager.getImage(w)
>>> >      setFrontWin(imp)
>>> >      wd=WaitForUserDialog("wait")
>>> >      wd.show()
>>> > #################################
>>> >
>>> > Below is a script where this does not work. I open an image, make a copy, and then want the original image
>>> > to be displayed in front. However, this does not happen -- the copy is left in front.
>>> > The test script above moves these same images as expected, and I cannot figure out where they are different.
>>> >
>>> > ######################
>>> >
>>> > import os
>>> > from os import sys, sep, path, makedirs
>>> >
>>> > def open_image():
>>> >      """
>>> >      opens an image, returns an imagePlus object and its name in that order
>>> >      """
>>> >      # Prompt user for the input image file.
>>> >      print "open_image begin"
>>> >      op = OpenDialog("Choose input image...", "")
>>> >      print op.getFileName()
>>> >
>>> >      if op.getFileName() == None:
>>> >          sys.exit('User canceled dialog')
>>> >      # open selected image and prepare it for analysis
>>> >
>>> >      inputName = op.getFileName()
>>> >      inputDirPath = op.getDirectory()
>>> >      inputPath = inputDirPath + inputName
>>> >
>>> >      # Strip the suffix off of the input image name
>>> >      if inputName[-4] == ".":
>>> >          inputPrefix = inputName[:-4]    # assumes that a suffix exists
>>> >      else:
>>> >          inputPrefix = inputName
>>> >
>>> >      #opens image and returns it.
>>> >      inputImp = ImagePlus(inputPath)
>>> >
>>> >      print "open_image finis"
>>> >      return inputImp, inputPrefix, inputDirPath
>>> >
>>> > def setFrontWin(imp):
>>> >      win = WindowManager.getWindow(imp.getTitle())
>>> >      if win == None:
>>> >          imp.show()
>>> >          win = WindowManager.getWindow(imp.getTitle())
>>> >      print win
>>> >      win.toFront()
>>> >      return win
>>> >
>>> > fullInputImp, inputPrefix, inputDirPath = open_image()
>>> > fullInputImp.show()
>>> > print fullInputImp, inputPrefix, inputDirPath
>>> >
>>> > inputImp = fullInputImp.duplicate()
>>> > inputImp.show()
>>> > inputImp.setTitle(inputPrefix+"_copy")
>>> > fullInputImp.setTitle(inputPrefix+"_orig")
>>> > setFrontWin(fullInputImp)
>>> >
>>> > #############################
>>> >
>>> > For testing purposes I am using auto-import, and these scripts should be able to run anywhere.
>>> >
>>> > Where have I gone wrong?
>>> >
>>> > Tnx in advance
>>> > --aryeh
>>> >
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html