[GRASS-SVN] r47744 - grass/trunk/gui/wxpython/gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 18 11:46:21 EDT 2011


Author: martinl
Date: 2011-08-18 08:46:21 -0700 (Thu, 18 Aug 2011)
New Revision: 47744

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


Modified: grass/trunk/gui/wxpython/gui_modules/mapdisp_window.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/mapdisp_window.py	2011-08-18 15:35:29 UTC (rev 47743)
+++ grass/trunk/gui/wxpython/gui_modules/mapdisp_window.py	2011-08-18 15:46:21 UTC (rev 47744)
@@ -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
         """
@@ -223,8 +308,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
         
@@ -276,6 +360,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
         """

Modified: grass/trunk/gui/wxpython/gui_modules/nviz_mapdisp.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/nviz_mapdisp.py	2011-08-18 15:35:29 UTC (rev 47743)
+++ grass/trunk/gui/wxpython/gui_modules/nviz_mapdisp.py	2011-08-18 15:46:21 UTC (rev 47744)
@@ -151,9 +151,8 @@
         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
         self.Bind(wx.EVT_SIZE,             self.OnSize)
         self.Bind(wx.EVT_PAINT,            self.OnPaint)
-        self.Bind(wx.EVT_MOUSE_EVENTS,     self.OnMouseAction)
-        self.Bind(wx.EVT_MOTION,           self.OnMotion)
         self.Bind(wx.EVT_IDLE,             self.OnIdle)
+        self._bindMouseEvents()
         
         self.Bind(EVT_UPDATE_PROP,   self.UpdateMapObjProperties)
         self.Bind(EVT_UPDATE_VIEW,   self.UpdateView)
@@ -168,6 +167,10 @@
     def __del__(self):
         self.UnloadDataLayers(force = True)
         
+    def _bindMouseEvents(self):
+        self.Bind(wx.EVT_MOUSE_EVENTS,     self.OnMouseAction)
+        self.Bind(wx.EVT_MOTION,           self.OnMotion)
+        
     def InitCPlanes(self):
         """!Initialize cutting planes list"""
         for i in range(self._display.GetCPlanesCount()):



More information about the grass-commit mailing list