Skip to content

Commit

Permalink
Put logic in proper tool
Browse files Browse the repository at this point in the history
as suggested by Kyle Conroy.
  • Loading branch information
pllim committed May 13, 2022
1 parent ee24e2f commit d14f598
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
37 changes: 34 additions & 3 deletions jdaviz/configs/imviz/plugins/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ def on_limits_change(self, *args):
viewer.state.y_max = self.viewer.state.y_max


@viewer_tool
class JdavizPanZoomMode(BqplotPanZoomMode):
tool_id = 'jdaviz:panzoom'
tool_tip = 'Interactively pan (click-drag), zoom (scroll), and center (click)'

def activate(self):
super().activate()
self.viewer.add_event_callback(self.on_click, events=['click'])

def deactivate(self):
self.viewer.remove_event_callback(self.on_click)
super().deactivate()

def on_click(self, data):
# Find visible layers
visible_layers = [layer for layer in self.viewer.state.layers if layer.visible]
if len(visible_layers) == 0:
return

# Same data as mousemove event in Imviz viewer.
# Any other config that wants this functionality has to have the following:
# viewer._get_real_xy()
# viewer.center_on() --> inherited from AstrowidgetsImageViewerMixin
image = visible_layers[0].layer
x = data['domain']['x']
y = data['domain']['y']
if x is None or y is None: # Out of bounds
return
x, y, _ = self.viewer._get_real_xy(image, x, y)
self.viewer.center_on((x, y))


@viewer_tool
class BlinkOnce(CheckableTool):
icon = os.path.join(ICON_DIR, 'blink.svg')
Expand All @@ -68,8 +100,7 @@ class BlinkOnce(CheckableTool):
'or you can also press the "b" key anytime')

def activate(self):
self.viewer.add_event_callback(self.on_click,
events=['click'])
self.viewer.add_event_callback(self.on_click, events=['click'])

def deactivate(self):
self.viewer.remove_event_callback(self.on_click)
Expand All @@ -87,7 +118,7 @@ class MatchBoxZoom(_MatchedZoomMixin, BoxZoom):


@viewer_tool
class MatchPanZoom(_MatchedZoomMixin, BqplotPanZoomMode):
class MatchPanZoom(_MatchedZoomMixin, JdavizPanZoomMode):
icon = os.path.join(ICON_DIR, 'panzoom_match.svg')
tool_id = 'jdaviz:panzoommatch'
action_text = 'Pan, matching between viewers'
Expand Down
18 changes: 3 additions & 15 deletions jdaviz/configs/imviz/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class ImvizImageView(BqplotImageView, AstrowidgetsImageViewerMixin, JdavizViewer
inherit_tools = False

tools = ['bqplot:home', 'jdaviz:boxzoom', 'jdaviz:boxzoommatch',
'bqplot:panzoom', 'jdaviz:panzoommatch',
'jdaviz:panzoom', 'jdaviz:panzoommatch',
'jdaviz:contrastbias', 'jdaviz:blinkonce',
'bqplot:rectangle', 'bqplot:circle', 'bqplot:ellipse']

# categories: zoom resets, zoom, pan, subset, select tools, shortcuts
tools_nested = [
['bqplot:home'],
['jdaviz:boxzoom', 'jdaviz:boxzoommatch'],
['bqplot:panzoom', 'jdaviz:panzoommatch'],
['jdaviz:panzoom', 'jdaviz:panzoommatch'],
['bqplot:circle', 'bqplot:rectangle', 'bqplot:ellipse'],
['jdaviz:blinkonce', 'jdaviz:contrastbias'],
['jdaviz:sidebar_plot', 'jdaviz:sidebar_export', 'jdaviz:sidebar_compass']
Expand All @@ -49,8 +49,7 @@ def __init__(self, *args, **kwargs):
self.line_profile_xy = None

self.add_event_callback(self.on_mouse_or_key_event, events=['mousemove', 'mouseenter',
'mouseleave', 'keydown',
'click'])
'mouseleave', 'keydown'])
self.state.add_callback('x_min', self.on_limits_change)
self.state.add_callback('x_max', self.on_limits_change)
self.state.add_callback('y_min', self.on_limits_change)
Expand Down Expand Up @@ -163,17 +162,6 @@ def on_mouse_or_key_event(self, data):
self.line_profile_xy.selected_viewer = self.reference_id
self.line_profile_xy.vue_draw_plot()

elif (data['event'] == 'click' and
self.toolbar_nested.active_tool_id in ('bqplot:panzoom', 'jdaviz:panzoommatch')):
# Same data as mousemove above.
image = visible_layers[0].layer
x = data['domain']['x']
y = data['domain']['y']
if x is None or y is None: # Out of bounds
return
x, y, _ = self._get_real_xy(image, x, y)
self.center_on((x, y))

def blink_once(self):
# Simple blinking of images - this will make it so that only one
# layer is visible at a time and cycles through the layers.
Expand Down

0 comments on commit d14f598

Please sign in to comment.