Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
""" Defines the PointMarker tool class.
"""
from __future__ import with_statement
# Major library imports
from numpy import array, take, transpose
# Enthought library imports
from enable.api import BaseTool, ColorTrait
from traits.api import Enum, Float
class PointMarker(BaseTool):
""" This tool looks at an XY plot's index data source and draws a
line corresponding to the index indicated by the "selections" metadata.
"""
#: The axis that this tool is parallel to.
axis = Enum("index", "value")
#: This tool is visible (overrides BaseTool).
visible = True
#: This tool is drawn as an overlay (overrides BaseTool).
draw_mode = "overlay"
# TODO:STYLE
#: The color of the line.
color = ColorTrait("red")
"""Simple Inspector tool for plots
This module provides a simple tool that reports the data-space coordinates of
the current mouse cursor position in a plot. It is intended for use with
SimpleInspectorOverlay, but other objects can potentially hook into its API.
"""
from chaco.image_plot import ImagePlot
from enable.api import BaseTool, KeySpec
from traits.api import Bool, Event, Tuple, Enum, Callable
class SimpleInspectorTool(BaseTool):
""" Simple inspector tool for plots
This is a simple tool that reports the data-space coordinates of the
current mouse cursor position in a plot.
Interested overlays and other objects can listen for new_value events,
which is a dictionary of data about the current location in data space,
and can look at the last_mouse_position trait which holds the mouse
position in screen space.
The tool also provides a visible trait which listeners can use to hide
themselves. By default the 'p' key toggles this.
Instances can provide a value_generator function that performs computations
to generate additional values in the dictionary that is passed to the
new_value event. Subclasses can override gather_values() to similar
def dispatch(self, event, suffix):
''' Divertes double click events and dispatches all others
using the base class functionality
'''
if self.component.x_axis.is_in(event.x, event.y) and suffix == "left_dclick":
''' When the X-Axis is double clicked, this shows an input dialog
for manually entering the slection boundaries.
'''
diag = setSelectionDialog(self.parent)
diag.selectionSet.connect(self.setSelection)
diag.show()
enable.api.BaseTool.dispatch(self, event, suffix)
from numpy import linspace, sin
from chaco.api import ArrayPlotData, Plot
from enable.api import BaseTool
from enable.component_editor import ComponentEditor
from traits.api import Enum, HasTraits, Instance
from traitsui.api import Item, View
class CustomTool(BaseTool):
def normal_mouse_move(self, event):
print("Screen point:", event.x, event.y)
def normal_left_down(self, event):
print("Mouse went down at", event.x, event.y)
def normal_left_up(self, event):
print("Mouse went up at:", event.x, event.y)
class ScatterPlot(HasTraits):
plot = Instance(Plot)
traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
width=800, height=600, resizable=True,
def __init__(self, obj, timeout=0):
QtCore.QObject.__init__(self)
self.obj = obj
timer = QtCore.QTimer(self)
timer.timeout.connect(self.update)
timer.start(timeout)
def update(self):
self.obj.update()
# a global variable for the current time to be displayed by the timeMarker in all plot
timeMark = None
class TimeMarker(enable.api.BaseTool):
''' The timeMarker draws a vertical line at the current mouse position in the current
plot and at the according timeStamp in all other plots.
'''
# Requires global variable timeMark
draw_layer = 'overlay'
def __init__(self, plot):
enable.api.BaseTool.__init__(self, plot)
self.visible = True
self.updateHack = updateHack(self, 50)
def normal_mouse_move(self, event):
global timeMark
timeMark = self.component.x_axis.mapper.map_data(event.x)
self.component.request_redraw()
""" Defines the PanTool class.
"""
from numpy import inf
# Enthought library imports
from enable.api import BaseTool, Pointer, KeySpec
from traits.api import Bool, Enum, Float, Tuple, Instance
class PanTool(BaseTool):
""" A tool that enables the user to pan a plot by clicking a mouse
button and dragging.
"""
#: The mouse button that initiates the drag operation.
drag_button = Enum("left", "middle", "right")
#: The cursor to use when panning.
drag_pointer = Pointer("hand")
#: Scaling factor on the panning "speed".
speed = Float(1.0)
#: The modifier key that, if depressed when the drag is initiated, constrains
#: the panning to happen in the only direction of largest initial motion.
#: It is possible to permanently restrict this tool to always drag along one
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy
from chaco.grid_mapper import GridMapper
from enable.api import BaseTool, KeySpec
from traits.api import Enum, Float, Instance, Bool, HasTraits, List
from .tool_history_mixin import ToolHistoryMixin
from .tool_states import ZoomState, PanState, GroupedToolState, ToolState
class BetterZoom(BaseTool, ToolHistoryMixin):
# Keys to zoom in/out
zoom_in_key = Instance(KeySpec, args=("+",), kw={'ignore': ['shift']})
zoom_out_key = Instance(KeySpec, args=("-",))
# Keys to zoom in/out in x direction only
zoom_in_x_key = Instance(KeySpec, args=("Right", "shift"))
zoom_out_x_key = Instance(KeySpec, args=("Left", "shift"))
# Keys to zoom in/out in y direction only
zoom_in_y_key = Instance(KeySpec, args=("Up", "shift"))
zoom_out_y_key = Instance(KeySpec, args=("Down", "shift"))
# Key to go to the previous state in the history.
prev_state_key = Instance(KeySpec, args=("z", "control"))
Implementation of a plot using a custom overlay and tool
"""
import numpy
from traits.api import HasTraits, Instance, Enum
from traitsui.api import View, Item
from enable.api import ComponentEditor
from chaco.api import Plot, ArrayPlotData, AbstractOverlay
from enable.api import BaseTool
from enable.markers import DOT_MARKER, DotMarker
class BoxSelectTool(BaseTool):
""" Tool for selecting all points within a box
There are 2 states for this tool, normal and selecting. While the
left mouse button is down the metadata on the datasources will be
updated with the current selected bounds.
Note that the tool does not actually store the selected point, but the
bounds of the box.
"""
event_state = Enum("normal", "selecting")
def normal_left_down(self, event):
self.event_state = "selecting"
self.selecting_mouse_move(event)
gc.show_text(self.label, (text_x, text_y))
def perform(self):
self.callback(*self.callback_args)
class ButtonCanvas(Container):
def draw(self, gc, view_bounds=None, mode="default"):
for component in self.components:
component.draw(gc, view_bounds, mode)
def add_button(self, button):
button.container = self
self.components.append(button)
class ButtonSelectionTool(BaseTool):
""" Listens for double-clicks and tries to open a traits editor on the
graph node under the mouse.
"""
def normal_left_down(self, event):
for component in self.component.components:
if isinstance(component, CanvasButton) \
and component.is_in(event.x, event.y):
component.state = 'down'
component.request_redraw()
component.perform()
break
def normal_left_up(self, event):
for component in self.component.components:
if isinstance(component, CanvasButton):
# Enthought library imports
from enable.api import BaseTool, KeySpec
from traits.api import Enum, Float, Instance
class SelectTool(BaseTool):
""" Base class for tools that handle some level of click-to-select
interaction. Handles the logic of different kinds of selection
modes. Subclasses only need to implement a few concrete methods
to handle actual selection/deselection.
"""
#: The threshold, in pixels, around the cursor location to search for points.
threshold = Float(5.0)
#: How selections are handled:
#:
#: "toggle"
#: The user clicks on points (while optionally holding down a modifier
#: key) to select or deselect them. If the point is already selected,
#: clicking it again deselects it. The modifier key to use is set by
#: **multiselect_modifier**. The only way to deselect points is by