
    ը	fD+                    >   d Z ddlmZ ddlZddlZddlZddlZddl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mZmZmZ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) 	 ddl*m+c m,Z- dZ. G d de#e      Z1y# e/e0f$ r dZ.Y w xY w)a}  
@package mapwin.mapwindow

@brief Map display canvas - buffered window.

Classes:
 - mapwindow::BufferedWindow
 - mapwindow::GraphicsSet
 - mapwindow::GraphicsSetItem

(C) 2006-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 Martin Landa <landa.martin gmail.com>
@author Michael Barton
@author Jachym Cepicky
@author Stepan Turek <stepan.turek seznam.cz> (handlers support, GraphicsSet)
@author Anna Petrasova <kratochanna gmail.com> (refactoring)
@author Vaclav Petras <wenzeslaus gmail.com> (refactoring)
    )print_functionN)Signal)wxPythonPhoenix)SavedRegion)	DragImagePseudoDCEmptyBitmapBitmapFromImageWindowMenuRectNewId)
RunCommand
GExceptionGError)Debug)UserSettings)MapWindowBase)GraphicsSet)gThreadTFc                   2   e Zd ZdZej
                  dej                  fdZd Zd Z	d Z
d Zd Zd	 Zd
 Zdddg dddfdZdLdZd Zd Zd ZdMdZdMdZd Zd ZdNdZd ZdOdZd Zd Zd ZdPdZdNdZd Z d Z!d  Z"d! Z#dQd"Z$dRd#Z%dMd$Z&	 	 	 	 	 	 dSd%Z'dRd&Z(dRd'Z)dRd(Z*d) Z+d* Z,d+ Z-d, Z.d- Z/d. Z0d/ Z1d0 Z2d1 Z3d2 Z4d3 Z5d4 Z6d5 Z7d6 Z8dMd7Z9d8 Z:d9 Z;d: Z<d; Z=d< Z>d= Z?d> Z@dTd?ZAd@ ZBdA ZCdB ZDdC ZEdNdDZFdNdEZGdF ZHdG ZIdNdHZJdI ZK	 dUdJZLdK ZMy)VBufferedMapWindowa4  A Buffered window class (2D view mode)

    Superclass for VDigitWindow (vector digitizer).

    When the drawing needs to change, you app needs to call the
    UpdateMap() method. Since the drawing is stored in a bitmap, you
    can also save the drawing to file by calling the
    SaveToFile() method.
    Nc           	         t        j                  | |||       t        j                  j                  | f|||d| | j	                  t        j
                  t        j                  ddd              || _        d| _	        d| _
        d| _        d| _        d| _        d| _        g | _        d| _        d| _        t%               | _        d| _        d| _        d	| _        dx| _        | _        t3        d
      | _        t3        d      | _        t3        d      | _        t3        d      | _        t3        d      | _        t3        d      | _        t3        d      | _         t3        d      | _!        t3        d      | _"        t3        d      | _#        t3        d      | _$        | jK                  t        jL                  | jN                         | jK                  t        jP                  | jR                         | jK                  t        jT                  | jV                         | jY                          d| _-        d| _.        || _/        i | _0        i | _1        i | _2        g | _3        d	| _4        d| _5        d| _6        d| _7        | jq                          d| _9        tu        tw        d| jx                  jz                        tw        d| jx                  j|                              | _?        | jK                  t        j                  d        | jx                  j                  j                  | j                         | jx                  j                         j                  j                  | j                         d| _G        d| _H        g | _I        i | _J        y)aK  
        :param parent: parent window
        :param giface: grass interface instance
        :param map: map instance
        :param properties: instance of MapWindowProperties
        :param id: wx window id
        :param style: wx window style
        :param kwargs: keyword arguments passed to MapWindow and wx.Window
        )parentgifaceMap)r   idstyledisplaybgcolorcolorgroupkeysubkeyNFr   zBufferedWindow.zoomChangedzBufferedWindow.mapQueriedz%BufferedWindow.zoomHistoryUnavailablez#BufferedWindow.zoomHistoryAvailablezBufferedWindow.mouseEnteredz!BufferedWindow.mouseLeftUpPointerzBufferedWindow.mouseLeftUpzBufferedWindow.mouseRightUpzBufferedWindow.mouseLeftDownzBufferedWindow.mouseDClickzBufferedWindow.mouseMovingT   
      c                      y N xs    0/usr/lib/grass83/gui/wxpython/mapwin/buffered.py<lambda>z,BufferedMapWindow.__init__.<locals>.<lambda>   s        r   r   )Kr   __init__wxr   SetBackgroundColourColourr   Get_propertiesdigitresizedragimgalwaysRenderpenpolypen
polycoordslineidplineidr   renderTimingThr
timerRunIdlastUpdateMapRequpdDelayrenderrenderVectorr   zoomChanged
mapQueriedzoomHistoryUnavailablezoomHistoryAvailablemouseEnteredmouseLeftUpPointermouseLeftUpmouseRightUpmouseLeftDownmouseDClickmouseMovingBind	EVT_PAINTOnPaintEVT_SIZEOnSizeEVT_IDLEOnIdle_bindMouseEventsprocessMouseimgoverlays	imagedictselecttextdictzoomhistorycurrzoomzoomtype	hitradiusdialogOffset_definePseudoDC	redrawAllr	   maxr   widthheight_bufferEVT_ERASE_BACKGROUNDlayerChangedconnectOnUpdateMapGetRenderMgr
renderDone_updateMFinisheddragidlastposgraphicsSetList_extraContextActions)	selfr   r   r   
propertiesr   r\   r   kwargss	            r.   r2   zBufferedMapWindow.__init__L   s   ( 	tF6sK
		4M2UMfM 	  II|''iYwWX	
 &
  	
     'y $*//d' "">? !!<= '--T&U#$*+P$Q! ##@A #))L"M!">?"#@A#$BC!">? "">? 			",,-		"++t{{+		"++t{{+     	 #3q$((..#93q$((//;RS		"))>: 	%%d&6&67**2243H3HI   " %'!r0   c                 $    | j                          y r*   )	UpdateMaprv   s    r.   rn   zBufferedMapWindow.OnUpdateMap   s     	r0   c                 b    | j                   j                  j                  | j                         y)z2Use when the class instance is hidden in MapFrame.N)r   rl   
disconnectrn   r{   s    r.   DisactivateWinz BufferedMapWindow.DisactivateWin   s     (()9)9:r0   c                 b    | j                   j                  j                  | j                         y)z6Used when the class instance is activated in MapFrame.N)r   rl   rm   rn   r{   s    r.   ActivateWinzBufferedMapWindow.ActivateWin   s     %%d&6&67r0   c                 l    t               | _        d| _        t               | _        t               | _        y)zDefine PseudoDC objects to useN)r   pdc	pdcVectorpdcTransparentpdcTmpr{   s    r.   re   z!BufferedMapWindow._definePseudoDC   s'     :&jjr0   c                     | j                  t        j                  | j                         | j                  t        j                  | j
                         | j                  t        j                  | j                         y r*   )rR   r3   EVT_MOUSE_EVENTSMouseActions
EVT_MOTIONOnMotionEVT_CONTEXT_MENUOnContextMenur{   s    r.   rY   z"BufferedMapWindow._bindMouseEvents   sL    		"%%t'8'89		"--/		"%%t'9'9:r0   c                 (    ||d| j                   |<   y)zRegister context menu item.

        :param name: action name
        :param label: callback function returning label
        :param action: handler
        )labelactionN)ru   )rv   namer   r   s       r.   RegisterContextActionz'BufferedMapWindow.RegisterContextAction  s     5:V*L!!$'r0   c                 l     j                   r|j                          yt               }t         d      sEt	                _         j                  t        j                   j                   j
                         |j                   j
                  t        d              j                  r|j                           j                  j                         D ]q  \  }}t         |      s<t	               }t         ||        j                  t        j                  |d   |       |j                  t!         |       |d                 s  j#                  |j%                               } j&                  j)                  |d   |d    j*                         j,                  rlriD cg c]*  }|t/         j,                  j1                               v s)|, c}r2|j                          t	               } j                  t        j                   fd	|       |j                  | j,                  d      j2                          j,                  d      j4                  d
k(  rKt	               }	 j                  t        j                   fd|	       |j                  |	t        d             t	               }
 j                  t        j                   fd|
       |j                  |
 j,                  d      j6                          j9                  |       |j;                          yc c}w )zShow Map Display context menuNpopupCopyCoordinates)r   zCopy coordinates to clipboardr   r   r   r&   c                 B    j                   j                  d         S Nr   	overlayId)overlayRemovedemitevtidlistrv   s    r.   r/   z1BufferedMapWindow.OnContextMenu.<locals>.<lambda>-  s    D//44vay4I r0   legendc                 D    j                   d      j                         S Nr   )r\   StartResizingr   s    r.   r/   z1BufferedMapWindow.OnContextMenu.<locals>.<lambda>7  s    fQi 8 F F H r0   zResize and move legendc                 B    j                   j                  d         S r   )overlayActivatedr   r   s    r.   r/   z1BufferedMapWindow.OnContextMenu.<locals>.<lambda>?  s    D11666K r0   )r8   Skipr   hasattrr   r   rR   r3   EVT_MENUOnCopyCoordinatesAppend_ru   AppendSeparatoritemssetattrgetattrScreenToClientGetPositionr   FindObjectsrc   r\   listkeysremoveLabelr   activateLabel	PopupMenuDestroy)rv   eventmenur$   action_dictaidposiremoveIdresizeLegendId
activateIdr   s   `          @r.   r   zBufferedMapWindow.OnContextMenu  s   ::JJL vt34(-D%IIbkk4#9#9d>W>WIXD--q1P/QR$$  " $ 9 9 ? ? A 	DC4%gc3'		"++{8'<	EKKc*,@K,@,BC	D !!%"3"3"56%%c!fc!fdnnEMM"Fqa40B0B0D+E&EF  "wHIII  
 KK$--q	":"F"FG }}VAY',,8!&		KKH%  
 NA.F,GHJIIK  
 KK
DMM&)$<$J$JKt; Gs   **L1L1image)r   r   r   r   c           
         |)|dk(  r|r| j                   |   }n|dk(  rd}n
t               }|s|dk(  r| j                  }n| j                  }|r$|dk(  rd| j                  | j                   |   d   <   |j                          |dk7  rt        j                  }n#t        j                  | j                               }|j                  |       t        j                  dd	|d
|d|       ||j                  |       |dk(  rbt        j                  }|j                  |       |j                          |j!                          |j#                          | j%                          y|dk(  r^t'        |      }	|	j)                         \  }
}|j+                  |	|d   |d   d       |j-                  |t/        |d   |d   |
|             n|dk(  r|r|s2t        j                  t        j0                  t        j2                        }|j5                  |       |j7                  |       t9        |d   |d         }t;        |d   |d         }t9        |d   |d         }t;        |d   |d         }||z
  }||z
  }t/        ||||      }|j=                  |       |j-                  ||       n|dk(  r|r|j5                  t        j                  t        j0                  t        j2                               |j7                  |       |j?                  t        j@                  |d   |d         t        j@                  |d   |d                |j-                  |t/        |d   |d   |d   |d                n|dv r|r|j5                  t        j                  t        j0                  t        j2                               |j7                  |       tC        |      dk  ry|dk(  r}d}|tC        |      k  r|j?                  t        j@                  ||dz
     d   ||dz
     d         t        j@                  ||   d   ||   d                |dz  }|tC        |      k  rln'|D ]"  }|jE                  |d   |d   |d   |d          $ g }g }tC        |      dkD  r|dk(  r/|D ])  }|\  }}|jG                  |       |jG                  |       + n4|D ]/  }|\  }}}}|jI                  ||g       |jI                  ||g       1 t;        |      }t9        |      }t;        |      }t9        |      }|j-                  |t/        ||||             n |dk(  r|r|j7                  |       |st        j                  }|j5                  |       |jK                  |       t;        |d       d   }t;        |d       d   }t9        |d       d   |z
  }
t9        |d       d   |z
  }|j-                  |t/        |||
|             nH|dk(  r|r@|j7                  |       |st        j                  }|j5                  |       tM        |d   |d   z
        dz  }|jO                  t9        |d   |d         |z
  t9        |d   |d         |z
  |       |j-                  |t/        |d   |d   |d   |d                n|dk(  rg|r|j7                  |       |jQ                  |d   |d          |d   dz
  |d   dz
  |d   dz   |d   dz   f}|j-                  |t/        |             n#|dk(  r|d   syd |v rtS        |d          }nd!}| jU                  |d         dd \  }
}|jW                  |d"          |jY                  |d#          d$|v r3|j[                  t        j\                         |j_                  |d$          | ja                  |      \  }}|dk(  r/|jc                  |d   te        |d         te        |d                n/|jg                  |d   te        |d         te        |d         |       |j-                  ||       |j#                          | j%                          |S )%z!Draws map and overlay decorationsNr   clearpolylineFr   c   r(   zBufferedWindow.Draw(): id=z, pdctype = z, coord=r   r&   Tbox      line)r   linespolygon)pointsc                     | d   S r   r+   r,   s    r.   r/   z(BufferedMapWindow.Draw.<locals>.<lambda>  
    ad r0   )r$   c                     | d   S Nr&   r+   r,   s    r.   r/   z(BufferedMapWindow.Draw.<locals>.<lambda>  r   r0   c                     | d   S r   r+   r,   s    r.   r/   z(BufferedMapWindow.Draw.<locals>.<lambda>  r   r0   c                     | d   S r   r+   r,   s    r.   r/   z(BufferedMapWindow.Draw.<locals>.<lambda>  r   r0   circle)radiuspointtextactiverotation        fontr!   
background)4r]   r   r=   r<   r^   BeginDrawingr3   TRANSPARENT_BRUSHBrushGetBackgroundColourSetBackgroundr   msgSetIdWHITE_BRUSH	RemoveAllClear
EndDrawingRefreshr
   GetSize
DrawBitmapSetIdBoundsr   CYANTRANSPARENTSetBrushSetPenrg   minDrawRectangleRectDrawLinePointPointlenDrawLineappendextendDrawPolygonabs
DrawCircle	DrawPointfloatGetFullTextExtentSetFontSetTextForegroundSetBackgroundModeSOLIDSetTextBackground
TextBoundsDrawTextintDrawRotatedText)rv   r   r[   drawidpdctypecoordsr<   brushbgbitmapwhx2x1y2y1rwidthrheightrectr   r   xlistylistr   r-   yr   coordsBoundr   bboxs                                 r.   DrawzBufferedMapWindow.DrawF  s    >'!c,G# *$llhh7g%5:DKKs+D12R<%%B$2245B"		w(	
 IIfgBb!MMOIIKNNLLNg$S)F>>#DAqNN66!9fQi>OOFDF1Iq!$DEHHRWWbnn=EU#

3F1I.F1I.F1I.F1I.br'BFG4%%d+-RXXbggr~~>?

3!!HHVAYq	2BHHVAYq	4R DF1Ivay&)L --RXXbggr~~>?

3v;?j(Ac&k/))HHVAE]1%5va!e}Q7GHHHVAYq\6!9Q<@ Q c&k/ !' IT!Wd1gtAwQHI v;?*,%+ ,E#(DAq!LLO!LLO,
 %+ 3D-1NBB!LL"b2!LL"b23 UBUBUBUBOOFDRR,@A	!

300EU#v.N3A6N3A6N3A6:N3A6:Q1a(89 

300EU#VAY23a7q	6!9-6q	6!9-6!  
 DF1Ivay&)L 

3fQi31IM1IM1IM1IM	 [(9:x=S  Z1))#f+6q;DAqKKF$!!#g,/s"%%bhh/%%c,&78??3/LFD1}S[#fQi.#fQi.I##KVAYVAY OOFD)r0   c                 \   d|v rt        |d         }nd}|d   }t        |d   |d   dd      }d}t        j                  dd|d	   |fz         | j	                          | j                  |d
          | j                  |d	         \  }}|dk(  r||c|d<   |d<   |r|||fS ||fS t        j                  t        j                  t        j                  |            |z        |z   }	t        j                  t        j                  t        j                  |            |z        |z   }
|dkD  r|dk  r|dxx   |	z  cc<   d|	f}nD|dk\  r$|dk  r|dxx   |
z  cc<   |dxx   |	z  cc<   |
|	f}n|dk\  r|dk  r|dxx   |
z  cc<   |
df}|
|d<   |	|d<   |j                  ||       |r|||fS ||fS )a7  Return text boundary data

        :param textinfo: text metadata (text, font, color, rotation)
        :param coords: reference point

        :return: coords of nonrotated text bbox (TL corner)
        :return: bbox of rotated text bbox (wx.Rect)
        :return: relCoords are text coord inside bbox
        r   r   r  r   r&   r1      z1BufferedWindow.TextBounds(): text=%s, rotation=%fr   r   r   r   Z      i  )r   r   r   r   Updater   GetTextExtentmathfabssinradianscosInflate)rv   textinfo	relcoordsr   r  r  	relCoordsr
  r  boxhboxws              r.   r   zBufferedMapWindow.TextBounds  s    !Xj12HH(#F1Ivay!Q/			?*+	
 	Xf%&!!(6"231q= !DGT!WtY..t|#yy$,,x"89A=>Byy$,,x"89A=>Ba<HrMGtOGD	I^3GtOGGtOGtI_CGtOGq	IQQQ4**4<r0   c                    t        j                  dd| j                  z         t        j                  | | j
                        }|j                          | j                         j                         }t        r|j                  |       n|j                  |       d}| j                  	d| _        d}| j                  rn| j                  j                  ||       | j                  r>	 t        j                  |      }| j                   r| j                   j                  ||       d| _        n| j*                  | j                  j-                  |       | j                  r1	 t        j                  |      }| j                   j-                  |       |j/                  t1        dd| j2                  j4                  | j2                  j6                              | _        | j                  j9                  | j*                  ddd       | j                  j-                  |       	 t        j                  |      }| j:                  j-                  |       | j<                  j-                  |       |rd| _        yy# t"        $ rB}t%        |t&        j(                         | j                   j                  ||       Y d}~d}~ww xY w# t"        $ rA}t%        |t&        j(                         | j                   j-                  |       Y d}~wd}~ww xY w# t"        $ rA}t%        |t&        j(                         | j:                  j-                  |       Y d}~d}~ww xY w)ztDraw PseudoDC's to buffered paint DC

        If self.redrawAll is False on self.pdcTmp content is re-drawn
        r(   z&BufferedWindow.OnPaint(): redrawAll=%sFNT)filer   )r   r   rf   r3   BufferedPaintDCrj   r   GetUpdateRegionGetBoxr   SetClippingRegionSetClippingRectr   DrawToDCClippedr8   GCDCr   NotImplementedErrorprintsysstderr
bufferLastDrawToDCGetAsBitmapr   r   rh   ri   r   r   r   )rv   r   dcrgn
switchDrawgcdces          r.   rT   zBufferedMapWindow.OnPaintJ  sz   
 			!=NOdll3

 ""$++-  %s#
>>!!DNJ>>HH$$R- zz<772;D~~66tSA
 #DO&!!"%::4!wwr{//5 #%..Atxx~~txx?# HHAu=HHb!	-772;D((. 	R "DN Q + <!#**-NN222s;;< / 4acjj1//334$ # 	-!#**%((,,	-sH   '=I4  0K 0L 4	J?=7J::J?	L6LL	M6MMc                 6    t        j                         | _        y)z9Scale map image so that it is the same size as the WindowN)grassclockr9   rv   r   s     r.   rV   zBufferedMapWindow.OnSize  s     kkmr0   c                    | j                   r| j                   dz   t        j                         k  rt        j                  dd       | j
                  j                  | j                                | j                  j                          t        t        d| j
                  j                        t        d| j
                  j                              | _        | j                         | _        d}| j                  r| j
                  j                  | j
                  j                  z   dkD  r| j                  j!                  | j
                  j                  | j
                  j                        | _        t#        | j
                  j%                               dkD  r| j'                          d}|r| j'                  d       d| _         n| j                   r|j)                          |j+                          y	)
z|Only re-render a composite map image from GRASS during
        idle time instead of multiple times during resizing.
        皙?r   zBufferedWindow.OnSize():r&   Tr   FrE   N)r9   r@  rA  r   r   r   ChangeMapSizeGetClientSizerj   r   r	   rg   rh   ri   GetImager[   Scaler   GetListOfLayersrz   RequestMorer   )rv   r   	updatemaps      r.   rX   zBufferedMapWindow.OnIdle  sG    ;;4;;,u{{}<IIa34 HH""4#5#5#78
 LL  "&s1dhhnn'=s1dhhoo?VWDL }}DH ITXX^^dhhoo=A88>>$((..$((//Jtxx//12Q6NN$ %Id+DK[[

r0   c                 R   t        j                  dd|||       || _        || _        || _        t        j                  t        d      |       | _        t        j                         j                          | j                  j                  ||f       | j                  j                         }|j                  j                  | j                          |j                  j#                  | j$                         | j                  j'                  d| j(                  j*                         y)zThis draws the pseudo DC to a buffer that can be saved to
        a file.

        :param filename: file name
        :param FileType: type of bitmap
        :param width: image width
        :param height: image height
        r&   z"MapWindow.SaveToFile(): %s (%dx%d)zPlease wait, exporting image...)r   TforcewindresN)r   r   	_fileName	_fileType_saveToFileCallbackr3   BusyInfor   _busyGetAppYieldr   rF  ro   rp   r}   rq   rm   _saveToFileDoneRenderr7   
resolution)rv   FileNameFileTyperh   ri   callback	renderMgrs          r.   
SaveToFilezBufferedMapWindow.SaveToFile  s     			!98UFS!!#+ [[#D!EdS

		v/HH))+	 	''(=(=>$$T%9%9:dD,<,<,G,GHr0   c                    | j                   j                         }|j                  j                  | j                         t        t        d| j                   j                        t        d| j                   j                              }| j                         }| j                  j                          | j                  | j                  |d       | j                         }t        | j                   j                        |d   z  t        | j                   j                        |d   z  f}| j                         D ]  }| j                   |   d   j#                         s$| j                   |   d   }t%        |d   | j&                  |   j(                  d   z        t%        |d   | j&                  |   j(                  d   z        f}| j                  | j                  ||| j&                  |   j*                  |        t-        | j.                  j1                               D ]f  }| j.                  |   }	|	d   }
|d   |	d   d   z  |d   |	d   d   z  f|	d<   | j                  | j                  | j.                  |   |d	
       |
|	d<   h t3        j4                  d |      }|j7                          | j                  j9                  |       | j:                  r| j<                  j9                  |       |j?                  | j@                  | jB                         | `"| ` | `!|j                  jG                  | jH                         | jK                  d       | jM                          | jN                  r| jO                          y y )Nr&   r   r  r   layerr   r[   r  r  r  r  r   )r[   r  r  TrE  )(r   ro   rp   r}   rX  r	   rg   rh   ri   rH  r   r   r  rG  r   
GetOverlayr]   IsActiver  r\   r  pdcTyper   r_   r   r3   
BufferedDCr   r8  r8   r   SaveFilerQ  rR  rU  rm   rq   rz   r   rS  )rv   r]  r^  ibufferr[   cSizeratior   r  r%  	oldCoordsr:  s               r.   rX  z!BufferedMapWindow._saveToFileDone  s   HH))+	''(<(<=c!TXX^^4c!TXX__6MNmmo		$((C	+ ""$dhhnn%a0%2H5QR82SS ??$ 	C~~c"7+446^^C(.U1Xb(9(@(@(CCDc!Ht}}R077::G  		HH MM"-55!  	  t}}))+, 		+B}}R(H *Ia8H-a00a8H-a00"HX IIdhhDMM"$5b&IQ!*HX		+ ]]4)

 	"::NN##B'8JNN$$T%:%:;d###$$& $r0   c                 V   g }| j                   j                  dd      D ]  }|j                  t        j                  j                  |j                        s;t        j                  j                  |j                        set        j                  |j                        }t        | j                  j                               D ]/  }| j                  |   d   |j                  k(  s#| j                  |= 1 |j                  |d| j                  |<   |j                  |        |S )zuConverts rendered overlay files to wx.Image

        Updates self.imagedict

        :return: list of images
        overlayT)ltyper   r   )r   rb  )r   rJ  mapfileospathisfilegetsizeutilsautoCropImageFromFiler   r]   r   r   r   )rv   imgsrn  r[   r$   s        r.   rd  zBufferedMapWindow.GetOverlay   s     xx//i/M 	!G+GGNN7??3GGOOGOO411'//B 3 3 56 0C~~c*40GJJ> NN3/0 .5ZZ'&Js#C 	! r0   c                 0   d}| j                   j                  rt        j                  j	                  | j                   j                        rlt        j                  j                  | j                   j                        r9t        j                  | j                   j                  t        j                        }nd}t        | j                  j                               D ]%  }| j                  |   d   |k(  s| j                  |= ' d|i| j                  |<   |S )zConverts rendered map files to wx.Image

        Updates self.imagedict (id=99)

        :return: wx.Image instance (map composition)
        r   Nr   )r   rp  rq  rr  rs  rt  r3   ImageBITMAP_TYPE_ANYr   r]   r   )rv   imgIdr[   r$   s       r.   rH  zBufferedMapWindow.GetImage9  s     HHtxx//0 0 01((488++R-?-?@CC++-. 	(C~~c"4(E1NN3'	(  $Ums
r0   c                     || _         y r*   r;   )rv   r;   s     r.   SetAlwaysRenderEnabledz(BufferedMapWindow.SetAlwaysRenderEnabledR  s
    (r0   c                     | j                   S r*   r}  r{   s    r.   IsAlwaysRenderEnabledz'BufferedMapWindow.IsAlwaysRenderEnabledU  s       r0   c                    | j                   || j                  k  r|| _        |r|| _        |r|| _        t	        j                         }|| _        | j                  dk  r| j                          y| j                  j                         | _         | j                  j                  | j                  | j                  | j                          y)a*  Updates the canvas anytime there is a change to the
        underlying images or to the geometry of the canvas.

        This method should not be called directly.

        .. todo::
            change direct calling of UpdateMap method to emitting grass
            interface updateMap signal

        .. todo::
            consider using strong/weak signal instead of delay limit in
            giface

        :param render: re-render map composition
        :param renderVector: re-render vector map layer enabled for editing (used for digitizer)
        :param delay: defines time threshold  in seconds for postponing
                      rendering to merge more update requests.

        If another request comes within the limit, rendering is delayed
        again. Next delay limit is chosen according to the smallest
        delay value of all requests which have come during waiting period.

        Let say that first UpdateMap request come with 5 second delay
        limit. After 4  seconds of waiting another UpdateMap request
        come with delay limit of 2.5 seconds. New waiting period is set
        to 2.5 seconds, because limit of the second request is the
        smallest. If no other request comes rendering will be done
        after 6.5 seconds from the first request.

        Arguments 'render' and 'renderVector' have priority for True.
        It means that if more UpdateMap requests come within waiting
        period and at least one request has argument set for True, map
        will be updated with the True value of the argument.
        Nr   )callableondonepid)rB   rD   rE   rF   timerC   _runUpdateMaprA   GetIdRun_timingFunction_onUpdateMap)rv   rE   rF   delayupdTimes        r.   rz   zBufferedMapWindow.UpdateMapX  s    H ??"edmm&;!DM DK ,D))+ '==3 "2288:DO  $$--((OO % r0   c                     	 t        j                          }t        j                  d       || j                  | j                  z   kD  s|| j                  k7  ryV)aM  Timer measuring elapsed time, since last update request.

        It terminates, when delay limit is exceeded.

        :param pid: id which defines whether it is newest timer, or
                    there is another one (representing newer Update map
                    request). If it is not the newest, it is terminated.
        g{Gz?N)r  sleeprC   rD   rB   )rv   r  r  s      r.   r  z!BufferedMapWindow._timingFunction  sI     iikGJJt$//$--??$//) r0   c                 ^    | r+| j                   |j                  k(  r| j                          y y y r*   )rB   r  r  rB  s     r.   r  zBufferedMapWindow._onUpdateMap  s'    DOOuyy0  14r0   c                 z    d| _         | j                  | j                  | j                         dx| _        | _        y)z$Update map when delay limit is over.NF)rB   _updateMrE   rF   r{   s    r.   r  zBufferedMapWindow._runUpdateMap  s0    dkk4#4#45*//d'r0   c                    t        j                  dd|d|d       | j                         r| j                  d}	 |r)| j                  j                  | j                                | j                  j                  || j                  j                         y# t        $ r }t        |j                         Y d}~yd}~ww xY w)	z>
        :func:`UpdateMap` for arguments description.
        r&   z,BufferedWindow.UpdateMap(): started (render=z, renderVector=)NTrN  )message)r   r   r  r[   r   rF  rG  rY  r7   rZ  r   r   value)rv   rE   rF   r>  s       r.   r  zBufferedMapWindow._updateM  s     			.4lD	
 %%'DHH,<F	$&&t'9'9';<HHOO&$2B2B2M2MON 	$177##	$s   AB 	C#B>>Cc           	         t        j                  dd       | j                         | _        | j                  | j
                  | j                  fD ]"  }|j                          |j                          $ | j                  s| j                  | j                  d       nE	 | j                  | j                     d   }| j                  | j                  | j                  |       |r| j                  r| j                          | j                         D ]  }| j                  |   d	   j!                         s$| j                  |   d   }| j                  | j                  ||| j"                  |   j$                  | j"                  |   j&                  
        t)        | j*                  j-                               D ]1  }| j                  | j                  | j*                  |   |dg d
       3 | j/                          t1        | j2                        dkD  rH| j4                  }| j6                  }| j2                  D ]  }	 |j                           || _        || _        t1        | j>                        dkD  r| jA                  | j                         y# t        $ r!}t        j                  dd|       Y d }~yd }~ww xY w#  t9        | t;        d             | j=                  |       Y xY w)Nr&   z$BufferedWindow.UpdateMap(): finishedr   r  r   zUpdateMap() failed: %sFra  rb  rc  r   )r'   r'   r'   r'   r   zBUnable to draw registered graphics. The graphics was unregistered.r   r  T)!r   r   rH  r[   r   r   r   r   r   r  r]   	Exceptionmgsr8   
