Skip to content

Commit

Permalink
adding static text and button for setting the current region to the r…
Browse files Browse the repository at this point in the history
…egion from command history
  • Loading branch information
lindakladivova committed Mar 19, 2024
1 parent b656141 commit 02d9e77
Showing 1 changed file with 85 additions and 13 deletions.
98 changes: 85 additions & 13 deletions gui/wxpython/history/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from gui_core.wrap import SearchCtrl, StaticText, StaticBox, Button
from history.tree import HistoryBrowserTree

import grass.script as gs
from grass.script.utils import parse_key_val

from grass.grassdb import history

from grass.pydispatch.signal import Signal
Expand Down Expand Up @@ -76,6 +79,8 @@ def __init__(self, parent, giface, title=("Command Info"), style=wx.TAB_TRAVERSA
self.giface = giface
self.title = title

self.region_settings = None

self._createGeneralInfoBox()
self._createRegionSettingsBox()

Expand Down Expand Up @@ -119,20 +124,32 @@ def _createGeneralInfoBox(self):
def _createRegionSettingsBox(self):
"""Create a static box for displaying region settings of the command"""
self.region_settings_box = StaticBox(
parent=self, id=wx.ID_ANY, label=_("Region settings")
parent=self,
id=wx.ID_ANY,
label=_("Computational region upon command execution"),
)
self.region_settings_box_sizer = wx.StaticBoxSizer(
self.region_settings_box, wx.VERTICAL
)

self.sizer_region_settings = wx.GridBagSizer(hgap=0, vgap=0)
self.sizer_region_settings.SetCols(2)
self.sizer_region_settings.SetRows(9)
self.sizer_region_settings_grid = wx.GridBagSizer(hgap=0, vgap=0)
self.sizer_region_settings_grid.SetCols(2)
self.sizer_region_settings_grid.SetRows(9)

self.region_settings_box_sizer.Add(
self.sizer_region_settings_grid,
proportion=1,
flag=wx.ALL | wx.EXPAND,
border=5,
)

self.sizer_region_settings_grid.AddGrowableCol(1)

self.sizer_region_settings_text = wx.BoxSizer(wx.VERTICAL)
self.region_settings_box_sizer.Add(
self.sizer_region_settings, proportion=1, flag=wx.ALL | wx.EXPAND, border=5
self.sizer_region_settings_text, proportion=0, flag=wx.EXPAND, border=5
)
self.sizer_region_settings.AddGrowableCol(1)

self.region_settings_box.Hide()

def _general_info_filter(self, key, value):
Expand All @@ -142,7 +159,7 @@ def _general_info_filter(self, key, value):
)

def _region_settings_filter(self, key):
return (key != "projection") and (key != "zone")
return (key != "projection") and (key != "zone") and (key != "cells")

def _updateGeneralInfoBox(self, command_info):
"""Update a static box for displaying general info about the command"""
Expand Down Expand Up @@ -180,13 +197,13 @@ def _updateGeneralInfoBox(self, command_info):

def _updateRegionSettingsBox(self, command_info):
"""Update a static box for displaying region settings of the command"""
self.sizer_region_settings.Clear(True)
self.sizer_region_settings_grid.Clear(True)

region_settings = command_info["region"]
self.region_settings = command_info["region"]
idx = 0
for key, value in region_settings.items():
for key, value in self.region_settings.items():
if self._region_settings_filter(key):
self.sizer_region_settings.Add(
self.sizer_region_settings_grid.Add(
StaticText(
parent=self.region_settings_box,
id=wx.ID_ANY,
Expand All @@ -197,7 +214,7 @@ def _updateRegionSettingsBox(self, command_info):
border=5,
pos=(idx, 0),
)
self.sizer_region_settings.Add(
self.sizer_region_settings_grid.Add(
StaticText(
parent=self.region_settings_box,
id=wx.ID_ANY,
Expand All @@ -210,6 +227,37 @@ def _updateRegionSettingsBox(self, command_info):
)
idx += 1

self.sizer_region_settings_text.Clear(True)

textSetRegion = StaticText(
parent=self.region_settings_box,
id=wx.ID_ANY,
label=_("Region is same as the current region"),
)
textSetRegion.Wrap(self.GetSize()[0])
self.sizer_region_settings_text.Add(
textSetRegion,
proportion=1,
flag=wx.ALL | wx.EXPAND,
border=5,
)

if self.region_settings != self._get_current_region():
textSetRegion.SetLabel(_("Region is different from the current region"))
btnSetRegion = Button(parent=self.region_settings_box, id=wx.ID_ANY)
btnSetRegion.SetLabel(_("&Set as current region"))
btnSetRegion.SetToolTip(
_("Set current computational region to the region of executed command")
)
btnSetRegion.Bind(wx.EVT_BUTTON, self.OnSetCurrentRegionToHistory)

self.sizer_region_settings_text.Add(
btnSetRegion,
proportion=0,
flag=wx.ALL | wx.EXPAND,
border=5,
)

self.region_settings_box.Layout()
self.region_settings_box.Show()

Expand All @@ -226,11 +274,35 @@ def showCommandInfo(self, command_info):
def clearCommandInfo(self):
"""Clear command info."""
self.sizer_general_info.Clear(True)
self.sizer_region_settings.Clear(True)
self.sizer_region_settings_grid.Clear(True)
self.sizer_region_settings_text.Clear(True)
self._createGeneralInfoBox()
self._createRegionSettingsBox()
self._layout()

def _get_current_region(self):
"""Get current computational region settings."""
current_region = dict(
parse_key_val(gs.read_command("g.region", flags="g"), val_type=float)
)
for key, value in current_region.items():
current_region[key] = int(value) if value.is_integer() else value
return current_region

def _get_history_region(self):
"""Get computational region settings of executed command."""
history_region = {}
for key, value in self.region_settings.items():
if self._region_settings_filter(key):
history_region[key] = value
return history_region

def OnSetCurrentRegionToHistory(self, event):
"""Set current region to the region of executed command."""
history_region = self._get_history_region()
gs.run_command("g.region", **history_region)
self.giface.updateMap.emit(render=False, renderVector=False)


class HistoryBrowser(wx.SplitterWindow):
"""History browser window for executing the commands from history log
Expand Down

0 comments on commit 02d9e77

Please sign in to comment.