3
ƽhw              ,   @   s  d Z ddlmZ ddlZddlZddlZddlmZ ddlZddl	m
Z
 ddlZddlZddlmZ ddljZejeZG dd deZeZd	ZG d
d dZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ ej!dG dd de Z"G dd deZ#ej!dG dd  d e#Z$G d!d" d"eZ%G d#d$ d$eZ&G d%d& d&eZ'G d'd( d(eZ(G d)d* d*e(Z)G d+d, d,e(Z*G d-d. d.eZ+G d/d0 d0eZ,G d1d2 d2e,Z-G d3d4 d4e,Z.G d5d6 d6e,Z/G d7d8 d8eZ0G d9d: d:eZ1G d;d< d<eZ2G d=d> d>e2Z3G d?d@ d@e2Z4G dAdB dBeZ5G dCdD dDeZ6dEe-dFe.dGe/dHe3dIe4dJdKdLdMdNe%dOe&dPe'dQedRedSe dTe#dUe*dVe)dWeee+dXdYdZd[d\d]d^d_iZ7d`dEdFdGggdadIdHdJggdbdLd\gggZ8e7fdcddZ9e8fdedfZ:dS )gaR  
Abstract base classes define the primitives for Tools.
These tools are used by `matplotlib.backend_managers.ToolManager`

:class:`ToolBase`
    Simple stateless tool

:class:`ToolToggleBase`
    Tool that has two states, only one Toggle tool can be
    active at any given time for the same
    `matplotlib.backend_managers.ToolManager`
    )IntEnumN)SimpleNamespace)WeakKeyDictionary)Gcfc               @   s"   e Zd ZdZed\ZZZZZ	dS )Cursorsz!Backend-independent cursor types.   N)
__name__
__module____qualname____doc__rangeZHANDPOINTERSELECT_REGIONMOVEZWAIT r   r   >/tmp/pip-build-7iwl8md4/matplotlib/matplotlib/backend_tools.pyr      s   r   Zviewposc               @   s   e Zd ZdZdZdZdZdd Zedd Z	e	j
dd Z	edd	 Zed
d Zdd Zdd ZdddZedd Zdd ZdS )ToolBasea  
    Base tool class.

    A base tool, only implements `trigger` method or not method at all.
    The tool is instantiated by `matplotlib.backend_managers.ToolManager`.

    Attributes
    ----------
    toolmanager : `matplotlib.backend_managers.ToolManager`
        ToolManager that controls this Tool.
    figure : `FigureCanvas`
        Figure instance that is affected by this Tool.
    name : str
        Used as **Id** of the tool, has to be unique among tools of the same
        ToolManager.
    Nc             C   s    t jd || _|| _d | _d S )Nz|The new Tool classes introduced in v1.5 are experimental; their API (including names) will likely change in future versions.)cbookZ_warn_external_name_toolmanager_figure)selftoolmanagernamer   r   r   __init__R   s
    zToolBase.__init__c             C   s   | j S )N)r   )r   r   r   r   figureZ   s    zToolBase.figurec             C   s   | j | d S )N)
set_figure)r   r   r   r   r   r   ^   s    c             C   s   | j s
d S | j jS )N)r   canvas)r   r   r   r   r   b   s    zToolBase.canvasc             C   s   | j S )N)r   )r   r   r   r   r   h   s    zToolBase.toolmanagerc             C   s   t | jdS )z
        Return a placeholder object with a single `canvas` attribute.

        This is useful to reuse the implementations of tools already provided
        by the classic Toolbars.
        )r   )r   r   )r   r   r   r   "_make_classic_style_pseudo_toolbarl   s    z+ToolBase._make_classic_style_pseudo_toolbarc             C   s
   || _ dS )zp
        Assign a figure to the tool.

        Parameters
        ----------
        figure : `.Figure`
        N)r   )r   r   r   r   r   r   u   s    zToolBase.set_figurec             C   s   dS )a  
        Called when this tool gets used.

        This method is called by
        `matplotlib.backend_managers.ToolManager.trigger_tool`.

        Parameters
        ----------
        event : `.Event`
            The canvas event that caused this tool to be called.
        sender : object
            Object that requested the tool to be triggered.
        data : object
            Extra data.
        Nr   )r   sendereventdatar   r   r   trigger   s    zToolBase.triggerc             C   s   | j S )zTool Id.)r   )r   r   r   r   r      s    zToolBase.namec             C   s   dS )z
        Destroy the tool.

        This method is called when the tool is removed by
        `matplotlib.backend_managers.ToolManager.remove_tool`.
        Nr   )r   r   r   r   destroy   s    zToolBase.destroy)N)r   r	   r
   r   default_keymapdescriptionimager   propertyr   setterr   r   r   r   r"   r   r#   r   r   r   r   r   (   s   	

