[GRASS-SVN] r47745 - grass/branches/develbranch_6/gui/wxpython/gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 18 11:49:09 EDT 2011


Author: martinl
Date: 2011-08-18 08:49:09 -0700 (Thu, 18 Aug 2011)
New Revision: 47745

Modified:
   grass/branches/develbranch_6/gui/wxpython/gui_modules/mapdisp_window.py
Log:
wxGUI: processing mouse events from MapDisplay? (patch by Vaclav Petras)
       see  http://lists.osgeo.org/pipermail/grass-dev/2011-August/055371.html
       (merge r47744 from trunk)


Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/mapdisp_window.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/mapdisp_window.py	2011-08-18 15:46:21 UTC (rev 47744)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/mapdisp_window.py	2011-08-18 15:49:09 UTC (rev 47745)
@@ -49,6 +49,9 @@
     
     Superclass for BufferedWindow class (2D display mode), and GLWindow
     (3D display mode).
+    
+    Subclasses have to define _bindMouseEvents() method which binds
+    MouseEvent handlers.
     """
     def __init__(self, parent, id = wx.ID_ANY,
                  Map = None, tree = None, lmgr = None, **kwargs):
@@ -65,7 +68,89 @@
             'use'  : "pointer",
             'box'  : "point"
             }
+        
+        # stores overriden cursor
+        self._overridenCursor = None
 
+    def RegisterMouseEventHandler(self, event, handler, cursor = None):
+        """!Binds event handler
+        
+        Call event.Skip() in handler to allow default processing in MapWindow.
+        
+        \code
+        # your class methods
+        def OnButton(self, event):
+            # current map display's map window
+            # expects LayerManager to be the parent
+            self.mapwin = self.parent.GetLayerTree().GetMapDisplay().GetWindow()
+            if self.mapwin.RegisterMouseEventHandler(wx.EVT_LEFT_DOWN, self.OnMouseAction,
+                                                     wx.StockCursor(wx.CURSOR_CROSS)):
+                self.mapwin.Raise()
+            else:
+                # handle that you cannot get coordinates
+        
+        def OnMouseAction(self, event):
+            # get real world coordinates of mouse click
+            coor = self.mapwin.Pixel2Cell(event.GetPositionTuple()[:])
+            self.text.SetLabel('Coor: ' + str(coor))
+            self.mapwin.UnregisterMouseEventHandler(wx.EVT_LEFT_DOWN)
+            event.Skip()
+        \endcode
+        
+        @param event one of mouse events
+        @param handler function to handle event
+        @param cursor cursor which temporary overrides current cursor
+        
+        @return True if successfull
+        @return False if event cannot be bind
+        """
+        
+        # if it is a VDigitWindow it cannot be used
+        # hasattr is ugly
+        if hasattr(self, "digit"):
+            return False
+        
+        self.Bind(event, handler)
+        self.mouse['useBeforeGenericEvent'] = self.mouse['use']
+        self.mouse['use'] = 'genericEvent'
+        
+        if cursor:
+            self._overridenCursor = self.GetCursor()
+            self.SetCursor(cursor)
+        
+        return True
+
+
+    def UnregisterMouseEventHandler(self, event):
+        """!Unbinds event handler a restores previous state
+        
+        You should unbind to restore normal MapWindow behaviour.
+        Note that this operation will unbind any other external (non-MapWindow) handlers.
+        
+        @param event event to unbind
+        
+        @return True if successfull
+        @return False if event cannot be unbind
+        """
+        if hasattr(self, "digit"):
+            return False
+        
+        # it is not yet posible in wxPython to unbind exact event
+        ret = self.Unbind(event)
+        
+        # restore bind state
+        self._bindMouseEvents()
+        
+        # restore mouse use (previous state)
+        self.mouse['use'] = self.mouse['useBeforeGenericEvent']
+        
+        # restore overriden cursor
+        if self._overridenCursor:
+            self.SetCursor(self._overridenCursor)
+        
+        return ret
+
+
     def OnMotion(self, event):
         """!Track mouse motion and update statusbar
         """
@@ -222,8 +307,7 @@
         self.Bind(wx.EVT_PAINT,        self.OnPaint)
         self.Bind(wx.EVT_SIZE,         self.OnSize)
         self.Bind(wx.EVT_IDLE,         self.OnIdle)
-        self.Bind(wx.EVT_MOUSE_EVENTS, self.MouseActions)
-        self.Bind(wx.EVT_MOTION,       self.OnMotion)
+        self._bindMouseEvents()
         
         self.processMouse = True
         
@@ -275,6 +359,10 @@
         # pseudoDC for temporal objects (select box, measurement tool, etc.)
         self.pdcTmp = wx.PseudoDC()
         
+    def _bindMouseEvents(self):
+        self.Bind(wx.EVT_MOUSE_EVENTS, self.MouseActions)
+        self.Bind(wx.EVT_MOTION,       self.OnMotion)
+        
     def Draw(self, pdc, img = None, drawid = None, pdctype = 'image', coords = [0, 0, 0, 0]):
         """!Draws map and overlay decorations
         """



More information about the grass-commit mailing list