mapwindow.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, Map, frame,
  26. tree = None, lmgr = None, **kwargs):
  27. BufferedWindow.__init__(self, parent = parent, Map = Map, frame = frame, tree = tree, lmgr = lmgr, **kwargs)
  28. Debug.msg(2, "SwipeBufferedWindow.__init__()")
  29. self.specialSize = super(SwipeBufferedWindow, self).GetClientSize()
  30. self.specialCoords = [0, 0]
  31. self.imageId = 99
  32. self.movingSash = False
  33. def _bindMouseEvents(self):
  34. """!Binds wx mouse events and custom mouse events"""
  35. wx.EVT_MOUSE_EVENTS(self, self._mouseActions)
  36. wx.EVT_MOTION(self, self._mouseMotion)
  37. self.Bind(EVT_MOTION, self.OnMotion)
  38. self.Bind(EVT_MOUSE_EVENTS, self.MouseActions)
  39. self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)
  40. def _RaiseMouseEvent(self, Event, EventType):
  41. """!This is called in various other places to raise a Mouse Event
  42. """
  43. Debug.msg(5, "SwipeBufferedWindow._RaiseMouseEvent()")
  44. # this computes the new coordinates from the mouse coords.
  45. x, y = Event.GetPosition()
  46. pt = x - self.GetImageCoords()[0], y - self.GetImageCoords()[1]
  47. evt = _MouseEvent(EventType, Event, self.GetId(), pt)
  48. self.GetEventHandler().ProcessEvent(evt)
  49. # this skip was not in the original code but is needed here
  50. Event.Skip()
  51. def _mouseActions(self, event):
  52. self._RaiseMouseEvent(event, EVT_MY_MOUSE_EVENTS)
  53. def _mouseMotion(self, event):
  54. self._RaiseMouseEvent(event, EVT_MY_MOTION)
  55. def GetClientSize(self):
  56. """!Overriden method which returns simulated window size.
  57. """
  58. return self.specialSize
  59. def SetClientSize(self, size):
  60. """!Overriden method which sets simulated window size.
  61. """
  62. Debug.msg(3, "SwipeBufferedWindow.SetClientSize(): size = %s" % size)
  63. self.specialSize = size
  64. def GetImageCoords(self):
  65. """!Returns coordinates of rendered image"""
  66. return self.specialCoords
  67. def SetImageCoords(self, coords):
  68. """!Sets coordinates of rendered image"""
  69. Debug.msg(3, "SwipeBufferedWindow.SetImageCoords(): coords = %s, %s" % (coords[0], coords[1]))
  70. self.specialCoords = coords
  71. def OnSize(self, event):
  72. """!Calls superclass's OnSize method only when needed"""
  73. Debug.msg(5, "SwipeBufferedWindow.OnSize()")
  74. if not self.movingSash:
  75. super(SwipeBufferedWindow, self).OnSize(event)
  76. def ZoomToMap(self, layers = None, ignoreNulls = False, render = True):
  77. super(SwipeBufferedWindow, self).ZoomToMap(layers, ignoreNulls, render)
  78. self.frame.UpdateRegion()
  79. def ZoomBack(self):
  80. """!Zoom last (previously stored position)
  81. """
  82. super(SwipeBufferedWindow, self).ZoomBack()
  83. self.frame.UpdateRegion()
  84. def Draw(self, pdc, img = None, drawid = None, pdctype = 'image', coords = [0, 0, 0, 0]):
  85. """!Draws image (map) with translated coordinates.
  86. """
  87. Debug.msg(2, "SwipeBufferedWindow.Draw()")
  88. if pdctype == 'image':
  89. coords = self.GetImageCoords()
  90. return super(SwipeBufferedWindow, self).Draw(pdc, img, drawid, pdctype, coords)
  91. def TranslateImage(self, dx, dy):
  92. """!Translate image and redraw.
  93. """
  94. Debug.msg(5, "SwipeBufferedWindow.TranslateImage(): dx = %s, dy = %s" % (dx, dy))
  95. self.pdc.TranslateId(self.imageId, dx, dy)
  96. self.Refresh()
  97. def MouseActions(self, event):
  98. """!Handle mouse events and if needed let parent frame know"""
  99. super(SwipeBufferedWindow, self).MouseActions(event)
  100. if event.GetWheelRotation() != 0 or \
  101. event.LeftUp() or \
  102. event.MiddleUp():
  103. self.frame.UpdateRegion()
  104. event.Skip()
  105. def MouseDraw(self, pdc = None, begin = None, end = None):
  106. """!Overriden method to recompute coordinates back to original values
  107. so that e.g. drawing of zoom box is done properly"""
  108. Debug.msg(5, "SwipeBufferedWindow.MouseDraw()")
  109. offsetX, offsetY = self.GetImageCoords()
  110. begin = (self.mouse['begin'][0] + offsetX, self.mouse['begin'][1] + offsetY)
  111. end = (self.mouse['end'][0] + offsetX, self.mouse['end'][1] + offsetY)
  112. super(SwipeBufferedWindow, self).MouseDraw(pdc, begin, end)
  113. class _MouseEvent(wx.PyCommandEvent):
  114. """!
  115. This event class takes a regular wxWindows mouse event as a parameter,
  116. and wraps it so that there is access to all the original methods. This
  117. is similar to subclassing, but you can't subclass a wxWindows event.
  118. The goal is to be able to it just like a regular mouse event.
  119. Difference is that it is a CommandEvent, which propagates up the
  120. window hierarchy until it is handled.
  121. """
  122. def __init__(self, EventType, NativeEvent, WinID, changed = None):
  123. Debug.msg(5, "_MouseEvent:__init__()")
  124. wx.PyCommandEvent.__init__(self)
  125. self.SetEventType(EventType)
  126. self._NativeEvent = NativeEvent
  127. self.changed = changed
  128. def GetPosition(self):
  129. return wx.Point(*self.changed)
  130. def GetPositionTuple(self):
  131. return self.changed
  132. def GetX(self):
  133. return self.changed[0]
  134. def GetY(self):
  135. return self.changed[1]
  136. # this delegates all other attributes to the native event.
  137. def __getattr__(self, name):
  138. return getattr(self._NativeEvent, name)