r   c               @   sV   e Zd ZdZdZdZdZdd ZdddZddd	Z	dd
dZ
edd Zdd ZdS )ToolToggleBaseac  
    Toggleable tool.

    Every time it is triggered, it switches between enable and disable.

    Parameters
    ----------
    ``*args``
        Variable length argument to be used by the Tool.
    ``**kwargs``
        `toggled` if present and True, sets the initial state of the Tool
        Arbitrary keyword arguments to be consumed by the Tool
    NFc             O   s&   |j d| j| _tj| f|| d S )Ntoggled)popdefault_toggled_toggledr   r   )r   argskwargsr   r   r   r      s    zToolToggleBase.__init__c             C   s*   | j r| j| n
| j| | j  | _ dS )z5Calls `enable` or `disable` based on `toggled` value.N)r-   disableenable)r   r   r    r!   r   r   r   r"      s    
zToolToggleBase.triggerc             C   s   dS )zg
        Enable the toggle tool.

        `trigger` calls this method when `toggled` is False.
        Nr   )r   r    r   r   r   r1      s    zToolToggleBase.enablec             C   s   dS )ay  
        Disable the toggle tool.

        `trigger` call this method when `toggled` is True.

        This can happen in different circumstances.

        * Click on the toolbar tool button.
        * Call to `matplotlib.backend_managers.ToolManager.trigger_tool`.
        * Another `ToolToggleBase` derived tool is triggered
          (from the same `.ToolManager`).
        Nr   )r   r    r   r   r   r0      s    zToolToggleBase.disablec             C   s   | j S )zState of the toggled tool.)r-   )r   r   r   r   r*      s    zToolToggleBase.toggledc             C   sP   | j }|r$| jr| j| d  nd| _tj| | |rL|rF| j| d  nd| _d S )NFT)r*   r   r"   r-   r   r   )r   r   r*   r   r   r   r      s    zToolToggleBase.set_figure)N)N)N)r   r	   r
   r   radio_groupcursorr,   r   r"   r1   r0   r'   r*   r   r   r   r   r   r)      s   


r)   c               @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )SetCursorBasez
    Change to the current cursor while inaxes.

    This tool, keeps track of all `ToolToggleBase` derived tools, and calls
    `set_cursor` when a tool gets triggered.
    c             O   sb   t j| f|| d | _d | _tj| _| j| _| jj	d| j
 x| jjj D ]}| j| qLW d S )NZtool_added_event)r   r   _id_drag_cursorcursorsr   _default_cursor_last_cursorr   toolmanager_connect_add_tool_cbktoolsvalues	_add_tool)r   r.   r/   toolr   r   r   r      s    zSetCursorBase.__init__c             C   s:   | j r| jj| j  tj| | |r6| jjd| j| _ d S )Nmotion_notify_event)r5   r   mpl_disconnectr   r   mpl_connect_set_cursor_cbk)r   r   r   r   r   r     s    zSetCursorBase.set_figurec             C   s*   |j jr|j j| _nd | _| j|j d S )N)r?   r*   r3   r6   rC   Zcanvasevent)r   r    r   r   r   _tool_trigger_cbk  s    zSetCursorBase._tool_trigger_cbkc             C   s*   t |dddk	r&| jjd|j | j dS )z*Set the cursor when the tool is triggered.r3   Nztool_trigger_%s)getattrr   r:   r   rD   )r   r?   r   r   r   r>     s    zSetCursorBase._add_toolc             C   s   |j | krdS | j|j  dS )zProcess every newly added tool.N)r?   r>   )r   r    r   r   r   r;   "  s    
zSetCursorBase._add_tool_cbkc             C   sn   |sd S t |dd s| j r@| j| jkrj| j| j | j| _n*| jrj| j}|rj| j|krj| j| || _d S )NinaxesF)rE   r6   r9   r8   
set_cursor)r   r    r3   r   r   r   rC   (  s    

zSetCursorBase._set_cursor_cbkc             C   s   t dS )zY
        Set the cursor.

        This method has to be implemented per backend.
        N)NotImplementedError)r   r3   r   r   r   rG   6  s    zSetCursorBase.set_cursorN)r   r	   r
   r   r   r   rD   r>   r;   rC   rG   r   r   r   r   r4      s   r4   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ToolCursorPositionz
    Send message with the current pointer position.

    This tool runs in the background reporting the position of the cursor.
    c             O   s   d | _ tj| f|| d S )N)r5   r   r   )r   r.   r/   r   r   r   r   E  s    zToolCursorPosition.__init__c             C   s:   | j r| jj| j  tj| | |r6| jjd| j| _ d S )Nr@   )r5   r   rA   r   r   rB   send_message)r   r   r   r   r   r   I  s    zToolCursorPosition.set_figurec                s   | j jj rdS d} jr jj ry jj j j}W n tt	fk
rR   Y nfX  fdd jj
D }|rtj|}| jjk	r|j }|dk	r|j|}|dk	r|d | }|}| j j||  dS )z=Call `matplotlib.backend_managers.ToolManager.message_event`.N c                s"   g | ]}|j  r|j r|qS r   )containsZget_visible).0a)r    r   r   
<listcomp>^  s    z3ToolCursorPosition.send_message.<locals>.<listcomp>)r   messagelocklockedrF   get_navigateZformat_coordZxdataZydata
ValueErrorOverflowErrorZ_mouseover_setr   Z_topmost_artistpatchZget_cursor_dataZformat_cursor_datamessage_event)r   r    messagesZartistsrN   r!   Zdata_strr   )r    r   rJ   Q  s&    


zToolCursorPosition.send_messageN)r   r	   r
   r   r   r   rJ   r   r   r   r   rI   ?  s   rI   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	RubberbandBasezDraw and remove a rubberband.c             C   s4   | j jjj|sdS |dk	r(| j|  n| j  dS )z<Call `draw_rubberband` or `remove_rubberband` based on data.N)r   r   
widgetlock	availabledraw_rubberbandremove_rubberband)r   r   r    r!   r   r   r   r"   p  s
    zRubberbandBase.triggerc             G   s   t dS )zY
        Draw rubberband.

        This method must get implemented per backend.
        N)rH   )r   r!   r   r   r   r\   y  s    zRubberbandBase.draw_rubberbandc             C   s   dS )z]
        Remove rubberband.

        This method should get implemented per backend.
        Nr   )r   r   r   r   r]     s    z RubberbandBase.remove_rubberbandN)r   r	   r
   r   r"   r\   r]   r   r   r   r   rY   n  s   	rY   c               @   s(   e Zd ZdZdZejd ZdddZdS )ToolQuitz/Tool to call the figure manager destroy method.zQuit the figurezkeymap.quitNc             C   s   t j| j d S )N)r   Zdestroy_figr   )r   r   r    r!   r   r   r   r"     s    zToolQuit.trigger)N)	r   r	   r
   r   r%   mplrcParamsr$   r"   r   r   r   r   r^     s   
r^   c               @   s(   e Zd ZdZdZejd ZdddZdS )ToolQuitAllz/Tool to call the figure manager destroy method.zQuit all figureszkeymap.quit_allNc             C   s   t j  d S )N)r   Zdestroy_all)r   r   r    r!   r   r   r   r"     s    zToolQuitAll.trigger)N)	r   r	   r
   r   r%   r_   r`   r$   r"   r   r   r   r   ra     s   
ra   c               @   s(   e Zd ZdZdZejd ZdddZdS )_ToolEnableAllNavigationz4Tool to enable all axes for toolmanager interaction.zEnable all axes toolmanagerzkeymap.all_axesNc             C   s   t jj|| jjd  d S )N)r_   backend_baseskey_press_handlerr   r   )r   r   r    r!   r   r   r   r"     s    z _ToolEnableAllNavigation.trigger)N)	r   r	   r
   r   r%   r_   r`   r$   r"   r   r   r   r   rb     s   
rb   z3.3c               @   s   e Zd ZdS )ToolEnableAllNavigationN)r   r	   r
   r   r   r   r   re     s   re   c            	   @   s"   e Zd ZdZdZdZdddZdS )_ToolEnableNavigationz;Tool to enable a specific axes for toolmanager interaction.zEnable one axes toolmanager123456789Nc             C   s   t jj|| jjd  d S )N)r_   rc   rd   r   r   )r   r   r    r!   r   r   r   r"     s    z_ToolEnableNavigation.trigger)	rg   rh   ri   rj   rk   rl   rm   rn   ro   )N)r   r	   r
   r   r%   r$   r"   r   r   r   r   rf     s   rf   c               @   s   e Zd ZdS )ToolEnableNavigationN)r   r	   r
   r   r   r   r   rp     s   rp   c               @   s(   e Zd ZdZdZejd ZdddZdS )ToolGridz-Tool to toggle the major grids of the figure.zToggle major gridszkeymap.gridNc             C   sV   t tj }tj||d2 tjd|i tjj|| j	j
 W d Q R X W d Q R X d S )N)keyzkeymap.grid)struuiduuid4r   _setattr_cmr_   
rc_contextrc   rd   r   r   )r   r   r    r!   sentinelr   r   r   r"     s    zToolGrid.trigger)N)	r   r	   r
   r   r%   r_   r`   r$   r"   r   r   r   r   rq     s   
rq   c               @   s(   e Zd ZdZdZejd ZdddZdS )ToolMinorGridz7Tool to toggle the major and minor grids of the figure.zToggle major and minor gridszkeymap.grid_minorNc             C   sV   t tj }tj||d2 tjd|i tjj|| j	j
 W d Q R X W d Q R X d S )N)rr   zkeymap.grid_minor)rs   rt   ru   r   rv   r_   rw   rc   rd   r   r   )r   r   r    r!   rx   r   r   r   r"     s    zToolMinorGrid.trigger)N)	r   r	   r
   r   r%   r_   r`   r$   r"   r   r   r   r   ry     s   
ry   c               @   s.   e Zd ZdZdZejd Zdd Zdd Z	dS )	ToolFullScreenzTool to toggle full screen.zToggle fullscreen modezkeymap.fullscreenc             C   s   | j jjj  d S )N)r   r   managerfull_screen_toggle)r   r    r   r   r   r1     s    zToolFullScreen.enablec             C   s   | j jjj  d S )N)r   r   r{   r|   )r   r    r   r   r   r0     s    zToolFullScreen.disableN)
r   r	   r
   r   r%   r_   r`   r$   r1   r0   r   r   r   r   rz     s
   
rz   c               @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
AxisScaleBasez3Base Tool to toggle between linear and logarithmic.Nc             C   s"   |j d krd S tj| ||| d S )N)rF   r)   r"   )r   r   r    r!   r   r   r   r"     s    
zAxisScaleBase.triggerc             C   s   | j |jd | jjj  d S )Nlog)	set_scalerF   r   r   	draw_idle)r   r    r   r   r   r1     s    zAxisScaleBase.enablec             C   s   | j |jd | jjj  d S )NZlinear)r   rF   r   r   r   )r   r    r   r   r   r0     s    zAxisScaleBase.disable)N)r   r	   r
   r   r"   r1   r0   r   r   r   r   r}     s   
r}   c               @   s&   e Zd ZdZdZejd Zdd ZdS )
ToolYScalezCTool to toggle between linear and logarithmic scales on the Y axis.zToggle scale Y axiszkeymap.yscalec             C   s   |j | d S )N)Z
set_yscale)r   axscaler   r   r   r     s    zToolYScale.set_scaleN)	r   r	   r
   r   r%   r_   r`   r$   r   r   r   r   r   r     s   
