瀏覽代碼

wxGUI: refactor changing mapset through file menu and catalog with signals (#919)

Anna Petrasova 4 年之前
父節點
當前提交
bc0728bed0

+ 2 - 0
gui/wxpython/core/giface.py

@@ -212,6 +212,8 @@ class StandaloneGrassInterface():
         # add: if map should be added to layer tree (questionable attribute)
         self.mapCreated = Signal('StandaloneGrassInterface.mapCreated')
 
+        self.currentMapsetChanged = Signal('LayerManagerGrassInterface.currentMapsetChanged')
+
         # Signal emitted to request updating of map
         self.updateMap = Signal('StandaloneGrassInterface.updateMap')
 

+ 0 - 4
gui/wxpython/datacatalog/catalog.py

@@ -33,8 +33,6 @@ class DataCatalog(wx.Panel):
                  title=_("Data catalog"), name='catalog', **kwargs):
         """Panel constructor  """
         self.showNotification = Signal('DataCatalog.showNotification')
-        self.changeMapset = Signal('DataCatalog.changeMapset')
-        self.changeLocation = Signal('DataCatalog.changeLocation')
         self.parent = parent
         self.baseTitle = title
         wx.Panel.__init__(self, parent=parent, id=id, **kwargs)
@@ -50,8 +48,6 @@ class DataCatalog(wx.Panel):
         self.thread = gThread()
         self._loaded = False
         self.tree.showNotification.connect(self.showNotification)
-        self.tree.changeMapset.connect(self.changeMapset)
-        self.tree.changeLocation.connect(self.changeLocation)
 
         # some layout
         self._layout()

+ 0 - 37
gui/wxpython/datacatalog/frame.py

@@ -55,15 +55,6 @@ class DataCatalogFrame(wx.Frame):
         self.tree.ReloadTreeItems()
         self.tree.UpdateCurrentDbLocationMapsetNode()
         self.tree.ExpandCurrentMapset()
-        self.tree.changeMapset.connect(lambda mapset:
-                                       self.ChangeDbLocationMapset(
-                                               location=None,
-                                               mapset=mapset))
-        self.tree.changeLocation.connect(lambda mapset, location, dbase:
-                                         self.ChangeDbLocationMapset(
-                                                 dbase=dbase,
-                                                 location=location,
-                                                 mapset=mapset))
 
         # buttons
         self.btnClose = Button(parent=self.panel, id=wx.ID_CLOSE)
@@ -137,33 +128,5 @@ class DataCatalogFrame(wx.Frame):
         """Allow editing other mapsets or restrict editing to current mapset"""
         self.tree.SetRestriction(restrict)
 
-    def ChangeDbLocationMapset(self, mapset, location=None, dbase=None):
-        """Change mapset, location or db"""
-        if dbase:
-            if RunCommand('g.mapset', parent=self,
-                          location=location,
-                          mapset=mapset,
-                          dbase=dbase) == 0:
-                GMessage(parent=self,
-                         message=_("Current GRASS database is <%(dbase)s>.\n"
-                                   "Current location is <%(loc)s>.\n"
-                                   "Current mapset is <%(mapset)s>."
-                                   ) %
-                         {'dbase': dbase, 'loc': location, 'mapset': mapset})
-        elif location:
-            if RunCommand('g.mapset', parent=self,
-                          location=location,
-                          mapset=mapset) == 0:
-                GMessage(parent=self,
-                         message=_("Current location is <%(loc)s>.\n"
-                                   "Current mapset is <%(mapset)s>.") %
-                         {'loc': location, 'mapset': mapset})
-        else:
-            if RunCommand('g.mapset',
-                          parent=self,
-                          mapset=mapset) == 0:
-                GMessage(parent=self,
-                         message=_("Current mapset is <%s>.") % mapset)
-
     def Filter(self, text):
         self.tree.Filter(text=text)

+ 12 - 13
gui/wxpython/datacatalog/tree.py

@@ -45,7 +45,8 @@ from startup.guiutils import (
     delete_locations_interactively,
     download_location_interactively,
     delete_grassdb_interactively,
-    can_switch_mapset_interactive
+    can_switch_mapset_interactive,
+    switch_mapset_interactively
 )
 
 from grass.pydispatch.signal import Signal
