Bladeren bron

coordinates select: show inserted coordinates in mapwindow

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@59132 15284696-431f-4ddb-bdfa-cd5b030d7da7
Štěpán Turek 11 jaren geleden
bovenliggende
commit
4a8eb14409
3 gewijzigde bestanden met toevoegingen van 109 en 19 verwijderingen
  1. 53 10
      gui/wxpython/gui_core/gselect.py
  2. 48 5
      gui/wxpython/gui_core/widgets.py
  3. 8 4
      gui/wxpython/mapwin/graphics.py

+ 53 - 10
gui/wxpython/gui_core/gselect.py

@@ -57,7 +57,7 @@ except ImportError, e:
     print >> sys.stderr, _("Unable to import pyGRASS: %s\n"
                            "Some functionality will be not accessible") % e
 
-from gui_core.widgets  import ManageSettingsWidget
+from gui_core.widgets  import ManageSettingsWidget, CoordinatesValidator
 
 from core.gcmd     import RunCommand, GError, GMessage
 from core.utils    import GetListOfLocations, GetListOfMapsets, \
@@ -66,7 +66,6 @@ from core.utils    import GetSettingsPath, GetValidLayerName, ListSortLower
 from core.utils    import GetVectorNumberOfLayers, _
 from core.settings import UserSettings
 from core.debug    import Debug
-
 from grass.pydispatch.signal import Signal
 
 class Select(wx.combo.ComboCtrl):
@@ -2126,16 +2125,18 @@ class CoordinatesSelect(wx.Panel):
         self._giface = giface
         self.multiple = multiple
         self.mapWin   = None
+        self.drawMapWin = None
+
+        super(CoordinatesSelect, self).__init__(parent=parent, id=wx.ID_ANY)
         
-        super(CoordinatesSelect, self).__init__(parent = parent, id = wx.ID_ANY)
-        
-        self.coordsField = wx.TextCtrl(parent = self, id = wx.ID_ANY, 
-                                       size = globalvar.DIALOG_TEXTCTRL_SIZE)
+        self.coordsField = wx.TextCtrl(parent=self, id=wx.ID_ANY, 
+                                       size=globalvar.DIALOG_TEXTCTRL_SIZE,
+                                       validator=CoordinatesValidator())
         
         icon = wx.Bitmap(os.path.join(globalvar.ETCICONDIR, "grass", "pointer.png"))
-        self.buttonInsCoords = buttons.ThemedGenBitmapToggleButton(parent = self, id = wx.ID_ANY,
-                                                                   bitmap = icon,
-                                                                   size = globalvar.DIALOG_COLOR_SIZE)
+        self.buttonInsCoords = buttons.ThemedGenBitmapToggleButton(parent=self, id=wx.ID_ANY,
+                                                                   bitmap=icon,
+                                                                   size=globalvar.DIALOG_COLOR_SIZE)
         self.registered = False
         self.buttonInsCoords.Bind(wx.EVT_BUTTON, self._onClick)
         switcher = self._giface.GetMapDisplay().GetToolSwitcher()
@@ -2143,6 +2144,7 @@ class CoordinatesSelect(wx.Panel):
                                       btnId=self.buttonInsCoords.GetId(), 
                                       toggleHandler=self.buttonInsCoords.SetValue)
         self._doLayout()
+        self.coordsField.Bind(wx.EVT_TEXT, lambda event : self._draw())
         
     def _doLayout(self):
         self.dialogSizer = wx.BoxSizer(wx.HORIZONTAL)
@@ -2172,6 +2174,42 @@ class CoordinatesSelect(wx.Panel):
                 self.registered = False
                 return
 
+
+    def drawCleanUp(self):
+        if self.drawMapWin:
+            self.drawMapWin.UnregisterGraphicsToDraw(self.pointsToDraw)
+
+    def _draw(self):
+        """!Draws points representing inserted coordinates in mapwindow."""
+        if self.drawMapWin != self.mapWin:
+            self.drawCleanUp()
+            if self.mapWin:
+                self.drawMapWin = self.mapWin
+                self.pointsToDraw = self.drawMapWin.RegisterGraphicsToDraw(graphicsType="point")
+
+        if self.drawMapWin:
+                items = self.pointsToDraw.GetAllItems()
+                for i in items:
+                    self.pointsToDraw.DeleteItem(i)
+                
+                coords = self._getCoords()
+                if coords is not None:
+                    for i in range(len(coords)/2):
+                        i = i * 2
+                        self.pointsToDraw.AddItem(coords=(coords[i], coords[i + 1]))
+
+                self._giface.updateMap.emit(render=False, renderVector=False)
+
+    def _getCoords(self):
+        """!Get list of coordinates.
+
+        @return None if values are not valid 
+        """
+        if self.coordsField.GetValidator().Validate():
+            return self.coordsField.GetValue().split(',')
+
+        return None
+
     def _onMapClickHandler(self, event):
         """!Gets coordinates from mapwindow"""
         if event == "unregistered":
