Skip to content
Snippets Groups Projects
properties_window.py 6.76 KiB
Newer Older
  • Learn to ignore specific revisions
  • from qtpy.QtCore import Qt
    from qtpy.QtGui import QDoubleValidator
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
    from qtpy.QtWidgets import (
        QCheckBox,
        QDialog,
        QGridLayout,
        QHBoxLayout,
        QLabel,
        QLineEdit,
        QPushButton,
        QVBoxLayout,
    )
    
    
    
    class PropertiesWindow(QDialog):
        def __init__(self, operation, main_window):
    
            self.operation = operation
            self._window = main_window
            self.setWindowFlags(Qt.WindowTitleHint | Qt.WindowCloseButtonHint)
            self.setWindowTitle("Properties")
    
    
            self._name_layout = QHBoxLayout()
            self._name_layout.setSpacing(50)
            self._name_label = QLabel("Name:")
            self._edit_name = QLineEdit(self.operation.name or self.operation.type_name)
            self._name_layout.addWidget(self._name_label)
            self._name_layout.addWidget(self._edit_name)
            self._latency_fields = {}
    
            self._vertical_layout = QVBoxLayout()
            self._vertical_layout.addLayout(self._name_layout)
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            if hasattr(self.operation.operation, "value") or hasattr(
                self.operation.operation, "initial_value"
            ):
    
                self._constant_layout = QHBoxLayout()
                self._constant_layout.setSpacing(50)
                self._constant_value = QLabel("Value:")
                constant = (
                    self.operation.operation.value
                    if hasattr(self.operation.operation, "value")
                    else self.operation.operation.initial_value
                )
                self._edit_constant = QLineEdit(str(constant))
    
                self._only_accept_float = QDoubleValidator()
                self._edit_constant.setValidator(self._only_accept_float)
                self._constant_layout.addWidget(self._constant_value)
                self._constant_layout.addWidget(self._edit_constant)
                self._vertical_layout.addLayout(self._constant_layout)
    
            self._show_name_layout = QHBoxLayout()
            self._check_show_name = QCheckBox("Show name?")
    
            self._check_show_name.setChecked(self.operation.show_name)
    
            self._check_show_name.setLayoutDirection(Qt.RightToLeft)
            self._check_show_name.setStyleSheet("spacing: 170px")
            self._show_name_layout.addWidget(self._check_show_name)
            self._vertical_layout.addLayout(self._show_name_layout)
    
    
            if self.operation.operation.input_count > 0:
    
                self._latency_layout = QHBoxLayout()
                self._latency_label = QLabel("Set latency for input ports (-1 for None):")
                self._latency_layout.addWidget(self._latency_label)
                self._vertical_layout.addLayout(self._latency_layout)
    
    
                input_grid = QGridLayout()
                x, y = 0, 0
                for i in range(self.operation.operation.input_count):
                    input_layout = QHBoxLayout()
                    input_layout.addStretch()
                    if i % 2 == 0 and i > 0:
                        x += 1
                        y = 0
    
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                    input_label = QLabel(f"in{i}")
    
                    input_layout.addWidget(input_label)
                    input_value = QLineEdit()
                    try:
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                        input_value.setPlaceholderText(
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                            str(self.operation.operation.latency)
                        )
    
                    except ValueError:
                        input_value.setPlaceholderText("-1")
                    int_valid = QDoubleValidator()
                    int_valid.setBottom(-1)
                    input_value.setValidator(int_valid)
                    input_value.setFixedWidth(50)
    
                    self._latency_fields[f"in{i}"] = input_value
    
                    input_layout.addWidget(input_value)
                    input_layout.addStretch()
                    input_layout.setSpacing(10)
                    input_grid.addLayout(input_layout, x, y)
                    y += 1
    
    
                self._vertical_layout.addLayout(input_grid)
    
    
            if self.operation.operation.output_count > 0:
    
                self._latency_layout = QHBoxLayout()
                self._latency_label = QLabel("Set latency for output ports (-1 for None):")
                self._latency_layout.addWidget(self._latency_label)
                self._vertical_layout.addLayout(self._latency_layout)
    
    
                input_grid = QGridLayout()
                x, y = 0, 0
                for i in range(self.operation.operation.output_count):
                    input_layout = QHBoxLayout()
                    input_layout.addStretch()
                    if i % 2 == 0 and i > 0:
                        x += 1
                        y = 0
    
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                    input_label = QLabel(f"out{i}")
    
                    input_layout.addWidget(input_label)
                    input_value = QLineEdit()
                    try:
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                        input_value.setPlaceholderText(
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                            str(self.operation.operation.latency)
                        )
    
                    except ValueError:
                        input_value.setPlaceholderText("-1")
                    int_valid = QDoubleValidator()
                    int_valid.setBottom(-1)
                    input_value.setValidator(int_valid)
                    input_value.setFixedWidth(50)
    
                    self._latency_fields[f"out{i}"] = input_value
    
                    input_layout.addWidget(input_value)
                    input_layout.addStretch()
                    input_layout.setSpacing(10)
                    input_grid.addLayout(input_layout, x, y)
                    y += 1
    
    
                self._vertical_layout.addLayout(input_grid)
    
            self._ok_button = QPushButton("OK")
            self._ok_button.clicked.connect(self.save_properties)
            self._vertical_layout.addWidget(self._ok_button)
            self.setLayout(self._vertical_layout)
    
            self._window._logger.info(
                f"Saving _properties of operation: {self.operation.name}."
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            )
    
            self.operation.name = self._edit_name.text()
            self.operation.operation.name = self._edit_name.text()
    
            self.operation.label.setPlainText(self.operation.name)
            if hasattr(self.operation.operation, "value"):
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                self.operation.operation.value = float(
    
                    self._edit_constant.text().replace(",", ".")
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                )
    
            elif hasattr(self.operation.operation, "initial_value"):
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                self.operation.operation.initial_value = float(
    
                    self._edit_constant.text().replace(",", ".")
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                )
    
            if self._check_show_name.isChecked():
    
                self.operation.show_name = True
    
                self.operation.show_name = False
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            self.operation.operation.set_latency_offsets(
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                {
    
                    port: float(latency_edit.text().replace(",", "."))
                    if latency_edit.text()
                    and float(latency_edit.text().replace(",", ".")) > 0
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                    else None
    
                    for port, latency_edit in self._latency_fields.items()