nqrduck-pulseprogrammer/src/nqrduck_pulseprogrammer/model.py

70 lines
2.4 KiB
Python
Raw Normal View History

import logging
from decimal import Decimal
from collections import OrderedDict
2023-07-17 15:47:25 +00:00
from PyQt6.QtCore import pyqtSignal, pyqtSlot
2023-07-07 13:07:27 +00:00
from nqrduck.module.module_model import ModuleModel
2023-07-17 10:00:10 +00:00
from nqrduck_spectrometer.pulsesequence import PulseSequence
2023-07-07 13:07:27 +00:00
logger = logging.getLogger(__name__)
2023-07-07 13:07:27 +00:00
2023-07-07 13:07:27 +00:00
class PulseProgrammerModel(ModuleModel):
pulse_parameter_options_changed = pyqtSignal()
2023-07-10 14:29:42 +00:00
events_changed = pyqtSignal()
2023-12-16 12:29:17 +00:00
pulse_sequence_changed = pyqtSignal()
def __init__(self, module):
super().__init__(module)
self.pulse_parameter_options = OrderedDict()
2023-07-11 11:58:55 +00:00
self.pulse_sequence = PulseSequence("Untitled pulse sequence")
def add_event(self, event_name: str, duration: Decimal = 20):
"""Add a new event to the current pulse sequence.
Args:
event_name (str): A human-readable name for the event
duration (Decimal): The duration of the event in µs. Defaults to 20.
"""
self.pulse_sequence.events.append(
PulseSequence.Event(event_name, "%.16gu" % duration)
)
logger.debug(
"Creating event %s with object id %s",
event_name,
id(self.pulse_sequence.events[-1]),
)
2023-07-11 11:58:55 +00:00
# Create a default instance of the pulse parameter options and add it to the event
for name, pulse_parameter_class in self.pulse_parameter_options.items():
2023-07-11 11:58:55 +00:00
logger.debug("Adding pulse parameter %s to event %s", name, event_name)
self.pulse_sequence.events[-1].parameters[name] = pulse_parameter_class(
name
)
logger.debug(
"Created pulse parameter %s with object id %s",
name,
id(self.pulse_sequence.events[-1].parameters[name]),
)
2023-07-10 14:29:42 +00:00
2023-07-24 06:21:06 +00:00
logger.debug(self.pulse_sequence.to_json())
2023-07-10 14:29:42 +00:00
self.events_changed.emit()
@property
def pulse_parameter_options(self):
return self._pulse_parameter_options
@pulse_parameter_options.setter
def pulse_parameter_options(self, value):
self._pulse_parameter_options = value
logger.debug("Pulse parameter options changed - emitting signal")
self.pulse_parameter_options_changed.emit()
2023-12-16 12:29:17 +00:00
@property
def pulse_sequence(self):
return self._pulse_sequence
2023-12-16 12:29:17 +00:00
@pulse_sequence.setter
def pulse_sequence(self, value):
self._pulse_sequence = value
self.pulse_sequence_changed.emit()