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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Sep 27 19:42:07 EDT 2011


Author: martinl
Date: 2011-09-27 16:42:07 -0700 (Tue, 27 Sep 2011)
New Revision: 48512

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


Modified: grass/trunk/gui/wxpython/gui_modules/gdialogs.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gdialogs.py	2011-09-27 22:52:49 UTC (rev 48511)
+++ grass/trunk/gui/wxpython/gui_modules/gdialogs.py	2011-09-27 23:42:07 UTC (rev 48512)
@@ -252,6 +252,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:
@@ -259,6 +265,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()
         
@@ -269,6 +277,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(item = self.element, proportion = 0,
@@ -280,7 +291,17 @@
         
         self.dataSizer.Add(item = 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,
@@ -303,6 +324,10 @@
         
         return name.split('@', 1)[0]
 
+    def GetKey(self):
+        """!Get key column name"""
+        return self.keycol.GetValue()
+    
     def IsChecked(self, key):
         """!Get dialog properties
 
@@ -354,101 +379,108 @@
                           disableAdd = disableAdd, disableTable = disableTable,
                           showType = showType)
     
-    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
-        if showType:
-            cmd[1]['type'] = dlg.GetFeatureType()
+    # update cmd -> output name defined
+    cmd[1][cmd[2]] = outmap
+    if showType:
+        cmd[1]['type'] = dlg.GetFeatureType()
         
-        if isNative:
-            listOfVectors = grass.list_grouped('vect')[grass.gisenv()['MAPSET']]
-        else:
-            listOfVectors = gcmd.RunCommand('v.external',
-                                            quiet = True,
-                                            parent = parent,
-                                            read = True,
-                                            flags = 'l',
-                                            dsn = vExternalOut['dsn'])
+    if isNative:
+        listOfVectors = grass.list_grouped('vect')[grass.gisenv()['MAPSET']]
+    else:
+        listOfVectors = gcmd.RunCommand('v.external',
+                                        quiet = True,
+                                        parent = parent,
+                                        read = True,
+                                        flags = 'l',
+                                        dsn = vExternalOut['dsn'])
         
-        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'):
+    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
-        
-        ret = gcmd.RunCommand(prog = cmd[0],
-                              parent = parent,
-                              overwrite = overwrite,
-                              **cmd[1])
-        if ret != 0:
+        else:
+            dlgOw.Destroy()
             dlg.Destroy()
             return None
         
-        if not isNative:
-            # create link for OGR layers
-            gcmd.RunCommand('v.external',
-                            overwrite = overwrite,
+    if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
+        overwrite = True
+        
+    ret = gcmd.RunCommand(prog = cmd[0],
+                          parent = parent,
+                          overwrite = overwrite,
+                          **cmd[1])
+    if ret != 0:
+        dlg.Destroy()
+        return None
+    
+    if not isNative:
+        # create link for OGR layers
+        gcmd.RunCommand('v.external',
+                        overwrite = overwrite,
+                        parent = parent,
+                        dsn = vExternalOut['dsn'],
+                        layer = outmap)
+        
+    # create attribute table
+    if dlg.table.IsEnabled() and dlg.table.IsChecked():
+        if isNative:
+            sql = 'CREATE TABLE %s (%s INTEGER)' % (outmap, key)
+            
+            gcmd.RunCommand('db.connect',
+                            flags = 'c')
+            
+            Debug.msg(1, "SQL: %s" % sql)
+            gcmd.RunCommand('db.execute',
+                            quiet = True,
                             parent = parent,
-                            dsn = vExternalOut['dsn'],
-                            layer = outmap)
+                            input = '-',
+                            stdin = sql)
             
-        # create attribute table
-        if dlg.table.IsEnabled() and dlg.table.IsChecked():
-            key = UserSettings.Get(group = 'atm', key = 'keycolumn', subkey = 'value')
-            if isNative:
-                sql = 'CREATE TABLE %s (%s INTEGER)' % (outmap, key)
-                
-                gcmd.RunCommand('db.connect',
-                                flags = 'c')
-                
-                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')
-            # TODO: how to deal with attribute tables for OGR layers?
+            gcmd.RunCommand('v.db.connect',
+                            quiet = True,
+                            parent = parent,
+                            map = outmap,
+                            table = outmap,
+                            key = key,
+                            layer = '1')
+        # TODO: how to deal with attribute tables for OGR layers?
             
-        # return fully qualified map name
-        if '@' not in outmap:
-            outmap += '@' + grass.gisenv()['MAPSET']
+    # 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
-    
+    if log:
+        log.WriteLog(_("New vector map <%s> created") % outmap)
+        
     return dlg
 
 class SavedRegion(wx.Dialog):



More information about the grass-commit mailing list