diff --git a/gui/wxpython/lmgr/workspace.py b/gui/wxpython/lmgr/workspace.py index b1e7aa400f0..869dd3f05bb 100644 --- a/gui/wxpython/lmgr/workspace.py +++ b/gui/wxpython/lmgr/workspace.py @@ -23,6 +23,8 @@ import wx import wx.aui +import grass.script as gs + from core.settings import UserSettings from core.gcmd import RunCommand, GError, GMessage from core.workspace import ProcessWorkspaceFile, WriteWorkspaceFile @@ -126,6 +128,17 @@ def Open(self): self.lmgr._setTitle() def _tryToSwitchMapsetFromWorkspaceFile(self, gxwXml): + gisenv = gs.gisenv() + grassdb = gisenv["GISDBASE"] + location = gisenv["LOCATION_NAME"] + mapset = gisenv["MAPSET"] + if ( + grassdb == gxwXml.database + and location == gxwXml.location + and mapset == gxwXml.mapset + ): + # Positive result but no message to the user is needed. + return True returncode, errors = RunCommand( "g.mapset", dbase=gxwXml.database, @@ -164,6 +177,34 @@ def _tryToSwitchMapsetFromWorkspaceFile(self, gxwXml): ) return True + # No need for method here, could be a function. + def CurrentMapsetWorkspaceFile(self): + gisenv = gs.gisenv() + grassdb = gisenv["GISDBASE"] + location = gisenv["LOCATION_NAME"] + mapset = gisenv["MAPSET"] + default_workspace_name = f"{mapset}.gxw" + return Path(grassdb) / location / mapset / default_workspace_name + + def IsCurrentMapsetWorkspaceFile(self): + if not self.workspaceFile: + return False + path = self.CurrentMapsetWorkspaceFile() + return Path(self.workspaceFile) == path + + def AutoLoad(self, filename=None): + if not filename: + path = self.CurrentMapsetWorkspaceFile() + filename = str(path) + if path.is_file(): + self.lmgr.DisplayCloseAll() + self.workspaceChanged = False + else: + self.workspaceFile = filename + self.SaveToFile(filename) + return True + return self.Load(filename) + def Load(self, filename): """Load layer tree definition stored in GRASS Workspace XML file (gxw) .. todo:: @@ -389,7 +430,7 @@ def SaveAs(self): "Do you want to overwrite this file?" ) % filename, - caption=_("Save workspace"), + caption=_("Save workspace (SaveAs)"), style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION, ) if dlg.ShowModal() != wx.ID_YES: @@ -404,6 +445,18 @@ def SaveAs(self): def Save(self): """Save file with workspace definition""" + gisenv = gs.gisenv() + grassdb = gisenv["GISDBASE"] + location = gisenv["LOCATION_NAME"] + mapset = gisenv["MAPSET"] + default_workspace_name = f"{mapset}.gxw" + path = Path(grassdb) / location / mapset / default_workspace_name + if path == Path(self.workspaceFile): + self.SaveToFile(self.workspaceFile) + self.lmgr._setTitle() + self.workspaceChanged = False + return + if self.workspaceFile: dlg = wx.MessageDialog( self.lmgr, @@ -412,7 +465,7 @@ def Save(self): "Do you want to overwrite this file?" ) % self.workspaceFile, - caption=_("Save workspace"), + caption=_("Save workspace (Save)"), style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION, ) if dlg.ShowModal() == wx.ID_NO: diff --git a/gui/wxpython/main_window/frame.py b/gui/wxpython/main_window/frame.py index 3574e7c8951..e8c46606c55 100644 --- a/gui/wxpython/main_window/frame.py +++ b/gui/wxpython/main_window/frame.py @@ -221,9 +221,8 @@ def show_menu_errors(messages): self.Show() # load workspace file if requested - if workspace: - if self.workspace_manager.Load(workspace): - self._setTitle() + if self.workspace_manager.AutoLoad(workspace): + self._setTitle() else: # start default initial display self.NewDisplay(show=False) @@ -271,6 +270,15 @@ def _setTitle(self): location = gisenv["LOCATION_NAME"] mapset = gisenv["MAPSET"] if self.workspace_manager.workspaceFile: + if self.workspace_manager.IsCurrentMapsetWorkspaceFile(): + # Be more verbose just for clarity during testing, but otherwise + # showing just location/mapset might be sufficient. + self.SetTitle( + "{location}/{mapset} (workspace in mapset) - {program}".format( + location=location, mapset=mapset, program=self.baseTitle + ) + ) + return filename = os.path.splitext( os.path.basename(self.workspace_manager.workspaceFile) )[0] @@ -1500,12 +1508,12 @@ def OnMapsetChanged(self, dbase, location, mapset): """Current mapset changed. If location is None, mapset changed within location. """ - if not location: - self._setTitle() - else: - # close current workspace and create new one - self.OnWorkspaceClose() + if location: + # Close was probably wrong action even before because it saved wrong mapset. + # self.OnWorkspaceClose() self.OnWorkspaceNew() + self.workspace_manager.AutoLoad() + self._setTitle() def OnChangeCWD(self, event=None, cmd=None): """Change current working directory