
    eV:                         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	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lmZmZ ddgZ G d dee      Z G d de      Zy)    N   )	functions)invisibleEye)Point)QtCoreQtGui	QtWidgets   )BarGraphItem)GraphicsWidget)GraphicsWidgetAnchor)	LabelItem)PlotDataItem)ScatterPlotItem
drawSymbol
LegendItem
ItemSamplec                       e Zd ZdZ	 	 	 d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 Zd Zd Zd Zd Zd Zd Zd Zy)r   a  
    Displays a legend used for describing the contents of a plot.

    LegendItems are most commonly created by calling :meth:`PlotItem.addLegend
    <pyqtgraph.PlotItem.addLegend>`.

    Note that this item should *not* be added directly to a PlotItem (via
    :meth:`PlotItem.addItem <pyqtgraph.PlotItem.addItem>`). Instead, make it a
    direct descendant of the PlotItem::

        legend.setParentItem(plotItem)

    Nc           	      L   t        j                  |        t        j                  |        | j                  | j                  j
                         t        j                         | _        | j                  j                  |       | j                  j                  |       | j                  | j                         g | _        || _        || _        || _        |
| _        d| _        |A| j%                  t'        j(                  dd| j                  d   | j                  d                |#t+        |t               st-        d      || _        nt0        | _        t3        j4                  |      t3        j6                  |      ||	|d| _        | j8                  j;                  |       y)a6  
        ==============  ===============================================================
        **Arguments:**
        size            Specifies the fixed size (width, height) of the legend. If
                        this argument is omitted, the legend will automatically resize
                        to fit its contents.
        offset          Specifies the offset position relative to the legend's parent.
                        Positive values offset from the left or top; negative values
                        offset from the right or bottom. If offset is None, the
                        legend must be anchored manually by calling anchor() or
                        positioned by calling setPos().
        horSpacing      Specifies the spacing between the line symbol and the label.
        verSpacing      Specifies the spacing between individual entries of the legend
                        vertically. (Can also be negative to have them really close)
        pen             Pen to use when drawing legend border. Any single argument
                        accepted by :func:`mkPen <pyqtgraph.mkPen>` is allowed.
        brush           QBrush to use as legend background filling. Any single argument
                        accepted by :func:`mkBrush <pyqtgraph.mkBrush>` is allowed.
        labelTextColor  Pen to use when drawing legend text. Any single argument
                        accepted by :func:`mkPen <pyqtgraph.mkPen>` is allowed.
        labelTextSize   Size to use when drawing legend text. Accepts CSS style
                        string arguments, e.g. '9pt'.
        colCount        Specifies the integer number of columns that the legend should
                        be divided into. The number of rows will be calculated
                        based on this argument. This is useful for plots with many
                        curves displayed simultaneously. Default: 1 column.
        sampleType      Customizes the item sample class of the `LegendItem`.
        ==============  ===============================================================

        r
   Nr   zBOnly classes of type `GraphicsWidgets` are allowed as `sampleType`)penbrushlabelTextColorlabelTextSizeoffset)r   __init__r   setFlagGraphicsItemFlagItemIgnoresTransformationsr	   QGraphicsGridLayoutlayoutsetVerticalSpacingsetHorizontalSpacing	setLayoutitemssizer   framecolumnCountrowCountsetGeometryr   QRectF
issubclassRuntimeError
sampleTyper   fnmkPenmkBrushoptsupdate)selfr%   r   
horSpacing
verSpacingr   r   r   r&   r   colCountr-   kwargss                D/usr/lib/python3/dist-packages/pyqtgraph/graphicsItems/LegendItem.pyr   zLegendItem.__init__    sM   B 	%%%d+T**EEF335&&z2((4t{{#
	
#V]]1a1tyy|LM!j.9" $A B B(DO(DO 88C=ZZ&,*
	 			     c                    || j                   u ryt        | j                        }|| _         | j                          |D ]/  \  }}|j                  }|j
                  }| j                  ||       1 | j                          y)zSet the new sample item claspesN)r-   listr$   clearitemtextaddItem
updateSize)r3   sampler$   label	plot_item	plot_names         r8   setSampleTypezLegendItem.setSampleTypec   sr    T__$ TZZ  

 # 	/MFEI

ILLI.	/
 	r9   c                      | j                   d   S )z/Get the offset position relative to the parent.r   r1   r3   s    r8   r   zLegendItem.offsetu   s    yy""r9   c                     || j                   d<   t        | j                   d         }|d   dk  rdnd}|d   dk  rdnd}||f}| j                  |||       y)z/Set the offset position relative to the parent.r   r   r
   itemPos	parentPosr   N)r1   r   anchor)r3   r   anchorxanchoryrM   s        r8   	setOffsetzLegendItem.setOffsety   sa    $		(tyy*+ayA~!1ayA~!17#FfVDr9   c                      | j                   d   S )z7Get the QPen used to draw the border around the legend.r   rG   rH   s    r8   r   zLegendItem.pen   s    yyr9   c                 l    t        j                  |i |}|| j                  d<   | j                          y)z~Set the pen used to draw a border around the legend.

        Accepts the same arguments as :func:`~pyqtgraph.mkPen`.
        r   N)r.   r/   r1   r2   )r3   argskargsr   s       r8   setPenzLegendItem.setPen   s.    
 hh&&		%r9   c                      | j                   d   S )z2Get the QBrush used to draw the legend background.r   rG   rH   s    r8   r   zLegendItem.brush   s    yy!!r9   c                     t        j                  |i |}| j                  d   |k(  ry|| j                  d<   | j                          y)z}Set the brush used to draw the legend background.

        Accepts the same arguments as :func:`~pyqtgraph.mkBrush`.
        r   N)r.   r0   r1   r2   )r3   rS   rT   r   s       r8   setBrushzLegendItem.setBrush   sB    
 

D*E*99W&"		'r9   c                      | j                   d   S )z(Get the QColor used for the item labels.r   rG   rH   s    r8   r   zLegendItem.labelTextColor   s    yy)**r9   c                     t        j                  |i || j                  d<   | j                  D ]$  \  }}|j	                  d| j                  d          & | j                          y)zmSet the color of the item labels.

        Accepts the same arguments as :func:`~pyqtgraph.mkColor`.
        r   colorN)r.   mkColorr1   r$   setAttrr2   )r3   rS   rT   rA   rB   s        r8   setLabelTextColorzLegendItem.setLabelTextColor   s]    
 ')jj$&@%&@		"#!ZZ 	@MFEMM'499-=#>?	@ 	r9   c                      | j                   d   S )z1Get the `labelTextSize` used for the item labels.r   rG   rH   s    r8   r   zLegendItem.labelTextSize   s    yy))r9   c                     || j                   d<   | j                  D ]$  \  }}|j                  d| j                   d          & | j                          y)zhSet the `size` of the item labels.

        Accepts the CSS style string arguments, e.g. '8pt'.
        r   r%   N)r1   r$   r]   r2   )r3   r%   _rB   s       r8   setLabelTextSizezLegendItem.setLabelTextSize   sK    
 &*		/"

 	>HAuMM&$))O"<=	> 	r9   c                     t        j                  | |      }| j                  d   Ht        | j                  d         }|d   dk  rdnd}|d   dk  rdnd}||f}| j	                  |||       |S )zSet the parent.r   r   r
   rJ   )r   setParentItemr1   r   rM   )r3   pretr   rN   rO   rM   s          r8   rd   zLegendItem.setParentItem   sy    **4399X*499X./F!!9>aqG!!9>aqGw'FKK&KH
r9   c                 (   t        || j                  d   d| j                  d         }t        || j                        r|}n| j                  |      }| j                  j                  ||f       | j                  ||       | j                          y)a[  
        Add a new entry to the legend.

        ==============  ========================================================
        **Arguments:**
        item            A :class:`~pyqtgraph.PlotDataItem` from which the line
                        and point style of the item will be determined or an
                        instance of ItemSample (or a subclass), allowing the
                        item display to be customized.
        title           The title to display for this item. Simple HTML allowed.
        ==============  ========================================================
        r   leftr   )r[   justifyr%   N)r   r1   
isinstancer-   r$   append_addItemToLayoutr@   )r3   r=   namerB   rA   s        r8   r?   zLegendItem.addItem   s|     $dii0@&A"(tyy/IKdDOO,F__T*F

65/*fe,r9   c                    | j                   j                         }| j                   j                         }|r|dz  }| j                  dz  }||k(  r?t        d|d      D ]   }| j                   j	                  ||      r  n |dz   |k(  rd}|dz  }| j                   j                  |||       | j                   j                  |||dz          t        | j                  |dz         | _        y )Nr
   r   r   )r    r'   r(   rangeitemAtr?   max)r3   rA   rB   colrownCols         r8   rl   zLegendItem._addItemToLayout   s    kk%%'kk""$1HC!#$;Qa( {{))#s3
 7d?C1HCFC-E3a0DMM373r9   c                    || j                   k7  r|| _         t        j                  t        | j                        |z        | _        t        | j                  j                         dz
  dd      D ]  }| j                  j                  |        | j                  D ]  \  }}| j                  ||        | j                          yy)z:change the orientation of all items of the legend
        r
   N)r'   mathceillenr$   r(   ro   r    countremoveAtrl   r@   )r3   r'   irA   rB   s        r8   setColumnCountzLegendItem.setColumnCount   s     $****D IIc$**o&CDDM4;;,,.2B; ($$Q'(!% 5%%fe45OO +r9   c                     | j                   D cg c]  \  }}|j                  |k(  s||f }}}	 |d   d   S c c}}w # t        $ r Y yw xY w)zReturn the labelItem inside the legend for a given plotItem

        The label-text can be changed via labelItem.setText
        r   r
   N)r$   r=   
IndexError)r3   plotItemitlabouts        r8   getLabelzLegendItem.getLabel  s[    
 )-

JWRbgg6ICyJJ	q6!9 K  		s   ::A   	AAc                     |D ]Q  }| j                   j                  |       |j                          |j                         }|sA|j                  |       S y N)r    
removeItemclosescene)r3   rS   r=   r   s       r8   _removeItemFromLayoutz LegendItem._removeItemFromLayout  sJ     		'DKK""4(JJL
 JJLE  &		'r9   c                     | j                   D ]c  \  }}|j                  |u s|j                  |k(  s$| j                   j                  ||f       | j	                  ||       | j                           y y)a  Removes one item from the legend.

        ==============  ========================================================
        **Arguments:**
        item            The item to remove or its name.
        ==============  ========================================================
        N)r$   r=   r>   remover   r@   )r3   r=   rA   rB   s       r8   r   zLegendItem.removeItem  se     "ZZ 	MFE{{d"ejjD&8

!!65/2**659!	r9   c                 ~    | j                   D ]  \  }}| j                  ||        g | _         | j                          y)z!Remove all items from the legend.N)r$   r   r@   )r3   rA   rB   s      r8   r<   zLegendItem.clear*  s<    !ZZ 	6MFE&&vu5	6 
r9   c                    | j                   y d}d}t        | j                  j                               D ]  }d}d}t        | j                  j	                               D ]Q  }| j                  j                  ||      }|s"||j                         dz   z  }t        ||j                               }S t        ||      }||z  } | j                  dd||       y )Nr      )
r%   ro   r    r(   r'   rp   widthrq   heightr)   )r3   r   r   rs   
row_height	col_widthrr   r=   s           r8   r@   zLegendItem.updateSize2  s    99 --/0 		!CJIT[[4467 @{{))#s3!11I!$Z!?J	@
 y)Ej F		! 	Auf-r9   c                 j    t        j                  dd| j                         | j                               S )Nr   )r   r*   r   r   rH   s    r8   boundingRectzLegendItem.boundingRectD  s#    }}Q4::<??r9   c                     | j                   r\|j                  | j                  d          |j                  | j                  d          |j	                  | j                                y y )Nr   r   )r&   rU   r1   rX   drawRectr   )r3   re   rS   s      r8   paintzLegendItem.paintG  sM    ::HHTYYu%&JJtyy)*JJt((*+ r9   c                 j    |j                  t        j                  j                  j                         y r   )acceptDragsr   QtMouseButton
LeftButton)r3   evs     r8   
hoverEventzLegendItem.hoverEventM  s    
vyy,,778r9   c                    |j                         t        j                  j                  j                  k(  rT|j                          |j                         |j                         z
  }| j                  | j                         |z          y y r   )	buttonr   r   r   r   acceptposlastPos
autoAnchor)r3   r   dposs      r8   mouseDragEventzLegendItem.mouseDragEventP  s[    99;&))//:::IIK668bjjl*DOODHHJ-. ;r9   )NN   r   NNNT9ptr
   N)__name__
__module____qualname____doc__r   rE   r   rP   r   rU   r   rX   r   r^   r   rb   rd   r?   rl   r}   r   r   r   r<   r@   r   r   r   r    r9   r8   r   r      s     JKBF=AA!F$#E "
+	*		.4,	
'$@,9/r9   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   zMClass responsible for drawing a single item in a LegendItem (sans label)
    c                 <    t        j                  |        || _        y r   )r   r   r=   )r3   r=   s     r8   r   zItemSample.__init__[  s    %	r9   c                 2    t        j                  dddd      S )Nr      )r   r*   rH   s    r8   r   zItemSample.boundingRect_  s    }}Q2r**r9   c           
         | j                   j                  }|j                  d      r%|j                  |j                  j
                         | j                   j                         }|sGt        j                  }|j                  t        j                  dd      |j                  dd             y t        | j                   t              s|j                  t!        j"                  |d                |j%                  dddd       |j                  dd       |j                  d	d       |j'                  t!        j(                  |d	                |j                  t!        j"                  |d                |j+                  t-        j.                  t        j0                  d
d      t        j0                  dd
      t        j0                  dd      g             |j                  dd       }|t        | j                   t2              r | j                   j4                  j                  }|j7                  dd       t9        |||d   t!        j"                  |d         t!        j(                  |d                t        | j                   t:              rO|j'                  t!        j(                  |d                |j=                  t        j>                  d
d
dd             y y )N	antialiasr
      r   r      r   	fillLevel	fillBrushr   symbol
   r%   r   ) r=   r1   getsetRenderHint
RenderHintAntialiasing	isVisibler   qicon
drawPixmapr   QPointpixmaprj   r   rU   r.   r/   drawLinerX   r0   drawPolygonr   	QPolygonFQPointFr   scatter	translater   r   r   r*   )r3   re   rS   r1   visibleiconr   s          r8   r   zItemSample.paintb  s   yy~~88K OOALL556))%%'%%DLLq!,dkk"b.AB$))_5HHRXXd5k*+JJq"b"%d+7HH[$/;

2::d;&789$u+./eoo^^Ar*FNN2q,A^^B+-. / (D)$))\2yy((--KKBq&$v,e0Ezz$w-02 dii.JJrzz$w-01JJv}}Q2r23 /r9   c                    |j                         t        j                  j                  j                  k(  r6| j
                  j                         }| j
                  j                  |        |j                          | j                          y)zJUse the mouseClick event to toggle the visibility of the plotItem
        N)
r   r   r   r   r   r=   r   
setVisibler   r2   )r3   eventr   s      r8   mouseClickEventzItemSample.mouseClickEvent  sZ     <<>VYY22===ii))+GII  W-r9   N)r   r   r   r   r   r   r   r   r   r9   r8   r   r   W  s    +!4Fr9   )rw    r   r.   iconsr   r   r   r   r   r	   r   r   r   r   r   r   r   __all__r   r   r   r9   r8   <module>r      sQ         ) ) & * 6   & 8
&C/%~ C/L
6 6r9   