"""
Companion manager for bridge menus and actions.
Extracted from PyDraw.WolfMapViewer.
"""
from __future__ import annotations
import logging
import wx
from typing import TYPE_CHECKING
from .PyTranslate import _
from ._action_kind import ActionKind
if TYPE_CHECKING:
from .PyDraw import WolfMapViewer
[docs]
class BridgeManager:
"""Manages the &Bridges menu and its associated actions."""
def __init__(self, viewer: 'WolfMapViewer') -> None:
[docs]
self._item_add: wx.MenuItem | None = None
[docs]
self._item_find: wx.MenuItem | None = None
[docs]
self._item_edit: wx.MenuItem | None = None
# ------------------------------------------------------------------
# Menu build
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# Action handlers
# ------------------------------------------------------------------
[docs]
def on_add(self, e: wx.Event) -> None:
from .PyDraw import draw_type
v = self._viewer
if v.active_bridges is None:
logging.warning(_('No bridge collection !'))
return
dlg = wx.TextEntryDialog(None, _('Enter the new bridge id'), _('New bridge id'), 'bridge')
if dlg.ShowModal() == wx.ID_OK:
newid = dlg.GetValue()
while newid in v.get_list_keys(drawing_type=draw_type.VECTORS):
newid = newid + '_'
newbridge = v.active_bridges.addnew(newid)
v.add_object('vector', newobj=newbridge, id=newid)
[docs]
def on_edit(self, e: wx.Event) -> None:
from .PyDraw import draw_type
v = self._viewer
if v.active_bridge is None:
logging.warning(_('No active bridge to edit !'))
return
keys = v.get_list_keys(drawing_type=draw_type.VECTORS)
newid = v.active_bridge.idx
while newid in keys:
newid = newid + '_'
v.add_object('vector', newobj=v.active_bridge, id=newid)
[docs]
def on_find(self, e: wx.Event) -> None:
self._viewer.start_action(ActionKind.PICK_BRIDGE, _('Right click to pick the nearest bridge'))
[docs]
def pick(self, x: float, y: float) -> None:
v = self._viewer
if v.active_bridges is None:
logging.warning(_('No bridges to pick !'))
return
v.active_bridge = v.active_bridges.find_nearest(x, y)