mapwindow.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. """
  2. @package animation.mapwindow
  3. @brief Animation window
  4. Classes:
  5. - mapwindow::BufferedWindow
  6. - mapwindow::AnimationWindow
  7. (C) 2013 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. @author Anna Petrasova <kratochanna gmail.com>
  11. """
  12. import wx
  13. from core.debug import Debug
  14. class BufferedWindow(wx.Window):
  15. """
  16. A Buffered window class (http://wiki.wxpython.org/DoubleBufferedDrawing).
  17. To use it, subclass it and define a Draw(DC) method that takes a DC
  18. to draw to. In that method, put the code needed to draw the picture
  19. you want. The window will automatically be double buffered, and the
  20. screen will be automatically updated when a Paint event is received.
  21. When the drawing needs to change, you app needs to call the
  22. UpdateDrawing() method. Since the drawing is stored in a bitmap, you
  23. can also save the drawing to file by calling the
  24. SaveToFile(self, file_name, file_type) method.
  25. """
  26. def __init__(self, *args, **kwargs):
  27. # make sure the NO_FULL_REPAINT_ON_RESIZE style flag is set.
  28. kwargs['style'] = kwargs.setdefault('style', wx.NO_FULL_REPAINT_ON_RESIZE) | \
  29. wx.NO_FULL_REPAINT_ON_RESIZE
  30. wx.Window.__init__(self, *args, **kwargs)
  31. Debug.msg(2, "BufferedWindow.__init__()")
  32. wx.EVT_PAINT(self, self.OnPaint)
  33. wx.EVT_SIZE(self, self.OnSize)
  34. # OnSize called to make sure the buffer is initialized.
  35. # This might result in OnSize getting called twice on some
  36. # platforms at initialization, but little harm done.
  37. self.OnSize(None)
  38. def Draw(self, dc):
  39. ## just here as a place holder.
  40. ## This method should be over-ridden when subclassed
  41. pass
  42. def OnPaint(self, event):
  43. Debug.msg(5, "BufferedWindow.OnPaint()")
  44. # All that is needed here is to draw the buffer to screen
  45. dc = wx.BufferedPaintDC(self, self._Buffer)
  46. def OnSize(self, event):
  47. Debug.msg(5, "BufferedWindow.OnSize()")
  48. # The Buffer init is done here, to make sure the buffer is always
  49. # the same size as the Window
  50. #Size = self.GetClientSizeTuple()
  51. size = self.GetClientSize()
  52. # Make new offscreen bitmap: this bitmap will always have the
  53. # current drawing in it, so it can be used to save the image to
  54. # a file, or whatever.
  55. self._Buffer = wx.EmptyBitmap(*size)
  56. self.UpdateDrawing()
  57. # event.Skip()
  58. def SaveToFile(self, FileName, FileType=wx.BITMAP_TYPE_PNG):
  59. ## This will save the contents of the buffer
  60. ## to the specified file. See the wxWindows docs for
  61. ## wx.Bitmap::SaveFile for the details
  62. self._Buffer.SaveFile(FileName, FileType)
  63. def UpdateDrawing(self):
  64. """
  65. This would get called if the drawing needed to change, for whatever reason.
  66. The idea here is that the drawing is based on some data generated
  67. elsewhere in the system. If that data changes, the drawing needs to
  68. be updated.
  69. This code re-draws the buffer, then calls Update, which forces a paint event.
  70. """
  71. dc = wx.MemoryDC()
  72. dc.SelectObject(self._Buffer)
  73. self.Draw(dc)
  74. del dc # need to get rid of the MemoryDC before Update() is called.
  75. self.Refresh()
  76. self.Update()
  77. class AnimationWindow(BufferedWindow):
  78. def __init__(self, parent, id=wx.ID_ANY,
  79. style=wx.DEFAULT_FRAME_STYLE | wx.FULL_REPAINT_ON_RESIZE |
  80. wx.BORDER_RAISED):
  81. Debug.msg(2, "AnimationWindow.__init__()")
  82. self.bitmap = wx.EmptyBitmap(1, 1)
  83. self.parent = parent
  84. self._pdc = wx.PseudoDC()
  85. self._overlay = None
  86. self._tmpMousePos = None
  87. BufferedWindow.__init__(self, parent=parent, id=id, style=style)
  88. self.SetBackgroundColour(wx.BLACK)
  89. self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  90. self.Bind(wx.EVT_SIZE, self.OnSize)
  91. self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
  92. def Draw(self, dc):
  93. """Draws bitmap."""
  94. Debug.msg(5, "AnimationWindow.Draw()")
  95. dc.Clear() # make sure you clear the bitmap!
  96. dc.DrawBitmap(self.bitmap, x=0, y=0)
  97. def OnSize(self, event):
  98. Debug.msg(5, "AnimationWindow.OnSize()")
  99. self.DrawBitmap(self.bitmap)
  100. BufferedWindow.OnSize(self, event)
  101. if event:
  102. event.Skip()
  103. def DrawBitmap(self, bitmap):
  104. """Draws bitmap.
  105. Does not draw the bitmap if it is the same one as last time.
  106. """
  107. if self.bitmap == bitmap:
  108. return
  109. self.bitmap = bitmap
  110. self.UpdateDrawing()
  111. def DrawOverlay(self, x, y):
  112. self._pdc.BeginDrawing()
  113. self._pdc.SetId(1)
  114. self._pdc.DrawBitmap(bmp=self._overlay, x=x, y=y)
  115. self._pdc.SetIdBounds(1, wx.Rect(x, y, self._overlay.GetWidth(),
  116. self._overlay.GetHeight()))
  117. self._pdc.EndDrawing()
  118. def SetOverlay(self, bitmap, xperc, yperc):
  119. """Sets overlay bitmap (legend)
  120. :param bitmap: instance of wx.Bitmap
  121. :param xperc: x coordinate of bitmap top left corner in % of screen
  122. :param yperc: y coordinate of bitmap top left corner in % of screen
  123. """
  124. Debug.msg(3, "AnimationWindow.SetOverlay()")
  125. if bitmap:
  126. if self._overlay:
  127. self._pdc.RemoveAll()
  128. self._overlay = bitmap
  129. size = self.GetClientSize()
  130. x = xperc * size[0]
  131. y = yperc * size[1]
  132. self.DrawOverlay(x, y)
  133. else:
  134. self._overlay = None
  135. self._pdc.RemoveAll()
  136. self.UpdateDrawing()
  137. def ClearOverlay(self):
  138. """Clear overlay (legend) """
  139. Debug.msg(3, "AnimationWindow.ClearOverlay()")
  140. self._overlay = None
  141. self._pdc.RemoveAll()
  142. self.UpdateDrawing()
  143. def OnPaint(self, event):
  144. Debug.msg(5, "AnimationWindow.OnPaint()")
  145. # All that is needed here is to draw the buffer to screen
  146. dc = wx.BufferedPaintDC(self, self._Buffer)
  147. if self._overlay:
  148. self._pdc.DrawToDC(dc)
  149. def OnMouseEvents(self, event):
  150. """Handle mouse events."""
  151. # If it grows larger, split it.
  152. current = event.GetPosition()
  153. if event.LeftDown():
  154. self._dragid = None
  155. idlist = self._pdc.FindObjects(current[0], current[1],
  156. radius=10)
  157. if 1 in idlist:
  158. self._dragid = 1
  159. self._tmpMousePos = current
  160. elif event.LeftUp():
  161. self._dragid = None
  162. self._tmpMousePos = None
  163. elif event.Dragging():
  164. if self._dragid is None:
  165. return
  166. dx = current[0] - self._tmpMousePos[0]
  167. dy = current[1] - self._tmpMousePos[1]
  168. self._pdc.TranslateId(self._dragid, dx, dy)
  169. self.UpdateDrawing()
  170. self._tmpMousePos = current
  171. def GetOverlayPos(self):
  172. """Returns x, y position in pixels"""
  173. rect = self._pdc.GetIdBounds(1)
  174. return rect.GetX(), rect.GetY()