Source code for gui.widget_shutter

"""
This widget contains the functionality of the shutter hardware class
provided in shutter.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 shutter which you want to control via a comboBox
2.  Set the state either to open or close
"""
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_shutter import Ui_GroupBox_shutter as Ui_shutter

from shutter import Shutter

# Logger Settings
import logging

# logging.basicConfig(level=logging.INFO)


[docs]class WidgetShutter(QtWidgets.QGroupBox, Ui_shutter): def __init__(self, *args, obj=None, shutter: dict = {}, **kwargs): super(QtWidgets.QGroupBox, self).__init__(*args, **kwargs) self.setupUi(self) self.shutter = shutter self.threadpool = QThreadPool() # In case shutter is handed over with init we need to update # combobox This only relevant when running the widget directly if self.shutter: self.update_combobox() self.update_values() # Setup signals --------------------------------------- self.pushButton_shutter.clicked.connect(self.update_shutter) # Changing shutter in ComboBox self.comboBox_shutter.currentIndexChanged.connect(self.update_values) # ------------------------------------------------------
[docs] def current_shutter(self): """ Create method which holds information about which combobox item is currently selected. Returns: str: Returns key to currently active shutter. """ # Create an attribute which holds information about which # combobox item is currently selected return self.comboBox_shutter.currentText()
[docs] def update_combobox(self): # Add different shutters to combo box drop down menu self.comboBox_shutter.addItems(self.shutter.keys())
[docs] def update_values(self): # Check if dictionary is empty if self.shutter: key = self.current_shutter() # If shutter is open set text of button to close if self.shutter[key].current_position == True: self.pushButton_shutter.setText("Close Shutter") # If shutter is closed set text of button to open elif self.shutter[key].current_position == False: self.pushButton_shutter.setText("Open Shutter") else: self.setDisabled(True)
[docs] def update_shutter(self): key = self.current_shutter() # If shutter is open, close it. if self.shutter[key].current_position == True: worker = Worker(self.shutter[key].close) # If shutter is closed, open it. elif self.shutter[key].current_position == False: worker = Worker(self.shutter[key].open) #! This does not work properly if there is more than one shutter in the #! setup and the waiting time is too long. #! You can only move one shutter 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 ----------------------------------------------------------- shutter = {} class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, obj=None, shutter=shutter, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("Please god let this work") widget = WidgetShutter(shutter=shutter) self.setCentralWidget(widget) ir_dev_name = "Dev1" ir_channel = "PFI12" ir_waiting_time = 200 ir_default_position = False ir_open_electrical_state = True ir_name = "IR Shutter" uv_dev_name = "Dev1" uv_channel = "PFI13" uv_waiting_time = 200 uv_default_position = False uv_open_electrical_state = True uv_name = "UV Shutter" app = QtWidgets.QApplication(sys.argv) with Shutter( ir_dev_name, ir_channel, waiting_time=ir_waiting_time, default_position=ir_default_position, open_electrical_state=ir_open_electrical_state, name=ir_name, ) as ir_shutter, Shutter( uv_dev_name, uv_channel, waiting_time=uv_waiting_time, default_position=uv_default_position, open_electrical_state=uv_open_electrical_state, name=uv_name, ) as uv_shutter: shutter = {} shutter["IR Shutter"] = ir_shutter shutter["UV Shutter"] = uv_shutter window = MainWindow(shutter=shutter) window.show() app.exec()