Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wxGUI: Make GUI workspace part of mapset #3113

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions gui/wxpython/lmgr/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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::
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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:
Expand Down
24 changes: 16 additions & 8 deletions gui/wxpython/main_window/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
Loading