Skip to content
Snippets Groups Projects
Commit f619fd21 authored by Oscar Gustafsson's avatar Oscar Gustafsson :bicyclist:
Browse files

Refactor signal generation GUI

parent fc3fa764
No related branches found
No related tags found
No related merge requests found
Pipeline #89884 passed
# -*- coding: utf-8 -*-
from qtpy.QtWidgets import (
QCheckBox,
QComboBox,
QDialog,
QFileDialog,
QFormLayout,
QFrame,
QGridLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QShortcut,
QSizePolicy,
QSpinBox,
QVBoxLayout,
)
from b_asic.signal_generator import Impulse, Step
class SignalGeneratorInput(QGridLayout):
"""Abstract class for graphically configuring and generating signal generators.
"""
def get_generator(self):
raise NotImplementedError
class DelayInput(SignalGeneratorInput):
"""
Abstract class for graphically configuring and generating signal generators that
have a single delay parameter.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.delay_label = QLabel("Delay")
self.addWidget(self.delay_label, 0, 0)
self.delay_spin_box = QSpinBox()
self.delay_spin_box.setRange(0, 2147483647)
self.addWidget(self.delay_spin_box, 0, 1)
class ImpulseInput(DelayInput):
"""
Class for graphically configuring and generating a
:class:`~b_asic.signal_generators.Impulse` signal generator.
"""
def get_generator(self):
return Impulse(self.delay_spin_box.value())
class StepInput(DelayInput):
"""
Class for graphically configuring and generating a
:class:`~b_asic.signal_generators.Step` signal generator.
"""
def get_generator(self):
return Step(self.delay_spin_box.value())
...@@ -26,8 +26,11 @@ from qtpy.QtWidgets import ( ...@@ -26,8 +26,11 @@ from qtpy.QtWidgets import (
QVBoxLayout, QVBoxLayout,
) )
from b_asic.GUI.signal_generator_input import ImpulseInput, StepInput
from b_asic.signal_generator import Impulse, Step, ZeroPad from b_asic.signal_generator import Impulse, Step, ZeroPad
_GENERATOR_MAPPING = {"Impulse": ImpulseInput, "Step": StepInput}
class SimulateSFGWindow(QDialog): class SimulateSFGWindow(QDialog):
simulate = Signal() simulate = Signal()
...@@ -58,12 +61,15 @@ class SimulateSFGWindow(QDialog): ...@@ -58,12 +61,15 @@ class SimulateSFGWindow(QDialog):
spin_box = QSpinBox() spin_box = QSpinBox()
spin_box.setRange(0, 2147483647) spin_box.setRange(0, 2147483647)
spin_box.setValue(100)
options_layout.addRow("Iteration count: ", spin_box) options_layout.addRow("Iteration count: ", spin_box)
check_box_plot = QCheckBox() check_box_plot = QCheckBox()
check_box_plot.setCheckState(Qt.CheckState.Checked)
options_layout.addRow("Plot results: ", check_box_plot) options_layout.addRow("Plot results: ", check_box_plot)
check_box_all = QCheckBox() check_box_all = QCheckBox()
check_box_all.setCheckState(Qt.CheckState.Checked)
options_layout.addRow("Get all results: ", check_box_all) options_layout.addRow("Get all results: ", check_box_all)
sfg_layout.addLayout(options_layout) sfg_layout.addLayout(options_layout)
...@@ -124,18 +130,8 @@ class SimulateSFGWindow(QDialog): ...@@ -124,18 +130,8 @@ class SimulateSFGWindow(QDialog):
param_grid = QGridLayout() param_grid = QGridLayout()
if text == "Impulse": if text in ("Impulse", "Step"):
delay_label = QLabel("Delay") param_grid = _GENERATOR_MAPPING[text]()
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": elif text == "Input":
input_label = QLabel("Input") input_label = QLabel("Input")
param_grid.addWidget(input_label, 0, 0) param_grid.addWidget(input_label, 0, 0)
...@@ -192,7 +188,7 @@ class SimulateSFGWindow(QDialog): ...@@ -192,7 +188,7 @@ class SimulateSFGWindow(QDialog):
if ic_value == 0: if ic_value == 0:
self._window.logger.error("Iteration count is set to zero.") self._window.logger.error("Iteration count is set to zero.")
tmp = [] input_values = []
for i in range(self.input_grid.rowCount()): for i in range(self.input_grid.rowCount()):
in_format = ( in_format = (
...@@ -200,33 +196,22 @@ class SimulateSFGWindow(QDialog): ...@@ -200,33 +196,22 @@ class SimulateSFGWindow(QDialog):
) )
in_param = self.input_grid.itemAtPosition(i, 2) in_param = self.input_grid.itemAtPosition(i, 2)
tmp2 = [] if in_format in ("Impulse", "Step"):
tmp2 = in_param.get_generator()
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": elif in_format == "Input":
widget = in_param.itemAtPosition(0, 1).widget() widget = in_param.itemAtPosition(0, 1).widget()
tmp3 = widget.text().split(",") tmp3 = widget.text().split(",")
if in_param.itemAtPosition(1, 1).widget().isChecked(): if in_param.itemAtPosition(1, 1).widget().isChecked():
g = ZeroPad(tmp3) tmp2 = ZeroPad(tmp3)
for j in range(ic_value):
tmp2.append(str(g(j)))
else: else:
tmp2 = tmp3 tmp2 = self.parse_input_values(tmp3)
elif in_format == "File": elif in_format == "File":
widget = in_param.itemAtPosition(0, 1).widget() widget = in_param.itemAtPosition(0, 1).widget()
path = widget.text() path = widget.text()
try: try:
tmp2 = np.loadtxt(path, dtype=str).tolist() tmp2 = self.parse_input_values(
np.loadtxt(path, dtype=str).tolist()
)
except FileNotFoundError: except FileNotFoundError:
self._window.logger.error( self._window.logger.error(
f"Selected input file not found." f"Selected input file not found."
...@@ -235,24 +220,9 @@ class SimulateSFGWindow(QDialog): ...@@ -235,24 +220,9 @@ class SimulateSFGWindow(QDialog):
else: else:
raise Exception("Input selection is not implemented") raise Exception("Input selection is not implemented")
tmp.append(tmp2) input_values.append(tmp2)
input_values = self.parse_input_values(tmp)
max_len = max(len(list_) for list_ in input_values)
min_len = min(len(list_) for list_ in input_values)
if max_len != min_len: if True:
self._window.logger.error(
"Minimum length of input lists are not equal to maximum "
f"length of input lists: {max_len} != {min_len}."
)
elif ic_value > min_len:
self._window.logger.error(
"Minimum length of input lists are less than the "
f"iteration count: {ic_value} > {min_len}."
)
else:
self.properties[sfg] = { self.properties[sfg] = {
"iteration_count": ic_value, "iteration_count": ic_value,
"show_plot": self.input_fields[sfg][ "show_plot": self.input_fields[sfg][
......
...@@ -76,6 +76,18 @@ GUI.show\_pc\_window module ...@@ -76,6 +76,18 @@ GUI.show\_pc\_window module
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
GUI.signal\_generator\_input module
-----------------------------------
.. inheritance-diagram:: b_asic.GUI.signal_generator_input
:parts: 1
:top-classes: b_asic.GUI.signal_generator_input.SignalGeneratorInput
.. automodule:: b_asic.GUI.signal_generator_input
:members:
:undoc-members:
:show-inheritance:
GUI.simulate\_sfg\_window module GUI.simulate\_sfg\_window module
-------------------------------- --------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment