
    e-<                         d 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	m
Z
mZ dgZ G d dej                        Zy	)
z
GraphicsView.py -   Extension of QGraphicsView
Copyright 2010  Luke Campagnola
Distributed under MIT/X11 license. See license.txt for more information.
   )	functions)getConfigOption)GraphicsScene)Point)QT_LIBQtCoreQtGui	QtWidgetsGraphicsViewc                       e Zd ZdZ ej
                  ee      Z ej
                  e      Z ej
                  e      Z	 ej
                  e      Z
 ej
                  e      ZdZd$dZd Zd Z fdZ fdZ fdZd%d	Zd
 Zd Zd Zd Zd Zd%dZd Zd Zd%dZd Zd Zd Zd&dZ d'dZ!d Z"d Z#d(dZ$d(dZ% fdZ&d Z'd Z( fdZ) fd Z* fd!Z+d" Z,d# Z- xZ.S ))r   a  Re-implementation of QGraphicsView that removes scrollbars and allows unambiguous control of the 
    viewed coordinate range. Also automatically creates a GraphicsScene and a central QGraphicsWidget
    that is automatically scaled to the full view geometry.
    
    This widget is the basis for :class:`PlotWidget <pyqtgraph.PlotWidget>`, 
    :class:`GraphicsLayoutWidget <pyqtgraph.GraphicsLayoutWidget>`, and the view widget in
    :class:`ImageView <pyqtgraph.ImageView>`.
    
    By default, the view coordinate system matches the widget's pixel coordinates and 
    automatically updates when the view is resized. This can be overridden by setting 
    autoPixelRange=False. The exact visible range can be set with setRange().
    
    The view can be panned using the middle mouse button and scaled using the right mouse button if
    enabled via enableMouse()  (but ordinarily, we use ViewBox for this functionality).Nc                 x   d| _         t        j                  j                  | |       ddlm}  |        |t        d      }| j                  |       | j                  | j                  j                         | j                  t        j                  j                  j                         | j!                  |       | j#                  t$        j&                  j(                  j*                         | j-                  t        j.                  j0                  j2                         | j5                  t$        j&                  j6                  j8                         | j;                  t$        j&                  j6                  j8                         | j=                  t        j                  j>                  j@                         | jC                  t        j                  j>                  jD                         | jG                  t        j                  jH                  jJ                         g | _&        d| _'        | jQ                  d       d| _)        t%        jT                  dddd      | _+        d| _,        d| _-        | j]                          | j_                          ta        | 	      | _1        | je                  | jb                         d| _3        | ji                  t        jj                                t        jl                         | _7        | jf                  jq                  | jn                         d| _9        d| _:        d| _;        y)
a  
        ==============  ============================================================
        **Arguments:**
        parent          Optional parent widget
        useOpenGL       If True, the GraphicsView will use OpenGL to do all of its
                        rendering. This can improve performance on some systems,
                        but may also introduce bugs (the combination of 
                        QGraphicsView and QOpenGLWidget is still an 'experimental'
                        feature of Qt)
        background      Set the background color of the GraphicsView. Accepts any
                        single argument accepted by 
                        :func:`mkColor <pyqtgraph.mkColor>`. By 
                        default, the background color is determined using the
                        'backgroundColor' configuration option (see 
                        :func:`setConfigOptions <pyqtgraph.setConfigOptions>`).
        ==============  ============================================================
        Fr   )_connectCleanupN	useOpenGLT       )parent)<closedr
   QGraphicsView__init__ r   r   r   setCacheModeCacheModeFlagCacheBackgroundsetBackgroundRoler	   QPalette	ColorRoleNoRolesetBackgroundsetFocusPolicyr   QtFocusPolicyStrongFocussetFrameShapeQFrameShapeNoFramesetVerticalScrollBarPolicyScrollBarPolicyScrollBarAlwaysOffsetHorizontalScrollBarPolicysetTransformationAnchorViewportAnchorNoAnchorsetResizeAnchorAnchorViewCentersetViewportUpdateModeViewportUpdateModeMinimalViewportUpdatelockedViewportslastMousePossetMouseTrackingaspectLockedQRectFrangeautoPixelRangecurrentItem
clearMouseupdateMatrixr   sceneObjsetScenecentralWidgetsetCentralItemQGraphicsWidgetQGraphicsGridLayoutcentralLayout	setLayoutmouseEnabledscaleCenterclickAccepted)selfr   r   
backgroundr   s        @/usr/lib/python3/dist-packages/pyqtgraph/widgets/GraphicsView.pyr   zGraphicsView.__init__(   sN   & ((v6 	''4Iy!$,,<<=
 	u~~77>>?:&FII11==>9++1199:''		(A(A(T(TU))&))*C*C*V*VW$$Y%<%<%K%K%T%TUY44CCTTU""9#:#:#M#M#c#cd  " d#!]]1aA.
"%T2dmm$ "I5578&::<$$T%7%78! "    c                    |rE| j                  | j                         t        j                  j                  j
                  z         y| j                  | j                         t        j                  j                  j
                   z         y)zEnable or disable default antialiasing.
        Note that this will only affect items that do not specify their own antialiasing options.N)setRenderHintsrenderHintsr	   QPainter
RenderHintAntialiasing)rH   aas     rJ   setAntialiasingzGraphicsView.setAntialiasings   sb      0 0 2U^^5N5N5[5[ [\ 0 0 2enn6O6O6\6\5\ \]rK   c                 ~    || _         |dk(  rt        d      }t        j                  |      }| j	                  |       y)z
        Set the background color of the GraphicsView.
        To use the defaults specified py pyqtgraph.setConfigOption, use background='default'.
        To make the background transparent, use background=None.
        defaultrI   N)_backgroundr   fnmkBrushsetBackgroundBrush)rH   rI   brushs      rJ   r   zGraphicsView.setBackground{   s:     &"(6J

:&&rK   c                 ^    | j                         j                          t        |   |      S N)sceneprepareForPaintsuper
paintEventrH   ev	__class__s     rJ   r`   zGraphicsView.paintEvent   s%    

$$&w!"%%rK   c                 ^    | j                         j                          t        |   |i |S r\   )r]   r^   r_   render)rH   argskwdsrc   s      rJ   re   zGraphicsView.render   s)    

$$&w~t,t,,rK   c                     d | _         | j                         j                          d | _        d | _        d| _        | j                  d        t        t        | '          y )NT)
r?   r]   clearr:   r=   r   setViewportr_   r   close)rH   rc   s    rJ   rk   zGraphicsView.close   sN    !

lD')rK   c                     |r2t        t        d      }|st        d      t        j                         }nt        j                         }| j                  |       y )NQOpenGLWidgetzORequested to use OpenGL with QGraphicsView, but QOpenGLWidget is not available.)hasattrr
   	Exceptionrm   QWidgetrj   )rH   bHAVE_OPENGLvs       rJ   r   zGraphicsView.useOpenGL   sJ    !)_=K qrr'')A!!#ArK   c                 B    | j                         j                  |       y r\   )r]   keyPressEventrH   rb   s     rJ   ru   zGraphicsView.keyPressEvent   s    

""2&rK   c                 $    | j                  |      S r\   )setCentralWidgetrH   items     rJ   r@   zGraphicsView.setCentralItem   s    $$T**rK   c                     | j                   )| j                         j                  | j                          || _         |-| j                  j	                  |       | j                  d       yy)zSets a QGraphicsWidget to automatically fill the entire view (the item will be automatically
        resize whenever the GraphicsView is resized).N)r?   r]   
removeItemr=   addItemresizeEventry   s     rJ   rx   zGraphicsView.setCentralWidget   s]     )JJL##D$6$67!MM!!$'T" rK   c                 <     | j                         j                  | S r\   )r]   r}   rH   rf   s     rJ   r}   zGraphicsView.addItem   s    #tzz|##T**rK   c                 <     | j                         j                  | S r\   )r]   r|   r   s     rJ   r|   zGraphicsView.removeItem   s    &tzz|&&--rK   c                 "    || _         | | _        y r\   )rE   r9   )rH   rq   s     rJ   enableMousezGraphicsView.enableMouse   s    #$urK   c                      g | _         d | _        y r\   )
mouseTraillastButtonReleasedrH   s    rJ   r;   zGraphicsView.clearMouse   s    "&rK   c                 F   | j                   ry | j                  rUt        j                  dd| j	                         j                         | j	                         j                               | _        t        j                  | | j                  dd       | j                          y )Nr   F)paddingdisableAutoPixel)r   r9   r   r7   sizewidthheightr8   r   setRanger<   rv   s     rJ   r~   zGraphicsView.resizeEvent   sp    ;;q!TYY[->->-@$))+BTBTBVWDJdDJJERrK   c                 h   | j                  | j                         | j                  r| j                          n| j                  r?| j                  | j                  t        j                  j                  j                         n>| j                  | j                  t        j                  j                  j                         |r.| j                  D ]  }|j                  | j                  d       ! | j                  j                  | | j                         | j                  j                  |        y )Nr   r   )setSceneRectr8   r9   resetTransformr6   	fitInViewr   r    AspectRatioModeKeepAspectRatioIgnoreAspectRatior3   	setXRangesigDeviceRangeChangedemitsigDeviceTransformChanged)rH   	propagaters   s      rJ   r<   zGraphicsView.updateMatrix   s    $**%!  tzz699+D+D+T+TUtzz699+D+D+V+VW)) 3DJJ23 	""''djj9&&++D1rK   c                     t        j                  | j                               }| j                         j	                         d   j                  |      S )z6Return the boundaries of the view in scene coordinatesr   )r   r7   rectviewportTransforminvertedmapRect)rH   rs     rJ   viewRectzGraphicsView.viewRect   s@     MM$))+&%%'00215==a@@rK   c                 "    | j                         S r\   )r   r   s    rJ   visibleRangezGraphicsView.visibleRange   s    }}rK   c                 `    | j                   j                  ||||       | j                          y r\   )r8   adjustr<   )rH   dxdys      rJ   	translatezGraphicsView.translate   s&    

"b"b)rK   c                    ||g}| j                   r|d   |d<   | j                  rd }|| j                  j                         }| j                  j	                         |d   z  }| j                  j                         |d   z  }t        j                  |j                         |j                         | j                  j                         z
  |d   z  z
  |j                         |j                         | j                  j                         z
  |d   z  z
  ||      | _        | j                          | j                  j                  |        y )Nr   r   )r6   rF   r8   centerr   r   r   r7   xleftytopr<   sigScaleChangedr   )rH   sxsyr   scalewhs          rJ   r   zGraphicsView.scale   sJ   RQxE!HF>ZZ&&(FJJ%(*JJ%(*]]688:DJJOO<M1MQVWXQY0Y#Y[a[c[c[eioiqiqistxt~t~  uC  uC  uE  jE  IN  OP  IQ  iQ  \Q  ST  VW  X
!!$'rK   c                    |rd| _         || j                         }d}t        |      }t        j                  |      }|j                         |d   z  }|j                         |d   z  }|j                  | | ||      }d}| j                  j                         |j                         k7  s+| j                  j                         |j                         k7  rd}|| _        | j                  %| j                  j                  | j                         | j                  |       |r| j                  j                  |        y y )NFr   r   T)r9   r   r   r   r7   r   r   adjustedr8   r?   setGeometryr<   r   r   )	rH   newRectr   
lockAspectr   r   pwphscaleChangeds	            rJ   r   zGraphicsView.setRange   s    %D?'')GG.--(]]_wqz)^^
*""B3R4::0DJJ4E4E4G7>>K[4[L
)**4::6)$  %%d+ rK   c                 j   |j                         }|j                  d       	 | j                  j                  |j                         |j                         j                         }| j                         j                         |d   z  }| j                         j                         |d   z  }t        j                  |j                         |j                         ||      }t         j#                  | |d       | j                  j%                  |j                         y# t
        t        f$ r Y w xY w)z{Scales such that pixels in image are the same size as screen pixels. This may result in a significant performance increase.Tr   r   r   N)	pixelSize	setPxModer   
disconnectsetScaledMode	TypeErrorRuntimeErrorsceneBoundingRecttopLeftr   r   r   r   r7   r   r   r   r   connect)rH   imagepxSizetlr   r   r8   s          rJ   scaleToImagezGraphicsView.scaleToImage  s    "	  ++E,?,?@ $$&..0IIK&)+IIK 6!9,bddfbddfa3dE15$$U%8%89 <( 		s   %D   D21D2c                 X    || j                   vr| j                   j                  |       y y r\   )r3   append)rH   v1s     rJ   
lockXRangezGraphicsView.lockXRange%  s)    T)))  ''+ *rK   c                     t        j                  | j                        }|j                  |j	                                |j                  |j                                t        j                  | ||dgd       y Nr   F)r   r   )	r   r7   r8   setLeftr   setRightrightr   r   rH   r   r   r1s       rJ   r   zGraphicsView.setXRange)  sS    ]]4::&


1668
AGGIdB!NrK   c                     t        j                  | j                        }|j                  |j	                                |j                  |j                                t        j                  | |d|gd       y r   )	r   r7   r8   setTopr   	setBottombottomr   r   r   s       rJ   	setYRangezGraphicsView.setYRange/  sS    ]]4::&
		!%%'
QXXZ dBGNrK   c                     t         |   |       | j                  sy |j                         j	                         }|dk(  r|j                         j                         }d|z  }| j                  ||       y )Nr   gjt?)r_   
wheelEventrE   
angleDeltar   r   r   )rH   rb   deltascrc   s       rJ   r   zGraphicsView.wheelEvent5  sd    2  !!#A:MMO%%'Ee^ 	

2rrK   c                     || _         y r\   )r6   )rH   ss     rJ   setAspectLockedzGraphicsView.setAspectLockedB  s
    rK   c                 B    | j                         j                  |       y r\   )r]   
leaveEventrv   s     rJ   r   zGraphicsView.leaveEventE  s    

#rK   c                 0   t         |   |       | j                  sy t        |d      r|j	                         n|j                         }|| _        || _        |j                         | _	        | j                  s| j                         j                          y )Nposition)r_   mousePressEventrE   rn   r   localPosr4   mousePressPos
isAcceptedrG   r]   clearSelection)rH   rb   lposrc   s      rJ   r   zGraphicsView.mousePressEventH  sv    #    'J 7r{{}R[[] !]]_!!JJL'')rK   c                     t         |   |       | j                  sy | j                  j	                  |       |j                         | _        y r\   )r_   mouseReleaseEventrE   sigMouseReleasedr   buttonr   ra   s     rJ   r   zGraphicsView.mouseReleaseEventV  sA    !"%  ""2&"$))+rK   c                    t        |d      r|j                         n|j                         }| j                  || _        t	        || j                  z
        }|| _        t
        |   |       | j                  sy | j                  j                  | j                  |j                                      | j                  ry |j                         t        j                  j                   j"                  k(  rt	        t%        j&                  |d   dd      t%        j&                  |d    dd            }d|z  }| j)                  |d   |d   | j                  | j*                  j                                      | j,                  j                  | | j.                         y |j                         t        j                  j                   j0                  t        j                  j                   j2                  fv rU| j5                         }| |z  }| j7                  |d   |d          | j,                  j                  | | j.                         y y )Nr   r   i2   r   g)\(?)r   )rn   r   r   r4   r   r_   mouseMoveEventrE   sigSceneMouseMovedr   
mapToScenetoPointrG   buttonsr   r    MouseButtonRightButtonrW   clip_scalarr   r   r   r8   MiddleButton
LeftButtonr   r   )rH   rb   r   r   r   pxtrrc   s          rJ   r   zGraphicsView.mouseMoveEvent^  s    'J 7r{{}R[[]$ $DdT.../ r"  $$T__T\\^%DE::<69900<<<"..q3;R^^USTXIWZ\^=_`EEMEJJuQxq$//$BTBTB\B\B^2_J`&&++D$**=ZZ\fii33@@&))BWBWBbBbcc!B"BNN2a5"Q%(&&++D$**= drK   c                     t        dd      }t        dd      }| j                         j                         d   }|j                  |      }|j                  |      }t        ||z
        S )zNReturn vector with the length and width of one view pixel in scene coordinatesr   r   )r   	transformr   map)rH   p0p1r   p01p11s         rJ   r   zGraphicsView.pixelSizez  s[    1QZ1QZ^^&&(+ffRjffRjS3YrK   c                 $    |j                          y r\   )ignorerv   s     rJ   dragEnterEventzGraphicsView.dragEnterEvent  s    
		rK   )NNrU   )Tr\   )N皙?NTT)r
  )/__name__
__module____qualname____doc__r   Signalobjectr   r   r   r   r   lastFileDirr   rS   r   r`   re   rk   r   ru   r@   rx   r}   r|   r   r;   r~   r<   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  __classcell__)rc   s   @rJ   r   r      s   [ *FMM&&9 -f 5$v}}V,&v.#fmmF+OKI#V^
'&-
*
'
+#+.&'2"A(",.:",OO$>8 rK   N)r  r   r   rW   r   r   r   r    r   r   r	   r
   __all__r   r    rK   rJ   <module>r     s:      )  1 1
t9** trK   