| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | """Widget for editing a list of properties of a DocumentObject.""" |
| |
|
| | import re |
| | from PySide import QtGui, QtCore |
| | from .property import BasePropertyEditorWidget |
| |
|
| |
|
| | def _get_label_text(prop_name): |
| | """Generate a human-readable label from a property name.""" |
| | |
| | s1 = re.sub(r"([A-Z][a-z]+)", r" \1", prop_name) |
| | |
| | s2 = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1 \2", s1) |
| | |
| | s3 = re.sub(r"([A-Z]+)$", r" \1", s2) |
| | |
| | return s3.strip().capitalize() |
| |
|
| |
|
| | class DocumentObjectEditorWidget(QtGui.QWidget): |
| | """ |
| | A widget that displays a user friendly form for editing properties of a |
| | FreeCAD DocumentObject. |
| | """ |
| |
|
| | |
| | propertyChanged = QtCore.Signal() |
| |
|
| | def __init__(self, obj=None, properties_to_show=None, property_suffixes=None, parent=None): |
| | """ |
| | Initialize the editor widget. |
| | |
| | Args: |
| | obj (App.DocumentObject, optional): The object to edit. Defaults to None. |
| | properties_to_show (list[str], optional): List of property names to display. |
| | Defaults to None (shows nothing). |
| | property_suffixes (dict[str, str], optional): Dictionary mapping property names |
| | to suffixes for their labels. |
| | Defaults to None. |
| | parent (QWidget, optional): The parent widget. Defaults to None. |
| | """ |
| | super().__init__(parent) |
| | self._obj = obj |
| | self._properties_to_show = properties_to_show if properties_to_show else [] |
| | self._property_suffixes = property_suffixes if property_suffixes else {} |
| | self._property_editors = {} |
| |
|
| | self._layout = QtGui.QFormLayout(self) |
| | self._layout.setContentsMargins(0, 0, 0, 0) |
| | self._layout.setFieldGrowthPolicy(QtGui.QFormLayout.FieldGrowthPolicy.ExpandingFieldsGrow) |
| |
|
| | self._populate_form() |
| |
|
| | def _clear_form(self): |
| | """Remove all rows from the form layout.""" |
| | while self._layout.rowCount() > 0: |
| | self._layout.removeRow(0) |
| | self._property_editors.clear() |
| |
|
| | def _populate_form(self): |
| | """Create and add property editors to the form.""" |
| | self._clear_form() |
| | if not self._obj: |
| | return |
| |
|
| | for prop_name in self._properties_to_show: |
| | |
| | if not hasattr(self._obj, prop_name): |
| | continue |
| |
|
| | editor_widget = BasePropertyEditorWidget.for_property(self._obj, prop_name, self) |
| | label_text = _get_label_text(prop_name) |
| | suffix = self._property_suffixes.get(prop_name) |
| | if suffix: |
| | label_text = f"{label_text} ({suffix}):" |
| | else: |
| | label_text = f"{label_text}:" |
| |
|
| | label = QtGui.QLabel(label_text) |
| | self._layout.addRow(label, editor_widget) |
| | self._property_editors[prop_name] = editor_widget |
| |
|
| | |
| | editor_widget.propertyChanged.connect(self.propertyChanged) |
| |
|
| | def setObject(self, obj): |
| | """Set or change the DocumentObject being edited.""" |
| | if obj != self._obj: |
| | self._obj = obj |
| | |
| | |
| | |
| | for prop_name, editor in self._property_editors.items(): |
| | editor.attachTo(self._obj, prop_name) |
| |
|
| | def setPropertiesToShow(self, properties_to_show, property_suffixes=None): |
| | """Set or change the list of properties to display.""" |
| | self._properties_to_show = properties_to_show if properties_to_show else [] |
| | self._property_suffixes = property_suffixes if property_suffixes else {} |
| | self._populate_form() |
| |
|
| | def updateUI(self): |
| | """Update all child editor widgets from the object's properties.""" |
| | for editor in self._property_editors.values(): |
| | editor.updateWidget() |
| |
|
| | def updateObject(self): |
| | """Update the object's properties from all child editor widgets.""" |
| | |
| | |
| | for editor in self._property_editors.values(): |
| | editor.updateProperty() |
| |
|