classes.c92_render_panel_plugin

 1import json
 2import os
 3
 4
 5class RenderPanelPlugin:
 6    """Base class for RenderPanelManager panel plugins.
 7
 8    Provides the boilerplate that is *identical* across all panel
 9    implementations: JSON persistence (save/reload), the persistence-enabled
10    flag, and the raw-state accessor used for export.
11
12    Subclasses must set the following attributes in ``__init__`` before any
13    method here is called:
14
15    - ``_state`` (dict) — internal state dict with defaults
16    - ``_statePath`` (str) — absolute path to the JSON persistence file
17    - ``_persistenceEnabled`` (bool) — normally ``True``
18
19    Subclasses must also implement:
20
21    - ``_loadState()`` — read ``_statePath``, update ``_state``, validate
22    - ``getState() -> dict`` — return state (possibly with computed fields)
23    - ``build(parent, onChange)``
24    """
25
26    def getPanelWidth(self) -> int:
27        return self._panelWidth
28
29    def _triggerChange(self, immediate=False):
30        if self._onChange is not None:
31            self._onChange(immediate=immediate)
32
33    def getPersistedState(self) -> dict:
34        """Return the raw ``_state`` dict without computed transformations.
35
36        Unlike ``getState()``, no template compilation, pool expansion, or
37        delimiter resolution is performed.  Used by the panel manager for
38        export so that state round-trips correctly on import.
39        """
40        return dict(self._state)
41
42    def setPersistenceEnabled(self, enabled: bool):
43        """Enable or disable automatic state saving on UI changes."""
44        self._persistenceEnabled = enabled
45
46    def _saveState(self):
47        """Write ``_state`` to ``_statePath`` as JSON (no-op when persistence is off)."""
48        if not self._persistenceEnabled:
49            return
50        folder = os.path.dirname(self._statePath)
51        os.makedirs(folder, exist_ok=True)
52        with open(self._statePath, "w", encoding="utf-8") as f:
53            json.dump(self._state, f, indent=2)
54
55    def reloadState(self, data: dict):
56        """Overwrite persisted state with *data* and re-apply it via ``_loadState``."""
57        folder = os.path.dirname(self._statePath)
58        os.makedirs(folder, exist_ok=True)
59        with open(self._statePath, "w", encoding="utf-8") as f:
60            json.dump(data, f, indent=2)
61        self._loadState()
class RenderPanelPlugin:
 6class RenderPanelPlugin:
 7    """Base class for RenderPanelManager panel plugins.
 8
 9    Provides the boilerplate that is *identical* across all panel
10    implementations: JSON persistence (save/reload), the persistence-enabled
11    flag, and the raw-state accessor used for export.
12
13    Subclasses must set the following attributes in ``__init__`` before any
14    method here is called:
15
16    - ``_state`` (dict) — internal state dict with defaults
17    - ``_statePath`` (str) — absolute path to the JSON persistence file
18    - ``_persistenceEnabled`` (bool) — normally ``True``
19
20    Subclasses must also implement:
21
22    - ``_loadState()`` — read ``_statePath``, update ``_state``, validate
23    - ``getState() -> dict`` — return state (possibly with computed fields)
24    - ``build(parent, onChange)``
25    """
26
27    def getPanelWidth(self) -> int:
28        return self._panelWidth
29
30    def _triggerChange(self, immediate=False):
31        if self._onChange is not None:
32            self._onChange(immediate=immediate)
33
34    def getPersistedState(self) -> dict:
35        """Return the raw ``_state`` dict without computed transformations.
36
37        Unlike ``getState()``, no template compilation, pool expansion, or
38        delimiter resolution is performed.  Used by the panel manager for
39        export so that state round-trips correctly on import.
40        """
41        return dict(self._state)
42
43    def setPersistenceEnabled(self, enabled: bool):
44        """Enable or disable automatic state saving on UI changes."""
45        self._persistenceEnabled = enabled
46
47    def _saveState(self):
48        """Write ``_state`` to ``_statePath`` as JSON (no-op when persistence is off)."""
49        if not self._persistenceEnabled:
50            return
51        folder = os.path.dirname(self._statePath)
52        os.makedirs(folder, exist_ok=True)
53        with open(self._statePath, "w", encoding="utf-8") as f:
54            json.dump(self._state, f, indent=2)
55
56    def reloadState(self, data: dict):
57        """Overwrite persisted state with *data* and re-apply it via ``_loadState``."""
58        folder = os.path.dirname(self._statePath)
59        os.makedirs(folder, exist_ok=True)
60        with open(self._statePath, "w", encoding="utf-8") as f:
61            json.dump(data, f, indent=2)
62        self._loadState()

Base class for RenderPanelManager panel plugins.

Provides the boilerplate that is identical across all panel implementations: JSON persistence (save/reload), the persistence-enabled flag, and the raw-state accessor used for export.

Subclasses must set the following attributes in __init__ before any method here is called:

  • _state (dict) — internal state dict with defaults
  • _statePath (str) — absolute path to the JSON persistence file
  • _persistenceEnabled (bool) — normally True

Subclasses must also implement:

  • _loadState() — read _statePath, update _state, validate
  • getState() -> dict — return state (possibly with computed fields)
  • build(parent, onChange)
def getPanelWidth(self) -> int:
27    def getPanelWidth(self) -> int:
28        return self._panelWidth
def getPersistedState(self) -> dict:
34    def getPersistedState(self) -> dict:
35        """Return the raw ``_state`` dict without computed transformations.
36
37        Unlike ``getState()``, no template compilation, pool expansion, or
38        delimiter resolution is performed.  Used by the panel manager for
39        export so that state round-trips correctly on import.
40        """
41        return dict(self._state)

Return the raw _state dict without computed transformations.

Unlike getState(), no template compilation, pool expansion, or delimiter resolution is performed. Used by the panel manager for export so that state round-trips correctly on import.

def setPersistenceEnabled(self, enabled: bool):
43    def setPersistenceEnabled(self, enabled: bool):
44        """Enable or disable automatic state saving on UI changes."""
45        self._persistenceEnabled = enabled

Enable or disable automatic state saving on UI changes.

def reloadState(self, data: dict):
56    def reloadState(self, data: dict):
57        """Overwrite persisted state with *data* and re-apply it via ``_loadState``."""
58        folder = os.path.dirname(self._statePath)
59        os.makedirs(folder, exist_ok=True)
60        with open(self._statePath, "w", encoding="utf-8") as f:
61            json.dump(data, f, indent=2)
62        self._loadState()

Overwrite persisted state with data and re-apply it via _loadState.