Skip to content

Commit 38a9608

Browse files
committed
continue
1 parent 3e109f0 commit 38a9608

9 files changed

Lines changed: 326 additions & 264 deletions

File tree

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

src/compas_viewer/components/boundcomponent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BoundComponent(Component):
4242
>>> component.set_attr(20.0)
4343
>>> print(component.get_attr()) # prints 20.0
4444
"""
45-
45+
4646
def __init__(self, obj: Union[object, dict], attr: str, callback: Callable[[Component, float], None]):
4747
super().__init__()
4848

src/compas_viewer/components/color.py

Lines changed: 0 additions & 203 deletions
This file was deleted.

0 commit comments

Comments
 (0)