Login  Register

Re: Adding keyboard shortcut to toggle visibility of patches in TrakEM2

Posted by Albert Cardona-2 on Apr 18, 2016; 5:38pm
URL: http://imagej.273.s1.nabble.com/Adding-keyboard-shortcut-to-toggle-visibility-of-patches-in-TrakEM2-tp5016099p5016167.html

Dear Federico,

Glad to hear that it works for you, and sorry to hear about the error.
While the error seems puzzling given the code that you wrote, perhaps it
has to do with the fact that the thread context within which your code
executes is different, to which perhaps the older versions of jython have
trouble with (just speculating here). Something to try: either import the
java.awt.KeyEvent within your function call (it is a split second
operation, only performed when there is a key event anyway), or rewrite it
in another language like beanshell, clojure, ruby. Another, perhaps easier
workaround would be to declare a variable within the MyKeyListener
constructor and refer to the KeyEvent in that way, like:

class MyKeyListener(KeyAdapter):
  def __init__(self, *args, **kwargs):
   super(MyKeyListener, self).__init__(*args, **kwargs)
   self.KeyEvent = KeyEvent
  def keyPressed(self, event):
      # rest of the code here
      # Use self.KeyEvent instead of KeyEvent


Best,

Albert

2016-04-11 23:34 GMT-04:00 Federico Luzzati <[hidden email]>:

> Dear Albert,
> thank you very much for your answer, the code works fine (you'll see it at
> the bottom of this mail)!
> unfortunately however while scrolling back and forth through the sections
> it suddenly and randomly stop working; the System Log shows this message
>
> if KeyEvent.VK_1 == keyCode:
> NameError: global name 'KeyEvent' is not defined
>
> Running again the script is ineffective, closing and re-opening the canvas
> is the only way, but this is very annoying!
>
> Thank you very much for your help, and for your work.
>
> Federico
>
>
> from java.awt.event import KeyEvent, KeyAdapter
> from ini.trakem2.display import Display, Patch
>
> class MyKeyListener(KeyAdapter):
>   def keyPressed(self, event):
>   keyCode = event.getKeyCode()
>   if KeyEvent.VK_1 == keyCode:
> for layer in Display.getFront().getLayerSet().getLayers():
> patches = layer.getDisplayables(Patch)
> for patch in patches:
> if "NFL" in patch.title:
> if patch.visible == True:
> patch.visible = False
> else:
> patch.visible = True
> elif KeyEvent.VK_2 == keyCode:
> for layer in Display.getFront().getLayerSet().getLayers():
> patches = layer.getDisplayables(Patch)
> for patch in patches:
> if "GFP" in patch.title:
> if patch.visible == True:
> patch.visible = False
> else:
> patch.visible = True
>
> elif KeyEvent.VK_3 == keyCode:
> for layer in Display.getFront().getLayerSet().getLayers():
> patches = layer.getDisplayables(Patch)
> for patch in patches:
> if "Ki67" in patch.title:
> if patch.visible == True:
> patch.visible = False
> else:
> patch.visible = True
>
> elif KeyEvent.VK_4 == keyCode:
> for layer in Display.getFront().getLayerSet().getLayers():
> patches = layer.getDisplayables(Patch)
> for patch in patches:
> if "DCX" in patch.title:
> if patch.visible == True:
> patch.visible = False
> else:
> patch.visible = True
>
>
> Display.getFront().getCanvas().addKeyListener(MyKeyListener())
>
>
>
>
>
>
> 2016-04-11 21:31 GMT-04:00 Albert Cardona <[hidden email]>:
>
> > Hi Federico,
> >
> > Basically, you can create your own KeyListener and attach it to the
> TrakEM2
> > window canvas, and react upon its keyPressed KeyEvent.
> >
> > The canvas element you can get from Display.getFront().getCanvas().
> >
> > Would be something like this:
> >
> > from java.awt import KeyAdapter
> >
> > class MyKeyListener(KeyAdapter):
> >   def keyPressed(self, event):
> >     # Run your code here
> >     ...
> >
> > Display.getFront().getCanvas().addKeyListener(MyKeyListener())
> >
> > Best,
> >
> > Albert
> >
> > 2016-04-11 17:05 GMT-04:00 Federico Luzzati <[hidden email]>:
> >
> > > Dear List,
> > > I'm using TrakEM2 to analyze huge multi-channel confocal
> reconstructions.
> > > I'm
> > > importing the channels as separate 8-bit patches with the antigen
> > specified
> > > within the patch name, apply a LUT with "adjust image filters" and set
> > the
> > > composite mode to Add.
> > > To switch on and off a channel I'm using the "select all that match
> > > function". Nonetheless, this method is very slow, and it would be much
> > > better if one could define keyboard shortcuts for that.
> > > I wrote this simple script to toggle visibility of patches based on
> their
> > > names, but I can't figure out how I could call it from a keyboard
> > shortcut
> > > inside TraKEM2.
> > >
> > > Any hint will be greatly appreciated
> > > Thank you very much!
> > >
> > > Federico
> > >
> > > Here is an example of the script:
> > >
> > > from ini.trakem2.display import *
> > >
> > > for layer in Display.getFront().getLayerSet().getLayers():
> > > patches = layer.getDisplayables(Patch)
> > > for patch in patches:
> > > if "DCX" in patch.title:
> > > if patch.visible == True:
> > > patch.visible = False
> > > else:
> > > patch.visible = True
> > >
> > >
> > >
> > > --
> > > Federico Luzzati, PhD
> > > Assistant Professor
> > > University of Turin,
> > > Dept. LIfe Sciences and Systems Biology (DBIOS)
> > > Via Accademia Albertina, 13 - 10123 Torino - IT
> > >
> > > Researcher at
> > > Neuroscience Institute Cavalieri Ottolenghi (NICO)
> > > Regione Gonzole, 10 - 10043 Orbassano (To) - IT
> > >
> > > tel. +39-0116704683/ -6631
> > > http://www.nico.ottolenghi.unito.it/index.php/it/adult-neurogenesis
> > >
> > > Sostieni con il tuo 5xmille il NICO e la ricerca sulle malattie
> > > neurodegenerative
> > > Firma nel riquadro "Finanziamento della ricerca scientifica e
> > > dell'Università"
> > > CF 97564560015
> > >
> > > --
> > > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> > >
> >
> >
> >
> > --
> > http://albert.rierol.net
> > http://www.janelia.org/lab/cardona-lab
> > http://www.ini.uzh.ch/~acardona/
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> >
>
>
>
> --
> Federico Luzzati, PhD
> Assistant Professor
> University of Turin,
> Dept. LIfe Sciences and Systems Biology (DBIOS)
> Via Accademia Albertina, 13 - 10123 Torino - IT
>
> Researcher at
> Neuroscience Institute Cavalieri Ottolenghi (NICO)
> Regione Gonzole, 10 - 10043 Orbassano (To) - IT
>
> tel. +39-0116704683/ -6631
> http://www.nico.ottolenghi.unito.it/index.php/it/adult-neurogenesis
>
> Sostieni con il tuo 5xmille il NICO e la ricerca sulle malattie
> neurodegenerative
> Firma nel riquadro "Finanziamento della ricerca scientifica e
> dell'Università"
> CF 97564560015
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>



--
http://albert.rierol.net
http://www.janelia.org/lab/cardona-lab
http://www.ini.uzh.ch/~acardona/

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