
    ը	f5"                         d Z ddlZddlmZ ddlmZmZmZmZm	Z	 ddl
mZ  G d dej                        Z G d	 d
e      Zy)aY  
@package animation.mapwindow

@brief Animation window

Classes:
 - mapwindow::BufferedWindow
 - mapwindow::AnimationWindow

(C) 2013 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Anna Petrasova <kratochanna gmail.com>
    N)Debug)BitmapFromImageEmptyBitmapImageFromBitmapPseudoDCRect   )ComputeScaledRectc                   L    e Zd ZdZd Zd Zd Zd Zej                  fdZ
d Zy)	BufferedWindowah  
    A Buffered window class (http://wiki.wxpython.org/DoubleBufferedDrawing).

    To use it, subclass it and define a Draw(DC) method that takes a DC
    to draw to. In that method, put the code needed to draw the picture
    you want. The window will automatically be double buffered, and the
    screen will be automatically updated when a Paint event is received.

    When the drawing needs to change, you app needs to call the
    UpdateDrawing() method. Since the drawing is stored in a bitmap, you
    can also save the drawing to file by calling the
    SaveToFile(self, file_name, file_type) method.

    c                    |j                  dt        j                        t        j                  z  |d<   t        j                  j                  | g|i | t        j                  dd       | j                  t        j                  | j                         | j                  t        j                  | j                         | j                  d        y )Nstyle   zBufferedWindow.__init__())
setdefaultwxNO_FULL_REPAINT_ON_RESIZEWindow__init__r   msgBind	EVT_PAINTOnPaintEVT_SIZEOnSize)selfargskwargss      4/usr/lib/grass83/gui/wxpython/animation/mapwindow.pyr   zBufferedWindow.__init__(   s     gr'C'CD**+ 	w 			41$1&1		!01		",,-		"++t{{+ 	D    c                      y N r   dcs     r   DrawzBufferedWindow.Draw8   s     	r   c                 p    t        j                  dd       t        j                  | | j                        }y )N   zBufferedWindow.OnPaint())r   r   r   BufferedPaintDC_Bufferr   eventr$   s      r   r   zBufferedWindow.OnPaint=   s'    		!/0dll3r   c                     t        j                  dd       | j                         }t        |d   d      }t        |d   d      }t	        ||      | _        | j                          y )Nr'   zBufferedWindow.OnSize()r      r	   )r   r   GetClientSizemaxr   r)   UpdateDrawing)r   r+   sizewhs        r   r   zBufferedWindow.OnSizeB   s]    		!./ !!#
 QQ"1a(r   c                 <    | j                   j                  ||       y r!   )r)   SaveFile)r   FileNameFileTypes      r   
SaveToFilezBufferedWindow.SaveToFileR   s     	h1r   c                     t        j                         }|j                  | j                         | j	                  |       ~| j                          | j                          y)a_  
        This would get called if the drawing needed to change, for whatever reason.

        The idea here is that the drawing is based on some data generated
        elsewhere in the system. If that data changes, the drawing needs to
        be updated.

        This code re-draws the buffer, then calls Update, which forces a paint event.
        N)r   MemoryDCSelectObjectr)   r%   RefreshUpdater#   s     r   r0   zBufferedWindow.UpdateDrawingX   s?     [[]
%		"r   N)__name__
__module____qualname____doc__r   r%   r   r   r   BITMAP_TYPE_PNGr8   r0   r"   r   r   r   r      s0     
4
  -/,>,> 2r   r   c                       e Zd Zej                  ej
                  ej                  z  ej                  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y)AnimationWindowc                    t        j                  dd       t        dd      | _        || _        t               | _        d | _        d | _        dx| _	        | _
        d | _        t        j                  | |||       | j                  t        j                          | j#                  t        j$                         | j'                  t        j(                  | j*                         | j'                  t        j,                  | j.                         y )Nr   zAnimationWindow.__init__()r	   r   )parentidr   )r   r   r   bitmaprF   r   _pdc_overlay_tmpMousePosxybitmap_overlayr   r   SetBackgroundColourr   BLACKSetBackgroundStyleBG_STYLE_CUSTOMr   r   r   EVT_MOUSE_EVENTSOnMouseEvents)r   rF   rG   r   s       r   r   zAnimationWindow.__init__k   s     			!12!!Q'J	 "V%H  * 2 23		"++t{{+		"%%t'9'9:r   c                     t        j                  dd       |j                          | j                  j	                         dkD  r3|j                  | j                  | j                  | j                         yy)zDraws bitmap.r'   zAnimationWindow.Draw()r	   )rL   rM   N)r   r   ClearrH   GetWidth
DrawBitmaprL   rM   r#   s     r   r%   zAnimationWindow.Draw   sR    		!-.

;;!A%MM$++466M: &r   c                     t        j                  dd       t        j                  | |       |r|j	                          y y )Nr'   zAnimationWindow.OnSize())r   r   r   r   Skip)r   r+   s     r   r   zAnimationWindow.OnSize   s2    		!/0dE*JJL r   c                 ~   |j                         \  }}| j                         \  }}t        ||z
        dkD  st        ||z
        dkD  rt        ||f||f      }t	        |      }|j                  |d   |d          |d   | _        |d   | _        t        |      }| j                  rt	        | j                        }|j                  |j                         |d   z  |j                         |d   z         | j                  t        |      | j                  d   | j                  d   	       |S d| _        d| _        |S )
z>!If the bitmap has different size than the window, rescale it.r'   widthheightrL   rM   scaler   r	   )xpercyperc)GetSizer.   absr
   r   RescalerL   rM   r   rJ   rN   rW   	GetHeight_setOverlayperc)r   rH   bWbHwWwHparamsims           r   _rescaleIfNeededz AnimationWindow._rescaleIfNeeded   s+   !B##%BrBw<!s27|a/&Bx"b:F (BJJvgx(89C[DFC[DF$R(F}}$T%8%89

KKMF7O3R\\^fWo5U   #B'tyy|499Q< !   DFDFr   c                 t    | j                  |      }| j                  |k(  ry|| _        | j                          y)z[Draws bitmap.
        Does not draw the bitmap if it is the same one as last time.
        N)rm   rH   r0   )r   rH   s     r   rX   zAnimationWindow.DrawBitmap   s6     &&v.;;& r   c           
         | j                   j                          | j                   j                  d       | j                   j                  | j                  ||       | j                   j                  dt        ||| j                  j                         | j                  j                                      | j                   j                          y )Nr	   )bmprL   rM   )
rI   BeginDrawingSetIdrX   rJ   SetIdBoundsr   rW   rd   
EndDrawing)r   rL   rM   s      r   DrawOverlayzAnimationWindow.DrawOverlay   s    		 				!q9		tAq$--002DMM4K4K4MN	
 			r   c                     | j                   r| j                  j                          || _         | j                         }||d   z  }||d   z  }| j	                  ||       y )Nr   r	   )rJ   rI   	RemoveAllr.   ru   )r   rH   r_   r`   r1   rL   rM   s          r   re   zAnimationWindow._setOverlay   sX    ==II!!!#DGODGOAr   c                     t        j                  dd       |r$| j                  |||       || _        ||f| _        n(d| _        | j                  j                          d| _        | j                          y)zSets overlay bitmap (legend)

        :param bitmap: instance of wx.Bitmap
        :param xperc: x coordinate of bitmap top left corner in % of screen
        :param yperc: y coordinate of bitmap top left corner in % of screen
           zAnimationWindow.SetOverlay()N)	r   r   re   rN   rf   rJ   rI   rw   r0   )r   rH   r_   r`   s       r   
SetOverlayzAnimationWindow.SetOverlay   sf     			!34VUE2"(DDI DMII!"&Dr   c                     t        j                  dd       d| _        d| _        | j                  j                          | j                          y)zClear overlay (legend)ry   zAnimationWindow.ClearOverlay()N)r   r   rJ   rN   rI   rw   r0   )r   s    r   ClearOverlayzAnimationWindow.ClearOverlay   s<    		!56"		r   c                     t        j                  dd       t        j                  | | j                        }| j
                  r| j                  j                  |       y y )Nr'   zAnimationWindow.OnPaint())r   r   r   r(   r)   rJ   rI   DrawToDCr*   s      r   r   zAnimationWindow.OnPaint   sE    		!01dll3==IIr" r   c                 
   |j                         }|j                         r>d| _        | j                  j	                  |d   |d   d      }d|v rd| _        || _        y|j                         rd| _        d| _        y|j                         rv| j                  y|d   | j
                  d   z
  }|d   | j
                  d   z
  }| j                  j                  | j                  ||       | j                          || _        yy)zHandle mouse events.Nr   r	   
   )radius)
GetPositionLeftDown_dragidrI   FindObjectsrK   LeftUpDraggingTranslateIdr0   )r   r+   currentidlistdxdys         r   rT   zAnimationWindow.OnMouseEvents   s     ##%>>DLYY**71:wqz"*MFF{  'D\\^DL $D^^||#d//22Bd//22BII!!$,,B7  'D r   c                 x    | j                   j                  d      }|j                         |j                         fS )zReturns x, y position in pixelsr	   )rI   GetIdBoundsGetXGetY)r   rects     r   GetOverlayPoszAnimationWindow.GetOverlayPos   s-    yy$$Q'yy{DIIK''r   N)r>   r?   r@   r   ID_ANYDEFAULT_FRAME_STYLEFULL_REPAINT_ON_RESIZEBORDER_RAISEDr   r%   r   rm   rX   ru   re   rz   r|   r   rT   r   r"   r   r   rD   rD   j   sf     99$$r'@'@@2CSCSS	;,;0	$#(0(r   rD   )rA   r   
core.debugr   gui_core.wrapr   r   r   r   r   utilsr
   r   r   rD   r"   r   r   <module>r      s<   " 
  W W $ORYY OdW(n W(r   