
    e=                         d dl Z d dlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
m
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddgZ G d de      Z G d de      Zy)    N)atan2   )	functions)Point)QtCoreQtGui   )GraphicsObject)Symbols)TextItem)UIGraphicsItem)ViewBox
TargetItemTargetLabelc                        e Zd ZdZ ej
                  e      Z ej
                  e      Z	 	 	 	 	 	 	 	 	 	 d fd	Z	 fdZ
d Zd Zd Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZ xZS )r   zDraws a draggable target symbol (circle plus crosshair).

    The size of TargetItem will remain fixed on screen even as the view is zoomed.
    Includes an optional text label.
    c                    t         |           || _        d| _        d| _        d| _        |d}| j                  |       |d}| j                  |       |d}| j                  |       |d}| j                  |       | j                  | _        | j                  | _        d| _        t        dd      | _        |t        dd      }| j#                  |       t%        |t&              r	 t(        |   | _        n-t%        |t.        j0                        r|| _        nt3        d	      || _        | j7                  | j*                         | j9                  |	|
       y# t,        $ r t-        d      w xY w)
aF  
        Parameters
        ----------
        pos : list, tuple, QPointF, QPoint, Optional
            Initial position of the symbol.  Default is (0, 0)
        size : int
            Size of the symbol in pixels.  Default is 10.
        pen : QPen, tuple, list or str
            Pen to use when drawing line. Can be any arguments that are valid
            for :func:`~pyqtgraph.mkPen`. Default pen is transparent yellow.
        brush : QBrush, tuple, list, or str
            Defines the brush that fill the symbol. Can be any arguments that
            is valid for :func:`~pyqtgraph.mkBrush`. Default is transparent
            blue.
        movable : bool
            If True, the symbol can be dragged to a new position by the user.
        hoverPen : QPen, tuple, list, or str
            Pen to use when drawing symbol when hovering over it. Can be any
            arguments that are valid for :func:`~pyqtgraph.mkPen`. Default pen
            is red.
        hoverBrush : QBrush, tuple, list or str
            Brush to use to fill the symbol when hovering over it. Can be any
            arguments that is valid for :func:`~pyqtgraph.mkBrush`. Default is
            transparent blue.
        symbol : QPainterPath or str
            QPainterPath to use for drawing the target, should be centered at
            ``(0, 0)`` with ``max(width, height) == 1.0``.  Alternatively a string
            which can be any symbol accepted by
            :func:`~pyqtgraph.ScatterPlotItem.setSymbol`
        label : bool, str or callable, optional
            Text to be displayed in a label attached to the symbol, or None to
            show no label (default is None). May optionally include formatting
            strings to display the symbol value, or a callable that accepts x
            and y as inputs.  If True, the label is ``x = {: >.3n}\ny = {: >.3n}``
            False or None will result in no text being displayed
        labelOpts : dict
            A dict of keyword arguments to use when constructing the text
            label. See :class:`TargetLabel` and :class:`~pyqtgraph.TextItem`
        FN)   r   r   )r   r   r   )r   r   r   2   )r   r   r   d   r   z&symbol name found in available SymbolszUnknown type provided as symbol)super__init__movablemoving_labelmouseHoveringsetPensetHoverPensetBrushsetHoverBrushpen
currentPenbrushcurrentBrush_shaper   _possetPos
isinstancestrr   _pathKeyErrorr   QPainterPath	TypeErrorscalesetPathsetLabel)selfpossizesymbolr    hoverPenr"   
hoverBrushr   label	labelOpts	__class__s              D/usr/lib/python3/dist-packages/pyqtgraph/graphicsItems/TargetItem.pyr   zTargetItem.__init__   sW   h 	";CC$H"=#Ee+J:&(( JJ!QK	;1+CCfc"I$V_
  2 23DJ=>>
