Skip to content
Snippets Groups Projects
Commit 7c94e73b authored by Olle Hansson's avatar Olle Hansson
Browse files

Closes #128, #134, #138. Working on #136

parent 19f7803f
No related branches found
No related tags found
1 merge request!186Closes #128, #134, #138. Working on #136
Pipeline #89644 passed
...@@ -65,6 +65,8 @@ class MainWindow(QMainWindow): ...@@ -65,6 +65,8 @@ class MainWindow(QMainWindow):
self.operationDragDict = {} self.operationDragDict = {}
self.operationItemSceneList = [] self.operationItemSceneList = []
self.signalList = [] self.signalList = []
self.mouse_pressed = False
self.mouse_draggin = False
self.pressed_operations = [] self.pressed_operations = []
self.portDict = {} self.portDict = {}
self.signalPortDict = {} self.signalPortDict = {}
...@@ -191,6 +193,7 @@ class MainWindow(QMainWindow): ...@@ -191,6 +193,7 @@ class MainWindow(QMainWindow):
operation_positions[op_drag.operation.graph_id] = ( operation_positions[op_drag.operation.graph_id] = (
int(op_scene.x()), int(op_scene.x()),
int(op_scene.y()), int(op_scene.y()),
op_drag.is_flipped(),
) )
try: try:
...@@ -251,10 +254,17 @@ class MainWindow(QMainWindow): ...@@ -251,10 +254,17 @@ class MainWindow(QMainWindow):
if positions is None: if positions is None:
positions = {} positions = {}
# print(sfg)
for op in sfg.split(): for op in sfg.split():
# print(op)
self.create_operation( self.create_operation(
op, op,
positions[op.graph_id] if op.graph_id in positions else None, positions[op.graph_id][0:2]
if op.graph_id in positions
else None,
positions[op.graph_id][-1]
if op.graph_id in positions
else None,
) )
def connect_ports(ports): def connect_ports(ports):
...@@ -484,8 +494,14 @@ class MainWindow(QMainWindow): ...@@ -484,8 +494,14 @@ class MainWindow(QMainWindow):
namespace, self.ui.custom_operations_list namespace, self.ui.custom_operations_list
) )
def create_operation(self, op, position=None): def create_operation(self, op, position=None, is_flipped=False):
try: try:
if op in self.operationDragDict:
self.logger.warning(
"Multiple instances of operation with same name"
)
return
attr_button = DragButton(op.graph_id, op, True, window=self) attr_button = DragButton(op.graph_id, op, True, window=self)
if position is None: if position is None:
attr_button.move(GRID * 3, GRID * 2) attr_button.move(GRID * 3, GRID * 2)
...@@ -537,8 +553,14 @@ class MainWindow(QMainWindow): ...@@ -537,8 +553,14 @@ class MainWindow(QMainWindow):
) )
operation_label.moveBy(10, -20) operation_label.moveBy(10, -20)
attr_button.add_label(operation_label) attr_button.add_label(operation_label)
if isinstance(is_flipped, bool):
if is_flipped:
attr_button._flip()
self.operationDragDict[op] = attr_button self.operationDragDict[op] = attr_button
self.dragOperationSceneDict[attr_button] = attr_button_scene self.dragOperationSceneDict[attr_button] = attr_button_scene
except Exception as e: except Exception as e:
self.logger.error( self.logger.error(
"Unexpected error occurred while creating operation: " + str(e) "Unexpected error occurred while creating operation: " + str(e)
......
""" """
B-ASIC window to simulate an SFG. B-ASIC window to simulate an SFG.
""" """
import numpy as np
from matplotlib.backends.backend_qt5agg import ( from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas, FigureCanvasQTAgg as FigureCanvas,
) )
...@@ -9,6 +10,7 @@ from qtpy.QtCore import Qt, Signal ...@@ -9,6 +10,7 @@ from qtpy.QtCore import Qt, Signal
from qtpy.QtGui import QKeySequence from qtpy.QtGui import QKeySequence
from qtpy.QtWidgets import ( from qtpy.QtWidgets import (
QCheckBox, QCheckBox,
QComboBox,
QDialog, QDialog,
QFileDialog, QFileDialog,
QFormLayout, QFormLayout,
...@@ -24,6 +26,8 @@ from qtpy.QtWidgets import ( ...@@ -24,6 +26,8 @@ from qtpy.QtWidgets import (
QVBoxLayout, QVBoxLayout,
) )
from b_asic.signal_generator import Impulse, Step, ZeroPad
class SimulateSFGWindow(QDialog): class SimulateSFGWindow(QDialog):
simulate = Signal() simulate = Signal()
...@@ -42,6 +46,8 @@ class SimulateSFGWindow(QDialog): ...@@ -42,6 +46,8 @@ class SimulateSFGWindow(QDialog):
self.simulate_btn.clicked.connect(self.save_properties) self.simulate_btn.clicked.connect(self.save_properties)
self.dialog_layout.addWidget(self.simulate_btn) self.dialog_layout.addWidget(self.simulate_btn)
self.setLayout(self.dialog_layout) self.setLayout(self.dialog_layout)
self.input_grid = QGridLayout()
self.input_files = {}
def add_sfg_to_dialog(self, sfg): def add_sfg_to_dialog(self, sfg):
sfg_layout = QVBoxLayout() sfg_layout = QVBoxLayout()
...@@ -66,36 +72,37 @@ class SimulateSFGWindow(QDialog): ...@@ -66,36 +72,37 @@ class SimulateSFGWindow(QDialog):
"iteration_count": spin_box, "iteration_count": spin_box,
"show_plot": check_box_plot, "show_plot": check_box_plot,
"all_results": check_box_all, "all_results": check_box_all,
"input_values": [],
} }
if sfg.input_count > 0: if sfg.input_count > 0:
input_label = QLabel("Input values:") input_label = QLabel("Input values:")
options_layout.addRow(input_label) options_layout.addRow(input_label)
input_grid = QGridLayout()
x, y = 0, 0 x, y = 0, 0
for i in range(sfg.input_count): for i in range(sfg.input_count):
input_layout = QHBoxLayout()
input_layout.addStretch()
if i % 2 == 0 and i > 0: if i % 2 == 0 and i > 0:
x += 1 x += 1
y = 0 y = 0
input_label = QLabel(f"in{i}") input_label = QLabel(f"in{i}")
input_layout.addWidget(input_label) self.input_grid.addWidget(input_label, i, 0)
input_value = QLineEdit()
input_value.setPlaceholderText("e.g. 0, 0, 0") input_dropdown = QComboBox()
input_value.setFixedWidth(100) input_dropdown.insertItems(
input_layout.addWidget(input_value) 0, ["Impulse", "Step", "Input", "File"]
input_layout.addStretch() )
input_layout.setSpacing(10) input_dropdown.currentTextChanged.connect(
input_grid.addLayout(input_layout, x, y) lambda text, i=i: self.change_input_format(i, text)
)
self.input_fields[sfg]["input_values"].append(input_value) self.input_grid.addWidget(
input_dropdown, i, 1, alignment=Qt.AlignLeft
)
self.change_input_format(i, "Impulse")
y += 1 y += 1
sfg_layout.addLayout(input_grid) sfg_layout.addLayout(self.input_grid)
frame = QFrame() frame = QFrame()
frame.setFrameShape(QFrame.HLine) frame.setFrameShape(QFrame.HLine)
...@@ -105,6 +112,59 @@ class SimulateSFGWindow(QDialog): ...@@ -105,6 +112,59 @@ class SimulateSFGWindow(QDialog):
self.sfg_to_layout[sfg] = sfg_layout self.sfg_to_layout[sfg] = sfg_layout
self.dialog_layout.addLayout(sfg_layout) self.dialog_layout.addLayout(sfg_layout)
def change_input_format(self, i, text):
grid = self.input_grid.itemAtPosition(i, 2)
if grid:
for j in reversed(range(grid.count())):
item = grid.itemAt(j)
widget = item.widget()
if widget:
widget.hide()
self.input_grid.removeItem(grid)
param_grid = QGridLayout()
if text == "Impulse":
delay_label = QLabel("Delay")
param_grid.addWidget(delay_label, 0, 0)
delay_spin_box = QSpinBox()
delay_spin_box.setRange(0, 2147483647)
param_grid.addWidget(delay_spin_box, 0, 1)
elif text == "Step":
delay_label = QLabel("Delay")
param_grid.addWidget(delay_label, 0, 0)
delay_spin_box = QSpinBox()
delay_spin_box.setRange(0, 2147483647)
param_grid.addWidget(delay_spin_box, 0, 1)
elif text == "Input":
input_label = QLabel("Input")
param_grid.addWidget(input_label, 0, 0)
input_sequence = QLineEdit()
param_grid.addWidget(input_sequence, 0, 1)
zpad_label = QLabel("Zpad")
param_grid.addWidget(zpad_label, 1, 0)
zpad_button = QCheckBox()
param_grid.addWidget(zpad_button, 1, 1)
elif text == "File":
file_label = QLabel("Browse")
param_grid.addWidget(file_label, 0, 0)
file_browser = QPushButton("No file selected")
file_browser.clicked.connect(
lambda i=i: self.get_input_file(i, file_browser)
)
param_grid.addWidget(file_browser, 0, 1)
else:
raise Exception("Input selection is not implemented")
self.input_grid.addLayout(param_grid, i, 2)
return
def get_input_file(self, i, file_browser):
module, accepted = QFileDialog().getOpenFileName()
file_browser.setText(module)
return
def parse_input_values(self, input_values): def parse_input_values(self, input_values):
_input_values = [] _input_values = []
for _list in input_values: for _list in input_values:
...@@ -128,15 +188,60 @@ class SimulateSFGWindow(QDialog): ...@@ -128,15 +188,60 @@ class SimulateSFGWindow(QDialog):
def save_properties(self): def save_properties(self):
for sfg, _properties in self.input_fields.items(): for sfg, _properties in self.input_fields.items():
input_values = self.parse_input_values( ic_value = self.input_fields[sfg]["iteration_count"].value()
[ if ic_value == 0:
widget.text().split(",") if widget.text() else [0] self._window.logger.error("Iteration count is set to zero.")
for widget in self.input_fields[sfg]["input_values"]
] tmp = []
)
for i in range(self.input_grid.rowCount()):
in_format = (
self.input_grid.itemAtPosition(i, 1).widget().currentText()
)
in_param = self.input_grid.itemAtPosition(i, 2)
tmp2 = []
if in_format == "Impulse":
g = Impulse(in_param.itemAtPosition(0, 1).widget().value())
for j in range(ic_value):
tmp2.append(str(g(j)))
elif in_format == "Step":
g = Step(in_param.itemAtPosition(0, 1).widget().value())
for j in range(ic_value):
tmp2.append(str(g(j)))
elif in_format == "Input":
widget = in_param.itemAtPosition(0, 1).widget()
tmp3 = widget.text().split(",")
if in_param.itemAtPosition(1, 1).widget().isChecked():
g = ZeroPad(tmp3)
for j in range(ic_value):
tmp2.append(str(g(j)))
else:
tmp2 = tmp3
elif in_format == "File":
widget = in_param.itemAtPosition(0, 1).widget()
path = widget.text()
try:
tmp2 = np.loadtxt(path, dtype=str).tolist()
except FileNotFoundError:
self._window.logger.error(
f"Selected input file not found."
)
continue
else:
raise Exception("Input selection is not implemented")
tmp.append(tmp2)
input_values = self.parse_input_values(tmp)
max_len = max(len(list_) for list_ in input_values) max_len = max(len(list_) for list_ in input_values)
min_len = min(len(list_) for list_ in input_values) min_len = min(len(list_) for list_ in input_values)
ic_value = self.input_fields[sfg]["iteration_count"].value()
if max_len != min_len: if max_len != min_len:
self._window.logger.error( self._window.logger.error(
"Minimum length of input lists are not equal to maximum " "Minimum length of input lists are not equal to maximum "
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment