Source code for wolfhece._builtin_plugins.grid_adder.companion

"""Grid Adder — built-in companion plugin.

Adds a configurable reference grid to the active WolfMapViewer's object tree.

This is a one-shot action: no mouse/keyboard interaction is required.
The companion adds a single *"Grid"* top-level menu to the viewer's menu bar.
"""
from wolfhece.plugins.abc import AbstractUICompanion, MenuItem
from wolfhece._viewer_plugin_handlers import MouseContext
from wolfhece.PyTranslate import _


[docs] class GridAdderCompanion(AbstractUICompanion): """Adds a configurable reference grid to the viewer's object tree. Menu entry: *Grid → Add grid…* Calling :meth:`start` (or selecting the menu item) opens a float-input dialog asking for the cell size in map units, then inserts a :class:`~wolfhece.pyvertexvectors.Grid` instance into the viewer's vector layer list. """ def __init__(self, *, dialogs=None): super().__init__() if dialogs is not None: self.proxy._dialogs = dialogs
[docs] def menu_spec(self): return (_('Grid'), [ MenuItem(_('Add grid\u2026'), self._on_add, _('Add a reference grid to the viewer')), ])
[docs] def start(self) -> None: """Programmatic activation — show the size dialog and insert the grid.""" self._do_add_grid()
# -- handlers ------------------------------------------------------------
[docs] def _on_add(self, ctx: MouseContext) -> None: self.start()
# -- implementation ------------------------------------------------------
[docs] def _do_add_grid(self) -> None: """Ask for cell size, then add a Grid to the viewer's tree.""" # Lazy import keeps the plugin self-contained and avoids circular deps # when the companion module is imported early (e.g. during testing). from wolfhece.pyvertexvectors import Grid size = self.proxy.ask_float( _('Grid cell size (m):'), title=_('Add grid'), default=1000., ) if size is None: return mygrid = Grid(size) self.proxy._viewer.add_object('vector', newobj=mygrid, ToCheck=False, id='Grid') self.proxy.set_status(_('Grid added (cell size = {size} m).').format(size=size))