_updateMaprd  re  r\   rf  r  r   r_   r   DrawCompRegionExtentr   rt   r<   r=   r   r   UnregisterGraphicsToDrawr>   	DrawLines)	rv   rF   r   r   r>  r[   penOrigpolypenOrigitems	            r.   rq   z"BufferedMapWindow._updateMFinished  sy   		!;<==?
 HHd114;;? 	CIIKMMO	 xxIIdhhI0^^DHH-d3 IIdhhI4
 DJJOO
 ??$ 
	C~~c"7+446^^C(.		HH MM"-55==,33  	
	 t}}))+, 	BIIMM"%'  	 	!!# t##$q(hhG,,K,, 8
8IIK8 DH&DLt!#NN4;;'E  		!5q9d8# !=! 11$7s$   J >J?	J<J77J<?)K*c                    | j                   j                  r,| j                  j                         }| j                  j	                         }t        j                  ||      r|n|}g }|j                  |d   |d   f       |j                  |d   |d   f       |j                  |d   |d   f       |j                  |d   |d   f       |j                  |d   |d   f       t        j                  t        j                  dddd      dt        j                  	      | _        | j                  | j                  |
       yy)zDraw computational region extent in the display

        Display region is drawn as a blue box inside the computational region,
        computational region inside a display region as a red box).
        r
  nr>  s   r      r   )colourrh   r   )r   r>   N)r7   
showRegionr   	GetRegionGetCurrentRegionru  
isInRegionr   r3   Penr5   r   r=   r  r   )rv   compRegdispRegregregionCoordss        r.   r  z&BufferedMapWindow.DrawCompRegionExtent  s"    &&hh((*Ghh//1G"--gw?'WCLS3s8 45S3s8 45S3s8 45S3s8 45S3s8 45 66yyaC0DL NNt22|NL! 'r0   c                 8   | j                  | j                  d       | j                  r| j                  | j                  d       | j                  | j                  d       | j                  | j
                  d       | j                  j                          y)zErase map canvasr   r  N)r  r   r8   r   r   r   r   AbortAllThreadsr{   s    r.   EraseMapzBufferedMapWindow.EraseMap3  sk    		$((G	,::IIdnngI6		$%%w	7		$++w	/  "r0   c                     t        j                  t        j                  |             }|j                  t        j                  d             |j                          t        | j                        | _        | j                  j                  d|        | j                  j                  |       | j                  j                  |       | j                  j                  ||       | j                  j                          y)zMDrag the entire map image for panning.

        :param moveto: dx,dy
        Whiter1   N)r3   rg  ClientDCr   r   r   r   rj   r:   	BeginDragGetImageRectMoveDoDrawImageEndDrag)rv   movetor:  s      r.   DragMapzBufferedMapWindow.DragMap?  s    
 ]]2;;t,-
'*+

 .vt,!!&)&!  V,r0   c                 P   |dk(  s|dk(  s|yt        j                  dd|z         | j                  \  }}|d   |z
  }|d   |z
  }| j                  j	                  t        j                  | j                                      | j                  j                  |      }t        |t              rt        |d   |d   |d   |d	         }|| j                  v rR|d   |d   |d	   z
  |d   |d	   f}|j                  |      }|d   |d   z
  |d   |d   |d	   f}	|j                  |	      }| j                  j                  |||       | j                  j                  |      }
t        |
t              rt        |d   |d   |d   |d	         }
|| j                  v rL|
| j                  |   d
<   | j                  |   d   dxx   |z  cc<   | j                  |   d   dxx   |z  cc<   |j                  |
      }|j                  dd       | j!                  |d       |d   |d   f| _        y)zDrag an overlay decoration itemr    Nr(   z BufferedWindow.DragItem(): id=%dr   r&   r   r   r  r  r  F)r   r   rs   r   r   r3   r   r   GetIdBounds
isinstancer   r   r_   UnionTranslateIdr$  RefreshRect)rv   r   r  r-   r  dxdyrrtoprleftr2s              r.   DragItemzBufferedMapWindow.DragItemP  s   8rRx2:		!7"<=||1AY]AY]rxx(@(@(BCDHH  $aQqT1Q41qt,AaD!A$1+qtQqT2DAqTAaD[!A$!ad3EARR(XX!!"%b$adAaD!A$!-B(*DMM"f%MM"h'*b0*MM"h'*b0*GGBK			!QE"q	6!9-r0   c                    |sy|| j                   d   }|| j                   d   }t        j                  dd| j                   d   | j                   d   |d   |d	   |d   |d	   fz         | j                   d   dk(  rt        j                  }|d   |d	   |d   |d	   g}|j                  |      }t        |t              rt        |d   |d	   |d
   |d         }|j                  dd       	 |j                  |       | j                  |d       |j                  |       | j                  ||d|       y| j                   d   dk(  rt        j                  | _        |d   |d	   |d   |d	   g}t        |d   |d         }t!        |d   |d         }t        |d	   |d	         }	t!        |d	   |d	         }
t        ||	||z
  |
|	z
        }|j                  dd       	 |j                  | j                         | j                  |d       |j                  | j                         | j                  || j                  d|       yy#  Y OxY w#  Y ZxY w)zuMouse box or line from 'begin' to 'end'

        If not given from self.mouse['begin'] to self.mouse['end'].
        Nbeginendr(   zBBufferedWindow.MouseDraw(): use=%s, box=%s, begin=%f,%f, end=%f,%fuser   r   r&   r   r   r  Fr  r  r  r   )mouser   r   r3   ID_NEWr  r  r   r   r$  ClearIdr  r   r  r?   r   rg   )rv   r   r  r  boxidmousecoordsr  r  r  r  r  s              r.   	MouseDrawzBufferedMapWindow.MouseDrawr  sX   
 =JJw'E;**U#C		P

5!

5!aaAA	
 ::e%IIE 8U1Xs1vs1v>K&A!T"1qtQqT1Q40IIaOE" Q&IIeIIc%{IKZZ&())DK 8U1Xs1vs1v>KU1Xs1v&BU1Xs1v&BU1Xs1v&BU1Xs1v&BRR"Wb2g.AIIaODKK( Q&IIdkk"IIc$++vkIR ) s   -I I IIc                 |   |s| j                   }|s| j                  }t        |      dkD  rt        j                  dz   | _        g }|D ]"  }|j                  | j                  |             $ | j                  || j
                  d|       t        j                  dd|d| j
                         | j
                  S y)	zDraw polyline in PseudoDC

        Set self.pline to wx.NEW_ID + 1

        :param polycoords: list of polyline vertices, geographical
                           coordinates (if not given, self.polycoords
                           is used)
        r   r&   r   r  r  z#BufferedWindow.DrawLines(): coords=z, id=)r   r>   r   r3   r  r@   r   
Cell2Pixelr  r   r   )rv   r   r>   r  ps        r.   r  zBufferedMapWindow.DrawLines  s     ++CJz?Q99q=DLF 2dooa012 IIc$,,
6IRIIBH$,,W
 <<r0   c                     t        j                  dd|z         | j                  |dd||      | _        | j                  S )a]  Draw polyline in PseudoDC.

        This is similar to DrawLines but this is used with GraphicsSet,
        coordinates should be always in pixels.

        :param pdc: PseudoDC
        :param coords: list of coordinates (pixel coordinates)
        :param pen: pen to be used
        :param drawid: id of the drawn object (used by PseudoDC)
        r  z)BufferedWindow.DrawPolylines(): coords=%sNr   r  r  r  r<   )r   r   r  lineIdr?   )rv   r   r  r<   r  s        r.   DrawPolylineszBufferedMapWindow.DrawPolylines  sD     			!@6IJiijS   
 {{r0   c
                    t        j                  dd|||fz         |d   |d   |z
  |d   |d   |z   f|d   |z
  |d   |d   |z   |d   ff}
| j                  ||	d|
|      | _        |s| j                  S |dk(  r|d   |d   z
  |d   |d   z
  ddg}nT|dk(  r|d   |d   z   |d   |d   z
  ddg}n6|d	k(  r|d   |d   z   |d   |d   z   ddg}n|d   |d   z
  |d   |d   z   ddg}| j                  ||d
||       | j                  S )a  Draw cross in PseudoDC

        .. todo::
            implement rotation

        :param pdc: PseudoDC
        :param coords: center coordinates (pixel coordinates)
        :param rotation: rotate symbol
        :param text: draw also text (text, font, color, rotation)
        :param textAlign: alignment (default 'lower-right')
        :param textOffset: offset for text (from center point)
        :param drawid: id of the drawn object (used by PseudoDC)
        r  z6BufferedWindow.DrawCross(): pdc=%s, coords=%s, size=%dr   r&   r   r  ulurlrr   )r[   r  r  r<   r   r   r  r?   )rv   r   r  sizer   r<   r   	textAlign
textOffsetr  coordsCrosscoords               r.   	DrawCrosszBufferedMapWindow.DrawCross  s   2 			DFD!"	
 AYq	D(&)VAY5EFAYvay&)d*:F1IF

 ii   
 ;;AYA.q	JqM0I1aPE$AYA.q	JqM0I1aPE$AYA.q	JqM0I1aPEAYA.q	JqM0I1aPE		#43	G{{r0   c           	          t        j                  dd|d|d|       |d   |d   |d   |d   g}| j                  ||d|||      | _        | j                  S )	a!  Draw rectangle (not filled) in PseudoDC

        :param pdc: PseudoDC
        :param point1: top left corner (pixel coordinates)
        :param point2: bottom right corner (pixel coordinates)
        :param pen: pen
        :param drawid: id of the drawn object (used by PseudoDC)
        r  z$BufferedWindow.DrawRectangle(): pdc=z	, point1=z	, point2=r   r&   r   r  r  r  r<   r  r  )rv   r   point1point2r<   r  r  r  s           r.   DrawRectanglezBufferedMapWindow.DrawRectangle  sm     			FF$	

 )VAYq	6!9=iif#U   
 {{r0   c           	          t        j                  dd|d|d|       |d   |z
  |d   |z
  |d   |z   |d   |z   g}| j                  ||d|||      | _        | j                  S )	zDraw circle (not filled) in PseudoDC

        :param pdc: PseudoDC
        :param coords: center (pixel coordinates)
        :param radius: radius
        :param pen: pen
        :param drawid: id of the drawn object (used by PseudoDC)
        r  z!BufferedWindow.DrawCircle(): pdc=z	, coords=z	, radius=r   r&   r   r  r  )rv   r   r  r   r<   r  r  	newcoordss           r.   r   zBufferedMapWindow.DrawCircle*  s     			FF$	
 1I1I1I1I	
	 iiSX   
 {{r0   c                 p    t        |      dk  ry| j                  ||d|||      | _        | j                  S )a  Draws polygon from a list of points (do not append the first point)

        :param pdc: PseudoDC
        :param coords: list of coordinates (pixel coordinates)
        :param pen: pen
        :param drawid: id of the drawn object (used by PseudoDC)
        r&   Nr   r  )r   r  r?   )rv   r   r  r<   r  r  s         r.   r   zBufferedMapWindow.DrawPolygonC  sB     v;!ii	&cQV   
 {{r0   c                    |dkD  r|d   | j                   j                  dz  z
  |d   | j                   j                  dz  z
  f}|d   | j                   j                  dz  z   |d   | j                   j                  dz  z   f}||fS | j                   j                  |d   z
  dz  | j                   j                  |d   z
  dz  f}|d   | j                   j                  dz  z   |d   | j                   j                  dz  z   f}||fS )zComputes zoom parameters for recenter mode.

        Computes begin and end parameters for Zoom() method.
        Used for zooming by single click (not box)
        and mouse wheel zooming (zoom and recenter mode).
        r   r  r&   r   )r   rh   ri   )rv   positionrb   r  r  s        r.   _computeZoomToPointAndRecenterz0BufferedMapWindow._computeZoomToPointAndRecenterS  s    a<dhhnnq00dhhoo11E A;!!33Xa[488??UVCV5VWC cz	 (1+-28A;.!3E 8dhhnnq00%(TXX__q=P2PQCczr0   c                    | j                   sy|j                         dk7  r| j                  |       y|j                         r| j	                  |       y|j                         r| j                  |       y|j                         r| j                  |       y|j                         r| j                  |       y|j                         r| j                  |       y|j                         r| j                  |       y|j                         r| j!                  |       y|j#                         r| j%                  |       y|j'                         r| j)                  |       y|j+                         rV|j-                         }| j/                  |      }| j0                  j3                  |d   |d          | j5                  |       yy)z&Mouse motion and button click notifierNr   r&   r-   r  )rZ   GetWheelRotationOnMouseWheelLeftDown
OnLeftDownLeftUpOnLeftUpDragging
OnDraggingButtonDClickOnButtonDClick
MiddleDownOnMiddleDownMiddleUp
OnMiddleUp	RightDownOnRightDownRightUp	OnRightUpEnteringOnMouseEnterMovingr   
Pixel2CellrQ   r   OnMouseMoving)rv   r   pixelCoordinatescoordinatess       r.   r   zBufferedMapWindow.MouseActionsh  sj      !!#q(e$ ^^OOE" \\^MM%  ^^OOE" !& e$ ^^OOE" __U# ]]_NN5!^^e$\\^$002//*:;K!!KNk!n!Eu%	 r0   c                    t        j                  ddd      }|dk(  r|j                          yd| _        |j	                         }|j                         }t        j                  dd	|z         |d
kD  rd}nd}t        j                  ddd      r|dz  }|d
k(  r| j                  ||      \  }}n_|dk(  rZ|d
   dz  |d   dz  f}| j                  j                  |d
   z
  dz  |d
   z   | j                  j                  |d   z
  dz  |d   z   f}| j                  |       | j                  d       | j                          d| _        y)zMouse wheel movedr   mouseWheelZoom	selectionr"   r   NFr(   z&BufferedWindow.MouseAction(): wheel=%dr   r&   r  scrollDirectionr  rb   rD  )r  T)r   r6   r   rZ   r   r  r   r   r  r   rh   ri   Zoomrz   r   )rv   r   zoomBehaviourcurrentwheelrb   r  r  s           r.   r  zBufferedMapWindow.OnMouseWheel  s]   $((!1+
 AJJL!##%&&(		!=EF19HH)1B;WNHA<< 8 = JE3 aQZ!^WQZ!^4E'!*,1GAJ>71:-2WQZ?C 			%h' 	S! r0   c                 "   t        j                  dd       |j                         }| j                  d   }|d   |d   z
  |d   |d   z
  f}| j                  r| j
                  }nd}| j                  d   dk(  s|j                         r| j                  |       y| j                  d   d	k(  r;|s9| j                  -|j                         }| j                  | j                  |       y| j                  d   d	k(  r|sy|j                         | j                  d
<   |j                         rf|rF|j                         dv r4t        | j                  j                         j                               dkD  s| j                  | j                          yyy)zMouse draggingr(   z&BufferedWindow.MouseAction(): Draggingr  r   r&   Nr  panpointerr  )moveLine)r   )r   r   r   r  r8   toolbarMiddleIsDownr  rr   r  
LeftIsDown	GetActionr   
GetDisplayGetSelectedr  r   )rv   r   r  previousmovedigitToolbarr  s          r.   r  zBufferedMapWindow.OnDragging  s_   		!=>##%::g&
Xa[('!*x{*BC::<<LL ::e%););)=LL JJu* '&&(FMM$++v. zz% I-l % 1 1 3DJJu! **,=

--/;;=>B4;;/ C "r0   c                    t        j                  dd| j                  d   z         |j                         | j                  d<   | j                  d   dk(  r@| j                  r4|j                         r| j                  |       n| j                  |       n| j                  d   dk(  rg }d| _        | j                  d   | _	        | j                  j                  | j                  d   | j                  d   | j                        }d	|v r|j                  d	       |g k7  r|d   | _        n	 | j                  | j                  d         }| j                  j!                  |d   |d   
       |j#                          y)zLeft mouse button pressedr(   z#BufferedWindow.OnLeftDown(): use=%sr  r  r  r  r   r&   r   r  N)r   r   r  r   r8   ControlDownOnLeftDownUndo_onLeftDownrr   rs   r   r   rc   remover  rO   r   r   )rv   r   r   r  s       r.   r  zBufferedMapWindow.OnLeftDown  s@   		!:TZZ=NNO#//1

7 ::e	)djj  "##E*  'ZZ)+FDK::g.DLXX))Qa$..F V|b!|$Qi G!45&)vay9

r0   c                    t        j                  dd| j                  d   z         |j                         | j                  d<   | j	                  | j                  d         }| j                  d   dv r| j                  d   }| j                  d   }| j                  d   dk(  r<|d   |d   z
  dk(  s|d	   |d	   z
  dk(  r | j                  || j                  
      \  }}| j                  ||| j                         | j                  d       n@| j                  d   dk(  r?| j                  j                  | j                  d   d   | j                  d   d	          n| j                  d   dk(  r| j                  r| j                  |       n| j                  d   dk(  r| j                  r| j                  rZ| j                  | j                  v rB| j                  j!                  | j                        | j                  | j                     _        nY| j                  | j$                  v r@| j                  j!                  | j                        | j$                  | j                     d<   n	 d| _        | j&                  j                  |d   |d	          n| j                  d   dk(  r| j	                  | j                  d         }|d   |d   k  r|d   }|d   }n
|d   }|d   }|d	   |d	   k  r|d	   }|d	   }	n
|d	   }|d	   }	| j(                  j+                         }
t-        d| d|
d   |
d   |	|||	       | j                  d       | j.                  j                  |d   |d	          y)z_Left mouse button released

        Emits mapQueried signal when mouse use is 'query'.
        r(   z!BufferedWindow.OnLeftUp(): use=%sr  r  )zoomr  r  r,  r   r&   r  TrE  queryr  r  r  N
drawRegiong.regionansresewres)r   flagsr1  r2  r  r  r>  r
  F)r   r   r  r   r  r  rb   r  rz   rH   r   r8   	_onLeftUprr   r\   r   r  r  r_   rL   r   r  r   rM   )rv   r   r  r  r  coordinatesBeginwesteastsouthnorthregions              r.   r  zBufferedMapWindow.OnLeftUp  s2   
 			!84::e;LLM!--/

5oodjj&78::e/JJw'E**U#Czz% F*8c!f$)U1XA->!-C!%!D!D!$t}} "E "JE3 IIeS$--0 NN$N'ZZ')OO  4::e#4Q#74::e;LQ;O PZZ)+

NN5!ZZ)+{{==T[[DMM%A8<8L8L9DMM$++.5 [[DMM19=9M9M:DMM$++.v6 "##((;q>[^(LZZ,.#tzz'/BC"[^3'*"1~"1~'*"[^3(+#A#A(+XX'')FWoWo
 NN%N( 	A+a.Ar0   c                    t        j                  dd| j                  d   z         |j                         }| j                  d   dk(  rg| j                  j                  |d   |d   | j                        }|r8|d   dk7  r0|d   | _        | j                  j                  | j                         | j                  |      }| j                  j                  |d   |d   	       y
)zMouse button double clickr(   z'BufferedWindow.OnButtonDClick(): use=%sr  r  r   r&   r   r   r  N)r   r   r  r   r   r   rc   rr   r   r   r  rP   )rv   r   screenCoordsr   r  s        r.   r   z BufferedMapWindow.OnButtonDClick`  s    		!>EARRS((*::e	)XX))Qa$..F &)r/$Qi%%**T[[*A.q	VAY7r0   c                     t        j                  dd| j                  d   z         | j                  r| j	                  |       |j                          y)zRight mouse button pressedr(   z$BufferedWindow.OnRightDown(): use=%sr  N)r   r   r  r8   _onRightDownr   rB  s     r.   r  zBufferedMapWindow.OnRightDownr  s=    		!;djj>OOP::e$

r0   c                 \   t        j                  dd| j                  d   z         | j                  r| j	                  |       d| _        | j                          | j                  |j                               }| j                  j                  |d   |d          |j                          y)	zRight mouse button releasedr(   z"BufferedWindow.OnRightUp(): use=%sr  Tr   r&   r  N)r   r   r  r8   
_onRightUprf   r   r  r   rN   r   r   )rv   r   r  s      r.   r  zBufferedMapWindow.OnRightUp{  s    		!9DJJu<MMN::OOE"!2!2!45fQi8

r0   c                 D    |sy|j                         | j                  d<   y)zMiddle mouse button pressedNr  )r   r  rB  s     r.   r  zBufferedMapWindow.OnMiddleDown  s    #//1

7r0   c                     |j                         | j                  d<   | j                  d   }| j                  d   }| j                  ||d       | j                  d       y)zMiddle mouse button releasedr  r  r   TrE  N)r   r  r  rz   )rv   r   r  r  s       r.   r  zBufferedMapWindow.OnMiddleUp  sW    !--/

5 

7#jj		%a  	d#r0   c                 X    | j                   j                          |j                          y)zgMouse entered window and no mouse buttons were pressed

        Emits the mouseEntered signal.
        N)rK   r   r   rB  s     r.   r
  zBufferedMapWindow.OnMouseEnter  s    
 	 

r0   c                    | j                   d   dk(  r| j                  r| j                  |       |j                         }| j                  j                  |d   |d   | j                        }| j                  rJ|rH|D cg c]*  }|t        | j                  j                               v s)|, c}r| j                  d       n| j                  d       |j                          yc c}w )z.Motion event and no mouse buttons were pressedr  r  r   r&   zRight click to modify or removeN)r  r8   _onMouseMovingr   r   r   rc   r\   r   r   
SetToolTipr   )rv   r   r   r   r   s        r.   r  zBufferedMapWindow.OnMouseMoving  s    ::e	)djj&!%%c!fc!fdnnEMM"Fqa40B0B0D+E&EFOO=>OOD!

 Gs   ?*C%*C%c                 `   | j                         \  }}t        j                  j                         r}t        j                         }d}|j                  t        |      |z   t        |      z          t        j                  j                  |       t        j                  j                          yy)zCopy coordinates to cliboard,N)		GetLastENr3   TheClipboardOpenTextDataObjectSetTextstrSetDataClose)rv   r   r>  r  dodelims         r.   r   z#BufferedMapWindow.OnCopyCoordinates  s{    ~~1??!""$B EJJs1v~A./OO##B'OO!!# "r0   c                 x   |s| j                   }	 |j                  | j                         |j                  | j                         	 |j                  | j                         |j                  | j                         t        j                  dd| j                  d| j                         y#  Y lxY w#  Y <xY w)z*Clears temporary drawn lines from PseudoDCr  z$BufferedWindow.ClearLines(): lineid=z
, plineid=T)r   r  r?   RemoveIdr@   r   r   )rv   r   s     r.   
ClearLineszBufferedMapWindow.ClearLines  s    ++C	KK$LL%	KK%LL& 			{{DLL*	
 	
	s   6B. 6B5 .B25B9c                    	 t        |d         }t        |d         }| j                  j                  d   | j                  j                  d   kD  r| j                  j                  d   }n| j                  j                  d   }| j                  j                  d   | j                  j                  dz  |z  z
  }| j                  j                  d   | j                  j                  dz  |z  z   }|||z  z   }|||z  z
  }||fS #  Y yxY w)	zConvert image coordinates to real word coordinates

        :param xyCoords: image coordinates

        :return: easting, northing
        :return: None on error
        r   r&   Nr2  r1  center_eastingr   center_northing)r  r   r:  rh   ri   )	rv   xyCoordsr-   r  resr
  r  r7  r9  s	            r.   r  zBufferedMapWindow.Pixel2Cell  s    	HQK AHQK A 88??7#dhhoog&>>((//'*C((//'*CHHOO,-!1Cs0JJHHOO-.$((//A2E1LL1s7{AGe}	s   D Dc                 :   	 t        |d         }t        |d         }| j                  j                  d   | j                  j                  d   kD  r| j                  j                  d   }n| j                  j                  d   }| j                  j                  d   | j                  j                  dz  |z  z
  }| j                  j                  d   | j                  j                  dz  |z  z   }t        ||z
  |z        }t        ||z
  |z        }||fS #  Y yxY w)	z2Convert real word coordinates to image coordinatesr   r&   Nr2  r1  rW  r   rX  )r   r   r:  rh   ri   round)	rv   enCoordsr7  r9  rZ  r
  r  r-   r  s	            r.   r  zBufferedMapWindow.Cell2Pixel  s    	!%D(1+&E 88??7#dhhoog&>>((//'*C((//'*CHHOO,-!1Cs0JJHHOO-.$((//A2E1LL4!8s"#1u9#$1v	s   D Dc                    |\  }}|\  }}i }t        ||z
        dkD  rt        ||z
        dkD  r|dk7  r||kD  r||}}||kD  r||}}|dkD  r:| j                  ||f      \  |d<   |d<   | j                  ||f      \  |d<   |d<   n`|dk  rZ| j                  | dz  | dz  f      \  |d<   |d<   | j                  | j                  j                  d| j                  j                  |z
  z  z   | j                  j                  d| j                  j                  |z
  z  z   f      \  |d<   |d<   n|dk(  r||z
  }	||z
  }
