Re: question about Windowmanager.toFront()

Posted by Michael Schmid on
URL: http://imagej.273.s1.nabble.com/question-about-Windowmanager-toFront-tp5022379p5022386.html

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