[GRASS-SVN] r58018 - in grass/trunk/gui/wxpython: gui_core lmgr

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Oct 16 11:57:51 PDT 2013


Author: martinl
Date: 2013-10-16 11:57:51 -0700 (Wed, 16 Oct 2013)
New Revision: 58018

Modified:
   grass/trunk/gui/wxpython/gui_core/dialogs.py
   grass/trunk/gui/wxpython/lmgr/layertree.py
Log:
wxGUI: add 'make a copy' item to the map layer contextual menu


Modified: grass/trunk/gui/wxpython/gui_core/dialogs.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/dialogs.py	2013-10-16 16:35:31 UTC (rev 58017)
+++ grass/trunk/gui/wxpython/gui_core/dialogs.py	2013-10-16 18:57:51 UTC (rev 58018)
@@ -2512,9 +2512,9 @@
     It differs from wx.TextEntryDialog because it allows adding validator.
     """
     def __init__(self, parent, message, caption='',
-                 defaultValue='', pos=wx.DefaultPosition, validator=wx.DefaultValidator,
-                 style=wx.OK | wx.CANCEL):
-        wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=caption, pos=pos)
+                 defaultValue='', validator=wx.DefaultValidator,
+                 style=wx.OK | wx.CANCEL, **kwargs):
+        wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=caption, **kwargs)
 
         vbox = wx.BoxSizer(wx.VERTICAL)
 

Modified: grass/trunk/gui/wxpython/lmgr/layertree.py
===================================================================
--- grass/trunk/gui/wxpython/lmgr/layertree.py	2013-10-16 16:35:31 UTC (rev 58017)
+++ grass/trunk/gui/wxpython/lmgr/layertree.py	2013-10-16 18:57:51 UTC (rev 58018)
@@ -32,7 +32,7 @@
 from grass.script import vector as gvector
 
 from core                 import globalvar
-from gui_core.dialogs     import SqlQueryFrame, SetOpacityDialog
+from gui_core.dialogs     import SqlQueryFrame, SetOpacityDialog, TextEntryDialog
 from gui_core.forms       import GUI
 from mapdisp.frame        import MapFrame
 from core.render          import Map
@@ -42,13 +42,13 @@
 from core.debug           import Debug
 from core.settings        import UserSettings, GetDisplayVectSettings
 from vdigit.main          import haveVDigit
-from core.gcmd            import GWarning, GError
+from core.gcmd            import GWarning, GError, RunCommand
 from gui_core.toolbars    import BaseIcons
 from icons.icon           import MetaIcon
 from web_services.dialogs import SaveWMSLayerDialog
+from gui_core.widgets import GenericValidator
 from lmgr.giface import LayerManagerGrassInterfaceForMapDisplay
 
-
 TREE_ITEM_HEIGHT = 25
 
 LMIcons = {
@@ -404,9 +404,12 @@
             for key in ('remove', 'rename', 'opacity', 'nviz', 'zoom',
                         'region', 'export', 'attr', 'edit0', 'edit1', 'save_ws',
                         'bgmap', 'topo', 'meta', 'null', 'zoom1', 'region1',
-                        'color', 'hist', 'univar', 'prof', 'properties', 'sql'):
+                        'color', 'hist', 'univar', 'prof', 'properties', 'sql', 'copy'):
                 self.popupID[key] = wx.NewId()
         
+        # get current mapset
+        currentMapset = grass.gisenv()['MAPSET']
+
         self.popupMenu = wx.Menu()
         
         numSelected = len(self.GetSelections())
@@ -463,6 +466,11 @@
                                                                         'input=%s' % mapLayer.GetName()]),
                       id = self.popupID['export'])
             
+            lmapset = self.GetLayerInfo(self.layer_selected, key = 'maplayer').GetMapset()
+            if lmapset != currentMapset:
+                self.popupMenu.Append(self.popupID['copy'], text = _("Make a copy in the current mapset"))
+                self.Bind(wx.EVT_MENU, self.OnCopyMap, id = self.popupID['copy'])
+            
             self.popupMenu.AppendSeparator()
 
             self.popupMenu.Append(self.popupID['color'], _("Set color table"))
@@ -506,7 +514,7 @@
                     self.popupMenu.Append(self.popupID['sql'], text = _("SQL Spatial Query"))
                     self.Bind(wx.EVT_MENU, self.OnSqlQuery, id = self.popupID['sql'])
             
-            if layer.GetMapset() != grass.gisenv()['MAPSET']:
+            if layer.GetMapset() != currentMapset:
                 # only vector map in current mapset can be edited
                 self.popupMenu.Enable (self.popupID['edit0'], False)
                 self.popupMenu.Enable (self.popupID['edit1'], False)
@@ -543,6 +551,11 @@
                 self.Bind(wx.EVT_MENU, lambda x: self.lmgr.OnMenuCmd(cmd = ['r.out.gdal',
                                                                             'input=%s' % mapLayer.GetName()]),
                           id = self.popupID['export'])
+
+                lmapset = self.GetLayerInfo(self.layer_selected, key = 'maplayer').GetMapset()
+                if lmapset != currentMapset:
+                    self.popupMenu.Append(self.popupID['copy'], text = _("Make a copy in the current mapset"))
+                    self.Bind(wx.EVT_MENU, self.OnCopyMap, id = self.popupID['copy'])
                 
                 self.popupMenu.AppendSeparator()
                 
@@ -703,6 +716,73 @@
         GUI(parent = self, centreOnParent = False).ParseCommand(['v.colors',
                                                                  'map=%s' % name])
         
+    def _mapNameValidationFailed(self, ctrl):
+        message = _("Name <%(name)s> is not a valid name for GRASS map. "
+                    "Please use only ASCII characters excluding %(chars)s "
+                    "and space.") % {'name': ctrl.GetValue(), 'chars': '/"\'@,=*~'}
+        GError(parent=self, message=message, caption=_("Invalid name"))
+
+    def OnCopyMap(self, event):
+        """!Copy selected map into current mapset"""
+        layer = self.GetSelectedLayer()
+        ltype = self.GetLayerInfo(layer, key='type')
+        lnameSrc = self.GetLayerInfo(layer, key = 'maplayer').GetName()
+        
+        if ltype == 'raster':
+            key = 'rast'
+            label = _('Raster map')
+        elif ltype == 'vector':
+            key = 'vect'
+            label = _('Vector map')
+        elif ltype == '3d-raster':
+            key = 'rast3d'
+            label = _('3D raster map')
+        else:
+            GError(_("Unsupported map type <%s>") % ltype, parent = self)
+            return
+        
+        # TODO: replace by New[Raster|Vector]Dialog
+        dlg = TextEntryDialog(parent = self,
+                              message = _('Enter name for the new %s in the current mapset:') % label.lower(),
+                              caption = _('Make a copy of %s <%s>') % (label.lower(), lnameSrc),
+                              defaultValue = lnameSrc.split('@')[0],
+                              validator = GenericValidator(grass.legal_name, self._mapNameValidationFailed),
+                              size = (700, -1))
+        if dlg.ShowModal() == wx.ID_OK:
+            lnameDst = dlg.GetValue()
+            dlg.Destroy()
+        else:
+            dlg.Destroy()
+            return
+        
+        currentMapset = grass.gisenv()['MAPSET']
+        # check if map already exists
+        if lnameDst in grass.list_grouped(key)[currentMapset]:
+            dlgOw = wx.MessageDialog(parent = self, message = _("%s <%s> already exists "
+                                                                "in the current mapset. "
+                                                                "Do you want to overwrite it?") % (label, lnameDst),
+                                     caption = _("Overwrite?"),
+                                     style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
+            if dlgOw.ShowModal() != wx.ID_YES:
+                return
+        
+        kwargs = {key : '%s,%s' % (lnameSrc, lnameDst)}
+        if 0 != RunCommand('g.copy', overwrite = True, **kwargs):
+            GError(_("Unable to make copy of <%s>") % lnameSrc,
+                   parent=self)
+            return
+        
+        if '@' in lnameDst:
+            mapsetDst = lnameDst.split('@')[1]
+            if mapsetDst != currentMapset:
+                GError(_("Unable to make copy of <%s>. Mapset <%s> is not current mapset.") % \
+                           (lnameSrc, mapsetDst))
+                return
+            
+        lnameDst += '@' + currentMapset
+        # add copied map to the layer tree
+        self.AddLayer(ltype, lname = lnameDst, lcmd = ['d.%s' % key, 'map=%s' % lnameDst])
+
     def OnHistogram(self, event):
         """!Plot histogram for given raster map layer
         """



More information about the grass-commit mailing list