Added docstrings

This commit is contained in:
jupfi 2023-08-20 17:43:26 +02:00
parent 00d70decb0
commit 453449d336
2 changed files with 114 additions and 34 deletions

View file

@ -10,12 +10,20 @@ logger = logging.getLogger(__name__)
class PulseProgrammerController(ModuleController): class PulseProgrammerController(ModuleController):
def on_loading(self, pulse_parameter_options): def on_loading(self, pulse_parameter_options : dict) -> None:
"""This method is called when the module is loaded. It sets the pulse parameter options in the model.
Args:
pulse_parameter_options (dict): The pulse parameter options.
"""
logger.debug("Pulse programmer controller on loading") logger.debug("Pulse programmer controller on loading")
self.module.model.pulse_parameter_options = pulse_parameter_options self.module.model.pulse_parameter_options = pulse_parameter_options
@pyqtSlot(str) @pyqtSlot(str)
def delete_event(self, event_name): def delete_event(self, event_name : str) -> None:
"""This method deletes an event from the pulse sequence.
Args:
event_name (str): The name of the event to be deleted.
"""
logger.debug("Deleting event %s", event_name) logger.debug("Deleting event %s", event_name)
for event in self.module.model.pulse_sequence.events: for event in self.module.model.pulse_sequence.events:
if event.name == event_name: if event.name == event_name:
@ -24,7 +32,12 @@ class PulseProgrammerController(ModuleController):
self.module.model.events_changed.emit() self.module.model.events_changed.emit()
@pyqtSlot(str, str) @pyqtSlot(str, str)
def change_event_name(self, old_name : str, new_name : str): def change_event_name(self, old_name : str, new_name : str) -> None:
"""This method changes the name of an event.
Args:
old_name (str): The old name of the event.
new_name (str): The new name of the event.
"""
logger.debug("Changing event name from %s to %s", old_name, new_name) logger.debug("Changing event name from %s to %s", old_name, new_name)
for event in self.module.model.pulse_sequence.events: for event in self.module.model.pulse_sequence.events:
if event.name == old_name: if event.name == old_name:
@ -33,7 +46,13 @@ class PulseProgrammerController(ModuleController):
self.module.model.events_changed.emit() self.module.model.events_changed.emit()
@pyqtSlot(str, str) @pyqtSlot(str, str)
def change_event_duration(self, event_name:str, duration): def change_event_duration(self, event_name:str, duration) -> None:
"""This method changes the duration of an event.
Args:
event_name (str): The name of the event.
duration (str): The new duration of the event.
"""
logger.debug("Changing duration of event %s to %s", event_name, duration) logger.debug("Changing duration of event %s to %s", event_name, duration)
for event in self.module.model.pulse_sequence.events: for event in self.module.model.pulse_sequence.events:
if event.name == event_name: if event.name == event_name:
@ -48,8 +67,12 @@ class PulseProgrammerController(ModuleController):
self.module.model.events_changed.emit() self.module.model.events_changed.emit()
@pyqtSlot(str) @pyqtSlot(str)
def on_move_event_left(self, event_name: str): def on_move_event_left(self, event_name: str) -> None:
"""This method moves the event one position to the left if possible.""" """This method moves the event one position to the left if possible.
Args:
event_name (str): The name of the event to be moved.
"""
logger.debug("Moving event %s to the left", event_name) logger.debug("Moving event %s to the left", event_name)
for i, event in enumerate(self.module.model.pulse_sequence.events): for i, event in enumerate(self.module.model.pulse_sequence.events):
if event.name == event_name: if event.name == event_name:
@ -60,8 +83,12 @@ class PulseProgrammerController(ModuleController):
@pyqtSlot(str) @pyqtSlot(str)
def on_move_event_right(self, event_name : str): def on_move_event_right(self, event_name : str) -> None:
""" This method moves the event one position to the right if possible. """ """ This method moves the event one position to the right if possible.
Args:
event_name (str): The name of the event to be moved.
"""
logger.debug("Moving event %s to the right", event_name) logger.debug("Moving event %s to the right", event_name)
for i, event in enumerate(self.module.model.pulse_sequence.events): for i, event in enumerate(self.module.model.pulse_sequence.events):
if event.name == event_name: if event.name == event_name:
@ -70,14 +97,24 @@ class PulseProgrammerController(ModuleController):
break break
self.module.model.events_changed.emit() self.module.model.events_changed.emit()
def save_pulse_sequence(self, path): def save_pulse_sequence(self, path :str) -> None:
"""This method saves the pulse sequence to a file.
Args:
path (str): The path to the file.
"""
logger.debug("Saving pulse sequence to %s", path) logger.debug("Saving pulse sequence to %s", path)
sequence = self.module.model.pulse_sequence.to_json() sequence = self.module.model.pulse_sequence.to_json()
with open(path, "w") as file: with open(path, "w") as file:
file.write(json.dumps(sequence, cls=DecimalEncoder)) file.write(json.dumps(sequence, cls=DecimalEncoder))
def load_pulse_sequence(self, path): def load_pulse_sequence(self, path : str) -> None:
"""This method loads a pulse sequence from a file.
Args:
path (str): The path to the file.
"""
logger.debug("Loading pulse sequence from %s", path) logger.debug("Loading pulse sequence from %s", path)
sequence = None sequence = None
with open(path, "r") as file: with open(path, "r") as file:

View file

@ -23,8 +23,9 @@ class PulseProgrammerView(ModuleView):
logger.debug("Connecting pulse parameter options changed signal to on_pulse_parameter_options_changed") logger.debug("Connecting pulse parameter options changed signal to on_pulse_parameter_options_changed")
self.module.model.pulse_parameter_options_changed.connect(self.on_pulse_parameter_options_changed) self.module.model.pulse_parameter_options_changed.connect(self.on_pulse_parameter_options_changed)
def setup_ui(self): def setup_ui(self) -> None:
"""Setup the table for the pulse sequence. Also add buttons for saving and loading pulse sequences and editing and creation of events
"""
# Create pulse table # Create pulse table
title = QLabel("Pulse Sequence: %s" % self.module.model.pulse_sequence.name) title = QLabel("Pulse Sequence: %s" % self.module.model.pulse_sequence.name)
# Make title bold # Make title bold
@ -89,7 +90,9 @@ class PulseProgrammerView(ModuleView):
@pyqtSlot() @pyqtSlot()
def on_pulse_parameter_options_changed(self): def on_pulse_parameter_options_changed(self) -> None:
"""This method is called whenever the pulse parameter options change. It updates the view to reflect the changes.
"""
logger.debug("Updating pulse parameter options to %s", self.module.model.pulse_parameter_options.keys()) logger.debug("Updating pulse parameter options to %s", self.module.model.pulse_parameter_options.keys())
# We set it to the length of the pulse parameter options + 1 because we want to add a row for the parameter option buttons # We set it to the length of the pulse parameter options + 1 because we want to add a row for the parameter option buttons
self.pulse_table.setRowCount(len(self.module.model.pulse_parameter_options) + 1) self.pulse_table.setRowCount(len(self.module.model.pulse_parameter_options) + 1)
@ -99,7 +102,9 @@ class PulseProgrammerView(ModuleView):
self.pulse_table.setVerticalHeaderLabels(pulse_options) self.pulse_table.setVerticalHeaderLabels(pulse_options)
@pyqtSlot() @pyqtSlot()
def on_new_event_button_clicked(self): def on_new_event_button_clicked(self) -> None:
"""This method is called whenever the new event button is clicked. It creates a new event and adds it to the pulse sequence.
"""
# Create a QDialog for the new event # Create a QDialog for the new event
logger.debug("New event button clicked") logger.debug("New event button clicked")
dialog = AddEventDialog(self) dialog = AddEventDialog(self)
@ -110,8 +115,9 @@ class PulseProgrammerView(ModuleView):
self.module.model.add_event(event_name) self.module.model.add_event(event_name)
@pyqtSlot() @pyqtSlot()
def on_events_changed(self): def on_events_changed(self) -> None:
"""This method is called whenever the events in the pulse sequence change. It updates the view to reflect the changes.""" """This method is called whenever the events in the pulse sequence change. It updates the view to reflect the changes.
"""
logger.debug("Updating events to %s", self.module.model.pulse_sequence.events) logger.debug("Updating events to %s", self.module.model.pulse_sequence.events)
# Add label for the event lengths # Add label for the event lengths
@ -136,7 +142,9 @@ class PulseProgrammerView(ModuleView):
self.set_parameter_icons() self.set_parameter_icons()
def set_parameter_icons(self): def set_parameter_icons(self) -> None:
"""This method sets the icons for the pulse parameter options.
"""
for column_idx, event in enumerate(self.module.model.pulse_sequence.events): for column_idx, event in enumerate(self.module.model.pulse_sequence.events):
for row_idx, parameter in enumerate(self.module.model.pulse_parameter_options.keys()): for row_idx, parameter in enumerate(self.module.model.pulse_parameter_options.keys()):
if row_idx == 0: if row_idx == 0:
@ -175,7 +183,8 @@ class PulseProgrammerView(ModuleView):
button.clicked.connect(func) button.clicked.connect(func)
@pyqtSlot() @pyqtSlot()
def on_table_button_clicked(self, event, parameter): def on_table_button_clicked(self, event , parameter) -> None:
"""This method is called whenever a button in the pulse table is clicked. It opens a dialog to set the options for the parameter."""
logger.debug("Button for event %s and parameter %s clicked", event, parameter) logger.debug("Button for event %s and parameter %s clicked", event, parameter)
# Create a QDialog to set the options for the parameter. # Create a QDialog to set the options for the parameter.
dialog = OptionsDialog(event, parameter, self) dialog = OptionsDialog(event, parameter, self)
@ -189,7 +198,8 @@ class PulseProgrammerView(ModuleView):
self.set_parameter_icons() self.set_parameter_icons()
@pyqtSlot() @pyqtSlot()
def on_save_button_clicked(self): def on_save_button_clicked(self) -> None:
"""This method is called whenever the save button is clicked. It opens a dialog to select a file to save the pulse sequence to."""
logger.debug("Save button clicked") logger.debug("Save button clicked")
file_manager = QFileManager(self) file_manager = QFileManager(self)
file_name = file_manager.saveFileDialog() file_name = file_manager.saveFileDialog()
@ -197,7 +207,8 @@ class PulseProgrammerView(ModuleView):
self.module.controller.save_pulse_sequence(file_name) self.module.controller.save_pulse_sequence(file_name)
@pyqtSlot() @pyqtSlot()
def on_load_button_clicked(self): def on_load_button_clicked(self) -> None:
"""This method is called whenever the load button is clicked. It opens a dialog to select a file to load the pulse sequence from."""
logger.debug("Load button clicked") logger.debug("Load button clicked")
file_manager = QFileManager(self) file_manager = QFileManager(self)
file_name = file_manager.loadFileDialog() file_name = file_manager.loadFileDialog()
@ -269,7 +280,7 @@ class EventOptionsWidget(QWidget):
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
@pyqtSlot() @pyqtSlot()
def edit_event(self): def edit_event(self) -> None:
"""This method is called when the edit button is clicked. It opens a dialog that allows the user to change the event name and duration. """This method is called when the edit button is clicked. It opens a dialog that allows the user to change the event name and duration.
If the user clicks ok, the change_event_name and change_event_duration signals are emitted.""" If the user clicks ok, the change_event_name and change_event_duration signals are emitted."""
logger.debug("Edit button clicked for event %s", self.event.name) logger.debug("Edit button clicked for event %s", self.event.name)
@ -312,7 +323,7 @@ class EventOptionsWidget(QWidget):
@pyqtSlot() @pyqtSlot()
def create_delete_event_dialog(self): def create_delete_event_dialog(self) -> None:
"""This method is called when the delete button is clicked. It creates a dialog that asks the user if he is sure he wants to delete the event. """This method is called when the delete button is clicked. It creates a dialog that asks the user if he is sure he wants to delete the event.
If the user clicks yes, the delete_event signal is emitted. If the user clicks yes, the delete_event signal is emitted.
""" """
@ -333,13 +344,13 @@ class EventOptionsWidget(QWidget):
self.delete_event.emit(self.event.name) self.delete_event.emit(self.event.name)
@pyqtSlot() @pyqtSlot()
def move_event_left_button_clicked(self): def move_event_left_button_clicked(self) -> None:
"""This method is called when the move left button is clicked.""" """This method is called when the move left button is clicked."""
logger.debug("Move event left: %s", self.event.name) logger.debug("Move event left: %s", self.event.name)
self.move_event_left.emit(self.event.name) self.move_event_left.emit(self.event.name)
def move_event_right_button_clicked(self): def move_event_right_button_clicked(self) -> None:
"""This """ """This method is called when the move right button is clicked."""
logger.debug("Move event right: %s", self.event.name) logger.debug("Move event right: %s", self.event.name)
self.move_event_right.emit(self.event.name) self.move_event_right.emit(self.event.name)
@ -486,7 +497,10 @@ class FunctionOptionWidget(QWidget):
layout.addWidget(self.replot_button) layout.addWidget(self.replot_button)
@pyqtSlot() @pyqtSlot()
def on_replot_button_clicked(self): def on_replot_button_clicked(self) -> None:
"""This function is called when the replot button is clicked.
It will update the parameters of the function and replots the function.
"""
logger.debug("Replot button clicked") logger.debug("Replot button clicked")
# Update the resolution, start_x, end_x and expr lineedits # Update the resolution, start_x, end_x and expr lineedits
self.function_option.value.resolution = self.resolution_lineedit.text() self.function_option.value.resolution = self.resolution_lineedit.text()
@ -506,7 +520,10 @@ class FunctionOptionWidget(QWidget):
@pyqtSlot() @pyqtSlot()
def on_advanced_settings_button_clicked(self): def on_advanced_settings_button_clicked(self) -> None:
"""This function is called when the advanced settings button is clicked.
It will show or hide the advanced settings.
"""
if self.advanced_settings.isHidden(): if self.advanced_settings.isHidden():
self.advanced_settings.setHidden(False) self.advanced_settings.setHidden(False)
self.advanced_settings_button.setText('Hide Advanced Settings') self.advanced_settings_button.setText('Hide Advanced Settings')
@ -516,13 +533,19 @@ class FunctionOptionWidget(QWidget):
@pyqtSlot() @pyqtSlot()
def on_functionbutton_clicked(self, function): def on_functionbutton_clicked(self, function) -> None:
"""This function is called when a function button is clicked.
It will update the function_option.value to the function that was clicked.
"""
logger.debug("Button for function %s clicked", function.name) logger.debug("Button for function %s clicked", function.name)
self.function_option.set_value(function) self.function_option.set_value(function)
self.delete_active_function() self.delete_active_function()
self.load_active_function() self.load_active_function()
def delete_active_function(self): def delete_active_function(self) -> None:
"""This function is called when the active function is deleted.
It will remove the active function from the layout.
"""
# Remove the plotter with object name "plotter" from the layout # Remove the plotter with object name "plotter" from the layout
for i in reversed(range(self.layout().count())): for i in reversed(range(self.layout().count())):
item = self.layout().itemAt(i) item = self.layout().itemAt(i)
@ -530,7 +553,10 @@ class FunctionOptionWidget(QWidget):
item.widget().deleteLater() item.widget().deleteLater()
break break
def load_active_function(self): def load_active_function(self) -> None:
"""This function is called when the active function is loaded.
It will add the active function to the layout.
"""
# New QWidget for the active function # New QWidget for the active function
active_function_Widget = QWidget() active_function_Widget = QWidget()
active_function_Widget.setObjectName("active_function") active_function_Widget.setObjectName("active_function")
@ -624,10 +650,16 @@ class AddEventDialog(QDialog):
self.layout.addWidget(self.name_input) self.layout.addWidget(self.name_input)
self.layout.addWidget(self.buttons) self.layout.addWidget(self.buttons)
def get_name(self): def get_name(self) -> str:
"""Returns the name entered by the user.
Returns:
str: The name entered by the user"""
return self.name_input.text() return self.name_input.text()
def check_input(self): def check_input(self) -> None:
"""Checks if the name entered by the user is valid. If it is, the dialog is accepted. If not, the user is informed of the error.
"""
# Make sure that name is not empty and that event name doesn't already exist. # Make sure that name is not empty and that event name doesn't already exist.
if self.name_input.text() == "": if self.name_input.text() == "":
self.label.setText("Please enter a name for the event.") self.label.setText("Please enter a name for the event.")
@ -638,10 +670,16 @@ class AddEventDialog(QDialog):
class QFileManager: class QFileManager:
"""This class provides methods for opening and saving files."""
def __init__(self, parent=None): def __init__(self, parent=None):
self.parent = parent self.parent = parent
def loadFileDialog(self): def loadFileDialog(self) -> str:
"""Opens a file dialog for the user to select a file to open.
Returns:
str: The path of the file selected by the user.
"""
fileName, _ = QFileDialog.getOpenFileName(self.parent, fileName, _ = QFileDialog.getOpenFileName(self.parent,
"QFileManager - Open File", "QFileManager - Open File",
"", "",
@ -652,7 +690,12 @@ class QFileManager:
else: else:
return None return None
def saveFileDialog(self): def saveFileDialog(self) -> str:
"""Opens a file dialog for the user to select a file to save.
Returns:
str: The path of the file selected by the user.
"""
fileName, _ = QFileDialog.getSaveFileName(self.parent, fileName, _ = QFileDialog.getSaveFileName(self.parent,
"QFileManager - Save File", "QFileManager - Save File",
"", "",