2
@package gui_core.mapwindow
4
@brief Map display canvas - base class for buffered window.
9
(C) 2006-2011 by the GRASS Development Team
11
This program is free software under the GNU General Public License
12
(>=v2). Read the file COPYING that comes with GRASS for details.
14
@author Martin Landa <landa.martin gmail.com>
15
@author Michael Barton
16
@author Jachym Cepicky
21
from core.settings import UserSettings
23
class MapWindow(object):
24
"""!Abstract map display window class
26
Superclass for BufferedWindow class (2D display mode), and GLWindow
29
Subclasses have to define
30
- _bindMouseEvents method which binds MouseEvent handlers
32
- Cell2Pixel (if it is possible)
35
def __init__(self, parent, id = wx.ID_ANY,
36
Map = None, tree = None, lmgr = None, **kwargs):
37
self.parent = parent # MapFrame
42
# mouse attributes -- position on the screen, begin and end of
43
# dragging, and type of drawing
45
'begin': [0, 0], # screen coordinates
50
# last east, north coordinates, changes on mouse motion
53
# stores overridden cursor
54
self._overriddenCursor = None
56
def RegisterMouseEventHandler(self, event, handler, cursor = None):
57
"""!Binds event handler
59
Call event.Skip() in handler to allow default processing in MapWindow.
63
def OnButton(self, event):
64
# current map display's map window
65
# expects LayerManager to be the parent
66
self.mapwin = self.parent.GetLayerTree().GetMapDisplay().GetWindow()
67
if self.mapwin.RegisterMouseEventHandler(wx.EVT_LEFT_DOWN, self.OnMouseAction,
68
wx.StockCursor(wx.CURSOR_CROSS)):
69
self.parent.GetLayerTree().GetMapDisplay().Raise()
71
# handle that you cannot get coordinates
73
def OnMouseAction(self, event):
74
# get real world coordinates of mouse click
75
coor = self.mapwin.Pixel2Cell(event.GetPositionTuple()[:])
76
self.text.SetLabel('Coor: ' + str(coor))
77
self.mapwin.UnregisterMouseEventHandler(wx.EVT_LEFT_DOWN)
81
@param event one of mouse events
82
@param handler function to handle event
83
@param cursor cursor which temporary overrides current cursor
85
@return True if successful
86
@return False if event cannot be bind
89
# if it is a VDigitWindow it cannot be used
91
if hasattr(self, "digit"):
94
self.Bind(event, handler)
95
self.mouse['useBeforeGenericEvent'] = self.mouse['use']
96
self.mouse['use'] = 'genericEvent'
99
self._overriddenCursor = self.GetCursor()
100
self.SetCursor(cursor)
105
def UnregisterMouseEventHandler(self, event):
106
"""!Unbinds event handler a restores previous state
108
You should unbind to restore normal MapWindow behaviour.
109
Note that this operation will unbind any other external (non-MapWindow) handlers.
111
@param event event to unbind
113
@return True if successful
114
@return False if event cannot be unbind
116
if hasattr(self, "digit"):
119
# it is not yet possible in wxPython to unbind exact event
120
ret = self.Unbind(event)
123
self._bindMouseEvents()
125
# restore mouse use (previous state)
126
self.mouse['use'] = self.mouse['useBeforeGenericEvent']
128
# restore overridden cursor
129
if self._overriddenCursor:
130
self.SetCursor(self._overriddenCursor)
134
def Pixel2Cell(self, (x, y)):
135
raise NotImplementedError()
137
def Cell2Pixel(self, (east, north)):
138
raise NotImplementedError()
140
def OnMotion(self, event):
141
"""!Tracks mouse motion and update statusbar
146
self.lastEN = self.Pixel2Cell(event.GetPositionTuple())
149
# FIXME: special case for vdigit and access to statusbarManager
150
if self.parent.statusbarManager.GetMode() == 0: # Coordinates
152
if hasattr(self, "digit"):
153
precision = int(UserSettings.Get(group = 'projection', key = 'format',
154
subkey = 'precision'))
155
updated = self._onMotion(self.lastEN, precision)
158
self.parent.CoordinatesChanged()
163
"""!Returns last coordinates of mouse cursor.
169
def GetLayerByName(self, name, mapType, dataType = 'layer'):
170
"""!Get layer from layer tree by nam
172
@param name layer name
173
@param type 'item' / 'layer' / 'nviz'
175
@return layer / map layer properties / nviz properties
182
mapLayer = self.Map.GetListOfLayers(l_type = mapType, l_name = name)[0]
186
if dataType == 'layer':
188
item = self.tree.FindItemByData('maplayer', mapLayer)
191
if dataType == 'nviz':
192
return self.tree.GetPyData(item)[0]['nviz']
196
def GetSelectedLayer(self, type = 'layer', multi = False):
197
"""!Get selected layer from layer tree
199
@param type 'item' / 'layer' / 'nviz'
200
@param multi return first selected layer or all
202
@return layer / map layer properties / nviz properties
203
@return None / [] on failure
206
if not self.tree or \
207
not self.tree.GetSelection():
215
return self.tree.GetSelections()
217
for item in self.tree.GetSelections():
218
if not item.IsChecked():
224
if type == 'item': # -> multi = False
229
layer = self.tree.GetPyData(item)[0]['nviz']
231
layer = self.tree.GetPyData(item)[0]['maplayer']