From 6a32875bd3ba66ec5026010f6eac7ba39edbb31b Mon Sep 17 00:00:00 2001 From: Vaclav Petras Date: Wed, 29 Jan 2025 10:38:14 -0500 Subject: [PATCH] wxGUI: Use r.mask.status to get info about mask (#4519) This replaces low level checks of MASK raster in the current mapset with calls of r.mask.status. GConsole and SbMask require r.mask.status to output a potential name of the mask regerdless of mask being set or not, so this functionality still needs to be implemented first before moving this PR forward. History changes seem to be complete. --- gui/wxpython/core/gconsole.py | 6 ++++-- gui/wxpython/lmgr/statusbar.py | 26 +++++++++++++++----------- python/grass/grassdb/history.py | 11 +++++++---- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/gui/wxpython/core/gconsole.py b/gui/wxpython/core/gconsole.py index d5fc21c5b0d..b46d19254db 100644 --- a/gui/wxpython/core/gconsole.py +++ b/gui/wxpython/core/gconsole.py @@ -856,13 +856,15 @@ def OnCmdDone(self, event): for p in task.get_options()["flags"]: if p.get("name") == "r" and p.get("value"): action = "delete" + mask_full_name = gs.parse_command("r.mask.status", format="json")["name"] + mask_name, mask_mapset = mask_full_name.split("@", maxsplit=1) gisenv = gs.gisenv() self._giface.grassdbChanged.emit( grassdb=gisenv["GISDBASE"], location=gisenv["LOCATION_NAME"], - mapset=gisenv["MAPSET"], + mapset=mask_mapset, action=action, - map="MASK", + map=mask_name, element="raster", ) diff --git a/gui/wxpython/lmgr/statusbar.py b/gui/wxpython/lmgr/statusbar.py index ac4748bfe1f..c30fde67d5c 100644 --- a/gui/wxpython/lmgr/statusbar.py +++ b/gui/wxpython/lmgr/statusbar.py @@ -74,15 +74,14 @@ class SbMask: def __init__(self, parent, giface): self.name = "mask" - self.mask_layer = "MASK" self.parent = parent self.giface = giface self.widget = Button( - parent=parent, id=wx.ID_ANY, label=_(self.mask_layer), style=wx.NO_BORDER + parent=parent, id=wx.ID_ANY, label=_("Mask"), style=wx.NO_BORDER ) self.widget.Bind(wx.EVT_BUTTON, self.OnRemoveMask) self.widget.SetForegroundColour(wx.Colour(255, 0, 0)) - self.widget.SetToolTip(tip=_("Left mouse click to remove the MASK")) + self.widget.SetToolTip(tip=_("Left mouse click to remove the raster mask")) self.giface.currentMapsetChanged.connect(self.Refresh) if not watchdog_used: self.giface.grassdbChanged.connect(self.dbChanged) @@ -94,7 +93,12 @@ def dbChanged(self, map=None, newname=None): :param str map: map that is changed :param str newname: new map """ - if self.mask_layer in {map, newname}: + mask_layer = gs.parse_command("r.mask.status", format="json")["name"].split( + "@", maxsplit=1 + )[0] + # This assumes mask is always in the current mapset (or the event is triggered + # only for the current mapset). + if mask_layer in {map, newname}: self.Refresh() self.giface.updateMap.emit() @@ -124,9 +128,7 @@ def GetWidget(self): def Refresh(self): """Show mask in the statusbar if mask file found""" - if gs.find_file( - name=self.mask_layer, element="cell", mapset=gs.gisenv()["MAPSET"] - )["name"]: + if gs.parse_command("r.mask.status", format="json")["present"]: self.Show() else: self.Hide() @@ -134,20 +136,22 @@ def Refresh(self): def OnRemoveMask(self, event): dlg = wx.MessageDialog( self.parent, - message=_("Are you sure that you want to remove the MASK?"), - caption=_("Remove MASK"), + message=_("Are you sure that you want to remove the current raster mask?"), + caption=_("Remove raster mask"), style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION, ) if dlg.ShowModal() != wx.ID_YES: dlg.Destroy() return RunCommand("r.mask", flags="r") + mask_full_name = gs.parse_command("r.mask.status", format="json")["name"] + mask_name, mask_mapset = mask_full_name.split("@", maxsplit=1) gisenv = gs.gisenv() self.giface.grassdbChanged.emit( grassdb=gisenv["GISDBASE"], location=gisenv["LOCATION_NAME"], - mapset=gisenv["MAPSET"], - map=self.mask_layer, + mapset=mask_mapset, + map=mask_name, action="delete", element="raster", ) diff --git a/python/grass/grassdb/history.py b/python/grass/grassdb/history.py index 8c9bc2223b3..faf9e44eb4d 100644 --- a/python/grass/grassdb/history.py +++ b/python/grass/grassdb/history.py @@ -258,12 +258,13 @@ def get_initial_command_info(env_run): exec_time = datetime.now().isoformat() # 2D raster MASK presence - env = gs.gisenv(env_run) - mapset_path = Path(env["GISDBASE"]) / env["LOCATION_NAME"] / env["MAPSET"] - mask2d_present = (mapset_path / "cell" / "MASK").exists() + mask2d_status = gs.parse_command("r.mask.status", format="json", env=env_run) # 3D raster MASK presence + env = gs.gisenv(env_run) + mapset_path = Path(env["GISDBASE"]) / env["LOCATION_NAME"] / env["MAPSET"] mask3d_present = (mapset_path / "grid3" / "RASTER3D_MASK").exists() + mask3d_name = f"RASTER3D_MASK@{env['MAPSET']}" # Computational region settings region_settings = gs.region(env=env_run) @@ -271,8 +272,10 @@ def get_initial_command_info(env_run): # Finalize the command info dictionary return { "timestamp": exec_time, - "mask2d": mask2d_present, + "mask2d": mask2d_status["present"], + "mask2d_name": mask2d_status["name"], "mask3d": mask3d_present, + "mask3d_name": mask3d_name, "region": region_settings, "status": Status.RUNNING.value, }