[GRASS-SVN] r57394 - in grass/trunk/gui/wxpython: core gcp gui_core mapdisp

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Aug 4 06:05:27 PDT 2013


Author: wenzeslaus
Date: 2013-08-04 06:05:26 -0700 (Sun, 04 Aug 2013)
New Revision: 57394

Modified:
   grass/trunk/gui/wxpython/core/units.py
   grass/trunk/gui/wxpython/core/utils.py
   grass/trunk/gui/wxpython/gcp/mapdisplay.py
   grass/trunk/gui/wxpython/gui_core/mapwindow.py
   grass/trunk/gui/wxpython/mapdisp/frame.py
   grass/trunk/gui/wxpython/mapdisp/mapwindow.py
Log:
wxGUI: moving code from BufferedWindow and map display to utils, adding doctests, removing unused functions

Modified: grass/trunk/gui/wxpython/core/units.py
===================================================================
--- grass/trunk/gui/wxpython/core/units.py	2013-08-04 12:29:21 UTC (rev 57393)
+++ grass/trunk/gui/wxpython/core/units.py	2013-08-04 13:05:26 UTC (rev 57394)
@@ -22,6 +22,15 @@
 @author Martin Landa <landa.martin gmail.com>
 """
 
+import math
+
+if __name__ == '__main__':
+    import os
+    import sys
+    gui_wx_path = os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython')
+    if gui_wx_path not in sys.path:
+        sys.path.append(gui_wx_path)
+
 from core.utils import _
 
 class BaseUnits:
@@ -117,3 +126,96 @@
             f = 1.0e-4
 
     return f * value
+
+
+def formatDist(distance, mapunits):
+        """!Formats length numbers and units in a nice way.
+
+        Formats length numbers and units as a function of length.
+
+        @code
+        >>> formatDist(20.56915, 'metres')
+        (20.57, 'm')
+        >>> formatDist(6983.4591, 'metres')
+        (6.983, 'km')
+        >>> formatDist(0.59, 'feet')
+        (0.59, 'ft')
+        >>> formatDist(8562, 'feet')
+        (1.622, 'miles')
+        >>> formatDist(0.48963, 'degrees')
+        (29.38, 'min')
+        >>> formatDist(20.2546, 'degrees')
+        (20.25, 'deg')
+        >>> formatDist(82.146, 'unknown')
+        (82.15, 'meters')
+
+        @endcode
+
+        Accepted map units are 'meters', 'metres', 'feet', 'degree'. Any
+        other units will be considered as meters (output 'meters').
+
+        @param distance map units
+        @param mapunits map units
+
+        From code by Hamish Bowman Grass Development Team 2006.
+        """
+        if mapunits == 'metres':
+            mapunits = 'meters'
+        outunits = mapunits
+        distance = float(distance)
+        divisor = 1.0
+
+        # figure out which units to use
+        if mapunits == 'meters':
+            if distance > 2500.0:
+                outunits = 'km'
+                divisor = 1000.0
+            else:
+                outunits = 'm'
+        elif mapunits == 'feet':
+            # nano-bug: we match any "feet", but US Survey feet is really
+            #  5279.9894 per statute mile, or 10.6' per 1000 miles. As >1000
+            #  miles the tick markers are rounded to the nearest 10th of a
+            #  mile (528'), the difference in foot flavours is ignored.
+            if distance > 5280.0:
+                outunits = 'miles'
+                divisor = 5280.0
+            else:
+                outunits = 'ft'
+        elif 'degree' in mapunits:
+            # was: 'degree' in mapunits and not haveCtypes (for unknown reason)
+            if distance < 1:
+                outunits = 'min'
+                divisor = (1/60.0)
+            else:
+                outunits = 'deg'
+        else:
+            outunits = 'meters'
+
+        # format numbers in a nice way
+        if (distance / divisor) >= 2500.0:
+            outdistance = round(distance / divisor)
+        elif (distance / divisor) >= 1000.0:
+            outdistance = round(distance / divisor, 1)
+        elif (distance / divisor) > 0.0:
+            outdistance = round(distance / divisor,
+                                int(math.ceil(3 - math.log10(distance / divisor))))
+        else:
+            outdistance = float(distance / divisor)
+
+        return (outdistance, outunits)
+
+
+def doc_test():
+    """Tests the module using doctest
+
+    @return a number of failed tests
+    """
+    import doctest
+    from core.utils import do_doctest_gettext_workaround
+    do_doctest_gettext_workaround()
+    return doctest.testmod().failed
+
+
+if __name__ == '__main__':
+    sys.exit(doc_test())

Modified: grass/trunk/gui/wxpython/core/utils.py
===================================================================
--- grass/trunk/gui/wxpython/core/utils.py	2013-08-04 12:29:21 UTC (rev 57393)
+++ grass/trunk/gui/wxpython/core/utils.py	2013-08-04 13:05:26 UTC (rev 57394)
@@ -21,6 +21,11 @@
 import re
 import inspect
 
+if __name__ == '__main__':
+    gui_wx_path = os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython')
+    if gui_wx_path not in sys.path:
+        sys.path.append(gui_wx_path)
+
 from core.globalvar import ETCDIR
 if os.path.join(ETCDIR, "python") not in sys.path:
     sys.path.append(os.path.join(ETCDIR, "python"))
@@ -1062,3 +1067,75 @@
         os._exit(0)
     else:
         mainfn()
+
+
+def isInRegion(regionA, regionB):
+    """!Tests if 'regionA' is inside of 'regionB'.
+
+    For example, region A is a display region and region B is some reference
+    region, e.g., a computational region.
+
+    @code
+    >>> displayRegion = {'n': 223900, 's': 217190, 'w': 630780, 'e': 640690}
+    >>> compRegion = {'n': 228500, 's': 215000, 'w': 630000, 'e': 645000}
+    >>> isInRegion(displayRegion, compRegion)
+    True
+    >>> displayRegion = {'n':226020, 's': 212610, 'w': 626510, 'e': 646330}
+    >>> isInRegion(displayRegion, compRegion)
+    False
+
+    @endcode
+
+    @param regionA input region A as dictionary
+    @param regionB input region B as dictionary
+
+    @return True if region A is inside of region B
+    @return False othewise
+    """
+    if regionA['s'] >= regionB['s'] and \
+            regionA['n'] <= regionB['n'] and \
+            regionA['w'] >= regionB['w'] and \
+            regionA['e'] <= regionB['e']:
+        return True
+
+    return False
+
+
+def do_doctest_gettext_workaround():
+    """Setups environment for doing a doctest with gettext usage.
+
+    When using gettext with dynamically defined underscore function
+    (`_("For translation")`), doctest does not work properly. One option is to
+    use `import as` instead of dynamically defined underscore function but this
+    would require change all modules which are used by tested module. This
+    should be considered for the future. The second option is to define dummy
+    underscore function and one other function which creates the right
+    environment to satisfy all. This is done by this function.
+    """
+    def new_displayhook(string):
+        """A replacement for default `sys.displayhook`"""
+        if string is not None:
+            sys.stdout.write("%r\n" % (string,))
+
+    def new_translator(string):
+        """A fake gettext underscore function."""
+        return string
+
+    sys.displayhook = new_displayhook
+
+    import __builtin__
+    __builtin__._ = new_translator
+
+
+def doc_test():
+    """Tests the module using doctest
+
+    @return a number of failed tests
+    """
+    import doctest
+    do_doctest_gettext_workaround()
+    return doctest.testmod().failed
+
+
+if __name__ == '__main__':
+    sys.exit(doc_test())

Modified: grass/trunk/gui/wxpython/gcp/mapdisplay.py
===================================================================
--- grass/trunk/gui/wxpython/gcp/mapdisplay.py	2013-08-04 12:29:21 UTC (rev 57393)
+++ grass/trunk/gui/wxpython/gcp/mapdisplay.py	2013-08-04 13:05:26 UTC (rev 57394)
@@ -431,25 +431,6 @@
 
         win.EraseMap()
 
-    def OnZoomRegion(self, event):
-        """
-        Zoom to region
-        """
-        self.Map.getRegion()
-        self.Map.getResolution()
-        self.UpdateMap()
-        # event.Skip()
-
-    def OnAlignRegion(self, event):
-        """
-        Align region
-        """
-        if not self.Map.alignRegion:
-            self.Map.alignRegion = True
-        else:
-            self.Map.alignRegion = False
-        # event.Skip()
-    
     def SaveToFile(self, event):
         """!Save map to image
         """
@@ -516,54 +497,7 @@
         # will be called before PopupMenu returns.
         self.PopupMenu(printmenu)
         printmenu.Destroy()
-    
-    
-    def FormatDist(self, dist):
-        """!Format length numbers and units in a nice way,
-        as a function of length. From code by Hamish Bowman
-        Grass Development Team 2006"""
 
-        mapunits = self.Map.projinfo['units']
-        if mapunits == 'metres': mapunits = 'meters'
-        outunits = mapunits
-        dist = float(dist)
-        divisor = 1.0
-
-        # figure out which units to use
-        if mapunits == 'meters':
-            if dist > 2500.0:
-                outunits = 'km'
-                divisor = 1000.0
-            else: outunits = 'm'
-        elif mapunits == 'feet':
-            # nano-bug: we match any "feet", but US Survey feet is really
-            #  5279.9894 per statute mile, or 10.6' per 1000 miles. As >1000
-            #  miles the tick markers are rounded to the nearest 10th of a
-            #  mile (528'), the difference in foot flavours is ignored.
-            if dist > 5280.0:
-                outunits = 'miles'
-                divisor = 5280.0
-            else:
-                outunits = 'ft'
-        elif 'degree' in mapunits:
-            if dist < 1:
-                outunits = 'min'
-                divisor = (1/60.0)
-            else:
-                outunits = 'deg'
-
-        # format numbers in a nice way
-        if (dist/divisor) >= 2500.0:
-            outdist = round(dist/divisor)
-        elif (dist/divisor) >= 1000.0:
-            outdist = round(dist/divisor,1)
-        elif (dist/divisor) > 0.0:
-            outdist = round(dist/divisor,int(math.ceil(3-math.log10(dist/divisor))))
-        else:
-            outdist = float(dist/divisor)
-
-        return (outdist, outunits)
-
     def OnZoomToRaster(self, event):
         """!
         Set display extents to match selected raster map (ignore NULLs)

Modified: grass/trunk/gui/wxpython/gui_core/mapwindow.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/mapwindow.py	2013-08-04 12:29:21 UTC (rev 57393)
+++ grass/trunk/gui/wxpython/gui_core/mapwindow.py	2013-08-04 13:05:26 UTC (rev 57394)
@@ -107,7 +107,12 @@
         self.parent = parent
         self.Map = Map
         self._giface = giface
-        
+
+        # Emitted when someone registers as mouse event handler
+        self.mouseHandlerRegistered = Signal('BufferedWindow.mouseHandlerRegistered')
+        # Emitted when mouse event handler is unregistered
+        self.mouseHandlerUnregistered = Signal('BufferedWindow.mouseHandlerUnregistered')        
+
         # mouse attributes -- position on the screen, begin and end of
         # dragging, and type of drawing
         self.mouse = {
@@ -205,7 +210,9 @@
             self.mapwin.UnregisterMouseEventHandler(wx.EVT_LEFT_DOWN, self.OnMouseAction)
             event.Skip()
         @endcode
-        
+
+        Emits mouseHandlerRegistered signal before handler is registered.        
+
         @param event one of mouse events
         @param handler function to handle event
         @param cursor cursor which temporary overrides current cursor
@@ -213,6 +220,7 @@
         @return True if successful
         @return False if event cannot be bind
         """
+        self.mouseHandlerRegistered.emit()
         # inserts handler into list
         for containerEv, handlers in self.handlersContainer.iteritems():
             if event == containerEv: 
@@ -254,6 +262,8 @@
         Before handler is unregistered it is called with string value
         "unregistered" of event parameter.
 
+        Emits mouseHandlerUnregistered signal after handler is unregistered.
+
         @param handler handler to unbind
         @param event event from which handler will be unbinded
         
@@ -283,7 +293,8 @@
         # restore overridden cursor
         if self._overriddenCursor:
             self.SetCursor(self._overriddenCursor)
-        
+
+        self.mouseHandlerUnregistered.emit()
         return True
     
     def Pixel2Cell(self, xyCoords):

Modified: grass/trunk/gui/wxpython/mapdisp/frame.py
===================================================================
--- grass/trunk/gui/wxpython/mapdisp/frame.py	2013-08-04 12:29:21 UTC (rev 57393)
+++ grass/trunk/gui/wxpython/mapdisp/frame.py	2013-08-04 13:05:26 UTC (rev 57394)
@@ -37,6 +37,7 @@
     sys.path.append(os.path.join(globalvar.ETCDIR, "python"))
 
 from core               import globalvar
+import core.units as units
 from core.render        import Map
 from vdigit.toolbars    import VDigitToolbar
 from mapdisp.toolbars   import MapToolbar, NvizIcons
@@ -559,26 +560,7 @@
         # change the cursor
         self.MapWindow.SetCursor(self.cursors["hand"])
         self.MapWindow.SetFocus()
-    
-    # TODO: can be replaced/merged by ZoomToWind
-    def OnZoomRegion(self, event):
-        """!Zoom to region
-        """
-        self.Map.getRegion()
-        self.Map.getResolution()
-        self.UpdateMap()
-        # event.Skip()
 
-    # TODO: delete this here and in gcp
-    def OnAlignRegion(self, event):
-        """!Align region
-        """
-        if not self.Map.alignRegion:
-            self.Map.alignRegion = True
-        else:
-            self.Map.alignRegion = False
-        # event.Skip()        
-        
     def SaveToFile(self, event):
         """!Save map to image
         """
@@ -933,11 +915,12 @@
         dist, (north, east) = self.MapWindow.Distance(beginpt, endpt)
         
         dist = round(dist, 3)
-        d, dunits = self.FormatDist(dist)
-        
+        d, dunits = units.formatDist(dist, self.Map.projinfo['units'])
+
         self.totaldist += dist
-        td, tdunits = self.FormatDist(self.totaldist)
-        
+        td, tdunits = units.formatDist(self.totaldist,
+                                       self.Map.projinfo['units'])
+
         strdist = str(d)
         strtotdist = str(td)
         
@@ -981,58 +964,6 @@
         # the desired raster) is selected to be profiled
         win.OnSelectRaster(None)
 
-    # TODO: move somewhere where can be reused (utils?), remove from gcp
-    def FormatDist(self, dist):
-        """!Format length numbers and units in a nice way,
-        as a function of length. From code by Hamish Bowman
-        Grass Development Team 2006"""
-        
-        mapunits = self.Map.projinfo['units']
-        if mapunits == 'metres':
-            mapunits = 'meters'
-        outunits = mapunits
-        dist = float(dist)
-        divisor = 1.0
-        
-        # figure out which units to use
-        if mapunits == 'meters':
-            if dist > 2500.0:
-                outunits = 'km'
-                divisor = 1000.0
-            else: outunits = 'm'
-        elif mapunits == 'feet':
-            # nano-bug: we match any "feet", but US Survey feet is really
-            #  5279.9894 per statute mile, or 10.6' per 1000 miles. As >1000
-            #  miles the tick markers are rounded to the nearest 10th of a
-            #  mile (528'), the difference in foot flavours is ignored.
-            if dist > 5280.0:
-                outunits = 'miles'
-                divisor = 5280.0
-            else:
-                outunits = 'ft'
-        elif 'degree' in mapunits and \
-                not haveCtypes:
-            if dist < 1:
-                outunits = 'min'
-                divisor = (1/60.0)
-            else:
-                outunits = 'deg'
-        else:
-            outunits = 'meters'
-        
-        # format numbers in a nice way
-        if (dist/divisor) >= 2500.0:
-            outdist = round(dist/divisor)
-        elif (dist/divisor) >= 1000.0:
-            outdist = round(dist/divisor,1)
-        elif (dist/divisor) > 0.0:
-            outdist = round(dist/divisor,int(math.ceil(3-math.log10(dist/divisor))))
-        else:
-            outdist = float(dist/divisor)
-        
-        return (outdist, outunits)
-    
-
     def OnHistogramPyPlot(self, event):
         """!Init PyPlot histogram display canvas and tools
         """

Modified: grass/trunk/gui/wxpython/mapdisp/mapwindow.py
===================================================================
--- grass/trunk/gui/wxpython/mapdisp/mapwindow.py	2013-08-04 12:29:21 UTC (rev 57393)
+++ grass/trunk/gui/wxpython/mapdisp/mapwindow.py	2013-08-04 13:05:26 UTC (rev 57394)
@@ -8,7 +8,7 @@
  - mapwindow::GraphicsSet
  - mapwindow::GraphicsSetItem
 
-(C) 2006-2012 by the GRASS Development Team
+(C) 2006-2013 by the GRASS Development Team
 
 This program is free software under the GNU General Public License
 (>=v2). Read the file COPYING that comes with GRASS for details.
@@ -16,8 +16,9 @@
 @author Martin Landa <landa.martin gmail.com>
 @author Michael Barton
 @author Jachym Cepicky
- at author Vaclav Petras <wenzeslaus gmail.com> (handlers support)
 @author Stepan Turek <stepan.turek seznam.cz> (handlers support, GraphicsSet)
+ at author Anna Petrasova <kratochanna gmail.com> (refactoring)
+ at author Vaclav Petras <wenzeslaus gmail.com> (refactoring)
 """
 
 import os
@@ -38,6 +39,7 @@
 from core.settings      import UserSettings
 from gui_core.mapwindow import MapWindow
 from core.utils         import GetGEventAttribsForHandler, _
+import core.utils as utils
 
 try:
     import grass.lib.gis as gislib
@@ -100,11 +102,6 @@
         # Emitted when the zoom history stack is not empty
         self.zoomHistoryAvailable = Signal('BufferedWindow.zoomHistoryAvailable')
 
-        # Emitted when someone registers as mouse event handler
-        self.mouseHandlerRegistered = Signal('BufferedWindow.mouseHandlerRegistered')
-        # Emitted when mouse event handler is unregistered
-        self.mouseHandlerUnregistered = Signal('BufferedWindow.mouseHandlerUnregistered')
-
         # Emitted when map enters the window
         self.mouseEntered = Signal('BufferedWindow.mouseEntered')
         # Emitted when left mouse button is released and mouse use is 'pointer'
@@ -766,7 +763,7 @@
             compReg = self.Map.GetRegion()
             dispReg = self.Map.GetCurrentRegion()
             reg = None
-            if self.IsInRegion(dispReg, compReg):
+            if utils.isInRegion(dispReg, compReg):
                 self.polypen = wx.Pen(colour = wx.Colour(0, 0, 255, 128), width = 3, style = wx.SOLID)
                 reg = dispReg
             else:
@@ -783,25 +780,6 @@
             # draw region extent
             self.DrawLines(pdc=self.pdcDec, polycoords=regionCoords)
 
-    # TODO: move to utils
-    def IsInRegion(self, region, refRegion):
-        """!
-        Test if 'region' is inside of 'refRegion'
-
-        @param region input region
-        @param refRegion reference region (e.g. computational region)
-
-        @return True if region is inside of refRegion
-        @return False 
-        """
-        if region['s'] >= refRegion['s'] and \
-                region['n'] <= refRegion['n'] and \
-                region['w'] >= refRegion['w'] and \
-                region['e'] <= refRegion['e']:
-            return True
-        
-        return False
-
     def EraseMap(self):
         """!Erase map canvas
         """
@@ -1817,22 +1795,6 @@
         """!Get render.Map() instance"""
         return self.Map
 
-    def RegisterMouseEventHandler(self, event, handler, cursor = None):
-        """Registeres mouse event handler.
-
-        Emits mouseHandlerRegistered signal before handler is registered.
-        """
-        self.mouseHandlerRegistered.emit()
-        MapWindow.RegisterMouseEventHandler(self, event, handler, cursor)
-
-    def UnregisterMouseEventHandler(self, event, handler):
-        """Unregisteres mouse event handler.
-
-        Emits mouseHandlerUnregistered signal after handler is unregistered.
-        """
-        MapWindow.UnregisterMouseEventHandler(self, event, handler)
-        self.mouseHandlerUnregistered.emit()
-        
     def RegisterGraphicsToDraw(self, graphicsType, setStatusFunc = None, drawFunc = None):
         """! This method registers graphics to draw.
         



More information about the grass-commit mailing list