[GRASS-SVN] r49456 - in grass/branches/releasebranch_6_4/gui/wxpython: . gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Dec 1 06:49:04 EST 2011


Author: martinl
Date: 2011-12-01 03:49:04 -0800 (Thu, 01 Dec 2011)
New Revision: 49456

Modified:
   grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/dbm.py
   grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/gdialogs.py
   grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/toolbars.py
   grass/branches/releasebranch_6_4/gui/wxpython/wxgui.py
Log:
wxGUI: show attribute table manager when creating new vector map (bugfix)


Modified: grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/dbm.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/dbm.py	2011-12-01 08:49:48 UTC (rev 49455)
+++ grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/dbm.py	2011-12-01 11:49:04 UTC (rev 49456)
@@ -529,7 +529,7 @@
     """
     def __init__(self, parent, id=wx.ID_ANY,
                  size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE,
-                 title=None, vectorName=None, item=None, log=None):
+                 title=None, vectorName=None, item=None, log=None, selection = 0):
 
         self.vectorName = vectorName
         self.parent     = parent # GMFrame
@@ -649,9 +649,8 @@
         self.__createBrowsePage()
         self.__createManageTablePage()
         self.__createManageLayerPage()
-
-        self.notebook.SetSelection(0) # select browse tab
-
+        wx.CallAfter(self.notebook.SetSelection, selection)
+        
         #
         # buttons
         #
@@ -2051,8 +2050,7 @@
         event.Skip()
 
     def OnExtractSelected(self, event):
-        """!
-        Extract vector objects selected in attribute browse window
+        """!Extract vector objects selected in attribute browse window
         to new vector map
         """
         list = self.FindWindowById(self.layerPage[self.layer]['data'])
@@ -2065,20 +2063,23 @@
             return
         else:
             # dialog to get file name
-            name, add = gdialogs.CreateNewVector(parent = self, title = _('Extract selected features'),
-                                                 log = self.cmdLog,
-                                                 cmd = (('v.extract',
-                                                         { 'input' : self.vectorName,
-                                                           'list' : utils.ListOfCatsToRange(cats) },
-                                                         'output')),
-                                                 disableTable = True)
-            if name and add:
+            dlg = gdialogs.CreateNewVector(parent = self, title = _('Extract selected features'),
+                                           log = self.cmdLog,
+                                           cmd = (('v.extract',
+                                                   { 'input' : self.vectorName,
+                                                     'list' : utils.ListOfCatsToRange(cats) },
+                                                   'output')),
+                                           disableTable = True)
+            if not dlg:
+                return
+            
+            name = dlg.GetName(full = True)
+            if name and dlg.IsChecked('add'):
                 # add layer to map layer tree
-                self.parent.curr_page.maptree.AddLayer(ltype='vector',
-                                                       lname=name,
-                                                       lchecked=True,
-                                                       lopacity=1.0,
-                                                       lcmd=['d.vect', 'map=%s' % name])
+                self.parent.curr_page.maptree.AddLayer(ltype = 'vector',
+                                                       lname = name,
+                                                       lcmd = ['d.vect', 'map=%s' % name])
+            dlg.Destroy()
             
     def OnDeleteSelected(self, event):
         """

Modified: grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/gdialogs.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/gdialogs.py	2011-12-01 08:49:48 UTC (rev 49455)
+++ grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/gdialogs.py	2011-12-01 11:49:04 UTC (rev 49456)
@@ -46,6 +46,7 @@
 import gselect
 import menuform
 import utils
+from debug import Debug
 from preferences import globalSettings as UserSettings
 
 class ElementDialog(wx.Dialog):
@@ -216,14 +217,25 @@
         return self.GetElement()
     
 class NewVectorDialog(ElementDialog):
-    """!Dialog for creating new vector map"""
-    def __init__(self, parent, id, title, disableAdd=False, disableTable=False,
-                 style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
+    def __init__(self, parent, id = wx.ID_ANY, title = _('Create new vector map'),
+                 disableAdd = False, disableTable = False,
+                 style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, *kwargs):
+        """!Dialog for creating new vector map
+
+        @param parent parent window
+        @param id window id
+        @param title window title
+        @param disableAdd disable 'add layer' checkbox
+        @param disableTable disable 'create table' checkbox
+        @param style window style
+        @param kwargs other argumentes for ElementDialog
         