r   c               @   s&   e Zd ZdZdZejd Zdd ZdS )
ToolXScalezCTool to toggle between linear and logarithmic scales on the X axis.zToggle scale X axiszkeymap.xscalec             C   s   |j | d S )N)Z
set_xscale)r   r   r   r   r   r   r     s    zToolXScale.set_scaleN)	r   r	   r
   r   r%   r_   r`   r$   r   r   r   r   r   r     s   
r   c               @   s   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	dddZ
ejddddd Zdd Zdd Zdd Zdd Zd
S ) ToolViewsPositionsa%  
    Auxiliary Tool to handle changes in views and positions.

    Runs in the background and should get used by all the tools that
    need to access the figure's history of views and positions, e.g.

    * `ToolZoom`
    * `ToolPan`
    * `ToolHome`
    * `ToolBack`
    * `ToolForward`
    c             O   s.   t  | _t  | _t  | _tj| f|| d S )N)r   views	positions
home_viewsr   r   )r   r.   r/   r   r   r   r     s    zToolViewsPositions.__init__c                sR   | j krNtj  j |< tj  j|< t  j|<  j| |j fdd dS )z;Add the current figure to the stack of views and positions.c                s
    j | S )N)update_home_views)Zfig)r   r   r   <lambda>*  s    z/ToolViewsPositions.add_figure.<locals>.<lambda>N)r   r   ZStackr   r   r   push_currentZadd_axobserver)r   r   r   )r   r   
add_figure   s    

zToolViewsPositions.add_figurec             C   s@   || j kr<| j | j  | j| j  | j| j  | j  dS )zReset the axes stack.N)r   clearr   r   r   )r   r   r   r   r   r   ,  s
    
zToolViewsPositions.clearc             C   s   | j | j  }|dkrdS | j| j  }|dkr4dS | j| j }| jj }x0|D ](}||krf|| }n|| }|j| qPW t|j|rx4|D ],}|j|| d d |j|| d d qW | jj	j
  dS )a  
        Update the view limits and position for each axes from the current
        stack position. If any axes are present in the figure that aren't in
        the current stack position, use the home view limits for those axes and
        don't update *any* positions.
        Nr   original   active)r   r   r   r   get_axesZ	_set_viewsetissubsetZ_set_positionr   r   )r   r   posr   Zall_axesrN   Zcur_viewr   r   r   update_view4  s$    



zToolViewsPositions.update_viewNc             C   sf   |s
| j }t }t }x*|j D ]}|j ||< | j|||< q W | j| j| | j| j| dS )zY
        Push the current view limits and position onto their respective stacks.
        N)r   r   r   	_get_view	_axes_posr   pushr   )r   r   r   r   rN   r   r   r   r   S  s    zToolViewsPositions.push_currentc             C   s   |j dj |j  j fS )aY  
        Return the original and modified positions for the specified axes.

        Parameters
        ----------
        ax : matplotlib.axes.Axes
            The `.Axes` to get the positions for.

        Returns
        -------
        original_position, modified_position
            A tuple of the original and modified positions.
        T)get_positionfrozen)r   r   r   r   r   r   a  s    zToolViewsPositions._axes_posc             C   s@   |s
| j }x0|j D ]$}|| j| kr|j | j| |< qW dS )zm
        Make sure that ``self.home_views`` has an entry for all axes present
        in the figure.
        N)r   r   r   r   )r   r   rN   r   r   r   r   s  s
    z$ToolViewsPositions.update_home_viewsz3.3zself.figure.canvas.draw_idle())alternativec             C   s   | j   dS )z)Redraw the canvases, update the locators.N)_refresh_locators)r   r   r   r   refresh_locators  s    z#ToolViewsPositions.refresh_locatorsc             C   s   x| j j D ]}t|dd }t|dd }t|dd }g }|d k	r\|j|j  |j|j  |d k	r|j|j  |j|j  |d k	r|j|j  |j|j  x|D ]}tjj| qW qW | j j	j
  d S )Nxaxisyaxiszaxis)r   r   rE   appendZget_major_locatorZget_minor_locatorr_   ZtickerZ+_if_refresh_overridden_call_and_emit_deprecr   r   )r   rN   r   r   r   Zlocatorslocr   r   r   r     s"    
z$ToolViewsPositions._refresh_locatorsc             C   s$   | j | j j  | j| j j  dS )z2Recall the first view and position from the stack.N)r   r   homer   )r   r   r   r   r     s    zToolViewsPositions.homec             C   s$   | j | j j  | j| j j  dS )z2Back one step in the stack of views and positions.N)r   r   backr   )r   r   r   r   r     s    zToolViewsPositions.backc             C   s$   | j | j j  | j| j j  dS )z5Forward one step in the stack of views and positions.N)r   r   forwardr   )r   r   r   r   r     s    zToolViewsPositions.forward)N)N)r   r	   r
   r   r   r   r   r   r   r   r   r   
deprecatedr   r   r   r   r   r   r   r   r   r     s   

r   c               @   s   e Zd ZdZdZdddZdS )ViewsPositionsBasez8Base class for `ToolHome`, `ToolBack` and `ToolForward`.Nc             C   s>   | j jtj| j t| j jt| j  | j jtj  d S )N)r   get_tool_views_positionsr   r   rE   _on_triggerr   )r   r   r    r!   r   r   r   r"     s    
zViewsPositionsBase.trigger)N)r   r	   r
   r   r   r"   r   r   r   r   r     s   r   c               @   s&   e Zd ZdZdZdZejd ZdZ	dS )ToolHomez!Restore the original view limits.zReset original viewr   zkeymap.homeN)
r   r	   r
   r   r%   r&   r_   r`   r$   r   r   r   r   r   r     s
   
