Skip to content
Snippets Groups Projects

General gui module

Merged Johannes Kung requested to merge refactor_gui into main
All threads resolved!
9 files
+ 1026
57
Compare changes
  • Side-by-side
  • Inline
Files
9
from typing import Optional
from qtpy.QtCore import Qt
from qtpy.QtCore import Signal as pyqtSignal
from qtpy.QtCore import Slot
from qtpy.QtWidgets import (
QComboBox,
QDialog,
@@ -12,65 +11,97 @@ from qtpy.QtWidgets import (
)
from simudator.core import Module
from simudator.gui.formatting import format_to_str, parse_str
from simudator.gui.module_graphics_item.module_widget import ModuleWidget
class ModuleStateDialog(QDialog):
accepted = pyqtSignal(str, str, str, name='okSignal')
"""
A dialog for letting the user edit the state of a processor module.
Parameters
----------
module : Module
The module to edit the state of.
module_widget : ModuleWidget
The corresponding module widget for the module. Used mainly for
formatting and parsing the values of the state variables of the module.
parent : QWidget | None
Optional parent widget of this dialog.
flags : Qt.WindowFlags | Qt.WindowType
Optional window flags for the window of the dialog.
"""
state_edited = pyqtSignal()
def __init__(
self,
module: Module,
parent: Optional['QWidget'] = None,
module_widget: ModuleWidget,
parent: QWidget | None = None,
flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags(),
) -> None:
super().__init__(parent, flags)
self.module = module
states = module.get_gui_state()
# Remove the 'name' state of the module from the drop down menu so
# that the user cannot e.g. edit or add a breakpoint of a module
states.pop('name')
self._module = module
self._state_vars = module_widget.get_state_vars()
# Set up the drop down menu for selecting a state of the module
# to perform an action to
self.stateSelectWidget = QComboBox()
self.stateSelectWidget.addItems(states.keys())
self.stateSelectWidget.activated.connect(self.updateState)
# Set up the drop down menu for selecting a state variable of the module
self._state_var_widget = QComboBox()
self._state_var_widget.addItems(self._state_vars.keys())
# Set up a text field for entering the value used for the e.g. editing
# the selected state
self.valueWidget = QLineEdit()
# Set up a text field for displaying and editing the value of the
# selected state variable
self._value_widget = QLineEdit()
# Set up buttons
self.okButton = QPushButton('OK')
self.okButton.clicked.connect(self.signalOK)
self.cancelButton = QPushButton('Cancel')
self.cancelButton.clicked.connect(self.close)
self._accept_button = QPushButton('OK')
self._accept_button.clicked.connect(self.edit_state)
self._cancel_button = QPushButton('Cancel')
self._cancel_button.clicked.connect(self.close)
# Set up the layout of the widget
self.HBoxLayout = QHBoxLayout()
self.HBoxLayout.addWidget(self.stateSelectWidget, 0)
self.HBoxLayout.addWidget(self.valueWidget, 1)
self.HBoxLayout.addWidget(self.okButton, 2)
self.HBoxLayout.addWidget(self.cancelButton, 3)
self.setLayout(self.HBoxLayout)
self._layout = QHBoxLayout()
self._layout.addWidget(self._state_var_widget, 0)
self._layout.addWidget(self._value_widget, 1)
self._layout.addWidget(self._accept_button, 2)
self._layout.addWidget(self._cancel_button, 3)
self.setLayout(self._layout)
# Show the widget
self.updateState()
self.show()
def updateState(self) -> None:
selectedState = self.stateSelectWidget.currentText()
value = self.module.get_gui_state()[selectedState]
self.valueWidget.setText(str(value))
def signalOK(self) -> None:
module_name = self.module.get_state()['name']
selectedState = self.stateSelectWidget.currentText()
enteredValue = self.valueWidget.text()
self.accepted.emit(module_name, selectedState, enteredValue)
self.close()
def close(self) -> bool:
return super().close()
self.select_state_var()
self.exec()
@Slot()
def select_state_var(self) -> None:
"""
Select a state variable of the module to edit.
"""
selected_state = self._state_var_widget.currentText()
format_info = self._state_vars[selected_state]
value = format_to_str(format_info, self._module.get_state()[selected_state])
self._value_widget.setText(value)
@Slot()
def edit_state(self) -> None:
"""
Parse the user input and edit the state of the module associated with
this dialog.
This triggers the `state_edited` signal if the parsing is
successful. Else, the user is given an error message.
"""
state = self._module.get_state()
selected_state_var = self._state_var_widget.currentText()
try:
value = parse_str(
self._state_vars[selected_state_var], self._value_widget.text()
)
state[selected_state_var] = value
self._module.set_state(state)
self.state_edited.emit()
self.close()
except ValueError as e:
# TODO: error handling
pass
Loading