Implemented automatic loading of settings.

This commit is contained in:
jupfi 2023-07-10 08:03:01 +02:00
parent ca9e73c497
commit 4d56beba1c
9 changed files with 187 additions and 53 deletions

View file

@ -9,9 +9,19 @@ class BaseSpectrometer(Module):
super().__init__(model, None, controller)
# This stops the view from being added to the main window.
self._view = None
self._inner_view = view(self)
self.settings_view = view(self)
def set_active(self):
"""Sets the spectrometer as the active spectrometer.
"""
self.change_spectrometer.emit(self._model.name)
@property
def settings_view(self):
"""The settings view of the spectrometer.
"""
return self._settings_view
@settings_view.setter
def settings_view(self, value):
self._settings_view = value

View file

@ -1,42 +1,38 @@
import logging
from collections import OrderedDict
from PyQt6.QtCore import pyqtSlot
from nqrduck.module.module_model import ModuleModel
class BaseSpectrometerModel(ModuleModel):
logger = logging.getLogger(__name__)
class BaseSpectrometerModel(ModuleModel):
def __init__(self, module):
super().__init__(module)
self.settings = OrderedDict()
self.pulse_parameter_options = OrderedDict()
def add_setting(self, name, value, description) -> None:
self.settings[name] = self.Setting(name, value, description)
def add_pulse_parameter_option(self, name, options) -> None:
self.pulse_parameter_options[name] = options
class Setting():
class Setting:
"""A setting for the spectrometer is a value that is the same for all events in a pulse sequence.
E.g. the number of averages or the number of points in a spectrum."""
def __init__(self, name, default, description) -> None:
self.name = name
self.value = default
self.description = description
class PulseParameter():
@pyqtSlot(str)
def on_value_changed(self, value):
logger.debug("Setting %s changed to %s", self.name, value)
self.value = value
class PulseParameter:
def __init__(self, name):
self.name = name
class PulseSequence():
def __init__(self) -> None:
self.events = list()
def get_event_names(self) -> list:
return [event.name for event in self.events]
class Event():
parameters = list()
def __init__(self, name : str, duration : float) -> None:
self.name = name
self.duration = duration
def add_parameter(self, parameter) -> None:
self.parameters.append(parameter)

View file

@ -1,9 +1,52 @@
import logging
from PyQt6.QtWidgets import QTableWidget, QTableWidgetItem
from PyQt6.QtWidgets import QWidget, QLabel, QLineEdit, QHBoxLayout, QSizePolicy
from nqrduck.module.module_view import ModuleView
logger = logging.getLogger(__name__)
class BaseSpectrometerView(ModuleView):
pass
def __init__(self, module):
super().__init__(module)
def load_settings_ui(self):
"""This method automaticall generates a view for the settings of the module.
If there is a widget file that has been generated by Qt Designer, it will be used. Otherwise, a default view will be generated."""
from .base_spectrometer_widget import Ui_Form
widget = QWidget()
widget.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
self._ui_form = Ui_Form()
self.widget = widget
self._ui_form.setupUi(self)
# Add name of the spectrometer to the view
label = QLabel("%s Settings:" % self.module.model.toolbar_name)
label.setStyleSheet("font-weight: bold;")
self._ui_form.verticalLayout.setSpacing(5)
self._ui_form.verticalLayout.addWidget(label)
for setting in self.module.model.settings.values():
logger.debug("Adding setting to settings view: %s", setting.name)
# Create a label for the setting
label = QLabel(setting.name)
label.setMinimumWidth(70)
# Add an QLineEdit for the setting
line_edit = QLineEdit(str(setting.value))
line_edit.setMinimumWidth(100)
# Add a horizontal layout for the setting
layout = QHBoxLayout()
# Connect the editingFinished signal to the on_value_changed slot of the setting
line_edit.editingFinished.connect(lambda: setting.on_value_changed(line_edit.text()))
# Add the label and the line edit to the layout
layout.addWidget(label)
layout.addWidget(line_edit)
layout.addStretch(1)
# Add the layout to the vertical layout of the widget
self._ui_form.verticalLayout.addLayout(layout)
# Push all the settings to the top of the widget
self._ui_form.verticalLayout.addStretch(1)

View file

@ -0,0 +1,28 @@
# Form implementation generated from reading ui file '../Modules/nqrduck-spectrometer/src/nqrduck_spectrometer/resources/base_spectrometer_widget.ui'
#
# Created by: PyQt6 UI code generator 6.5.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(800, 647)
self.verticalLayoutWidget = QtWidgets.QWidget(parent=Form)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 811, 651))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))

View file

@ -0,0 +1,18 @@
class PulseSequence:
def __init__(self) -> None:
self.events = list()
def get_event_names(self) -> list:
return [event.name for event in self.events]
class Event:
"""An event is a part of a pulse sequence. It has a name and a duration and different parameters that have to be set."""
parameters = list()
def __init__(self, name: str, duration: float) -> None:
self.name = name
self.duration = duration
def add_parameter(self, parameter) -> None:
self.parameters.append(parameter)

View file

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>647</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>-1</x>
<y>-1</y>
<width>811</width>
<height>651</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout"/>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -6,30 +6,36 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
<width>1920</width>
<height>1080</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="gridLayoutWidget">
<widget class="QStackedWidget" name="stackedWidgetPulseProgrammer">
<property name="geometry">
<rect>
<x>-1</x>
<y>-1</y>
<width>811</width>
<height>611</height>
<x>860</x>
<y>0</y>
<width>1061</width>
<height>1081</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QStackedWidget" name="stackedWidget">
<widget class="QWidget" name="page"/>
<widget class="QWidget" name="page_2"/>
</widget>
</item>
</layout>
<widget class="QWidget" name="page_3"/>
<widget class="QWidget" name="page_4"/>
</widget>
<widget class="QStackedWidget" name="stackedWidgetSettings">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>831</width>
<height>1081</height>
</rect>
</property>
<widget class="QWidget" name="page"/>
<widget class="QWidget" name="page_2"/>
</widget>
</widget>
<resources/>

View file

@ -27,7 +27,7 @@ class SpectrometerView(ModuleView):
It changes the active view in the stacked widget to the one that was just activated.
:param module: The BaseSpectrometer module that was just activated.
"""
self._ui_form.stackedWidget.setCurrentWidget(module._inner_view)
self._ui_form.stackedWidgetSettings.setCurrentWidget(module.settings_view)
def on_spectrometer_widget_changed(self, widget):
"""This method is called when a new spectrometer widget is added to the module.
@ -35,8 +35,8 @@ class SpectrometerView(ModuleView):
:param widget: The widget that was added to the module.
"""
logger.debug("Adding module widget to stacked widget: %s", widget)
self._ui_form.stackedWidget.addWidget(widget)
self._ui_form.stackedWidget.setCurrentWidget(widget)
self._ui_form.stackedWidgetSettings.addWidget(widget)
self._ui_form.stackedWidgetSettings.setCurrentWidget(widget)
def on_spectrometer_added(self, module):
@ -44,7 +44,7 @@ class SpectrometerView(ModuleView):
:param module: The BaseSpectrometer module that was just added.
"""
module.change_spectrometer.connect(self.on_menu_button_clicked)
self.on_spectrometer_widget_changed(module._inner_view)
self.on_spectrometer_widget_changed(module.settings_view)
def create_menu_entry(self):
"""This method creates the menu entry for the spectrometer module. It creates a menu item for each spectrometer that is available.

View file

@ -12,22 +12,25 @@ from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(800, 600)
self.gridLayoutWidget = QtWidgets.QWidget(parent=Form)
self.gridLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 811, 611))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.stackedWidget = QtWidgets.QStackedWidget(parent=self.gridLayoutWidget)
self.stackedWidget.setObjectName("stackedWidget")
Form.resize(1920, 1080)
self.stackedWidgetPulseProgrammer = QtWidgets.QStackedWidget(parent=Form)
self.stackedWidgetPulseProgrammer.setGeometry(QtCore.QRect(860, 0, 1061, 1081))
self.stackedWidgetPulseProgrammer.setObjectName("stackedWidgetPulseProgrammer")
self.page_3 = QtWidgets.QWidget()
self.page_3.setObjectName("page_3")
self.stackedWidgetPulseProgrammer.addWidget(self.page_3)
self.page_4 = QtWidgets.QWidget()
self.page_4.setObjectName("page_4")
self.stackedWidgetPulseProgrammer.addWidget(self.page_4)
self.stackedWidgetSettings = QtWidgets.QStackedWidget(parent=Form)
self.stackedWidgetSettings.setGeometry(QtCore.QRect(10, 0, 831, 1081))
self.stackedWidgetSettings.setObjectName("stackedWidgetSettings")
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.stackedWidget.addWidget(self.page)
self.stackedWidgetSettings.addWidget(self.page)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.stackedWidget.addWidget(self.page_2)
self.gridLayout.addWidget(self.stackedWidget, 0, 0, 1, 1)
self.stackedWidgetSettings.addWidget(self.page_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)