+        @return dialog instance       
+        """
         ElementDialog.__init__(self, parent, title, label = _("Name for new vector map:"))
         
-        self.element = gselect.Select(parent=self.panel, id=wx.ID_ANY, size=globalvar.DIALOG_GSELECT_SIZE,
-                                      type='vector', mapsets=[grass.gisenv()['MAPSET'],])
+        self.element = gselect.Select(parent = self.panel, id = wx.ID_ANY, size = globalvar.DIALOG_GSELECT_SIZE,
+                                      type = 'vector', mapsets = [grass.gisenv()['MAPSET'],])
         
         self.table = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
                                  label = _("Create attribute table"))
@@ -231,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:
@@ -238,125 +256,179 @@
             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()
         
-        self.__Layout()
+        self._layout()
         self.SetMinSize(self.GetSize())
         
     def OnMapName(self, event):
         """!Name for vector map layer given"""
         self.OnElement(event)
         
-    def __Layout(self):
+    def OnTable(self, event):
+        self.keycol.Enable(event.IsChecked())
+        
+    def _layout(self):
         """!Do layout"""
-        self.dataSizer.Add(self.element, proportion=0,
-                      flag=wx.EXPAND | wx.ALL, border=1)
+        self.dataSizer.Add(self.element, proportion = 0,
+                      flag = wx.EXPAND | wx.ALL, border = 1)
         
-        self.dataSizer.Add(self.table, proportion=0,
-                      flag=wx.EXPAND | wx.ALL, border=1)
-        
+        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,
-                      flag=wx.EXPAND | wx.ALL, border=1)
+        self.dataSizer.Add(item = self.addbox, proportion = 0,
+                      flag = wx.EXPAND | wx.ALL, border = 1)
         
         self.panel.SetSizer(self.sizer)
         self.sizer.Fit(self)
 
-    def GetName(self):
-        """!Return (mapName, overwrite)"""
-        return self.GetElement().split('@', 1)[0]
-            
-def CreateNewVector(parent, cmd, title=_('Create new vector map'),
-                    exceptMap=None, log=None, disableAdd=False, disableTable=False):
+    def GetName(self, full = False):
+        """!Get name of vector map to be created
+
+        @param full True to get fully qualified name
+        """
+        name = self.GetElement()
+        if full:
+            if '@' in name:
+                return name
+            else:
+                return name + '@' + grass.gisenv()['MAPSET']
+        
+        return name.split('@', 1)[0]
+
+    def GetKey(self):
+        """!Get key column name"""
+        return self.keycol.GetValue()
+    
+    def IsChecked(self, key):
+        """!Get dialog properties
+
+        @param key window key ('add', 'table')
+
+        @return True/False
+        @return None on error
+        """
+        if key == 'add':
+            return self.addbox.IsChecked()
+        elif key == 'table':
+            return self.table.IsChecked()
+        
+        return None
+
+def CreateNewVector(parent, cmd, title = _('Create new vector map'),
+                    exceptMap = None, log = None, disableAdd = False, disableTable = False):
     """!Create new vector map layer
+    
+    @param cmd (prog, **kwargs)
+    @param title window title
+    @param exceptMap list of maps to be excepted
+    @param log
+    @param disableAdd disable 'add layer' checkbox
+    @param disableTable disable 'create table' checkbox
 
