Refactored FileDialog. Error handling for incompatible pulse sequence.

This commit is contained in:
jupfi 2024-04-28 07:38:15 +02:00
parent 668681cdf3
commit 06a2edc640
4 changed files with 34 additions and 58 deletions

View file

@ -1,4 +1,5 @@
"""Controller of the pulse programmer module.""" """Controller of the pulse programmer module."""
import logging import logging
import json import json
import decimal import decimal
@ -15,6 +16,7 @@ class PulseProgrammerController(ModuleController):
This class is responsible for handling the logic of the pulse programmer module. This class is responsible for handling the logic of the pulse programmer module.
""" """
def on_loading(self, pulse_parameter_options: dict) -> None: 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. """This method is called when the module is loaded. It sets the pulse parameter options in the model.

View file

@ -1,4 +1,5 @@
"""Model for the pulse programmer module.""" """Model for the pulse programmer module."""
import logging import logging
from collections import OrderedDict from collections import OrderedDict
from PyQt6.QtCore import pyqtSignal from PyQt6.QtCore import pyqtSignal
@ -13,11 +14,17 @@ class PulseProgrammerModel(ModuleModel):
This class is responsible for storing the data of the pulse programmer module. This class is responsible for storing the data of the pulse programmer module.
Attributes:
FILE_EXTENSION (str): The file extension for pulse programmer files.
Signals: Signals:
pulse_parameter_options_changed: Emitted when the pulse parameter options change. pulse_parameter_options_changed: Emitted when the pulse parameter options change.
events_changed: Emitted when the events in the pulse sequence change. events_changed: Emitted when the events in the pulse sequence change.
pulse_sequence_changed: Emitted when the pulse sequence changes. pulse_sequence_changed: Emitted when the pulse sequence changes.
""" """
FILE_EXTENSION = "quack"
pulse_parameter_options_changed = pyqtSignal() pulse_parameter_options_changed = pyqtSignal()
events_changed = pyqtSignal() events_changed = pyqtSignal()
pulse_sequence_changed = pyqtSignal() pulse_sequence_changed = pyqtSignal()

View file

@ -1,11 +1,14 @@
"""Initialize the PulseProgrammer module.""" """Initialize the PulseProgrammer module."""
from nqrduck.module.module import Module from nqrduck.module.module import Module
from .model import PulseProgrammerModel from .model import PulseProgrammerModel
from .controller import PulseProgrammerController from .controller import PulseProgrammerController
from .view import PulseProgrammerView from .view import PulseProgrammerView
class PulseProgrammer(Module): class PulseProgrammer(Module):
"""The pulse programmer module.""" """The pulse programmer module."""
def __init__(self, model, view, controller): def __init__(self, model, view, controller):
"""Initializes the pulse programmer module. """Initializes the pulse programmer module.
@ -18,4 +21,7 @@ class PulseProgrammer(Module):
self.view = None self.view = None
self.pulse_programmer_view = view(self) self.pulse_programmer_view = view(self)
pulse_programmer = PulseProgrammer(PulseProgrammerModel, PulseProgrammerView, PulseProgrammerController)
pulse_programmer = PulseProgrammer(
PulseProgrammerModel, PulseProgrammerView, PulseProgrammerController
)

View file

@ -338,7 +338,7 @@ class PulseProgrammerView(ModuleView):
def on_save_button_clicked(self) -> None: 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.""" """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 = self.QFileManager(self.module.model.FILE_EXTENSION, parent=self)
file_name = file_manager.saveFileDialog() file_name = file_manager.saveFileDialog()
if file_name: if file_name:
self.module.controller.save_pulse_sequence(file_name) self.module.controller.save_pulse_sequence(file_name)
@ -347,10 +347,19 @@ class PulseProgrammerView(ModuleView):
def on_load_button_clicked(self) -> None: 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.""" """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 = self.QFileManager(self.module.model.FILE_EXTENSION, parent=self)
file_name = file_manager.loadFileDialog() file_name = file_manager.loadFileDialog()
if file_name: if file_name:
self.module.controller.load_pulse_sequence(file_name) try:
self.module.controller.load_pulse_sequence(file_name)
except KeyError:
self.module.nqrduck_signal.emit(
"notification",
[
"Error",
"Error loading pulse sequence - maybe the version of the pulse sequence is not compatible?",
],
)
class EventOptionsWidget(QWidget): class EventOptionsWidget(QWidget):
@ -615,51 +624,3 @@ class AddEventDialog(QDialog):
return (QValidator.State.Invalid, value, position) return (QValidator.State.Invalid, value, position)
return (QValidator.State.Acceptable, value, position) return (QValidator.State.Acceptable, value, position)
# This class should be refactored in the module view so it can be used by all modules
class QFileManager:
"""This class provides methods for opening and saving files."""
def __init__(self, parent=None):
"""Initializes the QFileManager."""
self.parent = parent
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,
"QFileManager - Open File",
"",
"Quack Files (*.quack);;All Files (*)",
options=QFileDialog.Option.ReadOnly,
)
if fileName:
return fileName
else:
return None
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,
"QFileManager - Save File",
"",
"Quack Files (*.quack);;All Files (*)",
options=QFileDialog.Option.DontUseNativeDialog,
)
if fileName:
# Append the .quack extension if not present
if not fileName.endswith(".quack"):
fileName += ".quack"
return fileName
else:
return None