[GRASS-dev] Re: wxGUI: MapDisplay mouse events

Wenzeslaus wenzeslaus at gmail.com
Wed Aug 17 17:13:22 EDT 2011


Hi Michael,

it is file with patch
mapdisplay_mouse_events.diff
and simple example how to use API also in diff file
mapdisplay_mouse_events_example.diff

I'm not sure why, but mailing list thinks that MIME type text/x-patch
is a binary file. Then it produces the text and links you see. Some
mail clients can handle it and instead of links they displays
attachments but some cannot.

I don't understand it completely and I don't know how to avoid it.
I'll try to attach the patch with txt suffix to find out how it works.

Vasek


On 17 August 2011 20:06, Michael Barton <michael.barton at asu.edu> wrote:
> Vasek,
>
> What are the *.bin files that download from your html links?
>
> Michael
-------------- next part --------------
Index: gui_modules/mapdisp_window.py
===================================================================
--- gui_modules/mapdisp_window.py	(revision 47653)
+++ gui_modules/mapdisp_window.py	(working copy)
@@ -49,6 +49,7 @@
     
     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 +66,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 +306,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 +358,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
         """
Index: gui_modules/nviz_mapdisp.py
===================================================================
--- gui_modules/nviz_mapdisp.py	(revision 47653)
+++ gui_modules/nviz_mapdisp.py	(working copy)
@@ -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-dev mailing list