-    @cmd cmd (prog, **kwargs)
+    @return dialog instance
+    @return None on error
+    """
+    dlg = NewVectorDialog(parent, title = title,
+                          disableAdd = disableAdd, disableTable = disableTable)
     
-    @return tuple (name of create vector map, add to layer tree)
-    @return None of failure
-    """
-    dlg = NewVectorDialog(parent, wx.ID_ANY, title,
-                          disableAdd, disableTable)
-    if dlg.ShowModal() == wx.ID_OK:
-        outmap = dlg.GetName()
-        if outmap == exceptMap:
-            wx.MessageBox(parent=parent,
-                          message=_("Unable to create vector map <%s>.") % outmap,
-                          caption=_("Error"),
-                          style=wx.ID_OK | wx.ICON_ERROR | wx.CENTRE)
-            return (None, 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
-            return (None, 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
+    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
+        else:
+            dlgOw.Destroy()
+            dlg.Destroy()
+            return None
+    
+    if UserSettings.Get(group = 'cmd', key = 'overwrite', subkey = 'enabled'):
+        overwrite = True
         
-        try:
-            listOfVectors = grass.list_grouped('vect')[grass.gisenv()['MAPSET']]
-        except KeyError:
-            listOfVectors = []
+    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)
         
-        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()
-                return (None, None)
-
-        if UserSettings.Get(group='cmd', key='overwrite', subkey='enabled') is True:
-            overwrite = True
+        gcmd.RunCommand('db.connect',
+                        flags = 'c')
         
-        try:
-            gcmd.RunCommand(prog = cmd[0],
-                            overwrite = overwrite,
-                            **cmd[1])
-        except gcmd.GException, e:
-            gcmd.GError(parent = self,
-                        message = e.value)
-            return (None, None)
+        Debug.msg(1, "SQL: %s" % sql)
+        gcmd.RunCommand('db.execute',
+                        quiet = True,
+                        parent = parent,
+                        stdin = sql)
         
-        #
-        # 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')
-            
-        # return fully qualified map name
-        if '@' not in outmap:
-            outmap += '@' + grass.gisenv()['MAPSET']
-
-        if log:
-            log.WriteLog(_("New vector map <%s> created") % outmap)
-
-        return (outmap, dlg.addbox.IsChecked())
+        gcmd.RunCommand('v.db.connect',
+                        quiet = True,
+                        parent = parent,
+                        map = outmap,
+                        table = outmap,
+                        key = key,
+                        layer = '1')
     
-    return (None, dlg.addbox.IsChecked())
+    # 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):
     def __init__(self, parent, id = wx.ID_ANY, title="", loadsave='load',

Modified: grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/toolbars.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/toolbars.py	2011-12-01 08:49:48 UTC (rev 49455)
+++ grass/branches/releasebranch_6_4/gui/wxpython/gui_modules/toolbars.py	2011-12-01 11:49:04 UTC (rev 49456)
@@ -976,34 +976,41 @@
                 openVectorMap = self.mapLayer.GetName(fullyQualified = False)['name']
             else:
                 openVectorMap = None
-            mapName = gdialogs.CreateNewVector(self.parent,
-                                               exceptMap = openVectorMap, log = self.log,
-                                               cmd = (('v.edit',
-                                                       { 'tool' : 'create' },
-                                                       'map')),
-                                               disableAdd = True)[0]
-            if mapName:
+            dlg = gdialogs.CreateNewVector(self.parent,
+                                           exceptMap = openVectorMap, log = self.log,
+                                           cmd = (('v.edit',
+                                                   { 'tool' : 'create' },
+                                                   'map')),
+                                           disableAdd = True)
+            
+            if dlg and dlg.GetName():
                 # add layer to map layer tree
                 if self.layerTree:
+                    mapName = dlg.GetName() + '@' + grass.gisenv()['MAPSET']
                     self.layerTree.AddLayer(ltype = 'vector',
                                             lname = mapName,
-                                            lchecked = True,
-                                            lopacity = 1.0,
                                             lcmd = ['d.vect', 'map=%s' % mapName])
                     
                     vectLayers = self.UpdateListOfLayers(updateTool = True)
                     selection = vectLayers.index(mapName)
-                else:
-                    pass # TODO (no Layer Manager)
+                
+                # create table ?
+                if dlg.IsChecked('table'):
+                    lmgr = self.parent.GetLayerManager()
+                    if lmgr:
+                        lmgr.OnShowAttributeTable(None, selection = 1)
+                dlg.Destroy()
             else:
                 self.combo.SetValue(_('Select vector map'))
-                return 
+                if dlg:
+                    dlg.Destroy()
+                return
         else:
             selection = event.GetSelection() - 1 # first option is 'New vector map'
         
         # skip currently selected map
         if self.layers[selection] == self.mapLayer:
-            return False
+            return
         
         if self.mapLayer:
             # deactive map layer for editing
@@ -1013,8 +1020,6 @@
         self.StartEditing(self.layers[selection])
         
         event.Skip()
-
-        return True
     
     def StartEditing (self, mapLayer):
         """!Start editing selected vector map layer.

Modified: grass/branches/releasebranch_6_4/gui/wxpython/wxgui.py
===================================================================
--- grass/branches/releasebranch_6_4/gui/wxpython/wxgui.py	2011-12-01 08:49:48 UTC (rev 49455)
+++ grass/branches/releasebranch_6_4/gui/wxpython/wxgui.py	2011-12-01 11:49:04 UTC (rev 49456)
@@ -596,19 +596,27 @@
         
     def OnNewVector(self, event):
         """!Create new vector map layer"""
-        name, add = gdialogs.CreateNewVector(self, log = self.goutput,
-                                             cmd = (('v.edit',
-                                                     { 'tool' : 'create' },
-                                                     'map')))
+        dlg = gdialogs.CreateNewVector(self, log = self.goutput,
+                                       cmd = (('v.edit',
+                                               { 'tool' : 'create' },
+                                               'map')))
         
-        if name and add:
+        if not dlg:
+            return
+        
+        name = dlg.GetName(full = True)
+        if name and dlg.IsChecked('add'):
             # add layer to map layer tree
             self.curr_page.maptree.AddLayer(ltype = 'vector',
                                             lname = name,
-                                            lchecked = True,
-                                            lopacity = 1.0,
                                             lcmd = ['d.vect', 'map=%s' % name])
         
+        # create table ?
+        if dlg.IsChecked('table'):
+            self.OnShowAttributeTable(None, selection = 1)
+        
+        dlg.Destroy()
+        
     def OnAboutGRASS(self, event):
         """!Display 'About GRASS' dialog"""
         win = AboutWindow(self)
@@ -1173,7 +1181,7 @@
                 
         dlg.Destroy()
         
-    def OnShowAttributeTable(self, event):
+    def OnShowAttributeTable(self, event, selection = 0):
         """!Show attribute table of the given vector map layer
         """
         if not self.curr_page:
@@ -1210,7 +1218,8 @@
         
         dbmanager = dbm.AttributeManager(parent = self, id = wx.ID_ANY,
                                          size = wx.Size(500, 300),
-                                         item = layer, log = self.goutput)
+                                         item = layer, log = self.goutput,
+                                         selection = selection)
         
         busy.Destroy()
         



More information about the grass-commit mailing list