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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Sep 27 19:50:10 EDT 2011


Author: martinl
Date: 2011-09-27 16:50:10 -0700 (Tue, 27 Sep 2011)
New Revision: 48513

Modified:
   grass/branches/develbranch_6/gui/wxpython/gui_modules/gdialogs.py
Log:
wxGUI: add possibility to define key column when creating new map (#287)
       (merge r48512 from trunk)


Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/gdialogs.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/gdialogs.py	2011-09-27 23:42:07 UTC (rev 48512)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/gdialogs.py	2011-09-27 23:50:10 UTC (rev 48513)
@@ -46,6 +46,7 @@
 import gselect
 import menuform
 import utils
+from debug       import Debug
 from preferences import globalSettings as UserSettings
 
 class ElementDialog(wx.Dialog):
@@ -242,6 +243,12 @@
         if disableTable:
             self.table.Enable(False)
         
+        self.keycol = wx.TextCtrl(parent = self.panel, id =  wx.ID_ANY,
+                                  size = globalvar.DIALOG_SPIN_SIZE)
+        self.keycol.SetValue(UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value'))
+        if disableTable:
+            self.keycol.Enable(False)
+        
         self.addbox = wx.CheckBox(parent = self.panel,
                                   label = _('Add created map into layer tree'), style = wx.NO_BORDER)
         if disableAdd:
@@ -249,6 +256,8 @@
             self.addbox.Enable(False)
         else:
             self.addbox.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
+
+        self.table.Bind(wx.EVT_CHECKBOX, self.OnTable)
         
         self.PostInit()
         
@@ -259,6 +268,9 @@
         """!Name for vector map layer given"""
         self.OnElement(event)
         
+    def OnTable(self, event):
+        self.keycol.Enable(event.IsChecked())
+        
     def _layout(self):
         """!Do layout"""
         self.dataSizer.Add(self.element, proportion = 0,
@@ -266,7 +278,17 @@
         
         self.dataSizer.Add(self.table, proportion = 0,
                       flag = wx.EXPAND | wx.ALL, border = 1)
-        
+
+        keySizer = wx.BoxSizer(wx.HORIZONTAL)
+        keySizer.Add(item = wx.StaticText(parent = self.panel, label = _("Key column:")),
+                     proportion = 0,
+                     flag = wx.ALIGN_CENTER_VERTICAL)
+        keySizer.AddSpacer(10)
+        keySizer.Add(item = self.keycol, proportion = 0,
+                     flag = wx.ALIGN_RIGHT)
+        self.dataSizer.Add(item = keySizer, proportion = 1,
+                           flag = wx.EXPAND | wx.ALL, border = 1)
+
         self.dataSizer.AddSpacer(5)
         
         self.dataSizer.Add(item = self.addbox, proportion = 0,
@@ -289,6 +311,10 @@
         
         return name.split('@', 1)[0]
 
+    def GetKey(self):
+        """!Get key column name"""
+        return self.keycol.GetValue()
+    
     def IsChecked(self, key):
         """!Get dialog properties
 
@@ -321,83 +347,88 @@
     dlg = NewVectorDialog(parent, title = title,
                           disableAdd = disableAdd, disableTable = disableTable)
     
-    if dlg.ShowModal() == wx.ID_OK:
-        outmap = dlg.GetName()
-        if outmap == exceptMap:
-            gcmd.GError(parent = parent,
-                        message = _("Unable to create vector map <%s>.") % outmap)
-            dlg.Destroy()
-            return None
+    if dlg.ShowModal() != wx.ID_OK:
+        dlg.Destroy()
+        return None
+
+    outmap = dlg.GetName()
+    key    = dlg.GetKey()
+    if outmap == exceptMap:
+        gcmd.GError(parent = parent,
+                    message = _("Unable to create vector map <%s>.") % outmap)
+        dlg.Destroy()
+        return None
+    if dlg.table.IsEnabled() and not key:
+        gcmd.GError(parent = parent,
+                    message = _("Invalid or empty key column.\n"
+                                "Unable to create vector map <%s>.") % outmap)
+        dlg.Destroy()
+        return
         
-        if outmap == '': # should not happen
-            dlg.Destroy()
-            return None
+    if outmap == '': # should not happen
+        dlg.Destroy()
+        return None
+    
+    # update cmd -> output name defined
+    cmd[1][cmd[2]] = outmap
         
-        cmd[1][cmd[2]] = outmap
-        
-        try:
-            listOfVectors = grass.list_grouped('vect')[grass.gisenv()['MAPSET']]
-        except KeyError:
-            listOfVectors = []
-        
-        overwrite = False
-        if not UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled') and \
-                outmap in listOfVectors:
-            dlgOw = wx.MessageDialog(parent, message = _("Vector map <%s> already exists "
-                                                         "in the current mapset. "
-                                                         "Do you want to overwrite it?") % outmap,
-                                     caption = _("Overwrite?"),
-                                     style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
-            if dlgOw.ShowModal() == wx.ID_YES:
-                overwrite = True
-            else:
-                dlgOw.Destroy()
-                dlg.Destroy()
-                return None
-        
-        if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
+    listOfVectors = grass.list_grouped('vect')[grass.gisenv()['MAPSET']]
+    
+    overwrite = False
+    if not UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled') and \
+            outmap in listOfVectors:
+        dlgOw = wx.MessageDialog(parent, message = _("Vector map <%s> already exists "
+                                                     "in the current mapset. "
+                                                     "Do you want to overwrite it?") % outmap,
+                                 caption = _("Overwrite?"),
+                                 style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
+        if dlgOw.ShowModal() == wx.ID_YES:
             overwrite = True
-        
-        try:
-            gcmd.RunCommand(prog = cmd[0],
-                            overwrite = overwrite,
-                            **cmd[1])
-        except gcmd.GException, e:
-            gcmd.GError(parent = self,
-                        message = e.value)
+        else:
+            dlgOw.Destroy()
             dlg.Destroy()
             return None
+    
+    if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
+        overwrite = True
         
-        # create attribute table
-        if dlg.table.IsEnabled() and dlg.table.IsChecked():
-            key = UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value')
-            sql = 'CREATE TABLE %s (%s INTEGER)' % (outmap, key)
-            
-            gcmd.RunCommand('db.connect',
-                            flags = 'c')
-            
-            gcmd.RunCommand('db.execute',
-                            quiet = True,
-                            parent = parent,
-                            stdin = sql)
-            
-            gcmd.RunCommand('v.db.connect',
-                            quiet = True,
-                            parent = parent,
-                            map = outmap,
-                            table = outmap,
-                            key = key,
-                            layer = '1')
+    ret = gcmd.RunCommand(prog = cmd[0],
+                          parent = parent,
+                          overwrite = overwrite,
+                          **cmd[1])
+    if ret != 0:
+        dlg.Destroy()
+        return None
+    
+    # create attribute table
+    if dlg.table.IsEnabled() and dlg.table.IsChecked():
+        sql = 'CREATE TABLE %s (%s INTEGER)' % (outmap, key)
         
-        # return fully qualified map name
-        if '@' not in outmap:
-            outmap += '@' + grass.gisenv()['MAPSET']
+        gcmd.RunCommand('db.connect',
+                        flags = 'c')
         
-        if log:
-            log.WriteLog(_("New vector map <%s> created") % outmap)
-
-        return dlg
+        Debug.msg(1, "SQL: %s" % sql)
+        gcmd.RunCommand('db.execute',
+                        quiet = True,
+                        parent = parent,
+                        input = '-',
+                        stdin = sql)
+        
+        gcmd.RunCommand('v.db.connect',
+                        quiet = True,
+                        parent = parent,
+                        map = outmap,
+                        table = outmap,
+                        key = key,
+                        layer = '1')
     
+    # return fully qualified map name
+    if '@' not in outmap:
+        outmap += '@' + grass.gisenv()['MAPSET']
+    
+    if log:
+        log.WriteLog(_("New vector map <%s> created") % outmap)
+        
     return dlg
 
 class SavedRegion(wx.Dialog):



More information about the grass-commit mailing list