@@ -262,11 +263,10 @@ class DataCatalogTree(TreeView):
         self._restricted = True
 
         self.showNotification = Signal('Tree.showNotification')
-        self.changeMapset = Signal('Tree.changeMapset')
-        self.changeLocation = Signal('Tree.changeLocation')
         self.parent = parent
         self.contextMenu.connect(self.OnRightClick)
         self.itemActivated.connect(self.OnDoubleClick)
+        self._giface.currentMapsetChanged.connect(self._updateAfterMapsetChanged)
         self._iconTypes = ['grassdb', 'location', 'mapset', 'raster',
                            'vector', 'raster_3d']
         self._initImages()
@@ -1272,20 +1272,19 @@ class DataCatalogTree(TreeView):
         if can_switch_mapset_interactive(self, grassdb, location, mapset):
             # Switch to mapset in the same location
             if (grassdb == genv['GISDBASE'] and location == genv['LOCATION_NAME']):
-                self.changeMapset.emit(mapset=mapset)
+                switch_mapset_interactively(self, self._giface, None, None, mapset)
             # Switch to mapset in the same grassdb
             elif grassdb == genv['GISDBASE']:
-                self.changeLocation.emit(mapset=mapset,
-                                         location=location,
-                                         dbase=None)
+                switch_mapset_interactively(self, self._giface, None, location, mapset)
             # Switch to mapset in a different grassdb
             else:
-                self.changeLocation.emit(mapset=mapset,
-                                         location=location,
-                                         dbase=grassdb)
-            self.UpdateCurrentDbLocationMapsetNode()
-            self.ExpandCurrentMapset()
-            self.RefreshItems()
+                switch_mapset_interactively(self, self._giface, grassdb, location, mapset)
+
+    def _updateAfterMapsetChanged(self):
+        """Update tree after current mapset has changed"""
+        self.UpdateCurrentDbLocationMapsetNode()
+        self.ExpandCurrentMapset()
+        self.RefreshItems()
 
     def OnMetadata(self, event):
         """Show metadata of any raster/vector/3draster"""

+ 21 - 46
gui/wxpython/lmgr/frame.py

@@ -69,6 +69,7 @@ from lmgr.giface import LayerManagerGrassInterface
 from datacatalog.catalog import DataCatalog
 from gui_core.forms import GUI
 from gui_core.wrap import Menu, TextEntryDialog
+from startup.guiutils import switch_mapset_interactively
 
 
 class GMFrame(wx.Frame):
@@ -189,6 +190,7 @@ class GMFrame(wx.Frame):
 
         self._giface.mapCreated.connect(self.OnMapCreated)
         self._giface.updateMap.connect(self._updateCurrentMap)
+        self._giface.currentMapsetChanged.connect(self.OnMapsetChanged)
 
         # minimal frame size
         self.SetMinSize(globalvar.GM_WINDOW_MIN_SIZE)
@@ -320,11 +322,7 @@ class GMFrame(wx.Frame):
             parent=self.notebook, giface=self._giface)
         self.datacatalog.showNotification.connect(
             lambda message: self.SetStatusText(message))
-        self.datacatalog.changeMapset.connect(lambda mapset: self.ChangeMapset(mapset))
-        self.datacatalog.changeLocation.connect(lambda mapset, location, dbase:
-                                                self.ChangeLocation(dbase=dbase,
-                                                                    location=location,
-                                                                    mapset=mapset))
+
         self.notebook.AddPage(
             page=self.datacatalog,
             text=_("Data"),
@@ -1076,37 +1074,10 @@ class GMFrame(wx.Frame):
                                              gisenv['GISDBASE'],
                                              location,
                                              mapset):
