|
| 1 | +from typing import Callable |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +from PySide6.QtWidgets import QCheckBox |
| 5 | +from PySide6.QtWidgets import QHBoxLayout |
| 6 | +from PySide6.QtWidgets import QLabel |
| 7 | +from PySide6.QtWidgets import QWidget |
| 8 | + |
| 9 | +from .boundcomponent import BoundComponent |
| 10 | +from .component import Component |
| 11 | + |
| 12 | + |
| 13 | +class BooleanToggle(BoundComponent): |
| 14 | + """ |
| 15 | + This component creates a labeled checkbox that can be bound to an object's boolean attribute |
| 16 | + (either a dictionary key or object attribute). When the checkbox state changes, it automatically |
| 17 | + updates the bound attribute and optionally calls a action function. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + obj : Union[object, dict] |
| 22 | + The object or dictionary containing the boolean attribute to be edited. |
| 23 | + attr : str |
| 24 | + The name of the attribute/key to be edited. |
| 25 | + title : str, optional |
| 26 | + The label text to be displayed next to the checkbox. If None, uses the attr name. |
| 27 | + action : Callable[[Component, bool], None], optional |
| 28 | + A function to call when the checkbox state changes. Receives the component and new boolean value. |
| 29 | +
|
| 30 | + Attributes |
| 31 | + ---------- |
| 32 | + obj : Union[object, dict] |
| 33 | + The object or dictionary containing the boolean attribute being edited. |
| 34 | + attr : str |
| 35 | + The name of the attribute/key being edited. |
| 36 | + action : Callable[[Component, bool], None] or None |
| 37 | + The action function to call when the checkbox state changes. |
| 38 | + widget : QWidget |
| 39 | + The main widget containing the layout. |
| 40 | + layout : QHBoxLayout |
| 41 | + The horizontal layout containing the label and the checkbox. |
| 42 | + label : QLabel |
| 43 | + The label displaying the title. |
| 44 | + checkbox : QCheckBox |
| 45 | + The checkbox widget for toggling the boolean value. |
| 46 | +
|
| 47 | + Example |
| 48 | + ------- |
| 49 | + >>> class MyObject: |
| 50 | + ... def __init__(self): |
| 51 | + ... self.show_points = True |
| 52 | + >>> obj = MyObject() |
| 53 | + >>> component = BooleanToggle(obj, "show_points", title="Show Points") |
| 54 | + """ |
| 55 | + |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + obj: Union[object, dict], |
| 59 | + attr: str, |
| 60 | + title: str = None, |
| 61 | + action: Callable[[Component, bool], None] = None, |
| 62 | + ): |
| 63 | + super().__init__(obj, attr, action=action) |
| 64 | + |
| 65 | + self.widget = QWidget() |
| 66 | + self.layout = QHBoxLayout() |
| 67 | + |
| 68 | + title = title if title is not None else attr |
| 69 | + self.label = QLabel(title) |
| 70 | + self.checkbox = QCheckBox() |
| 71 | + self.checkbox.setMaximumSize(85, 25) |
| 72 | + |
| 73 | + # Set the initial state from the bound attribute |
| 74 | + initial_value = self.get_attr() |
| 75 | + if not isinstance(initial_value, bool): |
| 76 | + raise ValueError(f"Attribute '{attr}' must be a boolean value, got {type(initial_value)}") |
| 77 | + self.checkbox.setChecked(initial_value) |
| 78 | + |
| 79 | + self.layout.addWidget(self.label) |
| 80 | + self.layout.addWidget(self.checkbox) |
| 81 | + self.widget.setLayout(self.layout) |
| 82 | + |
| 83 | + # Connect the checkbox state change signal to the action |
| 84 | + self.checkbox.stateChanged.connect(self.on_state_changed) |
| 85 | + |
| 86 | + def on_state_changed(self, state): |
| 87 | + """Handle checkbox state change events by updating the bound attribute and calling the action.""" |
| 88 | + # Convert Qt checkbox state to boolean |
| 89 | + is_checked = state == 2 # Qt.Checked = 2 |
| 90 | + self.set_attr(is_checked) |
| 91 | + if self.action: |
| 92 | + self.action(self, is_checked) |
0 commit comments