|	dk(  r=|
dk(  r8|| j                  j                  dz  z
  }	|| j                  j                  dz  z
  }
| j                  |	|
f      \  |d<   |d<   | j                  | j                  j                  |	z   | j                  j                  |
z   f      \  |d<   |d<   |i k7  rI| j                  j
                  d   d	k(  rtt        | j                  j                  d   d
      | j                  j                  d<   t        | j                  j                  d   d      | j                  j                  d<   |d   |d   |d   z
  dz  z   }|d   |d   |d   z
  dz  z   }|| j                  j                  d<   || j                  j                  d<   |d   |d   z
  | j                  j                  z  | j                  j                  d<   |d   |d   z
  | j                  j                  z  | j                  j                  d<   | j                  j                  r| j                  j                          n#dD ]  }||   | j                  j                  |<     | j                  rt        | d      r| j                  d       | j                  | j                  j                  d   | j                  j                  d   | j                  j                  d   | j                  j                  d          | j                   du rd| _        yy)z,Calculates new region while (un)zoom/pan-ingr(   r   r
  r  r>  r  r   projllg     V@g     VrW  rX  r2  r1  )r  r  r>  r
  moveInfoNFT)r   r  r   rh   ri   projinfor   r:  rg   r7   alignExtentAlignExtentFromDisplayr8   r   _zoomZoomHistoryrf   )rv   r  r  rb   r  r  r  r  newregr  r  cecnks                 r.   r  zBufferedMapWindow.Zoom  s   BB rBw<!BGq 0X]BwRBBwRB !|+/??B8+D(sVC[+/??B8+D(sVC[ A+/??RC!GbS1W;M+N(sVC[+/??dhhnnr.A)BB!txx/C*DD,(sVC[ ]bBbBQw27$((..1,,$((//A--'+Bx'@$F3K'+"$dhhoo&:;($F3K
 R<xx  (D0'*488??3+?'F$'*488??3+?'G$sfSk 9Q>>BsfSk 9Q>>B 13DHHOO,-13DHHOO-.(.sfSk(ATXX^^'SDHHOOG$(.sfSk(ATXX__'TDHHOOG$++//1- 3A)/DHHOOA&3 zzgdJ7

4 $$$$	 >>U"!DN #r0   c                    t        j                  dd| j                  z         t               }t	        | j                        dkD  r)| j                  j                          | j                  d   }t	        | j                        dk  r| j                  j                          | j                  j                  |d   |d   |d   |d   d	       | j                          | j                  j                          y
)zZoom to previous extents in zoomhistory list

        Emits zoomChanged signal.
        Emits zoomHistoryUnavailable signal when stack is empty.
        r  z#BufferedWindow.ZoomBack(): hist)=%sr&   r  r   r   r   T)r  r  r>  r
  updateN)r   r   r`   r   r   poprI   r   r   r  rz   rG   )rv   r,  s     r.   ZoomBackzBufferedMapWindow.ZoomBackU  s     			!:T=M=MMNvt 1$  "##B'Dt 1$'',,. 	T!WQ47d1gdSr0   c                    d}| j                   j                  ||||f       t        | j                         dkD  r| j                   j                  d      }|r't	        j
                  dd| j                   d|       n#t	        j
                  dd| j                   z         t        | j                         dkD  r| j                  j                          n| j                  j                          | j                  j                          |S )	a  Manages a list of last 10 zoom extents

        Emits zoomChanged signal.
        Emits zoomHistoryAvailable signal when stack is not empty.
        Emits zoomHistoryUnavailable signal when stack is empty.

        All methods which are changing zoom should call this method
        to make a record in the history. The signal zoomChanged will be
        then emitted automatically.

        :param n,s,e,w: north, south, east, west

        :return: removed history item if exists (or None)
        Nr'   r   r  z#BufferedWindow.ZoomHistory(): hist=z
, removed=z%BufferedWindow.ZoomHistory(): hist=%sr&   )
r`   r   r   rm  r   r   rJ   r   rI   rG   )rv   r  r  r>  r
  removeds         r.   rf  zBufferedMapWindow.ZoomHistorym  s     Aq!-t 2%&&**1-GII##W. IIa@DDTDTUV t 1$%%**,'',,.r0   c                 @   | j                   j                  | j                  j                  d   | j                  j                  d   | j                  j                  d   | j                  j                  d   f       t	        j
                  dd| j                   z         y)a  Initializes zoom history.

        .. todo::
            First item is handled in some special way. Improve the
            documentation or fix the code.

        It does not emits any signals.

        This method can be possibly removed when the history will solve the
        fist item in different way or when GCP manager (and possibly others)
        will handle Map variable in the way that it will be prepared for
        MapWindow/BufferedWindow and thus usable to initialize history.
        r  r  r>  r
  r  z)BufferedWindow.InitZoomHistory(): hist=%sN)r`   r   r   r:  r   r   r{   s    r.   InitZoomHistoryz!BufferedMapWindow.InitZoomHistory  sy     	$$$$		
 			!@DDTDTUVr0   c                 "    t               | _        y)zReset zoom historyN)r   r`   r{   s    r.   ResetZoomHistoryz"BufferedMapWindow.ResetZoomHistory  s    6r0   c                 ~   |sC| j                   j                         j                  d      }|D cg c]  }|j                   }}|syg }d}g }d}|D ]=  }|j                  dk(  r |j                  |j                                3|j                  dk(  r|j                         }S|j                  dk(  r| j                  ro| j                  j                         |k(  rR| j                  j                         j                         \  }	}
}}}}| j                  j                  ||
|	|d       d}|j                  |j                         |j                  d	k(  s
|j                         j                         D ]  }|j                  |        @ |s | j                  j                  ||||d
       | j!                  | j                  j"                  d   | j                  j"                  d   | j                  j"                  d   | j                  j"                  d          |r| j%                          yyc c}w )a  Set display extents to match selected raster
        or vector map(s).

        :param layers: list of layers to be zoom to
        :param ignoreNulls: True to ignore null-values (valid only for rasters)
        :param render: True to re-render display
        F)checkedOnlyNraster	raster_3dvectorT)r  r  r
  r>  rl  rgb)rastrast3dvectr,  rl  r  r  r>  r
  )_gifaceGetLayerListGetSelectedLayersmaplayertyper   GetNamer8   r  GetLayerr!  GetMapBoundingBoxr   r  r   
splitlinesrf  r:  rz   )rv   layersignoreNullsrE   rb  r{  r|  r}  updatedr
  r  br>  r  trnames                   r.   	ZoomToMapzBufferedMapWindow.ZoomToMap  s    \\..0BBuBUF289enn9F9 	'EzzX%EMMO,{*x'::$,,"7"7"9U"B'+zz'<'<'>'P'P'R$Aq!Q1HH&&a1$&G"GKK