-                self.ChangeLocation(location, mapset)
-
-    def ChangeLocation(self, location, mapset, dbase=None):
-        if dbase:
-            if RunCommand('g.mapset', parent=self,
-                          dbase=dbase,
-                          location=location,
-                          mapset=mapset) != 0:
-                return  # error reported
-
-            # close current workspace and create new one
-            self.OnWorkspaceClose()
-            self.OnWorkspaceNew()
-            GMessage(parent=self,
-                     message=_("Current GRASS database is <%(dbase)s>.\n"
-                               "Current location is <%(loc)s>.\n"
-                               "Current mapset is <%(mapset)s>.") %
-                     {'dbase': dbase, 'loc': location, 'mapset': mapset})
-        else:
-            if RunCommand('g.mapset', parent=self,
-                          location=location,
-                          mapset=mapset) != 0:
-                return  # error reported
-
-            # close current workspace and create new one
-            self.OnWorkspaceClose()
-            self.OnWorkspaceNew()
-            GMessage(parent=self,
-                     message=_("Current location is <%(loc)s>.\n"
-                               "Current mapset is <%(mapset)s>.") %
-                     {'loc': location, 'mapset': mapset})
+                switch_mapset_interactively(self, self._giface,
+                                            None,
+                                            location,
+                                            mapset)
 
     def OnCreateMapset(self, event):
         """Create new mapset"""
@@ -1150,22 +1121,26 @@ class GMFrame(wx.Frame):
                                              gisenv['GISDBASE'],
                                              gisenv['LOCATION_NAME'],
                                              mapset):
-                self.ChangeMapset(mapset)
-
-    def ChangeMapset(self, mapset):
-        """Change current mapset and update map display title"""
-        if RunCommand('g.mapset',
-                      parent=self,
-                      mapset=mapset) == 0:
-            GMessage(parent=self,
-                     message=_("Current mapset is <%s>.") % mapset)
-
+                switch_mapset_interactively(self, self._giface,
+                                            None,
+                                            None,
+                                            mapset)
+
+    def OnMapsetChanged(self, dbase, location, mapset):
+        """Current mapset changed.
+        If location is None, mapset changed within location.
+        """
+        if not location:
             # TODO: this does not use the actual names if they were
             # renamed (it just uses the numbers)
             dispId = 1
             for display in self.GetMapDisplay(onlyCurrent=False):
                 display.SetTitleWithName(str(dispId))  # TODO: signal ?
                 dispId += 1
+        else:
+            # close current workspace and create new one
+            self.OnWorkspaceClose()
+            self.OnWorkspaceNew()
 
     def OnChangeCWD(self, event=None, cmd=None):
         """Change current working directory

+ 2 - 0
gui/wxpython/lmgr/giface.py

@@ -188,6 +188,8 @@ class LayerManagerGrassInterface(object):
         # add: if map should be added to layer tree (questionable attribute)
         self.mapCreated = Signal('LayerManagerGrassInterface.mapCreated')
 
+        self.currentMapsetChanged = Signal('LayerManagerGrassInterface.currentMapsetChanged')
+
         # Signal emitted to request updating of map
         self.updateMap = Signal('LayerManagerGrassInterface.updateMap')
 

+ 34 - 0
gui/wxpython/startup/guiutils.py

@@ -865,3 +865,37 @@ def import_file(guiparent, filePath):
                 "this imported map.") % {
                 'name': filePath},
             parent=guiparent)
+
+
+def switch_mapset_interactively(guiparent, giface, dbase, location, mapset):
+    """Switch current mapset. Emits giface.currentMapsetChanged signal."""
+    if dbase:
+        if RunCommand('g.mapset', parent=guiparent,
+                      location=location,
+                      mapset=mapset,
+                      dbase=dbase) == 0:
+            GMessage(parent=guiparent,
+                     message=_("Current GRASS database is <%(dbase)s>.\n"
+                               "Current location is <%(loc)s>.\n"
+                               "Current mapset is <%(mapset)s>."
+                               ) %
+                     {'dbase': dbase, 'loc': location, 'mapset': mapset})
+            giface.currentMapsetChanged.emit(dbase, location, mapset)
+    elif location:
+        if RunCommand('g.mapset', parent=guiparent,
+                      location=location,
+                      mapset=mapset) == 0:
+            GMessage(parent=guiparent,
+                     message=_("Current location is <%(loc)s>.\n"
+                               "Current mapset is <%(mapset)s>.") %
+                     {'loc': location, 'mapset': mapset})
+            giface.currentMapsetChanged.emit(dbase=None,
+                                             location=location,
+                                             mapset=mapset)
+    else:
+        if RunCommand('g.mapset',
+                      parent=guiparent,
+                      mapset=mapset) == 0:
+            GMessage(parent=guiparent,
+                     message=_("Current mapset is <%s>.") % mapset)
+            giface.currentMapsetChanged.emit(dbase=None, location=None, mapset=mapset)