Skip to content

Commit

Permalink
wxGUI/mapdisplay: register context menu items, addresses #1691 (#1704)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrasovaa committed Jul 7, 2021
1 parent e334471 commit f5976e7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 74 deletions.
17 changes: 0 additions & 17 deletions gui/wxpython/lmgr/giface.py
Expand Up @@ -258,23 +258,6 @@ def GetProgress(self):
def UpdateCmdHistory(self, cmd):
self.lmgr.goutput.GetPrompt().UpdateCmdHistory(cmd)

def ShowStatusbar(self, show=True):
self.lmgr.GetMapDisplay().ShowStatusbar(show)

def IsStatusbarShown(self):
return self.lmgr.GetMapDisplay().IsStatusbarShown()

def ShowAllToolbars(self, show=True):
if not show: # hide
action = self.lmgr.GetMapDisplay().RemoveToolbar
else:
action = self.lmgr.GetMapDisplay().AddToolbar
for toolbar in self.lmgr.GetMapDisplay().GetToolbarNames():
action(toolbar)

def AreAllToolbarsShown(self):
return self.lmgr.GetMapDisplay().GetMapToolbar().IsShown()


class LayerManagerGrassInterfaceForMapDisplay(object):
"""Provides reference only to the given layer list (according to tree),
Expand Down
45 changes: 45 additions & 0 deletions gui/wxpython/mapdisp/frame.py
Expand Up @@ -172,6 +172,9 @@ def __init__(
self.MapWindow2D.InitZoomHistory()
self.MapWindow2D.zoomChanged.connect(self.StatusbarUpdate)

# register context menu actions
self._registerContextMenuActions()

self._giface.updateMap.connect(self.MapWindow2D.UpdateMap)
# default is 2D display mode
self.MapWindow = self.MapWindow2D
Expand Down Expand Up @@ -246,6 +249,37 @@ def __init__(

self._resize()

def _registerContextMenuActions(self):
"""Register show/hide toolbars and statusbar context menu actions"""

def show_hide_toolbar_label():
return (
_("Hide toolbars") if self.AreAllToolbarsShown() else _("Show toolbars")
)

def on_show_hide_toolbar(event):
self.ShowAllToolbars(not self.AreAllToolbarsShown())

self.MapWindow2D.RegisterContextAction(
name="showAllToolbars",
label=show_hide_toolbar_label,
action=on_show_hide_toolbar,
)

def show_hide_statusbar_label():
return (
_("Hide statusbar") if self.IsStatusbarShown() else _("Show statusbar")
)

def on_show_hide_statusbar(event):
self.ShowStatusbar(not self.IsStatusbarShown())

self.MapWindow2D.RegisterContextAction(
name="showStatusbar",
label=show_hide_statusbar_label,
action=on_show_hide_statusbar,
)

def CreateStatusbar(self):
if self.statusbarManager:
return
Expand Down Expand Up @@ -671,6 +705,17 @@ def RemoveToolbar(self, name, destroy=False):

self._mgr.Update()

def ShowAllToolbars(self, show=True):
if not show: # hide
action = self.RemoveToolbar
else:
action = self.AddToolbar
for toolbar in self.GetToolbarNames():
action(toolbar)

def AreAllToolbarsShown(self):
return self.GetMapToolbar().IsShown()

def IsPaneShown(self, name):
"""Check if pane (toolbar, mapWindow ...) of given name is currently shown"""
if self._mgr.GetPane(name).IsOk():
Expand Down
30 changes: 0 additions & 30 deletions gui/wxpython/mapdisp/main.py
Expand Up @@ -456,36 +456,6 @@ def GetMapDisplay(self):
def GetProgress(self):
return self._mapframe.GetProgressBar()

def ShowStatusbar(self, show=True):
if not self._mapframe.statusbarManager:
self._mapframe.CreateStatusbar()

self._mapframe.ShowStatusbar(show)

def IsStatusbarShown(self):
if not self._mapframe.statusbarManager:
return False

return self._mapframe.IsStatusbarShown()

def ShowAllToolbars(self, show=True):
if not show: # hide
action = self._mapframe.RemoveToolbar
else:
action = self._mapframe.AddToolbar
toolbars = list(self._mapframe.GetToolbarNames())
if not toolbars:
toolbars.append("map")
for toolbar in toolbars:
action(toolbar)

def AreAllToolbarsShown(self):
toolbar = self._mapframe.GetMapToolbar()
if toolbar is None:
return False

return toolbar.IsShown()


class DMonFrame(MapFrame):
def OnZoomToMap(self, event):
Expand Down
47 changes: 20 additions & 27 deletions gui/wxpython/mapwin/buffered.py
Expand Up @@ -223,6 +223,9 @@ def __init__(
# list for registration of graphics to draw
self.graphicsSetList = []

# dict for registration of context menu actions
self._extraContextActions = {}

def OnUpdateMap(self):
# before lambda func was used, however it was problem
# to disconnect it from signal
Expand Down Expand Up @@ -253,6 +256,15 @@ def _bindMouseEvents(self):
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)

def RegisterContextAction(self, name, label, action):
"""Register context menu item.
:param name: action name
:param label: callback function returning label
:param action: handler
"""
self._extraContextActions[name] = {"label": label, "action": action}

def OnContextMenu(self, event):
"""Show Map Display context menu"""
if self.digit:
Expand All @@ -266,25 +278,14 @@ def OnContextMenu(self, event):
self.popupCopyCoordinates = NewId()
self.Bind(wx.EVT_MENU, self.OnCopyCoordinates, id=self.popupCopyCoordinates)
menu.Append(self.popupCopyCoordinates, _("Copy coordinates to clipboard"))
menu.AppendSeparator()
if not hasattr(self, "popupShowAllToolbars"):
self.popupShowAllToolbars = NewId()
self.Bind(wx.EVT_MENU, self.OnShowAllToolbars, id=self.popupShowAllToolbars)
menu.Append(
self.popupShowAllToolbars,
_("Hide toolbars")
if self._giface.AreAllToolbarsShown()
else _("Show toolbars"),
)
if not hasattr(self, "popupShowStatusbar"):
self.popupShowStatusbar = NewId()
self.Bind(wx.EVT_MENU, self.OnShowStatusbar, id=self.popupShowStatusbar)
menu.Append(
self.popupShowStatusbar,
_("Hide statusbar")
if self._giface.IsStatusbarShown()
else _("Show statusbar"),
)
if self._extraContextActions:
menu.AppendSeparator()
for key, action_dict in self._extraContextActions.items():
if not hasattr(self, key):
aid = NewId()
setattr(self, key, aid)
self.Bind(wx.EVT_MENU, action_dict["action"], id=aid)
menu.Append(getattr(self, key), action_dict["label"]())

pos = self.ScreenToClient(event.GetPosition())
idlist = self.pdc.FindObjects(pos[0], pos[1], self.hitradius)
Expand Down Expand Up @@ -1725,14 +1726,6 @@ def OnCopyCoordinates(self, event):
wx.TheClipboard.SetData(do)
wx.TheClipboard.Close()

def OnShowStatusbar(self, event):
"""Show/hide statusbar"""
self._giface.ShowStatusbar(not self._giface.IsStatusbarShown())

def OnShowAllToolbars(self, event):
"""Show/hide all toolbars"""
self._giface.ShowAllToolbars(not self._giface.AreAllToolbarsShown())

def ClearLines(self, pdc=None):
"""Clears temporary drawn lines from PseudoDC"""
if not pdc:
Expand Down

0 comments on commit f5976e7

Please sign in to comment.