Skip to content
Snippets Groups Projects
Commit c4904caf authored by Christoph Schmidt's avatar Christoph Schmidt
Browse files

Updated to Version 1.1.2. Has now the option to connect a property that is...

Updated to Version 1.1.2. Has now the option to connect a property that is automatically set on a field change.
parent 593fa9f2
No related branches found
No related tags found
No related merge requests found
Showing
with 63 additions and 35 deletions
...@@ -8,12 +8,28 @@ from rich.logging import RichHandler ...@@ -8,12 +8,28 @@ from rich.logging import RichHandler
from ApplicationConfig import ApplicationConfig from ApplicationConfig import ApplicationConfig
class TestClass:
def __init__(self):
self._wafer = 0
@property
def wafer(self):
return self._wafer
@wafer.setter
def wafer(self, value):
self._wafer = value
print("Wafer changed to", value)
if __name__ == "__main__": if __name__ == "__main__":
app = QApplication(sys.argv) app = QApplication(sys.argv)
# setup the logging module # setup the logging module
config = ApplicationConfig() config = ApplicationConfig()
testclass = TestClass()
time.sleep(1) time.sleep(1)
#config.autosave(enable=True, path='./configs_autosave') #config.autosave(enable=True, path='./configs_autosave')
(config.load('./configs/ApplicationConfig.yaml')) (config.load('./configs/ApplicationConfig.yaml'))
...@@ -45,6 +61,12 @@ if __name__ == "__main__": ...@@ -45,6 +61,12 @@ if __name__ == "__main__":
btn_save.clicked.connect(lambda: config.save('./configs/ApplicationConfig.yaml')) btn_save.clicked.connect(lambda: config.save('./configs/ApplicationConfig.yaml'))
grd.addWidget(btn_save, 4, 0) grd.addWidget(btn_save, 4, 0)
# Add a new combo box
combo = QtWidgets.QComboBox()
grd.addWidget(combo, 5, 0)
config.wafer_list1.view.add_new_view(combo)
config.wafer_list1.connect_property(testclass, TestClass.wafer)
window.setCentralWidget(wdg) window.setCentralWidget(wdg)
#print(config.load('config.yaml')) #print(config.load('config.yaml'))
......
...@@ -11,7 +11,6 @@ class CSignal: ...@@ -11,7 +11,6 @@ class CSignal:
self.connections = [] self.connections = []
def emit(self, *args, **kwargs): def emit(self, *args, **kwargs):
#print(f"CSignal emit: {args}, {kwargs}")
for connection in self.connections: for connection in self.connections:
connection() connection()
......
...@@ -23,6 +23,7 @@ class FieldData(object): ...@@ -23,6 +23,7 @@ class FieldData(object):
self.value = [value, friendly_name, description] self.value = [value, friendly_name, description]
T = TypeVar('T') T = TypeVar('T')
...@@ -47,6 +48,9 @@ class Field(Generic[T], CObject): ...@@ -47,6 +48,9 @@ class Field(Generic[T], CObject):
# The view, usd for handling the UI # The view, usd for handling the UI
self.view = FieldView(self) self.view = FieldView(self)
# Connected properties that should bet set if the field changes
self.props = []
self._internal_logger.debug(f"Field {self.__class__.__name__} created with value {value} of type {type(value)}") self._internal_logger.debug(f"Field {self.__class__.__name__} created with value {value} of type {type(value)}")
def __new__(cls, value, friendly_name: str = None, description: str = None): def __new__(cls, value, friendly_name: str = None, description: str = None):
...@@ -80,6 +84,13 @@ class Field(Generic[T], CObject): ...@@ -80,6 +84,13 @@ class Field(Generic[T], CObject):
"""Used for serializing instances. Returns the current field as a yaml-line.""" """Used for serializing instances. Returns the current field as a yaml-line."""
return f"{self.name}: {self._yaml_repr()} # {self.friendly_name}: {self.description}" return f"{self.name}: {self._yaml_repr()} # {self.friendly_name}: {self.description}"
def connect_property(self, instance, prop: property):
self.props.append((instance, prop))
def _set_all_props(self, value):
for inst, prop in self.props:
prop.fset(inst, value)
# ================================================================================================================== # ==================================================================================================================
# Register the field to a configuration # Register the field to a configuration
# ================================================================================================================== # ==================================================================================================================
...@@ -161,6 +172,7 @@ class Field(Generic[T], CObject): ...@@ -161,6 +172,7 @@ class Field(Generic[T], CObject):
By default, the value will be emitted. Overwrite if you need another behavior By default, the value will be emitted. Overwrite if you need another behavior
:return: :return:
""" """
return self._value return self._value
def get(self) -> T: def get(self) -> T:
...@@ -169,6 +181,7 @@ class Field(Generic[T], CObject): ...@@ -169,6 +181,7 @@ class Field(Generic[T], CObject):
def set(self, value: T, *args, force_emit: bool = False, **kwargs): def set(self, value: T, *args, force_emit: bool = False, **kwargs):
if not self._value_to_emit == value or force_emit: if not self._value_to_emit == value or force_emit:
self._internal_logger.info(f"{self.name} = {value} ({type(value)})") self._internal_logger.info(f"{self.name} = {value} ({type(value)})")
self._set_all_props(value)
self._set(value, *args, **kwargs) self._set(value, *args, **kwargs)
self.set_keywords() self.set_keywords()
# self.view.value_changed.emit(self.value) # self.view.value_changed.emit(self.value)
...@@ -186,7 +199,6 @@ class Field(Generic[T], CObject): ...@@ -186,7 +199,6 @@ class Field(Generic[T], CObject):
def _on_keyword_changed(self): def _on_keyword_changed(self):
self.set(self._value_to_emit) self.set(self._value_to_emit)
# print(f"Field {self.__class__.__name__}._on_keyword_changed called: {self.value}: {str(self.value)}")
self.view.value_changed.emit(self._value_to_emit) self.view.value_changed.emit(self._value_to_emit)
def _yaml_repr(self): def _yaml_repr(self):
......
...@@ -35,7 +35,6 @@ class FieldSelectableList(ch.Field): ...@@ -35,7 +35,6 @@ class FieldSelectableList(ch.Field):
return self._value.selected_index return self._value.selected_index
def _set(self, value, list = None, description = None): def _set(self, value, list = None, description = None):
print(f"FieldSelectableList: {value}, {list}, {description}")
if list is not None: if list is not None:
self._value = ch.SelectableList(list, selected_index=value, description=description) self._value = ch.SelectableList(list, selected_index=value, description=description)
self.csig_field_changed.emit(self._value_to_emit) self.csig_field_changed.emit(self._value_to_emit)
......
...@@ -20,6 +20,7 @@ class FieldView(QWidget): ...@@ -20,6 +20,7 @@ class FieldView(QWidget):
def __init__(self, parent_field): def __init__(self, parent_field):
super().__init__() super().__init__()
self.parent_field = parent_field self.parent_field = parent_field
self.label = None self.label = None
...@@ -33,11 +34,26 @@ class FieldView(QWidget): ...@@ -33,11 +34,26 @@ class FieldView(QWidget):
print(">>> String") print(">>> String")
def _on_value_changed(self, value): def _on_value_changed(self, value):
"""
Updates the UI when the value is changed.
:param value:
:return:
"""
self._on_value_changed_partial(value)
def _on_value_changed_partial(self, value):
"""
Additional actions when the value is changed.
:param value:
:return:
"""
raise NotImplementedError() raise NotImplementedError()
def add_new_view(self, view: QWidget): def add_new_view(self, view: QWidget):
self.ui_field(view) self.ui_field(view)
def ui_field(self, view: QWidget) -> QWidget: def ui_field(self, view: QWidget) -> QWidget:
""" """
Returns a QLineEdit for the UI. Returns a QLineEdit for the UI.
......
...@@ -41,7 +41,7 @@ class FieldViewBool(FieldView): ...@@ -41,7 +41,7 @@ class FieldViewBool(FieldView):
# def _on_keyword_changed(self, keywords): # def _on_keyword_changed(self, keywords):
# pass # pass
def _on_value_changed(self, value): def _on_value_changed_partial(self, value):
# print(f">>> {self.parent_field.name}: Value changed {value}") # print(f">>> {self.parent_field.name}: Value changed {value}")
for edit in self.ui_edit_fields: for edit in self.ui_edit_fields:
edit: QCheckBox # Just for typehinting edit: QCheckBox # Just for typehinting
......
...@@ -40,7 +40,7 @@ class FieldViewFloat(FieldView): ...@@ -40,7 +40,7 @@ class FieldViewFloat(FieldView):
def _on_value_edited(self, value): def _on_value_edited(self, value):
self.parent_field.set(float(value)) self.parent_field.set(float(value))
def _on_value_changed(self, value): def _on_value_changed_partial(self, value):
# print(f">>> {self.parent_field.name}: Value changed {value}") # print(f">>> {self.parent_field.name}: Value changed {value}")
for edit in self.ui_edit_fields: for edit in self.ui_edit_fields:
edit.setValue(float(value)) edit.setValue(float(value))
......
...@@ -42,7 +42,7 @@ class FieldViewInt(FieldView): ...@@ -42,7 +42,7 @@ class FieldViewInt(FieldView):
# def _on_keyword_changed(self, keywords): # def _on_keyword_changed(self, keywords):
# pass # pass
def _on_value_changed(self, value): def _on_value_changed_partial(self, value):
#print(f">>> {self.parent_field.name}: Value changed {value}") #print(f">>> {self.parent_field.name}: Value changed {value}")
for edit in self.ui_edit_fields: for edit in self.ui_edit_fields:
edit.setValue(int(value)) edit.setValue(int(value))
...@@ -39,7 +39,7 @@ class FieldViewList(FieldView): ...@@ -39,7 +39,7 @@ class FieldViewList(FieldView):
def _on_text_edited(self, value): def _on_text_edited(self, value):
self.parent_field.set(value) self.parent_field.set(value)
def _on_value_changed(self, value): def _on_value_changed_partial(self, value):
for edit in self.ui_edit_fields: for edit in self.ui_edit_fields:
edit.setText(str(value)) edit.setText(str(value))
......
...@@ -70,7 +70,7 @@ class FieldViewPath(FieldView): ...@@ -70,7 +70,7 @@ class FieldViewPath(FieldView):
def _on_text_edited(self, value): def _on_text_edited(self, value):
self.parent_field.set(value) self.parent_field.set(value)
def _on_value_changed(self, value: Path): def _on_value_changed_partial(self, value: Path):
# print(value) # print(value)
# Check if path exists # Check if path exists
for edit, lbl in zip(self.ui_edit_fields, self.ui_edit_fields_lbl): for edit, lbl in zip(self.ui_edit_fields, self.ui_edit_fields_lbl):
......
...@@ -14,26 +14,6 @@ from PySide6.QtWidgets import QWidget, QLineEdit, QComboBox, QVBoxLayout, QLabel ...@@ -14,26 +14,6 @@ from PySide6.QtWidgets import QWidget, QLineEdit, QComboBox, QVBoxLayout, QLabel
import confighandler as ch import confighandler as ch
class MyComboBox(QComboBox):
def __init__(self, parent=None):
super(MyComboBox, self).__init__(parent)
self.setContextMenuPolicy() # Qt.CustomContextMenu
def show_context_menu(self, pos):
index = self.currentIndex()
if index >= 0:
context_menu = QMenu(self)
delete_action = QAction('Delete', self)
delete_action.triggered.connect(self.delete_item)
context_menu.addAction(delete_action)
action = context_menu.exec_(self.mapToGlobal(pos))
def delete_item(self):
index = self.currentIndex()
if index >= 0:
self.removeItem(index)
class FieldViewAddEntry(QWidget): class FieldViewAddEntry(QWidget):
value_changed = Signal(tuple) value_changed = Signal(tuple)
...@@ -129,13 +109,12 @@ class FieldViewSelectableList(ch.FieldView): ...@@ -129,13 +109,12 @@ class FieldViewSelectableList(ch.FieldView):
cb.clear() cb.clear()
sel_list = self.parent_field.get_selectable_list() sel_list = self.parent_field.get_selectable_list()
for v in sel_list: for v in sel_list:
cb.addItem(f"{v[1]} - {v[0]} ", v[0]) cb.addItem(f"{v[1]}", v[0])
cb.addItem("<Add new ...>", self.add_entry.show) cb.addItem("<Add new ...>", self.add_entry.show)
#cb.currentIndexChanged.connect(self._on_index_changed) #cb.currentIndexChanged.connect(self._on_index_changed)
def show_context_menu(self, pos, field): def show_context_menu(self, pos, field):
print(field)
index = field.currentIndex() index = field.currentIndex()
if index >= 0: if index >= 0:
context_menu = QMenu(field) context_menu = QMenu(field)
...@@ -168,8 +147,9 @@ class FieldViewSelectableList(ch.FieldView): ...@@ -168,8 +147,9 @@ class FieldViewSelectableList(ch.FieldView):
self.parent_field.set(len(self.parent_field.get_selectable_list()) - 1) self.parent_field.set(len(self.parent_field.get_selectable_list()) - 1)
self.parent_field.csig_field_changed.emit() self.parent_field.csig_field_changed.emit()
def _on_value_changed(self, value): def _on_value_changed_partial(self, value):
for edit in self.ui_edit_fields: for edit in self.ui_edit_fields:
edit: QComboBox edit: QComboBox
self.parent_field.logger.info(f"{edit}: Setting index to {value}") # self.parent_field.logger.info(f"{edit}: Setting index to {value}")
edit.setCurrentIndex(value) edit.setCurrentIndex(value)
edit.setToolTip(f"<{edit.currentData()}> ({self.parent_field.name}) {self.parent_field._description}")
...@@ -41,7 +41,7 @@ class FieldViewString(FieldView): ...@@ -41,7 +41,7 @@ class FieldViewString(FieldView):
self.parent_field._internal_logger.debug(f"LineEdit {f} changed to {value}.") self.parent_field._internal_logger.debug(f"LineEdit {f} changed to {value}.")
self.parent_field.set(value) self.parent_field.set(value)
def _on_value_changed(self, value): def _on_value_changed_partial(self, value):
for edit in self.ui_edit_fields: for edit in self.ui_edit_fields:
edit.setText(value) edit.setText(value)
# for tree_item in self.tree_items: # for tree_item in self.tree_items:
......
...@@ -39,7 +39,7 @@ class FieldViewTuple(FieldView): ...@@ -39,7 +39,7 @@ class FieldViewTuple(FieldView):
def _on_text_edited(self, value): def _on_text_edited(self, value):
self.parent_field.set(value) self.parent_field.set(value)
def _on_value_changed(self, value): def _on_value_changed_partial(self, value):
for edit in self.ui_edit_fields: for edit in self.ui_edit_fields:
edit.setText(str(value)) edit.setText(str(value))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment