Skip to content

Commit

Permalink
Refactored FileDialog. Error handling for incompatible pulse sequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
jupfi committed Apr 28, 2024
1 parent 668681c commit 06a2edc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 58 deletions.
4 changes: 3 additions & 1 deletion src/nqrduck_pulseprogrammer/controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Controller of the pulse programmer module."""

import logging
import json
import decimal
Expand All @@ -12,9 +13,10 @@

class PulseProgrammerController(ModuleController):
"""Controller 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:
"""This method is called when the module is loaded. It sets the pulse parameter options in the model.
Expand Down
13 changes: 10 additions & 3 deletions src/nqrduck_pulseprogrammer/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Model for the pulse programmer module."""

import logging
from collections import OrderedDict
from PyQt6.QtCore import pyqtSignal
Expand All @@ -10,21 +11,27 @@

class PulseProgrammerModel(ModuleModel):
"""Model for 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:
pulse_parameter_options_changed: Emitted when the pulse parameter options change.
events_changed: Emitted when the events in the pulse sequence change.
pulse_sequence_changed: Emitted when the pulse sequence changes.
"""

FILE_EXTENSION = "quack"

pulse_parameter_options_changed = pyqtSignal()
events_changed = pyqtSignal()
pulse_sequence_changed = pyqtSignal()

def __init__(self, module):
"""Initializes the pulse programmer model.
Args:
module (Module): The module to which this model belongs.
"""
Expand Down
10 changes: 8 additions & 2 deletions src/nqrduck_pulseprogrammer/pulseprogrammer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""Initialize the PulseProgrammer module."""

from nqrduck.module.module import Module
from .model import PulseProgrammerModel
from .controller import PulseProgrammerController
from .view import PulseProgrammerView


class PulseProgrammer(Module):
"""The pulse programmer module."""

def __init__(self, model, view, controller):
"""Initializes the pulse programmer module.
Args:
model (PulseProgrammerModel): The model of the pulse programmer module.
view (PulseProgrammerView): The view of the pulse programmer module.
Expand All @@ -18,4 +21,7 @@ def __init__(self, model, view, controller):
self.view = None
self.pulse_programmer_view = view(self)

pulse_programmer = PulseProgrammer(PulseProgrammerModel, PulseProgrammerView, PulseProgrammerController)

pulse_programmer = PulseProgrammer(
PulseProgrammerModel, PulseProgrammerView, PulseProgrammerController
)
65 changes: 13 additions & 52 deletions src/nqrduck_pulseprogrammer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def on_table_button_clicked(self, event, parameter) -> 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."""
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()
if file_name:
self.module.controller.save_pulse_sequence(file_name)
Expand All @@ -347,10 +347,19 @@ def on_save_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."""
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()
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):
Expand Down Expand Up @@ -477,7 +486,7 @@ def edit_event(self) -> None:
@pyqtSlot()
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.
If the user clicks yes, the delete_event signal is emitted.
"""
Expand Down Expand Up @@ -615,51 +624,3 @@ def validate(self, value, position):
return (QValidator.State.Invalid, 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

0 comments on commit 06a2edc

Please sign in to comment.