| 1 |
"""Custom XML handler for wxPython FloatSpin controls. |
| 2 |
|
| 3 |
It processes XML parameters such as minimum/maximum values, increment, and |
| 4 |
initial value, and configures the FloatSpin control accordingly. |
| 5 |
""" |
| 6 |
|
| 7 |
import contextlib |
| 8 |
|
| 9 |
import wx |
| 10 |
from wx import xrc |
| 11 |
|
| 12 |
from DisplayCAL.log import safe_print |
| 13 |
|
| 14 |
try: |
| 15 |
from DisplayCAL import floatspin |
| 16 |
except ImportError: |
| 17 |
from wx.lib.agw import floatspin |
| 18 |
|
| 19 |
|
| 20 |
class FloatSpinCtrlXmlHandler(xrc.XmlResourceHandler): |
| 21 |
"""Custom XML resource handler for FloatSpin controls.""" |
| 22 |
|
| 23 |
def __init__(self): |
| 24 |
xrc.XmlResourceHandler.__init__(self) |
| 25 |
# Standard styles |
| 26 |
self.AddWindowStyles() |
| 27 |
|
| 28 |
def CanHandle(self, node): |
| 29 |
"""Check if the node can be handled by this handler. |
| 30 |
|
| 31 |
Args: |
| 32 |
node (xrc.XmlNode): The XML node to check. |
| 33 |
|
| 34 |
Returns: |
| 35 |
bool: True if the node is of class FloatSpin, False otherwise. |
| 36 |
""" |
| 37 |
return self.IsOfClass(node, "FloatSpin") |
| 38 |
|
| 39 |
# Process XML parameters and create the object |
| 40 |
def DoCreateResource(self) -> floatspin.FloatSpin: |
| 41 |
"""Create the FloatSpin control from XML parameters. |
| 42 |
|
| 43 |
Returns: |
| 44 |
FloatSpin: The created FloatSpin control. |
| 45 |
""" |
| 46 |
try: |
| 47 |
min_val = float(self.GetText("min_val")) |
| 48 |
except Exception: |
| 49 |
min_val = None |
| 50 |
try: |
| 51 |
max_val = float(self.GetText("max_val")) |
| 52 |
except Exception: |
| 53 |
max_val = None |
| 54 |
try: |
| 55 |
increment = float(self.GetText("increment")) |
| 56 |
except Exception: |
| 57 |
increment = 1.0 |
| 58 |
is_spinctrldbl = hasattr(wx, "SpinCtrlDouble") and issubclass( |
| 59 |
floatspin.FloatSpin, wx.SpinCtrlDouble |
| 60 |
) |
| 61 |
defaultstyle = wx.SP_ARROW_KEYS | wx.ALIGN_RIGHT if is_spinctrldbl else 0 |
| 62 |
w = floatspin.FloatSpin( |
| 63 |
parent=self.GetParentAsWindow(), |
| 64 |
id=self.GetID(), |
| 65 |
pos=self.GetPosition(), |
| 66 |
size=self.GetSize(), |
| 67 |
style=self.GetStyle(defaults=defaultstyle), |
| 68 |
min_val=min_val, |
| 69 |
max_val=max_val, |
| 70 |
increment=increment, |
| 71 |
name=self.GetName(), |
| 72 |
) |
| 73 |
|
| 74 |
with contextlib.suppress(Exception): |
| 75 |
w.SetValue(float(self.GetText("value"))) |
| 76 |
|
| 77 |
with contextlib.suppress(Exception): |
| 78 |
w.SetDigits(int(self.GetText("digits"))) |
| 79 |
|
| 80 |
self.SetupWindow(w) |
| 81 |
if self.GetBool("hidden") and w.Shown: |
| 82 |
safe_print(f"{self.Name} should have been hidden") |
| 83 |
w.Hide() |
| 84 |
return w |