
    ը	fy_                         d Z ddlZddlZddlmZmZmZ ddlmZm	Z	m
Z
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mZmZ dd
lmZmZmZmZmZmZm Z m!Z! ddl"m#Z#  G d dejH                        Z%y)aG  
@package animation.controller

@brief Animations management

Classes:
 - controller::AnimationController

(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)
GExceptionGErrorGMessage)writeAviwriteGifwriteImswriteSwf)gThread)UserSettings)
EmptyImageImageFromBitmap)TemporalManager)InputDialog
EditDialogExportDialog)TemporalModeTemporalTypeOrientation
RenderTextWxImageToPilsampleCmdMatrixAndCreateNameslayerListToCmdsMatrixHashCmds)AnimationDatac                      e Zd Zd Zd Zd Zd Z eee      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 eee      Zd Zd Zd Zd Zd Zd Zd'dZd'dZ d Z!d Z"d  Z#d! Z$d" Z%d# Z&d$ Z'd% Z(d& Z)y)(AnimationControllerc                     t         j                  j                          | _        | _        | _         j
                  d    _        d  _        d  _        g  _	        t        j                   t         j                         _        | _        | _        | _        | _        t#         j                   j                        D ]L  \  }}	|j%                  |	f fd	       |j'                  |	f fd	       |j)                   j*                         N  j
                  j-                         D ]S  }
|
j/                   j0                         |
j3                   j4                         |
j7                   j8                         U d  _        t=                _         jA                  t         jB                   jD                   j                         d _#        i  _$        y )Ntemporal)idc                 *    j                  | ||      S N)UpdateFrameindexdataIdwinselfs      5/usr/lib/grass83/gui/wxpython/animation/controller.py<lambda>z.AnimationController.__init__.<locals>.<lambda>@   s    t/?/?sF/S     c                 *    j                  | ||      S r!   )UpdateFrameEndr#   s      r(   r)   z.AnimationController.__init__.<locals>.<lambda>C   s    t/B/B5#v/V r*      )%wx
EvtHandler__init__
mapwindowsframeslidersslideranimationToolbartemporalModeanimationDataTimerID_ANYtimer
animations
bitmapPoolmapFilesPoolbitmapProviderzipSetCallbackUpdateFrameSetCallbackEndAnimationSetCallbackOrientationChangedOrientationChangedInReverseModevaluesSetCallbackSliderChangingSliderChangingSetCallbackSliderChangedSliderChangedSetCallbackFrameIndexChangedChangeFramerunAfterReleasingSliderr   temporalManagerBind	EVT_TIMEROnTimerTicktimeTick_dialogs)r'   r2   r3   r;   r1   providerr<   r=   animr&   r4   s   `          r(   r0   zAnimationController.__init__)   s    	t$$
ll:. $ XXdryy1
$$(&T__doo> 	UID#''*-S ((*-V ..t/S/ST	U ll))+ 	BF,,T-@-@A++D,>,>?//0@0@A	B
 (,$.0		",, 0 0$**=r*   c                     || _         y r!   )r5   )r'   toolbars     r(   SetAnimationToolbarz'AnimationController.SetAnimationToolbarU   s
     'r*   c                     | j                   S r!   )	_timeTickr'   s    r(   GetTimeTickzAnimationController.GetTimeTickX   s    ~~r*   c                     || _         | j                  j                         r?| j                  j                          | j                  j	                  | j                          | j                          y r!   )rX   r:   	IsRunningStopStartDisableSliderIfNeeded)r'   values     r(   SetTimeTickzAnimationController.SetTimeTick[   sI    ::!JJOOJJT^^,""$r*   )fgetfsetc                 F    | j                   D ]  }|j                           y r!   )r;   Update)r'   eventrS   s      r(   rO   zAnimationController.OnTimerTickd   s    OO 	DKKM	r*   c                 :   | j                   D ]<  }| j                  j                         r|j                          |j	                          > | j                  j                         s6| j                  j	                  | j
                         | j                          y y r!   )r;   r:   r\   NextFrameIndexr^   rP   r_   r'   rS   s     r(   StartAnimationz"AnimationController.StartAnimationh   su     OO 	Dzz##%##%JJL	 zz##%JJT]]+&&( &r*   c                 t   |rE| j                   j                         rz| j                   j                          | j                          nO| j                   j                         s5| j                   j	                  | j
                         | j                          | j                  D ]  }|j                  |        y r!   )r:   r\   r]   r_   r^   rP   r;   Pause)r'   pausedrS   s      r(   PauseAnimationz"AnimationController.PauseAnimationt   s    zz##%

!**,::'')

  /**,OO 	DJJv	r*   c                     | j                   j                         r*| j                   j                          | j                          | j                  D ]  }|j                           y r!   )r:   r\   r]   r_   r;   ri   s     r(   EndAnimationz AnimationController.EndAnimation   sH    ::!JJOO&&(OO 	DIIK	r*   c                     | j                   j                         r*| j                   j                          | j                          | j                  j                          | j                  |||       y r!   )r:   r\   r]   r_   r5   r"   )r'   r$   r&   r%   s       r(   r,   z"AnimationController.UpdateFrameEnd   sP    ::!JJOO&&(""$V,r*   c                     | j                   j                  |      }t        j                  ddddg      s|#|j	                  |       n|j	                  |       | j
                  j                  |       y )N	animationr   nodataenablegroupkeysubkey)r>   	GetBitmapr   Get
DrawBitmapr4   r"   )r'   r$   r&   r%   bitmaps        r(   r"   zAnimationController.UpdateFrame   sd    $$..v6:x6J
 !v&NN6"&r*   c                     | j                   | j                  j                         | _         | j                  d       | j	                  |       y )NT)rK   r:   r\   rn   rJ   )r'   r$   s     r(   rF   z"AnimationController.SliderChanging   s?    ''/+/::+?+?+AD(D!r*   c                 L    | j                   r| j                  d       d | _         y )NF)rK   rn   rY   s    r(   rH   z!AnimationController.SliderChanged   s!    ''&'+$r*   c                 H    | j                   D ]  }|j                  |        y r!   )r;   FrameChangedFromOutside)r'   r$   rS   s      r(   rJ   zAnimationController.ChangeFrame   s#    OO 	0D((/	0r*   c                     | j                   j                         r+| j                  dk  r| j                  j	                  d       y | j                  j	                  d       y )Nd   FT)r:   r\   rX   r4   EnableSliderrY   s    r(   r_   z)AnimationController.DisableSliderIfNeeded   sA    ::!dnns&:KK$$U+KK$$T*r*   c                     |t         j                  k(  r| j                  j                          y |t         j                  k(  r| j                  j                          y y r!   )r   FORWARDr5   PlayForwardBACKWARDPlayBackr'   modes     r(   rC   z3AnimationController.OrientationChangedInReverseMode   sG    ;&&&!!--/[)))!!**, *r*   c                 4    | j                   D ]	  }||_         y r!   )r;   
replayModer'   r   rS   s      r(   SetReplayModez!AnimationController.SetReplayMode   s    OO 	#D"DO	#r*   c                 4    | j                   D ]	  }||_         y r!   )r;   orientationr   s      r(   SetOrientationz"AnimationController.SetOrientation   s    OO 	$D#D	$r*   c                     || _         y r!   _temporalModer   s     r(   SetTemporalModez#AnimationController.SetTemporalMode   s
    !r*   c                     | j                   S r!   r   rY   s    r(   GetTemporalModez#AnimationController.GetTemporalMode   s    !!!r*   c                 r    | j                   t        j                  k(  r| j                  j	                         S y r!   )r6   r   TEMPORALrL   GetGranularityrY   s    r(   GetTimeGranularityz&AnimationController.GetTimeGranularity   s.     5 55''6688r*   c                 $    | j                          y)zIUsed sofar for updating slider time labels
        after change of formatN)_setAnimationsrY   s    r(   UpdateAnimationsz$AnimationController.UpdateAnimations   s     	r*   c                    | j                          t        | j                  | j                  | j                  t        | j                              }|j                          |j                         t        j                  k(  r|j                          y |j                         \  | _        | _        | _        |j                          | j                          y )N)parentevalFunctionr7   maxAnimations)rp   r   r2   EvaluateInputr7   lenr;   CenterOnParent	ShowModalr.   	ID_CANCELDestroy	GetResultr6   rL   r   r'   dlgs     r(   EditAnimationsz"AnimationController.EditAnimations   s     	::++,,doo.	
 	==?bll*KKMFImmoCD-t/Cr*   c                 |   d}| j                   D cg c]  }|j                   }}t        t        | j                              D ]
  }||vsd} n |s7t        | j                  t        d      t        | j                        z         y | j                          t               }t        | j                  D cg c]  }|j                         s| c}      }|j                  |       t        | j                  d|      }|j                          |j                         t        j                   k(  r!|j#                          |j%                          y |j%                          |j                  |v r!t        | j                  t        d             y 	 | j'                  | j                   |gz         \  }}	|| _        |	| _        |j                  }| j                   j3                  |       | j5                          y c c}w c c}w # t(        $ r,}
t+        | j                  |
j,                  d       Y d }
~
y d }
~
ww xY w)	NFTz#Maximum number of animations is %d.r   messageadd)r   r   r7   zXMore animations are using one window. Please select different window for each animation.r   r   showTraceback)r7   windowIndexranger   r;   r   r2   _rp   r   IsActiveSetDefaultValuesr   r   r   r.   r   UnInitr   r   r   r   r`   r6   rL   appendr   )r'   foundrS   indicesr   animDataanimationIndexr   r6   tempManageres              r(   AddAnimationz AnimationController.AddAnimation   s   040B0BC4##CC T__!56 	K')	
 zz?@3tCWW 
 	 !?tRt$--/dRS!!+~>%xP==?bll*JJLKKM7*zzJ 	(,(:(:""hZ/)%L+ )***!!(+k D* S.  	$**aggUK	s)   G<7HH"H 	H;"H66H;c           	      \   	 g }t        t        | j                              D ]I  }||   s	t               }|j	                  ||       |j                  ||          |j                  |       K 	 	 | j                  |      \  }}|| _        || _        || _        | j'                          y# t        t        t        f$ r5}t        | j                  t        |      dt        d             Y d}~yd}~ww xY w# t        $ r,}t        | j                  |j                  d       Y d}~yd}~ww xY w)zTSet animation data directly.

        :param layerLists: list of layerLists
        FzInvalid input)r   r   r   captionNr   )r   r   r;   r   r   SetLayerListr   r   
ValueErrorIOErrorr   r2   strr   r   r`   r7   rL   r6   r   )r'   
layerListsr7   irS   r   r6   r   s           r(   SetAnimationsz!AnimationController.SetAnimations!  s   
	M3t/0 /a=(?D))!Q/%%jm4!((./	(,(:(:=(I%L+ +*(! J0 	zzA#/*	 	  	$**aggUK	s5   (B* AB* 0C6 *C3>+C..C36	D+?"D&&D+c                 P   | j                   D cg c]  }|j                   }}| j                  |       | j                  t        j
                  k(  r| j                  j                         \  }}nd\  }}| j                   D ]S  }|j                  dk(  rt        |j                        |_        -|j                         d   D cg c]  }|f c}|_        U | j                  |       | j                  ||       | j                          | j!                          y c c}w c c}w )N)activeIndices)NN2dcommands)
timeLabels)r   mapNamesDict)r7   r   _updateWindowsr6   r   r   rL   GetLabelsAndMapsviewModer   	layerList	cmdMatrixGetNvizCommands_updateSlider_updateAnimations_updateBitmapDatarp   )r'   rS   r   r   r   cmds         r(   r   z"AnimationController._setAnimationsA  s
   040B0BC4##CC'2 5 55'+';';'L'L'N$J'1$J&& 	XD}}$!6t~~!F484H4H4J:4V!WS3&!W		X
 	j1W<P 
 	) D "Xs   D
D#Nc                 x   | j                   t        j                  k(  rd| j                  j	                  d       | j
                  d   | _        | j                  d   j                  }| j                  j                  |       y | j                   t        j                  k(  rz| j                  j	                  d       | j
                  d   | _        | j                  j                  | j                  j                         | j                  j                  |       y | j                  j	                  d        d | _        y )Nnontemporalr   r   )r6   r   NONTEMPORALr2   	SetSliderr3   r4   r7   mapCount	SetFramesr   SetTemporalTyperL   temporalType)r'   r   
frameCounts      r(   r   z!AnimationController._updateSliderX  s     8 88JJ  /,,}5DK++A.77JKK!!*-,"7"77JJ  ,,,z2DKKK''(<(<(I(IJKK!!*-JJ  &DKr*   c                    | j                   t        j                  k(  rt        t	        | j
                              D ]  }||vr| j
                  |   j                  d       &| j                  D cg c]  }|j                  |k(  s| c}d   }|j                         }|j                  dk(  rd gt	        |      z  }| j
                  |   j                  t        |j                  |      D cg c]  \  }}t        ||       c}}       | j
                  |   j                  d        y t        t	        | j
                              D ]  }||vr| j
                  |   j                  d       &| j                  D cg c]  }|j                  |k(  s| c}d   }|j                         }|j                  dk(  rd gt	        |      z  }t        |j                  ||j                   d      |      }| j
                  |   j                  |       | j
                  |   j                  d        y c c}w c c}}w c c}w )NFr   3dT)r6   r   r   r   r   r;   	SetActiver7   r   
GetRegionsr   r   r?   r   r   r   firstStdsNameType)	r'   r   r   r   rS   regionscmdListregionidentifierss	            r(   r   z%AnimationController._updateAnimationsg  s    8 883t/0 3M)OOA&007)-););Ut?O?OST?TUVWX//+==D(#fs7|3G",, 034>>7/K+GV !&1 ",,T23  3t/0 3M)OOA&007)-););Ut?O?OST?TUVWX//+==D(#fs7|3G;NNL1G1G1J$KW ",,[9",,T23 V
 Vs   1H3H3/H8 H>H>c                 8   t        t        | j                              D ]y  }| j                  j	                  |      s ||v r| j                  j                  |       >| j                  j	                  |      sZ||vs_| j                  j                  |       { y r!   )r   r   r;   r2   IsWindowShown	AddWindowRemoveWindow)r'   r   r   s      r(   r   z"AnimationController._updateWindows  sw     T__!56 
	5KJJ,,[9=0

$$[1

((5}4

''4
	5r*   c                    | j                   j                          | j                  D ]E  }|j                  dk(  r| j	                  |       n| j                  |       | j                  |       G t        j                  ddd      }t        j                  ddd      }| j                   j                  ||       | j                  j                          | j                  j                          y )	Nr   rs   bgcolorcolorrv   nprocsr`   )r   r   )r>   Unloadr7   r   
_set2DData_load3DData_loadLegendr   r{   Loadr<   Clearr=   )r'   r   r   cpuss       r(   r   z%AnimationController._updateBitmapData  s    ""$ ** 	'H  D()  *X&	'   {	'RkxP  e <!r*   c                     |j                   D cg c]  }|j                  s|j                   }}|j                         }| j                  j                  |j                  ||       y c c}w r!   )r   activeopacityr   r>   SetCmdsr   )r'   r7   layer	opacitiesr   s        r(   r   zAnimationController._set2DData  sW    0=0G0GXu5<<U]]X	X**,##M$;$;YP Ys
   A)A)c                 h    |j                         }| j                  j                  |d   |d          y )Nr   r   )r   r>   	SetCmds3D)r'   r7   nvizs      r(   r   zAnimationController._load3DData  s/    ,,.%%d:&6XGr*   c                 `   |j                   r	 | j                  j                  |j                         }	 ddlm} |j                   D ]\  }|j                  d      s|j                  d      d   j                  d      \  }}}}t        |      dz  dt        |      dz  z
  }
}	 n | j                  |j                     j                  |	
       y | j                  |j                     j                          y # t        $ r d\  }	}
Y cw xY w# t        $ r t        t        d	      
       Y y w xY w)Nr   )Imageat=   ,      Y@)r   r   zFailed to display legend.)r   )	legendCmdr>   LoadOverlayPILr  
startswithsplitfloatImportErrorr1   r   
SetOverlayr   r   r   ClearOverlay)r'   r7   r}   r  parambtlrxys              r(   r   zAnimationController._loadLegend  s   ""?,,889P9PQ	 )!.!8!8 " ++D1).S)9!)<)B)B3)GJAq!Q#(8e#3QqE9I5IqA!	"  9 9:EEfaQRS OOM556CCE #  DAq   ?q!<=>?s;   %D &C8 A
C8 %*D 8D	D D		D D-,D-c                    d}d}t               }d }g }|D ]  }|j                  D ]Y  }|j                  st        |d      s|j                  dv r|dz  }n|dz  }|j                  t        |j                               [ |j                  |j                          |r|rt        j                  }	n(|rt        j                  }	n|rt        j                  }	nd }	|	t        j                  k(  r&t        |      dkD  rt        t        d            |	|fS |	t        j                  k(  rSt               }|D ]  } |j                   |j"                     |j%                         }
|
rt'        | j(                  |
       |	|fS )Nr   maps)strdsstvdsstr3dsr	  z5Inconsistent number of maps, please check input data.r   )setr   r   hasattrmapTyper   r   r  r   r   r   r   r   r   r   r   AddTimeSeriesr   EvaluateInputDatar   r2   )r'   r7   stdsr  r   r   r   rS   r   r6   r   s              r(   r   z!AnimationController.EvaluateInput  ss   5! 	1D 2<<GE6$:}}(DD		LLUZZ12 t//0	1 D'33L'33L'00LL<3338}q  MN  [(( \222)+K% C)))4+A+ABC "335G

G<[((r*   c                     | j                          t        j                  ddd      }t        j                  ddd      }| j                  j	                  ||d       | j                          y )	Nrs   r   r   rv   r   r`   T)r   r   force)rp   r   r{   r>   r   )r'   r   r   s      r(   ReloadzAnimationController.Reload  s]      {	'RkxP  e4 Hr*   c                    | j                   s!t        | j                  t        d             y d| j                  v r;| j                  d   j                          | j                  d   j                          y t        | j                  | j                  | j                        }|j                          |j                  j                  | j                         || j                  d<   |j                          y )NzNo animation to export.r   export)r   rP   )r7   r   r2   r   rQ   ShowRaiser   r6   rP   r   doExportconnect_exportr   s     r(   ExportzAnimationController.Export  s    !!DJJ2K0LMt}}$MM(#((*MM(#))+

T%6%6C  LL  .&)DMM(#HHJr*   c           
           j                   j                  j                         } j                  t        j
                  k(  r) j                  j                         \  }}t        |      }n j                  d   j                  }g }g }g }	 j                  D 
cg c]  }
|
j                   }}
t        t         j                   j                              D ]j  \  }\  }}
|
j!                         s|j#                         }|j%                  |       |j%                  |j                                |	j%                  |       l g }t'        j(                  t+        d       j                         }t'        j,                         j/                          i }t1        j2                  ddd      }t1        j2                  ddd      }t5        |      D ]  }t7        | }|j9                  dddd	d	d	       |	D ]V  } j                  |   j;                  |      }t1        j2                  dd
ddg      s?|! j<                  j?                  |      }|||<   n<||vr8t'        j@                         ||<   n  j<                  j?                  |      }|||<   tC        ||         }||   }|ri j<                  jE                  |      } j                  |   jG                         \  }}tC        |      }|jI                          |jK                  |||       |j                         ||   k7  r |jL                  ||      |jJ                  |g||     Y |D ]*  }tO        |d   d   dz  |d   z        }tO        |d   d   dz  |d   z        }|d   dk(  rt'        jP                  |d         }n|d   dk(  r|   } | d   rt+        d      | d   d| d   dz  }!nH j                  jS                         tT        jV                  k(  r| d   }!nt+        d      | d   | d   dz  }!tY        |!|d   ||      j[                         }n,|d   dk(  r$|d   }!tY        |!|d   ||      j[                         }|jK                  ||       - |j%                  |        ~|D cg c]  }t]        |       }"}t'        j(                  t+        d       j                          _/        t'        j,                         j/                          	  fd}#|d   dk(  rKt`        jb                  je                  |d   |d    d!z   |d"   jg                         z         }$ti        |$|"#       n|d   d$k(  r)tk        |d   |" jl                  to        d%      z  d&'       n|d   d(k(  r)tq        |d   |" jl                  to        d%      z  d&'       nN|d   d)k(  rFts               }%|%ju                  tv        |d   |" jl                  to        d%      z  |d*   |d+   d&|#,       |d   d.v r `/y y c c}
w c c}w # tx        $ r,}& `/t{         j                   t}        |&      -       Y d }&~&y d }&~&ww xY w)/Nr   z Preparing export, please wait...)r   rs   fontfgcolorrv   r      r   rt   ru   posr  r	  nameimagefiletimez%(from)s %(dash)s %(to)su   –)fromdashtoz%(start)s %(unit)s   )startunittextz#Exporting animation, please wait...c                 V    | j                   }`|rt        j                  |       y y )Nr   )retbusyr   r2   )rf   errorr'   s     r(   export_avi_callbackz8AnimationController._export.<locals>.export_avi_callbackw  s*    		I$**e< r*   methodsequence	directoryprefix.format)filenameimagesgifi  T)rM  rN  durationrepeatswfaviencodingoptions)callablerM  rN  rP  rT  inputOptionsbg_taskondoner   )rH  rO  rR  )?r2   animationPanelGetSizer6   r   r   rL   r   r   r7   r   r  	enumerater?   r1   r;   r   GetPositionr   r.   BusyInfor   GetAppYieldr   r{   r   r   ReplaceGetFramer>   rz   
NullBitmapr   r  GetOverlayPosConvertAlphaToMaskPasteRescaleintr  GetTemporalTyper   ABSOLUTEr   ConvertToImager   rD  ospathjoinlowerr   r   rP   r  r	   r
   Runr   	Exceptionr   r   )'r'   
exportInfodecorationssizer   r   r   animWinSize
animWinPosanimWinIndexrS   legendsr   r&   r6  rN  rD  lastBitmapsr4  r   
frameIndexr8  frameIdr}   imlegendlegendBitmapr  r  legImage
decorationdecImage	timeLabelrA  	pilImagesrF  rM  threadr   s'   `                                      r(   r0  zAnimationController._export	  s@   zz((002 5 55'+';';'L'L'N$JZJ++A.77J
.2.@.@Ad4>>AA'DOOT__(MN 	'NA{T}}oo'!!#&""3;;=1##A&	' {{1?@T
		""&S""&S
+ I	!J%EMM!Q3S1! 0//!,55jA#''%:x>R *!%!4!4!>!>w!G)/AK/-/]]_KN!00::7CF%+KN$[^4 !#'#6#6#B#B6#JL??1-;;=DAq.|<H //1HHXq!,::<;q>1BJJA/B/A/=0@ * #,

5)!,u4tAw>?
5)!,u4tAw>?f%0!xx
6(:;H'61 *: 6I | !;<$-aL$,"+A,@   !00@@B+445 $-Q<D#$%9#:)21(1!> $D
  *j0'7 $n&   '61%f-D)j0'7 $n&  Ha+G#,J MM% SI	!T  7==U\%(=	=KK34TZZ
	 			,	 (#z177<<{+x(3.H1E1K1K1MM (9=H%.'/$!]]U4[8	 H%.'/$!]]U4[8	 H%. 

%'/$!]]U4[8'
3!+I!6 .  	 h#==	 >] Bx >\  		$**c!f5	s%   XX<DX 	Y""Y		Yr!   )*__name__
__module____qualname__r0   rV   rZ   ra   propertyrP   rO   rj   rn   rp   r,   r"   rF   rH   rJ   r_   rC   r   r   r   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)  r1  r0   r*   r(   r   r   (   s    *X(% [{;H
)-	' ,
0+-#$"" GL
(8t@.3@5"$QHF(()T"Zr*   r   )&__doc__rl  r.   	core.gcmdr   r   r   grass.imagingr   r   r   r	   core.gthreadr
   core.settingsr   gui_core.wrapr   r   animation.temporal_managerr   animation.dialogsr   r   r   animation.utilsr   r   r   r   r   r   r   r   animation.datar   r/   r   r  r*   r(   <module>r     sV    
 	 2 2 @ @   & 5 6 C C	 	 	 ){	"-- {	r*   