Newer
Older
from qtpy.QtCore import Qt
from qtpy.QtGui import QDoubleValidator
from qtpy.QtWidgets import (
QCheckBox,
QDialog,
QGridLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QVBoxLayout,
)
Angus Lothian
committed
class PropertiesWindow(QDialog):
def __init__(self, operation, main_window):
Angus Lothian
committed
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 = {}
Angus Lothian
committed
self._vertical_layout = QVBoxLayout()
self._vertical_layout.addLayout(self._name_layout)
Angus Lothian
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)
Angus Lothian
committed
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)
Angus Lothian
committed
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
Angus Lothian
committed
input_layout.addWidget(input_label)
input_value = QLineEdit()
try:
Angus Lothian
committed
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
Angus Lothian
committed
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)
Angus Lothian
committed
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)
Angus Lothian
committed
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
Angus Lothian
committed
input_layout.addWidget(input_label)
input_value = QLineEdit()
try:
Angus Lothian
committed
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
Angus Lothian
committed
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)
Angus Lothian
committed
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)
Angus Lothian
committed
def save_properties(self):
self._window._logger.info(
f"Saving _properties of operation: {self.operation.name}."
self.operation.name = self._edit_name.text()
self.operation.operation.name = self._edit_name.text()
Angus Lothian
committed
self.operation.label.setPlainText(self.operation.name)
if hasattr(self.operation.operation, "value"):
self._edit_constant.text().replace(",", ".")
Angus Lothian
committed
elif hasattr(self.operation.operation, "initial_value"):
self._edit_constant.text().replace(",", ".")
Angus Lothian
committed
if self._check_show_name.isChecked():
Angus Lothian
committed
self.operation.label.setOpacity(1)
self.operation.show_name = True
Angus Lothian
committed
else:
self.operation.label.setOpacity(0)
self.operation.show_name = False
Angus Lothian
committed
port: float(latency_edit.text().replace(",", "."))
if latency_edit.text()
and float(latency_edit.text().replace(",", ".")) > 0
for port, latency_edit in self._latency_fields.items()
Angus Lothian
committed