mapwindow.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 core.utils import _
  16. from core.settings import UserSettings
  17. from mapwin.buffered import BufferedMapWindow
  18. EVT_MY_MOUSE_EVENTS = wx.NewEventType()
  19. EVT_MY_MOTION = wx.NewEventType()
  20. EVT_MOUSE_EVENTS = wx.PyEventBinder(EVT_MY_MOUSE_EVENTS)
  21. EVT_MOTION = wx.PyEventBinder(EVT_MY_MOTION)
  22. class SwipeBufferedWindow(BufferedMapWindow):
  23. """A subclass of BufferedWindow class.
  24. Enables to draw the image translated.
  25. Special mouse events with changed coordinates are used.
  26. """
  27. def __init__(self, parent, giface, Map, properties, **kwargs):
  28. BufferedMapWindow.__init__(self, parent=parent, giface=giface, Map=Map,
  29. properties=properties, **kwargs)
  30. Debug.msg(2, "SwipeBufferedWindow.__init__()")
  31. self.specialSize = super(SwipeBufferedWindow, self).GetClientSize()
  32. self.specialCoords = [0, 0]
  33. self.imageId = 99
  34. self.movingSash = False
  35. self._mode = 'swipe'
  36. self.lineid = wx.NewId()
  37. def _bindMouseEvents(self):
  38. """Binds wx mouse events and custom mouse events"""
  39. wx.EVT_MOUSE_EVENTS(self, self._mouseActions)
  40. wx.EVT_MOTION(self, self._mouseMotion)
  41. self.Bind(EVT_MOTION, self.OnMotion)
  42. self.Bind(EVT_MOUSE_EVENTS, self.MouseActions)
  43. self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)
  44. def _RaiseMouseEvent(self, Event, EventType):
  45. """This is called in various other places to raise a Mouse Event
  46. """
  47. Debug.msg(5, "SwipeBufferedWindow._RaiseMouseEvent()")
  48. # this computes the new coordinates from the mouse coords.
  49. x, y = Event.GetPosition()
  50. pt = x - self.GetImageCoords()[0], y - self.GetImageCoords()[1]
  51. evt = _MouseEvent(EventType, Event, self.GetId(), pt)
  52. self.GetEventHandler().ProcessEvent(evt)
  53. # this skip was not in the original code but is needed here
  54. Event.Skip()
  55. def _mouseActions(self, event):
  56. self._RaiseMouseEvent(event, EVT_MY_MOUSE_EVENTS)
  57. def _mouseMotion(self, event):
  58. self._RaiseMouseEvent(event, EVT_MY_MOTION)
  59. def GetClientSize(self):
  60. """Overriden method which returns simulated window size.
  61. """
  62. if self._mode == 'swipe':
  63. return self.specialSize
  64. else:
  65. return super(SwipeBufferedWindow, self).GetClientSize()
  66. def SetClientSize(self, size):
  67. """Overriden method which sets simulated window size.
  68. """
  69. Debug.msg(3, "SwipeBufferedWindow.SetClientSize(): size = %s" % size)
  70. self.specialSize = size
  71. def SetMode(self, mode):
  72. """Sets mode of the window.
  73. :param mode: mode can be 'swipe' or 'mirror'
  74. """
  75. self._mode = mode
  76. def GetImageCoords(self):
  77. """Returns coordinates of rendered image"""
  78. if self._mode == 'swipe':
  79. return self.specialCoords
  80. else:
  81. return (0, 0)
  82. def SetImageCoords(self, coords):
  83. """Sets coordinates of rendered image"""
  84. Debug.msg(
  85. 3, "SwipeBufferedWindow.SetImageCoords(): coords = %s, %s" %
  86. (coords[0], coords[1]))
  87. self.specialCoords = coords
  88. def OnSize(self, event):
  89. """Calls superclass's OnSize method only when needed"""
  90. Debug.msg(5, "SwipeBufferedWindow.OnSize()")
  91. if not self.movingSash:
  92. super(SwipeBufferedWindow, self).OnSize(event)
  93. def Draw(self, pdc, img=None, drawid=None, pdctype='image',
  94. coords=[0, 0, 0, 0], pen=None, brush=None):
  95. """Draws image (map) with translated coordinates.
  96. """
  97. Debug.msg(2, "SwipeBufferedWindow.Draw()")
  98. if pdctype == 'image':
  99. coords = self.GetImageCoords()
  100. return super(SwipeBufferedWindow, self).Draw(
  101. pdc, img, drawid, pdctype, coords, pen, brush)
  102. def OnLeftDown(self, event):
  103. """Left mouse button pressed.
  104. In case of 'pointer' mode, coordinates must be adjusted.
  105. """
  106. if self.mouse['use'] == 'pointer':
  107. evX, evY = event.GetPositionTuple()[:]
  108. imX, imY = self.GetImageCoords()
  109. self.lastpos = evX + imX, evY + imY
  110. # get decoration or text id
  111. self.dragid = None
  112. idlist = self.pdc.FindObjects(self.lastpos[0], self.lastpos[1],
  113. self.hitradius)
  114. if 99 in idlist:
  115. idlist.remove(99)
  116. if idlist:
  117. self.dragid = idlist[0] # drag whatever is on top
  118. else:
  119. super(SwipeBufferedWindow, self).OnLeftDown(event)
  120. def OnDragging(self, event):
  121. """Mouse dragging - overlay (text) is moving.
  122. Coordinates must be adjusted.
  123. """
  124. if (self.mouse['use'] == 'pointer' and self.dragid is not None):
  125. evX, evY = event.GetPositionTuple()
  126. imX, imY = self.GetImageCoords()
  127. self.DragItem(self.dragid, (evX + imX, evY + imY))
  128. else:
  129. super(SwipeBufferedWindow, self).OnDragging(event)
  130. def TranslateImage(self, dx, dy):
  131. """Translate image and redraw.
  132. """
  133. Debug.msg(
  134. 5, "SwipeBufferedWindow.TranslateImage(): dx = %s, dy = %s" %
  135. (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. 'background': wx.LIGHT_GREY,
  143. 'rotation': 0, 'text': name,
  144. 'active': True}
  145. def MouseDraw(self, pdc=None, begin=None, end=None):
  146. """Overriden method to recompute coordinates back to original values
  147. so that e.g. drawing of zoom box is done properly"""
  148. Debug.msg(5, "SwipeBufferedWindow.MouseDraw()")
  149. offsetX, offsetY = self.GetImageCoords()
  150. begin = (
  151. self.mouse['begin'][0] +
  152. offsetX,
  153. self.mouse['begin'][1] +
  154. offsetY)
  155. end = (self.mouse['end'][0] + offsetX, self.mouse['end'][1] + offsetY)
  156. super(SwipeBufferedWindow, self).MouseDraw(pdc, begin, end)
  157. def DrawMouseCursor(self, coords):
  158. """Draw moving cross."""
  159. self.pdcTmp.ClearId(self.lineid)
  160. color = UserSettings.Get(
  161. group='mapswipe',
  162. key='cursor',
  163. subkey='color')
  164. cursType = UserSettings.Get(
  165. group='mapswipe', key='cursor', subkey=[
  166. 'type', 'selection'])
  167. size = UserSettings.Get(group='mapswipe', key='cursor', subkey='size')
  168. width = UserSettings.Get(
  169. group='mapswipe',
  170. key='cursor',
  171. subkey='width')
  172. if cursType == 0:
  173. self.lineid = self.DrawCross(
  174. pdc=self.pdcTmp,
  175. coords=coords,
  176. size=size,
  177. pen=wx.Pen(
  178. wx.Colour(
  179. *color),
  180. width))
  181. elif cursType == 1:
  182. self.lineid = self.DrawRectangle(
  183. pdc=self.pdcTmp,
  184. point1=(
  185. coords[0] - size / 2,
  186. coords[1] - size / 2),
  187. point2=(
  188. coords[0] + size / 2,
  189. coords[1] + size / 2),
  190. pen=wx.Pen(
  191. wx.Colour(
  192. *color),
  193. width))
  194. elif cursType == 2:
  195. self.lineid = self.DrawCircle(pdc=self.pdcTmp,
  196. coords=coords, radius=size / 2,
  197. pen=wx.Pen(wx.Colour(*color), width))
  198. class _MouseEvent(wx.PyCommandEvent):
  199. """
  200. This event class takes a regular wxWindows mouse event as a parameter,
  201. and wraps it so that there is access to all the original methods. This
  202. is similar to subclassing, but you can't subclass a wxWindows event.
  203. The goal is to be able to it just like a regular mouse event.
  204. Difference is that it is a CommandEvent, which propagates up the
  205. window hierarchy until it is handled.
  206. """
  207. def __init__(self, EventType, NativeEvent, WinID, changed=None):
  208. Debug.msg(5, "_MouseEvent:__init__()")
  209. wx.PyCommandEvent.__init__(self)
  210. self.__dict__['_NativeEvent'] = NativeEvent
  211. self.__dict__['changed'] = changed
  212. self.SetEventType(EventType)
  213. def GetPosition(self):
  214. return wx.Point(*self.changed)
  215. def GetPositionTuple(self):
  216. return self.changed
  217. def GetX(self):
  218. return self.changed[0]
  219. def GetY(self):
  220. return self.changed[1]
  221. # this delegates all other attributes to the native event.
  222. def __getattr__(self, name):
  223. return getattr(self._NativeEvent, name)