Source code for gui.widget_polarizer

"""
This widget contains the functionality of the polarizer hardware class
provided in polarizer.py. The widget can be executed as a standalone
version if the correct National Instruments adc is connected and the
correct port/settings are chosen.

########################################
Functionalities provided in this widget:
########################################

1.  Choose the polarizer which you want to control via a comboBox
2.  Set the polarization either to parallel or perpendicular
"""
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
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_polarizer import Ui_GroupBox_polarizer as Ui_polarizer

# Import pi_control
from polarizer import Polarizer

# Logger Settings
import logging

# logging.basicConfig(level=logging.INFO)


[docs]class WidgetPolarizer(QtWidgets.QGroupBox, Ui_polarizer): def __init__(self, *args, obj=None, polarizer: dict = {}, **kwargs): super(QtWidgets.QGroupBox, self).__init__(*args, **kwargs) self.setupUi(self) self.polarizer = polarizer self.threadpool = QThreadPool() # In case polarizer is handed over with init we need to update # combobox This only relevant when running the widget directly if self.polarizer: self.update_combobox() self.update_values() # Setup signals --------------------------------------- self.pushButton_polarizer.clicked.connect(self.update_polarizer) # Changing polarizer in ComboBox self.comboBox_polarizer.currentIndexChanged.connect(self.update_values) # ------------------------------------------------------
[docs] def current_polarizer(self): """ Create method which holds information about which combobox item is currently selected. Returns: str: Returns key to currently active polarizer. """ # Create an attribute which holds information about which # combobox item is currently selected return self.comboBox_polarizer.currentText()
[docs] def update_combobox(self): # Add different polarizers to combo box drop down menu self.comboBox_polarizer.addItems(self.polarizer.keys())
[docs] def update_values(self): # Check if dictionary is empty if self.polarizer: key = self.current_polarizer() # If polarizer is open set text of button to close if self.polarizer[key].current_position == True: self.pushButton_polarizer.setText("Set Perpendicular\nPolarisation") # If polarizer is closed set text of button to open elif self.polarizer[key].current_position == False: self.pushButton_polarizer.setText("Set Parallel\nPolarisation") else: self.setDisabled(True)
[docs] def update_polarizer(self): key = self.current_polarizer() # If polarizer is parallel, set it to perpendicular. if self.polarizer[key].current_position == True: worker = Worker(self.polarizer[key].set_perpendicular) # If polarizer is perpendicular, set it parallel. elif self.polarizer[key].current_position == False: worker = Worker(self.polarizer[key].set_parallel) #! This does not work properly if there is more than one polarizer in the #! setup and the waiting time is too long. #! You can only move one polarizer at a time. (Too bad!) worker.signals.started.connect(lambda: self.setDisabled(True)) worker.signals.finished.connect(lambda: self.setEnabled(True)) worker.signals.finished.connect(self.update_values) self.threadpool.start(worker)
if __name__ == "__main__": # TESTING ----------------------------------------------------------- polarizer = {} class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, obj=None, polarizer=polarizer, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("Please god let this work") widget = WidgetPolarizer(polarizer=polarizer) self.setCentralWidget(widget) ir_dev_name = "Dev1" ir_channel = "PFI12" ir_waiting_time = 200 ir_default_position = False ir_parallel_electrical_state = True ir_name = "IR polarizer" uv_dev_name = "Dev1" uv_channel = "PFI13" uv_waiting_time = 200 uv_default_position = False uv_parallel_electrical_state = True uv_name = "UV polarizer" app = QtWidgets.QApplication(sys.argv) with Polarizer( ir_dev_name, ir_channel, waiting_time=ir_waiting_time, default_position=ir_default_position, parallel_electrical_state=ir_parallel_electrical_state, name=ir_name, ) as ir_polarizer, Polarizer( uv_dev_name, uv_channel, waiting_time=uv_waiting_time, default_position=uv_default_position, parallel_electrical_state=uv_parallel_electrical_state, name=uv_name, ) as uv_polarizer: polarizer = {} polarizer["IR polarizer"] = ir_polarizer polarizer["UV polarizer"] = uv_polarizer window = MainWindow(polarizer=polarizer) window.show() app.exec()