Source code for gui.widget_triax
"""
This widget contains the functionality of the triax spectrometer
hardware class provided in triax.py. The widget can be executed as a
standalone version if the correct Triax spectrometer is connected and
the correct port/settings are chosen.
########################################
Functionalities provided in this widget:
########################################
1. Display the currently set Turret
2. Set and read out the Grating
3. Display the grating line density of the currently active grating
4. Set and read out the central wavelength in nm
5. Set and read out the central wavenumber in 1/cm
6. Set and read out the slit size in mu m
7. Switch between to user defined slit sizes
"""
if __name__ == "__main__":
# Add directories to path for imports
import os, sys, inspect
currentdir = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe()))
)
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, os.path.join(currentdir, "ui_files"))
sys.path.insert(0, parentdir)
sys.path.insert(0, os.path.join(parentdir, "hardware_interfaces"))
# PyQt imports
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QRunnable, QThreadPool
# Import RegEx input validators
from regex_validators import *
# Import Qt Threading wrapper
from qt_multithreading_wrapper import Worker
# Import of file made from UI designer
from ui_triax import Ui_GroupBox_triax as Ui_triax
# Import triax
from triax import Triax as Spectrometer
# Logger Settings
# import logging
# logging.basicConfig(level=logging.INFO)
[docs]class WidgetTriax(QtWidgets.QGroupBox, Ui_triax):
def __init__(self, *args, obj=None, triax: Spectrometer = None, **kwargs):
super(QtWidgets.QGroupBox, self).__init__(*args, **kwargs)
self.setupUi(self)
self.triax = triax
# Default state for slit toggle fields
self.toggle_state = "1"
self.lineEdit_triax_slit_toggle_1.setText("200")
self.lineEdit_triax_slit_toggle_2.setText("2000")
self.standard_style = self.lineEdit_triax_slit_toggle_1.styleSheet()
self.highlighted_style = """
QLineEdit {
color: red;
}
"""
# Instantiate threadpool
self.threadpool = QThreadPool()
self.update_values()
# Setting input validators -----------------------------
# Line Edits of "Wavegenerator" GroupBox
self.lineEdit_triax_wavelength.setValidator(
positive_float_input_validator
) # Assign input validator to this lineEdit
self.lineEdit_triax_wavenumber.setValidator(positive_float_input_validator)
self.lineEdit_triax_slit_toggle_1.setValidator(positive_float_input_validator)
self.lineEdit_triax_slit_toggle_2.setValidator(positive_float_input_validator)
self.lineEdit_triax_slit.setValidator(positive_float_input_validator)
self.lineEdit_triax_grating.setValidator(int_012_input_validator)
# ------------------------------------------------------
# Setup signal connections ----------------------------
# Set Grating
# Return pressed is used because of trouble with greying out the
# GUI elements while the triax is moving. ReturnPressed is used
# instead This makes sense because an active feedback (such as
# return) is a safer way for sensible hardware such as the triax
# spectrometer
self.lineEdit_triax_grating.returnPressed.connect(self.update_grating)
# Set Wavelength & Wavenumber
self.lineEdit_triax_wavelength.returnPressed.connect(self.update_wavelength)
self.lineEdit_triax_wavenumber.returnPressed.connect(self.update_wavenumber)
# Set Slit
self.lineEdit_triax_slit.returnPressed.connect(self.update_slit)
# Slit Toggle
self.pushButton_triax_toggle_slit.clicked.connect(self.update_toggle_slit)
# ------------------------------------------------------
[docs] def update_values(self):
if self.triax:
self.lineEdit_triax_cur_turret.setText(self.triax.turret)
self.lineEdit_triax_grating.setText(str(self.triax.grating))
self.lineEdit_triax_line_density.setText(
str(round(self.triax.gr_line_density, 3))
)
self.lineEdit_triax_wavelength.setText(str(round(self.triax.wavelength, 3)))
self.lineEdit_triax_wavenumber.setText(str(round(self.triax.wavenumber, 3)))
self.lineEdit_triax_slit.setText(str(round(self.triax.slit, 3)))
else:
self.setDisabled(True)
[docs] def work(self, qt_obj: QtWidgets, triax_attr: str, triax_method: str):
def run(qt_obj: QtWidgets, triax_attr: str, triax_method: str):
current_value = getattr(self.triax, triax_attr)
# Get requested value
requested_value = getattr(qt_obj, "text")()
# Convert requested value to correct type
requested_value = type(current_value)(requested_value)
if round(current_value, 3) != requested_value:
# Call method / Execute
getattr(self.triax, triax_method)(requested_value)
worker = Worker(run, qt_obj, triax_attr, triax_method)
# Deactivate GUI
worker.signals.started.connect(
lambda: getattr(qt_obj, "blockSignals")(True)
) # This is only necessary because of editingFinsihed
worker.signals.started.connect(lambda: self.setDisabled(True))
# Reactivate GUI
worker.signals.finished.connect(lambda: self.setEnabled(True))
worker.signals.finished.connect(
lambda: getattr(qt_obj, "blockSignals")(False)
) # This is only necessary because of editingFinsihed
# Update values
worker.signals.finished.connect(self.update_values)
self.threadpool.start(worker)
[docs] def update_wavelength(self):
self.work(self.lineEdit_triax_wavelength, "wavelength", "set_wavelength")
[docs] def update_wavenumber(self):
self.work(self.lineEdit_triax_wavenumber, "wavenumber", "set_wavenumber")
[docs] def update_toggle_slit(self):
# Set up toggle state so that the correct lineEdit will be
# highlighted when it is active
if self.toggle_state == "2":
self.toggle_state = "1"
self.lineEdit_triax_slit_toggle_1.setStyleSheet(self.highlighted_style)
self.lineEdit_triax_slit_toggle_2.setStyleSheet(self.standard_style)
elif self.toggle_state == "1":
self.toggle_state = "2"
self.lineEdit_triax_slit_toggle_1.setStyleSheet(self.standard_style)
self.lineEdit_triax_slit_toggle_2.setStyleSheet(self.highlighted_style)
self.work(
getattr(self, "lineEdit_triax_slit_toggle_" + self.toggle_state),
"slit",
"set_slit",
)
if __name__ == "__main__":
# TESTING -----------------------------------------------------------
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, spectrometer, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Please god let this work too")
widget = WidgetTriax(triax=spectrometer)
self.setCentralWidget(widget)
app = QtWidgets.QApplication(sys.argv)
# Initial settings for Triax spectrometer
# Spectrometer
from hardware_properties import HardwareProperties
hw = HardwareProperties(
r"C:\Users\hlab\Documents\iris\akb_software\hardware_config_files\h-lab_hardware_properties.json"
)
with Spectrometer(
port=hw.spectrometer.port,
turret=hw.spectrometer.active_turret,
tgratings=getattr(hw.spectrometer, hw.spectrometer.active_turret),
step_slit=hw.spectrometer.step_slit,
gamma=hw.spectrometer.gamma,
d=hw.spectrometer.d,
f=hw.spectrometer.focal_length,
init=True,
) as spectrometer:
print(getattr(hw.spectrometer, hw.spectrometer.active_turret))
window = MainWindow(spectrometer)
window.show()
app.exec()