@@ -2184,12 +2222,17 @@ class CoordinatesSelect(wx.Panel):
             prevCoords = self.coordsField.GetValue().strip()
             if prevCoords != "":
                 prevCoords += ","
-        
+
         value = prevCoords + str(e) + "," + str(n)
         self.coordsField.SetValue(value)
 
+        self._draw()
+
     def OnClose(self):
         """!Unregistrates _onMapClickHandler from mapWin"""
+        self.drawCleanUp()
+        self._giface.updateMap.emit(render=False, renderVector=False)
+ 
         switcher = self._giface.GetMapDisplay().GetToolSwitcher()
         switcher.RemoveCustomToolFromGroup(self.buttonInsCoords.GetId())
         if self.mapWin and self.registered:

+ 48 - 5
gui/wxpython/gui_core/widgets.py

@@ -11,6 +11,7 @@ Classes:
  - widgets::SymbolButton
  - widgets::StaticWrapText
  - widgets::BaseValidator
+ - widgets::CoordinatesValidator
  - widgets::IntegerValidator
  - widgets::FloatValidator
  - widgets::GListCtrl
@@ -21,7 +22,7 @@ Classes:
  - widgets::BarscalesComboBox
  - widgets::NArrowsComboBox
 
-(C) 2008-2013 by the GRASS Development Team
+(C) 2008-2014 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.
@@ -494,16 +495,26 @@ class BaseValidator(wx.PyValidator):
             try:
                 self.type(text)
             except ValueError:
-                textCtrl.SetBackgroundColour("grey")
-                textCtrl.SetFocus()
-                textCtrl.Refresh()
+                self._notvalid()
                 return False
         
+        self._valid()
+        return True
+
+    def _notvalid(self):
+        textCtrl = self.GetWindow()
+
+        textCtrl.SetBackgroundColour("grey")
+        textCtrl.SetFocus()
+        textCtrl.Refresh()
+
+    def _valid(self):
+        textCtrl = self.GetWindow()
+
         sysColor = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)
         textCtrl.SetBackgroundColour(sysColor)
         
         textCtrl.Refresh()
-        
         return True
 
     def TransferToWindow(self):
@@ -512,6 +523,38 @@ class BaseValidator(wx.PyValidator):
     def TransferFromWindow(self):
         return True # Prevent wxDialog from complaining.
 
+class CoordinatesValidator(BaseValidator):
+    """!Validator for coordinates input (list of floats separated by comma)"""
+
+    def __init__(self):
+        BaseValidator.__init__(self)
+
+    def Validate(self):
+        """Validate input"""
+
+        textCtrl = self.GetWindow()
+        text = textCtrl.GetValue()
+        if text:
+            try:
+                text = text.split(',')
+                
+                for t in text:
+                    float(t)
+
+                if len(text)%2 != 0:
+                    return False
+
+            except ValueError:
+                self._notvalid()
+                return False
+        
+        self._valid()
+        return True
+
+    def Clone(self):
+        """!Clone validator"""
+        return CoordinatesValidator()
+
 class IntegerValidator(BaseValidator):
     """!Validator for floating-point input"""
     def __init__(self):

+ 8 - 4
gui/wxpython/mapwin/graphics.py

@@ -98,10 +98,14 @@ class GraphicsSet:
                     coords = item.GetCoords()
                 size = self.properties["size"]
 
-                self.properties["text"]['coords'] = [coords[0] + size, coords[1] + size, size, size]
-                self.properties["text"]['color'] = self.parentMapWin.pen.GetColour()
-                self.properties["text"]['text'] = item.GetPropertyVal("label")
-
+                label = item.GetPropertyVal("label")
+                if label is None:
+                    self.properties["text"] = None
+                else:
+                    self.properties["text"]['coords'] = [coords[0] + size, coords[1] + size, size, size]
+                    self.properties["text"]['color'] = self.parentMapWin.pen.GetColour()
+                    self.properties["text"]['text'] = label
+                    
                 self.drawFunc(pdc=pdc, drawid=item.GetId(),
                               coords=coords,
                               text=self.properties["text"],