TZZ eY'  IGHHIs   *E E0c                     	 t        | }| j                  |k7  r<|| _        t        |   | j                         | j                  j                  |        yy# t        $ r  t        $ r t        d|      w xY w)a  Method to set the position to ``(x, y)`` within the plot view

        Parameters
        ----------
        args : tuple or list or QtCore.QPointF or QtCore.QPoint or Point or float
            Two float values or a container that specifies ``(x, y)`` position where the
            TargetItem should be placed

        Raises
        ------
        TypeError
            If args cannot be used to instantiate a Point
        z%Could not make Point from arguments: N)r   r,   	Exceptionr%   r   r&   sigPositionChangedemit)r0   argsnewPosr8   s      r9   r&   zTargetItem.setPos{   s    	ND\F 99DIGN499%##((.   	 	NCD8LMM	Ns   A !A8c                     t        j                  |i || _        | j                  s"| j                  | _        | j                          yy)zSet the brush that fills the symbol. Allowable arguments are any that
        are valid for :func:`~pyqtgraph.mkBrush`.
        N)fnmkBrushr"   r   r#   updater0   r>   kwargss      r9   r   zTargetItem.setBrush   s<     ZZ00
!! $

DKKM "    c                     t        j                  |i || _        | j                  r"| j                  | _        | j                          yy)zSet the brush that fills the symbol when hovering over it. Allowable
        arguments are any that are valid for :func:`~pyqtgraph.mkBrush`.
        N)rA   rB   r5   r   r#   rC   rD   s      r9   r   zTargetItem.setHoverBrush   s<     **d5f5 $DKKM rF   c                     t        j                  |i || _        | j                  s"| j                  | _        | j                          yy)ztSet the pen for drawing the symbol. Allowable arguments are any that
        are valid for :func:`~pyqtgraph.mkPen`.N)rA   mkPenr    r   r!   rC   rD   s      r9   r   zTargetItem.setPen   s;     88T,V,!!"hhDOKKM "rF   c                     t        j                  |i || _        | j                  r"| j                  | _        | j                          yy)zSet the pen for drawing the symbol when hovering over it. Allowable
        arguments are any that are valid for
        :func:`~pyqtgraph.mkPen`.N)rA   rI   r4   r   r!   rC   rD   s      r9   r   zTargetItem.setHoverPen   s;     $1&1"mmDOKKM rF   c                 >    | j                         j                         S N)shapeboundingRectr0   s    r9   rN   zTargetItem.boundingRect   s    zz|((**rF   c                     |j                  | j                         |j                  | j                         |j	                  | j                                y rL   )r   r!   r   r#   drawPathrM   )r0   p_s      r9   paintzTargetItem.paint   s6    	!	

4$$%	

4::< rF   c                 >    || j                   k7  r|| _         d | _        y rL   )r)   r$   )r0   paths     r9   r.   zTargetItem.setPath   s    4::DJDKrF   c                     | j                   5| j                         }|| j                  S || _         | j                          | j                   S rL   )r$   generateShaper)   prepareGeometryChange)r0   ss     r9   rM   zTargetItem.shape   sH    ;;""$Ayzz!DK &&({{rF   c                    | j                         }|| j                  | _        y |j                  t	        j
                  dd            |j                  t	        j
                  dd            z
  }t        j                  |      }|j                  t	        j
                  dd            }t        j                         }|j                  |j                         |j                                t        |j                         |j                               }|j                  |       |j                  | j                  | j                         |j                  |j                  | j                              S )Nr	   r   )deviceTransformr)   r$   mapr   QPointFrA   invertQTransformr   
QTransform	translatexyr   rotateRadiansr-   )r0   dtvdtidevPostrvas          r9   rX   zTargetItem.generateShape   s    !!#:**DKFF6>>!Q'(266&..A2F+GG!!"%q!,-
VXXZ,1335!##% 

TZZ(wwrvvdjj)**rF   c                 6   | j                   r5|j                         t        j                  j                  j
                  k7  ry |j                          |j                         r<| j                         | j                  |j                               z
  | _        d| _        | j                  sy | j                  | j                  | j                  |j                               z          |j                         r#d| _        | j                  j!                  |        y y NTF)r   buttonr   QtMouseButton
LeftButtonacceptisStartr1   	mapToViewbuttonDownPossymbolOffsetr   r&   isFinishsigPositionChangeFinishedr=   r0   evs     r9   mouseDragEventzTargetItem.mouseDragEvent   s    ||ryy{fii.C.C.N.NN
		::< $
T^^B<L<L<N-O ODDK{{D%%rvvx(@@A;;=DK**//5 rF   c                 $   | j                   r|j                         t        j                  j                  j
                  k(  rN|j                          d| _         | j                  j                  |        | j                  j                  |        y y y )NF)
r   rm   r   rn   ro   RightButtonrq   r<   r=   rw   rx   s     r9   mouseClickEventzTargetItem.mouseClickEvent   sf    ;;299;&))*?*?*K*KKIIKDK##((.**//5	 L;rF   c                     | j                   |u ry || _         |r#| j                  | _        | j                  | _        n"| j
                  | _        | j                  | _        | j                          y rL   )r   r5   r#   r4   r!   r"   r    rC   )r0   hovers     r9   setMouseHoverzTargetItem.setMouseHover   sU    &" $D"mmDO $

D"hhDOrF   c                     | j                   rU|j                         sE|j                  t        j                  j
                  j                        r| j                  d       y | j                  d       y rl   )r   isExitacceptDragsr   rn   ro   rp   r   rx   s     r9   
hoverEventzTargetItem.hoverEvent  sH    <<"..AVAVAaAa2bt$u%rF   c                 \    t        j                  |        d | _        | j                          y rL   )r
   viewTransformChangedr$   rC   rO   s    r9   r   zTargetItem.viewTransformChanged  s     ++D1rF   c                     | j                   S )zProvides the current position of the TargetItem

        Returns
        -------
        Point
            pg.Point of the current position of the TargetItem
        )r%   rO   s    r9   r1   zTargetItem.pos  s     yyrF   c                     | j                   S )zProvides the TargetLabel if it exists

        Returns
        -------
        TargetLabel or None
            If a TargetLabel exists for this TargetItem, return that, otherwise
            return None
        )r   rO   s    r9   r6   zTargetItem.label  s     {{rF   c                    |sa| j                   M| j                   j                         3| j                   j                         j                  | j                          d| _         y|du rd}|i n|}| j                   3| j                   j                         j                  | j                          t        | fd|i|| _         y)a  Method to call to enable or disable the TargetLabel for displaying text

        Parameters
        ----------
        text : Callable or str, optional
            Details how to format the text, by default None
            If None, do not show any text next to the TargetItem
            If Callable, then the label will display the result of ``text(x, y)``
            If a fromatted string, then the output of ``text.format(x, y)`` will be
            displayed
            If a non-formatted string, then the text label will display ``text``, by
            default None
        labelOpts : dict, optional
            These arguments are passed on to :class:`~pyqtgraph.TextItem`
        NTzx = {: .3n}
y = {: .3n}text)r   scene
removeItemr   )r0   r   r7   s      r9   r/   zTargetItem.setLabel%  s      {{&4;;+<+<+>+J!!#..t{{;DK t|1'/YI{{&!!#..t{{;%dCCCDKrF   )
N
   	crosshairNNNNTNN)NN)__name__
__module____qualname____doc__r   Signalobjectr<   rw   r   r&   r   r   r   r   rN   rT   r.   rM   rX   rz   r}   r   r   r   r1   r6   r/   __classcell__r8   s   @r9   r   r      s     'v. -f 5 `(D/4+!

+6 6&
	DrF   c                   R     e Zd ZdZ	 	 	 d	 fd	Zd Zd Zd Z fdZd Z	d Z
 xZS )
