mapwindow.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. """!
  2. @package swipe.mapwindow
  3. @brief Map Swipe map window.
  4. Class _MouseEvent taken from wxPython FloatCanvas source code (Christopher Barker).
  5. Classes:
  6. - mapwindow::SwipeBufferedWindow
  7. - mapwindow::_MouseEvent
  8. (C) 2012 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. @author Anna Kratochvilova <kratochanna gmail.com>
  12. """
  13. import wx
  14. from core.debug import Debug
  15. from mapdisp.mapwindow import BufferedWindow
  16. EVT_MY_MOUSE_EVENTS = wx.NewEventType()
  17. EVT_MY_MOTION = wx.NewEventType()
  18. EVT_MOUSE_EVENTS = wx.PyEventBinder(EVT_MY_MOUSE_EVENTS)
  19. EVT_MOTION = wx.PyEventBinder(EVT_MY_MOTION)
  20. class SwipeBufferedWindow(BufferedWindow):
  21. """!A subclass of BufferedWindow class.
  22. Enables to draw the image translated.
  23. Special mouse events with changed coordinates are used.
  24. """
  25. def __init__(self, parent, giface, Map, frame,
  26. tree = None, **kwargs):
  27. BufferedWindow.__init__(self, parent = parent, giface = giface,
  28. Map = Map, frame = frame, tree = tree, **kwargs)
  29. Debug.msg(2, "SwipeBufferedWindow.__init__()")
  30. self.specialSize = super(SwipeBufferedWindow, self).GetClientSize()
  31. self.specialCoords = [0, 0]
  32. self.imageId = 99
  33. self.movingSash = False
  34. self._mode = 'swipe'
  35. def _bindMouseEvents(self):
  36. """!Binds wx mouse events and custom mouse events"""
  37. wx.EVT_MOUSE_EVENTS(self, self._mouseActions)
  38. wx.EVT_MOTION(self, self._mouseMotion)
  39. self.Bind(EVT_MOTION, self.OnMotion)
  40. self.Bind(EVT_MOUSE_EVENTS, self.MouseActions)
  41. self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)
  42. def _RaiseMouseEvent(self, Event, EventType):
  43. """!This is called in various other places to raise a Mouse Event
  44. """
  45. Debug.msg(5, "SwipeBufferedWindow._RaiseMouseEvent()")
  46. # this computes the new coordinates from the mouse coords.
  47. x, y = Event.GetPosition()
  48. pt = x - self.GetImageCoords()[0], y - self.GetImageCoords()[1]
  49. evt = _MouseEvent(EventType, Event, self.GetId(), pt)
  50. self.GetEventHandler().ProcessEvent(evt)
  51. # this skip was not in the original code but is needed here
  52. Event.Skip()
  53. def _mouseActions(self, event):
  54. self._RaiseMouseEvent(event, EVT_MY_MOUSE_EVENTS)
  55. def _mouseMotion(self, event):
  56. self._RaiseMouseEvent(event, EVT_MY_MOTION)
  57. def GetClientSize(self):
  58. """!Overriden method which returns simulated window size.
  59. """
  60. if self._mode == 'swipe':
  61. return self.specialSize
  62. else:
  63. return super(SwipeBufferedWindow, self).GetClientSize()
  64. def SetClientSize(self, size):
  65. """!Overriden method which sets simulated window size.
  66. """
  67. Debug.msg(3, "SwipeBufferedWindow.SetClientSize(): size = %s" % size)
  68. self.specialSize = size
  69. def SetMode(self, mode):
  70. """!Sets mode of the window.
  71. @param mode mode can be 'swipe' or 'mirror'
  72. """
  73. self._mode = mode
  74. def GetImageCoords(self):
  75. """!Returns coordinates of rendered image"""
  76. if self._mode == 'swipe':
  77. return self.specialCoords
  78. else:
  79. return (0, 0)
  80. def SetImageCoords(self, coords):
  81. """!Sets coordinates of rendered image"""
  82. Debug.msg(3, "SwipeBufferedWindow.SetImageCoords(): coords = %s, %s" % (coords[0], coords[1]))
  83. self.specialCoords = coords
  84. def OnSize(self, event):
  85. """!Calls superclass's OnSize method only when needed"""
  86. Debug.msg(5, "SwipeBufferedWindow.OnSize()")
  87. if not self.movingSash:
  88. super(SwipeBufferedWindow, self).OnSize(event)
  89. def ZoomToMap(self, layers = None, ignoreNulls = False, render = True):
  90. super(SwipeBufferedWindow, self).ZoomToMap(layers, ignoreNulls, render)
  91. self.frame.UpdateRegion()
  92. def ZoomBack(self):
  93. """!Zoom last (previously stored position)
  94. """
  95. super(SwipeBufferedWindow, self).ZoomBack()
  96. self.frame.UpdateRegion()
  97. def Draw(self, pdc, img = None, drawid = None, pdctype = 'image', coords = [0, 0, 0, 0]):
  98. """!Draws image (map) with translated coordinates.
  99. """
  100. Debug.msg(2, "SwipeBufferedWindow.Draw()")
  101. if pdctype == 'image':
  102. coords = self.GetImageCoords()
  103. return super(SwipeBufferedWindow, self).Draw(pdc, img, drawid, pdctype, coords)
  104. def OnLeftDown(self, event):
  105. """!Left mouse button pressed.
  106. In case of 'pointer' mode, coordinates must be adjusted.
  107. """
  108. if self.mouse['use'] == 'pointer':
  109. evX, evY = event.GetPositionTuple()[:]
  110. imX, imY = self.GetImageCoords()
  111. self.lastpos = evX + imX, evY + imY
  112. # get decoration or text id
  113. self.dragid = None
  114. idlist = self.pdc.FindObjects(self.lastpos[0], self.lastpos[1],
  115. self.hitradius)
  116. if 99 in idlist:
  117. idlist.remove(99)
  118. if idlist:
  119. self.dragid = idlist[0] #drag whatever is on top
  120. else:
  121. super(SwipeBufferedWindow, self).OnLeftDown(event)
  122. def OnDragging(self, event):
  123. """!Mouse dragging - overlay (text) is moving.
  124. Coordinates must be adjusted.
  125. """
  126. if (self.mouse['use'] == 'pointer' and self.dragid != None):
  127. evX, evY = event.GetPositionTuple()
  128. imX, imY = self.GetImageCoords()
  129. self.DragItem(self.dragid, (evX + imX, evY + imY))
  130. else:
  131. super(SwipeBufferedWindow, self).OnDragging(event)
  132. def TranslateImage(self, dx, dy):
  133. """!Translate image and redraw.
  134. """
  135. Debug.msg(5, "SwipeBufferedWindow.TranslateImage(): dx = %s, dy = %s" % (dx, dy))
  136. self.pdc.TranslateId(self.imageId, dx, dy)
  137. self.Refresh()
  138. def SetRasterNameText(self, name, textId):
  139. """!Sets text label with map name."""
  140. self.textdict[textId] = {'bbox': wx.Rect(), 'coords': [10, 10],
  141. 'font': self.GetFont(), 'color': wx.BLACK,
  142. 'rotation': 0, 'text': name,
  143. 'active': True}
  144. def MouseActions(self, event):
  145. """!Handle mouse events and if needed let parent frame know"""
  146. super(SwipeBufferedWindow, self).MouseActions(event)
  147. if event.GetWheelRotation() != 0 or \
  148. event.LeftUp() or \
  149. event.MiddleUp():
  150. self.frame.UpdateRegion()
  151. event.Skip()
  152. def MouseDraw(self, pdc = None, begin = None, end = None):
  153. """!Overriden method to recompute coordinates back to original values
  154. so that e.g. drawing of zoom box is done properly"""
  155. Debug.msg(5, "SwipeBufferedWindow.MouseDraw()")
  156. offsetX, offsetY = self.GetImageCoords()
  157. begin = (self.mouse['begin'][0] + offsetX, self.mouse['begin'][1] + offsetY)
  158. end = (self.mouse['end'][0] + offsetX, self.mouse['end'][1] + offsetY)
  159. super(SwipeBufferedWindow, self).MouseDraw(pdc, begin, end)
  160. class _MouseEvent(wx.PyCommandEvent):
  161. """!
  162. This event class takes a regular wxWindows mouse event as a parameter,
  163. and wraps it so that there is access to all the original methods. This
  164. is similar to subclassing, but you can't subclass a wxWindows event.
  165. The goal is to be able to it just like a regular mouse event.
  166. Difference is that it is a CommandEvent, which propagates up the
  167. window hierarchy until it is handled.
  168. """
  169. def __init__(self, EventType, NativeEvent, WinID, changed = None):
  170. Debug.msg(5, "_MouseEvent:__init__()")
  171. wx.PyCommandEvent.__init__(self)
  172. self.SetEventType(EventType)
  173. self._NativeEvent = NativeEvent
  174. self.changed = changed
  175. def GetPosition(self):
  176. return wx.Point(*self.changed)
  177. def GetPositionTuple(self):
  178. return self.changed
  179. def GetX(self):
  180. return self.changed[0]
  181. def GetY(self):
  182. return self.changed[1]
  183. # this delegates all other attributes to the native event.
  184. def __getattr__(self, name):
  185. return getattr(self._NativeEvent, name)