r   c               @   s&   e Zd ZdZdZdZejd ZdZ	dS )ToolBackz#Move back up the view limits stack.zBack to previous viewr   zkeymap.backN)
r   r	   r
   r   r%   r&   r_   r`   r$   r   r   r   r   r   r     s
   
r   c               @   s&   e Zd ZdZdZdZejd ZdZ	dS )ToolForwardz#Move forward in the view lim stack.zForward to next viewr   zkeymap.forwardN)
r   r	   r
   r   r%   r&   r_   r`   r$   r   r   r   r   r   r     s
   
r   c               @   s   e Zd ZdZdZdZdS )ConfigureSubplotsBasez,Base tool for the configuration of subplots.zConfigure subplotssubplotsN)r   r	   r
   r   r%   r&   r   r   r   r   r     s   r   c               @   s"   e Zd ZdZdZdZejd ZdS )SaveFigureBasezBase tool for figure saving.zSave the figureZfilesavezkeymap.saveN)	r   r	   r
   r   r%   r&   r_   r`   r$   r   r   r   r   r     s   r   c               @   s:   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd ZdS )ZoomPanBasez(Base class for `ToolZoom` and `ToolPan`.c             G   sN   t j| f|  d | _d | _d | _d | _d | _d| _d| _t	j	 | j | _
d S )Ng       @g      ?)r)   r   _button_pressed_xypress_idPress
_idRelease	_idScroll
base_scalescrollthreshtime
lastscroll)r   r.   r   r   r   r     s    zZoomPanBase.__init__c             C   sN   | j jj|  | j jjd| j| _| j jjd| j| _| j jjd| j| _	dS )z1Connect press/release events and lock the canvas.Zbutton_press_eventZbutton_release_eventZscroll_eventN)
r   r   rZ   rB   _pressr   _releaser   scroll_zoomr   )r   r    r   r   r   r1     s    zZoomPanBase.enablec             C   sL   | j   | jjjj|  | jjj| j | jjj| j | jjj| j dS )z7Release the canvas and disconnect press/release events.N)	_cancel_actionr   r   rZ   releaserA   r   r   r   )r   r    r   r   r   r0     s
    zZoomPanBase.disableNc             C   s(   | j jtj| j tj| ||| d S )N)r   r   r   r   r   r)   r"   )r   r   r    r!   r   r   r   r"     s    zZoomPanBase.triggerc             C   s   |j d krd S |jdkr | j}n|jdkr6d| j }nd}|j }|j|j|j|g tj | j | jk rx| j	j
tj  | jjj  tj | _| j	j
tj  d S )NZupZdownr   )rF   buttonr   _set_view_from_bboxxyr   r   r   r   r   r   r   r   r   r   r   )r   r    Zsclr   r   r   r   r     s    



zZoomPanBase.scroll_zoom)N)	r   r	   r
   r   r   r1   r0   r"   r   r   r   r   r   r     s   

r   c               @   sd   e Zd ZdZdZdZejd Ze	j
ZdZdd Zdd	 Zd
d Zdd Zdd Zdd Zdd ZdS )ToolZoomz.A Tool for zooming using a rectangle selector.zZoom to rectangleZzoom_to_rectzkeymap.zoomdefaultc             G   s   t j| f|  g | _d S )N)r   r   	_ids_zoom)r   r.   r   r   r   r   )  s    zToolZoom.__init__c             C   sR   x| j D ]}| jjj| qW | jjd|  | jjtj  d | _	d | _
g | _ d S )N
rubberband)r   r   r   rA   r   trigger_toolr   r   r   r   r   )r   zoom_idr   r   r   r   -  s    zToolZoom._cancel_actionc       	      C   s   | j r| j  |jdkr d| _n|jdkr2d| _n| j  dS |j|j }}g | _x^t| jj	 D ]L\}}|dk	rb|dk	rb|j
|rb|j rb|j rb| jj|||||j f qbW | jjjd| j}| jjjd| j}| jjjd| j}|||f| _ |j| _dS )z<Callback for mouse button presses in zoom-to-rectangle mode.r      Nr@   Zkey_press_eventZkey_release_event)r   r   r   r   r   r   r   	enumerater   r   in_axesrR   Zcan_zoomr   r   r   rB   _mouse_move_switch_on_zoom_mode_switch_off_zoom_moderr   
_zoom_mode)	r   r    r   r   irN   Zid1Zid2Zid3r   r   r   r   7  s,    




zToolZoom._pressc             C   s   |j | _| j| d S )N)rr   r   r   )r   r    r   r   r   r   Y  s    zToolZoom._switch_on_zoom_modec             C   s   d | _ | j| d S )N)r   r   )r   r    r   r   r   r   ]  s    zToolZoom._switch_off_zoom_modec             C   s   | j r|j|j }}| j d \}}}}}tj||g||gg|jj|jj\\}	}
\}}| jdkrn|jj	\}
}n| jdkr|jj
\}	}| jjd| |	|
||fd dS )z3Callback for mouse moves in zoom-to-rectangle mode.r   r   r   r   )r!   N)r   r   r   npZclipZbboxminmaxr   Z	intervalyZ	intervalxr   r   )r   r    r   r   lastxlastyrN   indviewx1y1Zx2y2r   r   r   r   a  s    *

zToolZoom._mouse_movec             C   s>  x| j D ]}| jjj| qW g | _ | js6| j  dS g }x| jD ]}|j|j }}|\}}}	}
}t|| dk st|| dk r| j  dS d	\}}|rx4|D ],}|	j	 j
|	|rd}|	j j
|	|rd}qW |j|	 | jdkrd}n| jdkrBd}nqB|	j||||f|| j|| qBW d| _| jjtj  | j  dS )
z=Callback for mouse button releases in zoom-to-rectangle mode.Nr   FTr   inr   out)FF)r   r   r   rA   r   r   r   r   absZget_shared_x_axesZjoinedZget_shared_y_axesr   r   r   r   r   r   r   r   )r   r    r   Zlast_aZcur_xypressr   r   r   r   rN   _indr   ZtwinxZtwinyZla	directionr   r   r   r   p  s>     



zToolZoom._releaseN)r   r	   r
   r   r%   r&   r_   r`   r$   r7   r   r3   r2   r   r   r   r   r   r   r   r   r   r   r   r      s   

"r   c               @   sT   e Zd ZdZejd ZdZdZe	j
ZdZdd Zdd	 Zd
d Zdd Zdd ZdS )ToolPanz*Pan axes with left mouse, zoom with right.z
keymap.panz)Pan axes with left mouse, zoom with rightmover   c             G   s   t j| f|  d | _d S )N)r   r   r5   )r   r.   r   r   r   r     s    zToolPan.__init__c             C   s>   d | _ g | _| jjj| j | jjj|  | jj	t
j  d S )N)r   r   r   r   rA   r5   r   rP   r   r   r   r   )r   r   r   r   r     s
    zToolPan._cancel_actionc             C   s   |j dkrd| _n|j dkr$d| _n| j  d S |j|j }}g | _xt| jj D ]r\}}|d k	rT|d k	rT|j	|rT|j
 rT|j rT|j|||j  | jj||f | jj|  | jjjd| j| _qTW d S )Nr   r   r@   )r   r   r   r   r   r   r   r   r   r   rR   Zcan_panZ	start_panr   r   rP   r   rB   r   r5   )r   r    r   r   r   rN   r   r   r   r     s     

zToolPan._pressc             C   s~   | j d kr| j  d S | jjj| j | jjj|  x| j	D ]\}}|j
  q<W | j	sb| j  d S | jjtj  | j  d S )N)r   r   r   r   rA   r5   r   rP   r   r   Zend_panr   r   r   )r   r    rN   r   r   r   r   r     s    
zToolPan._releasec             C   s<   x*| j D ] \}}|j| j|j|j|j qW | jjj  d S )N)	r   Zdrag_panr   rr   r   r   r   r   r   )r   r    rN   r   r   r   r   r     s    zToolPan._mouse_moveN)r   r	   r
   r   r_   r`   r$   r%   r&   r7   r   r3   r2   r   r   r   r   r   r   r   r   r   r     s   
r   c               @   sJ   e Zd ZdZejd ZdZedd Z	dd Z
dd	 Zd
d Zdd ZdS )ToolHelpBasez*Print tool list, shortcuts and descriptionzkeymap.helphelpc             C   s"   t | dkr| S tjdd| j S )z
        Converts a shortcut string from the notation used in rc config to the
        standard notation for displaying shortcuts, e.g. 'ctrl+a' -> 'Ctrl+A'.
        r   z\+[A-Z]z+Shift\g<0>)lenresubtitle)Zkey_sequencer   r   r   format_shortcut  s    zToolHelpBase.format_shortcutc                s$    j j|}dj fdd|D S )Nz, c             3   s   | ]} j |V  qd S )N)r   )rM   Zkeymap)r   r   r   	<genexpr>  s    z3ToolHelpBase._format_tool_keymap.<locals>.<genexpr>)r   Zget_tool_keymapjoin)r   r   Zkeymapsr   )r   r   _format_tool_keymap  s    z ToolHelpBase._format_tool_keymapc                s    fddt  jjj D S )Nc                s(   g | ] \}}|j r| j||j fqS r   )r%   r   )rM   r   r?   )r   r   r   rO     s   z2ToolHelpBase._get_help_entries.<locals>.<listcomp>)sortedr   r<   items)r   r   )r   r   _get_help_entries  s    
zToolHelpBase._get_help_entriesc             C   s    | j  }dd |D }dj|S )Nc             S   s   g | ]}d j | qS )z
{}: {}
	{})format)rM   entryr   r   r   rO     s    z/ToolHelpBase._get_help_text.<locals>.<listcomp>
)r   r   )r   entriesr   r   r   _get_help_text  s    zToolHelpBase._get_help_textc                sP   d  j dddg}| fdd| j D 7 }d|d  d	j|d
d   d S )Nz*<tr><td>{}</td><td>{}</td><td>{}</td></tr>z<b>Action</b>z<b>Shortcuts</b>z<b>Description</b>c                s   g | ]} j | qS r   )r   )rM   row)fmtr   r   rO      s    z/ToolHelpBase._get_help_html.<locals>.<listcomp>z2<style>td {padding: 0px 4px}</style><table><thead>r   z</thead><tbody>r   z</tbody></table>)r   r   r   )r   Zrowsr   )r   r   _get_help_html  s
    zToolHelpBase._get_help_htmlN)r   r	   r
   r%   r_   r`   r$   r&   staticmethodr   r   r   r   r   r   r   r   r   r     s   
	r   c               @   s&   e Zd ZdZdZejd Zdd ZdS )ToolCopyToClipboardBasez)Tool to copy the figure to the clipboard.z#Copy the canvas figure to clipboardzkeymap.copyc             O   s   d}| j j||  d S )NzCopy tool is not available)r   rV   )r   r.   r/   rW   r   r   r   r"     s    zToolCopyToClipboardBase.triggerN)	r   r	   r
   r   r%   r_   r`   r$   r"   r   r   r   r   r     s   
r   r   r   r   ZzoomZpanr   ZToolConfigureSubplotssaveZToolSaveFigureZgridZ
grid_minorZ
fullscreenquitZquit_allZallnavnavZxscaleZyscalepositionr3   ZToolSetCursorr   ZToolRubberbandr   ZToolHelpcopyZToolCopyToClipboardZ
navigationZzoompanioc             C   s&   x |j  D ]\}}| j|| q
W dS )a/  
    Add multiple tools to a `.ToolManager`.

    Parameters
    ----------
    toolmanager : `.backend_managers.ToolManager`
        Manager to which the tools are added.
    tools : {str: class_like}, optional
        The tools to add in a {name: tool} dict, see `add_tool` for more
        info.
    N)r   add_tool)r   r<   r   r?   r   r   r   add_tools_to_manager-  s    r  c             C   s:   x4|D ],\}}x"t |D ]\}}| j||| qW qW dS )a  
    Add multiple tools to the container.

    Parameters
    ----------
    container : Container
        `backend_bases.ToolContainerBase` object that will get the tools added.
    tools : list, optional
        List in the form ``[[group1, [tool1, tool2 ...]], [group2, [...]]]``
        where the tools ``[tool1, tool2, ...]`` will display in group1.
        See `add_tool` for details.
    N)r   r   )	containerr<   groupZ
grouptoolsr   r?   r   r   r   add_tools_to_container>  s    r  );r   enumr   loggingr   r   typesr   rt   weakrefr   numpyr   Z
matplotlibr_   Zmatplotlib._pylab_helpersr   Zmatplotlib.cbookr   	getLoggerr   _logr   r7   r   r   r)   r4   rI   rY   r^   ra   rb   r   re   rf   rp   rq   ry   rz   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zdefault_toolsZdefault_toolbar_toolsr  r  r   r   r   r   <module>   s   

xXG/





 			@~B&