+u$"]]_779 'EKK&'	'" HH&t+d   	HHOOC HHOOC HHOOC HHOOC 		
 NN M :s   H:c                 V   | j                   j                         | j                   _        | j                  | j                   j                  d   | j                   j                  d   | j                   j                  d   | j                   j                  d          | j	                          y)z`Set display geometry to match computational region
        settings (set with g.region)
        r  r  r>  r
  N)r   r  r:  rf  rz   r{   s    r.   
ZoomToWindzBufferedMapWindow.ZoomToWind  sv     ((,,.HHOOC HHOOC HHOOC HHOOC 		
 	r0   c                    | j                   j                  d      | j                   _        | j                   j                          | j	                  | j                   j                  d   | j                   j                  d   | j                   j                  d   | j                   j                  d          | j                          y)z5Set display geometry to match default region settingsT)defaultr  r  r>  r
  N)r   r  r:  AdjustRegionrf  rz   r{   s    r.   ZoomToDefaultzBufferedMapWindow.ZoomToDefault  s    ((,,T,:HHOOC HHOOC HHOOC HHOOC 		
 	r0   c                 r   | j                   j                         }||c|d<   |d<   |d   |d   z  dz  }|d   |z   |d<   |d   |z
  |d<   |d   |d	   z  dz  }|d   |z   |d
<   |d   |z
  |d<   | j                   j                          | j                  |d   |d   |d
   |d          | j	                          y )NrW  rX  r1  rowsg       @r  r  r2  colsr>  r
  )r   r  r  rf  rz   )rv   r>  r  r:  dndes         r.   GoTozBufferedMapWindow.GoTo  s    **,>?; &):";Wov.#5./"4s./"4sWov.#5-.3s-.3s 	fSk6#;sLr0   c                 F   t        j                  d      }|rt         j                  d= | j                  j	                         }t        d| d|d   |d   |d   |d   t        |d         t        |d	         
	       |r|t         j                  d<   | j                  d       y)zNSet computational region (WIND file) to match display
        extents
        GRASS_REGIONr/  Tr  r  r>  r
  r  r  )r   	overwriter  r  r>  r
  r  r  FrE  N)rq  getenvenvironr   AlignResolutionr   r  rz   )rv   tmpregnews      r.   DisplayToWindzBufferedMapWindow.DisplayToWind  s     >*

>* hh&&(#h#h#h#hS[!S[!
	
 )/BJJ~&e$r0   c                    |rt        d      }nt        d      }t        | |d      }|j                         t        j                  k(  s|j                         s|j                          y|j                         }t        j                  |d      d   s*t        | t        d	      |z  
       |j                          y|j                          |r| j                  j                  |d       | j                  | j                  j                  d   | j                  j                  d   | j                  j                  d   | j                  j                  d          nt        d| |       | j                          y)zSet display extents/compulational region from named region
        file.

        :param zoomOnly: zoom to named region only (computational region is not saved)
        zZoom to saved region extentsz*Set compulational region from named regionloadr   titleloadsaveNwindowsr   elementr   z*Region <%s> not found. Operation canceled.r  T)
regionNamerl  r  r  r>  r
  r/  )r   r:  )r   r   	ShowModalr3   	ID_CANCELr  r   r@  	find_filer   r   r  rf  r:  r   rz   )rv   zoomOnlyr   dlgr:  s        r.   	SetRegionzBufferedMapWindow.SetRegion/  s    45EBCEUVD==?bll*#++-KKMFI>vFFG&P KKMHH&>$$$$	 z$v>r0   c                    |rt        d      }nt        d      }t        | |d      }|j                         t        j                  k(  s|j                         s|j                          yt        j                  |j                         d      d   rt        j                  | t        d	      |j                         z  t        d
      t        j                  t        j                  z        }|t        j                  k7  r|j                          y|r | j                  |j                                n| j                  |j                                |j                          y)zSave display extents/compulational region to named region
        file.

        :param display: True for display extends otherwise computational region
        z#Save display extents to region filez(Save computational region to region filesaver  Nr  r  r   z=Region file <%s> already exists. Do you want to overwrite it?Warning)r   r  captionr   )r   r   r  r3   r  r  r   r@  r  
MessageBoxYES_NOCENTREYES_saveDisplayRegion_saveCompRegion)rv   r   r  r  r  s        r.   
SaveRegionzBufferedMapWindow.SaveRegionY  s     ;<E@AEUVD==?bll*#++-KKM ??yA&IV ;;=" )ii"))+I BFF"##CKKM2  /r0   c                 $    t        dd| d|       y)zNSave region settings to region file

        :param name: region name
        r/  Tu)r  r   r3  r  N)r   )rv   r   s     r.   r  z!BufferedMapWindow._saveCompRegion  s    
 	:d#DQr0   c                 (   | j                   j                         }t        j                  d      }|rt        j                  d= t        dd| d|d   |d   |d   |d   t        |d	         t        |d
         |       |r|t        j                  d<   yy)zNSave display extents to region file

        :param name: region name
        r  r/  Tr  r  r  r>  r
  r  r  )
r  r   r3  r  r  r>  r
  r  r  r  N)r   r  rq  r  r  r   r  )rv   r   r  r  s       r.   r  z$BufferedMapWindow._saveDisplayRegion  s    
 hh'')>*

>*#h#h#h#hS[!S[!	
 )/BJJ~& r0   c                    |r)| j                  |      \  }}| j                  |      \  }}n
|\  }}|\  }}||z
  }||z
  }	| j                  j                  d   dk(  rt        rt	        j
                  ||||      }
n@t        j                  t        j                  |d      t        j                  |	d      z         }
|
||	ffS )zCalculates distance

        Ctypes required for LL-locations

        :param beginpt: first point
        :param endpt: second point
        :param screen: True for screen coordinates otherwise EN
        r_  r`  r   )	r  r   rb  
haveCtypesgislib
G_distancer  sqrtpow)rv   beginptendptscreene1n1e2n2dEastdNorthdists              r.   DistancezBufferedMapWindow.Distance  s     __W-FB__U+FBFBFBRb88V$,$$RR4D99TXXuq1DHHfq4IIJDufo&&r0   c                     | j                   S )zGet render.Map() instance)r   r{   s    r.   GetMapzBufferedMapWindow.GetMap  s    xxr0   c                 z    |s| j                   }t        | |||||      }| j                  j                  |       |S )ag  This method registers graphics to draw.

        :param type: (string) - graphics type: "point", "line" or "rectangle"
        :param pdc: PseudoDC object, default is pdcTmp
        :param setStatusFunc: function called before drawing each item
                              Status function should be in this form:
                              setStatusFunc(item, itemOrderNum)
                              item passes instance of GraphicsSetItem
                              which will be drawn itemOrderNum number of item
                              in drawing order (from O)
                              Hidden items are also counted in drawing order.
        :type setStatusFunc: function
        :param drawFunc: defines own function for drawing, if function
                         is not defined DrawCross method is used for
                         type "point", DrawLines method for type "line",
                         DrawRectangle for "rectangle".
        :param mapCoords: True if map coordinates should be set by user, otherwise pixels

        :return: reference to GraphicsSet, which was added.
        )parentMapWingraphicsTyper   setStatusFuncdrawFunc	mapCoords)r   r   rt   r   )rv   r  r   r  r  r  r  s          r.   RegisterGraphicsToDrawz(BufferedMapWindow.RegisterGraphicsToDraw  sG    . ++C%'
 	##D)r0   c                 X    || j                   v r| j                   j                  |       yy)zUnregisters GraphicsSet instance

        :param item: (GraphicsSetItem) - item to unregister

        :return: True - if item was unregistered
        :return: False - if item was not found
        TF)rt   r*  )rv   r  s     r.   r  z*BufferedMapWindow.UnregisterGraphicsToDraw  s,     4'''  ''-r0   )Fr*   )T)TTr   )TT)NNN)NN)r   NNr  )r(   r(   N)NFT)NNNT)N__name__
__module____qualname____doc__r3   ID_ANYNO_FULL_REPAINT_ON_RESIZEr2   rn   r~   r   re   rY   r   r   r  r   rT   rV   rX   r_  rX  rd  rH  r~  r  rz   r  r  r  r  rq   r  r  r  r  r  r  r  r  r  r   r   r  r   r  r  r  r  r   r  r  r  r  r
  r  r   rU  r  r  r  rn  rf  rr  rt  r  r  r  r  r  r  r  r  r  r  r  r  r  r+   r0   r.   r   r   A   s     99**W'r
;8
!;
M8z IV7 rN#`$
&PI6<'|22)!7r$!0$.UnM0
#" .D8StB. 5n(2 *0&d(!T$0LBSBj8$2$"
$06*H"T 0&PW0"1f$%8(T$LR08'4
 TX#Jr0   r   )2r  
__future__r   rq  r  r  r5  r3   grass.pydispatch.signalr   core.globalvarr   grass.scriptscriptr@  gui_core.dialogsr   gui_core.wrapr   r   r	   r
   r   r   r   r   	core.gcmdr   r   r   
core.debugr   core.settingsr   mapwin.baser   
core.utilsru  mapwin.graphicsr   core.gthreadr   grass.lib.gislibgisr  r  ImportError	TypeErrorr   r+   r0   r.   <module>r     s   . & 	   
 	 * *  (	 	 	 5 4  & %  '  ""J
p"v p"	 	Y Js   8B 	BB