|
| 1 | +from PySide6.QtWidgets import QWidget |
| 2 | +from PySide6.QtWidgets import QHBoxLayout |
| 3 | +from PySide6.QtWidgets import QLabel |
| 4 | +from PySide6.QtWidgets import QDoubleSpinBox |
| 5 | +from typing import Callable |
| 6 | +from typing import Union |
| 7 | +from .component import Component |
| 8 | +from .boundcomponent import BoundComponent |
| 9 | + |
| 10 | + |
| 11 | +class NumberEdit(BoundComponent): |
| 12 | + """ |
| 13 | + This component creates a labeled number spin box that can be bound to an object's attribute |
| 14 | + (either a dictionary key or object attribute). When the value changes, it automatically |
| 15 | + updates the bound attribute and optionally calls a callback function. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + obj : Union[object, dict] |
| 20 | + The object or dictionary containing the attribute to be edited. |
| 21 | + attr : str |
| 22 | + The name of the attribute/key to be edited. |
| 23 | + title : str, optional |
| 24 | + The label text to be displayed next to the spin box. If None, uses the attr name. |
| 25 | + min_val : float, optional |
| 26 | + The minimum value allowed in the spin box. If None, uses the default minimum. |
| 27 | + max_val : float, optional |
| 28 | + The maximum value allowed in the spin box. If None, uses the default maximum. |
| 29 | + step : float, optional |
| 30 | + The step size for the spin box. Defaults to 0.1. |
| 31 | + decimals : int, optional |
| 32 | + The number of decimal places to display. Defaults to 1. |
| 33 | + callback : Callable[[Component, float], None], optional |
| 34 | + A function to call when the value changes. Receives the component and new value. |
| 35 | +
|
| 36 | + Attributes |
| 37 | + ---------- |
| 38 | + obj : Union[object, dict] |
| 39 | + The object or dictionary containing the attribute being edited. |
| 40 | + attr : str |
| 41 | + The name of the attribute/key being edited. |
| 42 | + callback : Callable[[Component, float], None] or None |
| 43 | + The callback function to call when the value changes. |
| 44 | + widget : QWidget |
| 45 | + The main widget containing the layout. |
| 46 | + layout : QHBoxLayout |
| 47 | + The horizontal layout containing the label and the spin box. |
| 48 | + label : QLabel |
| 49 | + The label displaying the title. |
| 50 | + spinbox : QDoubleSpinBox |
| 51 | + The double spin box for editing the floating-point number. |
| 52 | +
|
| 53 | + Example |
| 54 | + ------- |
| 55 | + >>> class MyObject: |
| 56 | + ... def __init__(self): |
| 57 | + ... self.x = 5.0 |
| 58 | + >>> obj = MyObject() |
| 59 | + >>> component = NumberEdit(obj, "x", title="X Coordinate", min_val=0.0, max_val=10.0) |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__( |
| 63 | + self, |
| 64 | + obj: Union[object, dict], |
| 65 | + attr: str, |
| 66 | + title: str = None, |
| 67 | + min_val: float = None, |
| 68 | + max_val: float = None, |
| 69 | + step: float = 0.1, |
| 70 | + decimals: int = 1, |
| 71 | + callback: Callable[[Component, float], None] = None, |
| 72 | + ): |
| 73 | + super().__init__(obj, attr, callback=callback) |
| 74 | + |
| 75 | + self.widget = QWidget() |
| 76 | + self.layout = QHBoxLayout() |
| 77 | + |
| 78 | + title = title if title is not None else attr |
| 79 | + self.label = QLabel(title) |
| 80 | + self.spinbox = QDoubleSpinBox() |
| 81 | + self.spinbox.setDecimals(decimals) |
| 82 | + self.spinbox.setSingleStep(step) |
| 83 | + self.spinbox.setMaximumSize(85, 25) |
| 84 | + |
| 85 | + self.spinbox.setValue(self.get_attr()) |
| 86 | + |
| 87 | + if min_val is not None: |
| 88 | + self.spinbox.setMinimum(min_val) |
| 89 | + if max_val is not None: |
| 90 | + self.spinbox.setMaximum(max_val) |
| 91 | + |
| 92 | + self.layout.addWidget(self.label) |
| 93 | + self.layout.addWidget(self.spinbox) |
| 94 | + self.widget.setLayout(self.layout) |
| 95 | + self.spinbox.valueChanged.connect(self.on_value_changed) |
0 commit comments