r   a  A TextItem that attaches itself to a TargetItem.

    This class extends TextItem with the following features :
      * Automatically positions adjacent to the symbol at a fixed position.
      * Automatically reformats text when the symbol location has changed.

    Parameters
    ----------
    target : TargetItem
        The TargetItem to which this label will be attached to.
    text : str or callable, Optional
        Governs the text displayed, can be a fixed string or a format string
        that accepts the x, and y position of the target item; or be a callable
        method that accepts a tuple (x, y) and returns a string to be displayed.
        If None, an empty string is used.  Default is None
    offset : tuple or list or QPointF or QPoint
        Position to set the anchor of the TargetLabel away from the center of
        the target in pixels, by default it is (20, 0).
    anchor : tuple or list or QPointF or QPoint
        Position to rotate the TargetLabel about, and position to set the
        offset value to see :class:`~pyqtgraph.TextItem` for more information.
    kwargs : dict 
        kwargs contains arguments that are passed onto
        :class:`~pyqtgraph.TextItem` constructor, excluding text parameter
    c                 6   t        |t              r|| _        nt        |t        t        f      rt        | | _        nct        |t
        j                  t
        j                  f      r.t        |j                         |j                               | _        nt        d      t        | 0  dd|i| | j                  |       || _        | j                  |       | j                  j                   j#                  | j$                         | j%                          y )Nz'Offset parameter is the wrong data typeanchor )r'   r   offsettuplelistr   QPointr^   rb   rc   r,   r   r   setParentItemtarget	setFormatr<   connectvalueChanged)r0   r   r   r   r   rE   r8   s         r9   r   zTargetLabel.__init__a  s     fe$ DK..DK ?@
FHHJ7DKEFF11&16"t&&..t/@/@ArF   c                     | j                   S rL   )_formatrO   s    r9   formatzTargetLabel.formatz  s    ||rF   c                 F   t        |      smt        t        j                         j	                  |            }|r|d   d   | j                  dd       n6| j                  |       | j                  dd       n| j                  dd       || _        | j                          y)a  Method to set how the TargetLabel should display the text.  This
        method should be called from TargetItem.setLabel directly.

        Parameters
        ----------
        text : Callable or str
            Details how to format the text.
            If Callable, then the label will display the result of ``text(x, y)``
            If a fromatted string, then the output of ``text.format(x, y)`` will be
            displayed
            If a non-formatted string, then the text label will display ``text``
        r   r	   NformattableTextTF)	callabler   string	FormatterparsesetPropertysetTextr   r   )r0   r   parseds      r9   r   zTargetLabel.setFormat}  s     ~&**,22489F&)A,2  !2D9T"  !2E:.6rF   c                 J   | j                   j                         \  }}| j                  d      r>| j                  | j                  j                  t        |      t        |                   y t        | j                        r"| j                  | j	                  ||             y y )Nr   )r   r1   propertyr   r   r   floatr   )r0   rb   rc   s      r9   r   zTargetLabel.valueChanged  sp    {{ 1==*+LL,,U1XuQx@Adll#LLa+, $rF   c                 F   | j                         }t        |t              rs|j                         }t	        j
                  | j                  j                         |d   z  | j                  j                         |d   z        }| j                  |       t        | -         S )Nr   r	   )
getViewBoxr'   r   viewPixelSizer   r^   r   rb   rc   r&   r   r   )r0   viewboxr   scaledOffsetr8   s       r9   r   z TargetLabel.viewTransformChanged  s    //#gw'#113M!>>-"22DKKMMOmTUFV4VL KK%w+--rF   c                 @    | j                         j                  |      S rL   )
parentItemr}   rx   s     r9   r}   zTargetLabel.mouseClickEvent  s     0044rF   c                 V   | j                         }|j                  r5|j                         t        j                  j
                  j                  k7  ry |j                          |j                         r<|j                         | j                  |j                               z
  |_        d|_        |j                  sy |j                  |j                  | j                  |j                               z          |j                         r#d|_        |j                   j#                  |        y y rl   )r   r   rm   r   rn   ro   rp   rq   rr   r1   rs   rt   ru   r   r&   rv   rw   r=   )r0   ry   
targetItems      r9   rz   zTargetLabel.mouseDragEvent  s    __&
!!RYY[FII4I4I4T4T%T
		::<&0nn&6  ": 'J# !%J  *11DNN26684LLM;;= %J0055d; rF   ) )   r   )r   g      ?)r   r   r   r   r   r   r   r   r   r}   rz   r   r   s   @r9   r   r   F  s6    : 22-.5<rF   )r   mathr   r   r   rA   r   rn   r   r   r
   ScatterPlotItemr   r   r   r   __all__r   r   r   rF   r9   <module>r      sL         * $  * 
'tD tDn	u<( u<rF   