コード例 #1
0
class ObjMotorWidgetUI(QWidget):
    
#    waveforms_generated = pyqtSignal(object, object, list, int)
#    SignalForContourScanning = pyqtSignal(int, int, int, np.ndarray, np.ndarray)
#    MessageBack = pyqtSignal(str)
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
#        os.chdir('./')# Set directory to current folder.
        self.setFont(QFont("Arial"))
        
#        self.setMinimumSize(1350,900)
        self.setWindowTitle("ObjMotorWidget")
        self.layout = QGridLayout(self)
        self.connect_status = False
        #**************************************************************************************************************************************
        #--------------------------------------------------------------------------------------------------------------------------------------
        #-----------------------------------------------------------GUI for Objective Motor----------------------------------------------------
        #--------------------------------------------------------------------------------------------------------------------------------------          
        #**************************************************************************************************************************************
        
        # Movement based on relative positions.
        self.ObjMotorcontrolContainer = QGroupBox("Objective motor control")
        self.ObjMotorcontrolContainer.setStyleSheet("QGroupBox {\
                                font: bold;\
                                border: 1px solid silver;\
                                border-radius: 6px;\
                                margin-top: 12px;\
                                color:Navy; \
                                background-color: #FFFAFA}\
                                QGroupBox::title{subcontrol-origin: margin;\
                                                 left: 7px;\
                                                 padding: 5px 5px 5px 5px;}")
        self.ObjMotorcontrolLayout = QGridLayout()
        
        self.ObjMotor_connect = StylishQT.connectButton()
        self.ObjMotor_connect.setFixedWidth(70)
        self.ObjMotorcontrolLayout.addWidget(self.ObjMotor_connect, 0, 0)
        self.ObjMotor_connect.clicked.connect(lambda: self.ConnectMotor())     
        self.ObjMotor_connect.setGraphicsEffect(QGraphicsDropShadowEffect(blurRadius=3, xOffset=2, yOffset=2))
        
        self.ObjMotor_disconnect = StylishQT.disconnectButton()
        self.ObjMotor_disconnect.setFixedWidth(70)
        self.ObjMotor_disconnect.setGraphicsEffect(QGraphicsDropShadowEffect(blurRadius=3, xOffset=2, yOffset=2))
        self.ObjMotorcontrolLayout.addWidget(self.ObjMotor_disconnect, 0, 1)
        
        self.ObjMotor_disconnect.clicked.connect(lambda: self.DisconnectMotor()) 
        self.ObjMotor_disconnect.setEnabled(False)
        
        self.ObjMotor_upwards = QPushButton("↑")
        self.ObjMotor_upwards.setStyleSheet("QPushButton {color:white;background-color: teal; border-style: outset;border-radius: 10px;border-width: 2px;font: bold 14px;padding: 6px}"
                                            "QPushButton:pressed {color:red;background-color: white; border-style: outset;border-radius: 10px;border-width: 2px;font: bold 14px;padding: 6px}"
                                            "QPushButton:hover:!pressed {color:gray;background-color: teal; border-style: outset;border-radius: 10px;border-width: 2px;font: bold 14px;padding: 6px}")        
        self.ObjMotorcontrolLayout.addWidget(self.ObjMotor_upwards, 2, 2)
        self.ObjMotor_upwards.clicked.connect(lambda: self.MovingMotorThread("Motor_move_upwards"))
#        self.ObjMotor_upwards.setShortcut('w')
        
        self.ObjMotor_down = QPushButton("↓")
        self.ObjMotor_down.setStyleSheet("QPushButton {color:white;background-color: teal; border-style: outset;border-radius: 10px;border-width: 2px;font: bold 14px;padding: 6px}"
                                            "QPushButton:pressed {color:red;background-color: white; border-style: outset;border-radius: 10px;border-width: 2px;font: bold 14px;padding: 6px}"
                                            "QPushButton:hover:!pressed {color:gray;background-color: teal; border-style: outset;border-radius: 10px;border-width: 2px;font: bold 14px;padding: 6px}")        
        self.ObjMotorcontrolLayout.addWidget(self.ObjMotor_down, 3, 2)
        self.ObjMotor_down.clicked.connect(lambda: self.MovingMotorThread("Motor_move_downwards"))
#        self.stage_down.setShortcut('s')
        
        self.ObjMotor_target = QDoubleSpinBox(self)
        self.ObjMotor_target.setMinimum(-10000)
        self.ObjMotor_target.setMaximum(10000)
        self.ObjMotor_target.setDecimals(6)
#        self.ObjMotor_target.setValue(3.45)
        self.ObjMotor_target.setSingleStep(0.001)        
        self.ObjMotorcontrolLayout.addWidget(self.ObjMotor_target, 1, 1)
        self.ObjMotorcontrolLayout.addWidget(QLabel("Target:"), 1, 0)
        
        self.ObjMotor_current_pos_Label = QLabel("Current position: ")
        self.ObjMotorcontrolLayout.addWidget(self.ObjMotor_current_pos_Label, 2, 0, 1, 2)
        
        self.ObjMotor_goto = QPushButton()
        self.ObjMotor_goto.setIcon(QIcon('./Icons/move_coord.png')) 
        self.ObjMotor_goto.setToolTip("Move to absolute position")
        self.ObjMotor_goto.setStyleSheet("QPushButton {color:white;background-color: #CCFFFF;}"
                                      "QPushButton:hover:!pressed {color:white;background-color: #FFE5CC;}")
        self.ObjMotor_goto.setFixedWidth(35)
        self.ObjMotor_goto.setFixedHeight(35)
        self.ObjMotorcontrolLayout.addWidget(self.ObjMotor_goto, 1, 2)
        self.ObjMotor_goto.clicked.connect(lambda: self.MovingMotorThread("Motor_move_target"))
        
        self.ObjMotor_step = QDoubleSpinBox(self)
        self.ObjMotor_step.setMinimum(-10000)
        self.ObjMotor_step.setMaximum(10000)
        self.ObjMotor_step.setDecimals(6)
        self.ObjMotor_step.setValue(0.003)
        self.ObjMotor_step.setSingleStep(0.001)        
        self.ObjMotorcontrolLayout.addWidget(self.ObjMotor_step, 3, 1)
        self.ObjMotorcontrolLayout.addWidget(QLabel("Step: "), 3, 0)  
        
        self.FocusSlider = QScrollBar(Qt.Horizontal)
        self.FocusSlider.setMinimum(2500000)
        self.FocusSlider.setMaximum(4500000)
#        self.FocusSlider.setTickPosition(QSlider.TicksBothSides)
#        self.FocusSlider.setTickInterval(1000000)
        self.FocusSlider.setStyleSheet('color:white; background: lightblue')
        self.FocusSlider.setSingleStep(10000)
#        self.line640 = QLineEdit(self)
#        self.line640.setFixedWidth(60)
#        self.FocusSlider.sliderReleased.connect(lambda:self.updatelinevalue(640))
        self.FocusSlider.valueChanged.connect(lambda:self.MovingMotorThread("Motor_move_slider"))
        self.FocusSlider.setTracking(False)
#        self.line640.returnPressed.connect(lambda:self.updatesider(640))
        self.ObjMotorcontrolLayout.addWidget(self.FocusSlider, 4, 0, 1, 3)
        
        self.ObjMotorcontrolContainer.setLayout(self.ObjMotorcontrolLayout)
        self.ObjMotorcontrolContainer.setMaximumHeight(300)
        self.layout.addWidget(self.ObjMotorcontrolContainer, 4, 0)          
        
        #**************************************************************************************************************************************
        #--------------------------------------------------------------------------------------------------------------------------------------
        #-----------------------------------------------------------Fucs for Motor movement----------------------------------------------------
        #--------------------------------------------------------------------------------------------------------------------------------------          
        #**************************************************************************************************************************************         
    def ConnectMotor(self):
        self.ObjMotorcontrolContainer.setEnabled(False)
        self.ObjMotor_disconnect.setEnabled(True)
        self.ObjMotor_connect.setEnabled(False)
        
        self.device_instance = ConnectObj_Thread()
        self.device_instance.start()
        self.device_instance.finished.connect(self.getmotorhandle)

    def getmotorhandle(self):
        self.pi_device_instance = self.device_instance.getInstance()
        print('Objective motor connected.')
        self.connect_status = True
#        self.normalOutputWritten('Objective motor connected.'+'\n')
        
        self.ObjCurrentPos = self.pi_device_instance.pidevice.qPOS(self.pi_device_instance.pidevice.axes)
        self.ObjMotor_current_pos_Label.setText("Current position: {:.4f}".format(self.ObjCurrentPos['1'])) # Axis here is a string.
        self.ObjMotor_target.setValue(self.ObjCurrentPos['1'])
        
        decimal_places = len(str(self.ObjCurrentPos['1']).split('.')[1])
        print(int(self.ObjCurrentPos['1']*(10**decimal_places)))
        self.FocusSlider.setValue(int(self.ObjCurrentPos['1']*(10**6)))
        self.ObjMotorcontrolContainer.setEnabled(True)
        
    def MovingMotorThread(self, target):
        if target == "Motor_move_target":
            MoveMotorThread = threading.Thread(target = self.MoveMotor, args=('Target',))
            MoveMotorThread.start()
        elif target == "Motor_move_upwards":
            MoveMotorThread = threading.Thread(target = self.MoveMotor, args=('UP',))
            MoveMotorThread.start()            
        elif target == "Motor_move_downwards":
            MoveMotorThread = threading.Thread(target = self.MoveMotor, args=('DOWN',))
            MoveMotorThread.start()  
        elif target == "Motor_move_slider":
            MoveMotorThread = threading.Thread(target = self.MoveMotor, args=('Slider',))
            MoveMotorThread.start()          
        
    def MoveMotor(self, direction):
        if direction == "Target":
            pos = PIMotor.move(self.pi_device_instance.pidevice, self.ObjMotor_target.value())
        elif direction == "UP":
            self.MotorStep = self.ObjMotor_step.value()
            pos = PIMotor.move(self.pi_device_instance.pidevice, (self.ObjCurrentPos['1'] + self.MotorStep))
        elif direction == "DOWN":
            self.MotorStep = self.ObjMotor_step.value()
            pos = PIMotor.move(self.pi_device_instance.pidevice, (self.ObjCurrentPos['1'] - self.MotorStep))
        elif direction == "Slider":
            pos = PIMotor.move(self.pi_device_instance.pidevice, self.FocusSlider.value()/1000000)
            
        self.ObjCurrentPos = self.pi_device_instance.pidevice.qPOS(self.pi_device_instance.pidevice.axes)
        self.ObjMotor_current_pos_Label.setText("Current position: {:.4f}".format(self.ObjCurrentPos['1'])) # Axis here is a string.
        self.ObjMotor_target.setValue(self.ObjCurrentPos['1'])  
        
        # decimal_places = len(str(self.ObjCurrentPos['1']).split('.')[1])
        self.FocusSlider.setValue(int(self.ObjCurrentPos['1']*(10**6)))
      
    def DisconnectMotor(self):
        self.ObjMotor_connect.setEnabled(True)
        self.ObjMotor_disconnect.setEnabled(False)
        
        PIMotor.CloseMotorConnection(self.pi_device_instance.pidevice)
        print('Disconnected')
        self.connect_status = False
#        self.normalOutputWritten('Objective motor disconnected.'+'\n')
        
    def closeEvent(self, event):
        # ## Because the software combines both PyQt and PyQtGraph, using the
        # ## closeEvent() from PyQt will cause a segmentation fault. Calling 
        # ## also the exit() from PyQtGraph solves this problem. 
        # pg.exit()
        if self.connect_status == True:
            self.DisconnectMotor()
            
        QtWidgets.QApplication.quit()
        event.accept()
コード例 #2
0
ファイル: window.py プロジェクト: tomashruska1/musicplayer
class FoldersDialog(QDialog):
    """A dialog window responsible for providing access to adding/removing folders for the library class."""

    removeSignal = pyqtSignal(str)

    def __init__(self, window: QMainWindow, parent: QLabel, control) -> None:
        super().__init__(parent=parent, flags=Qt.FramelessWindowHint)
        self.setWindowModality(Qt.WindowModal)
        self.window = window
        self.parent = parent
        self.setFixedSize(500, 300)
        self.control = control
        self.activeDialog = None
        self.highlightedLabel = None
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        self.scrollArea = QScrollArea()
        self.scrollBar = QScrollBar(Qt.Vertical, self.scrollArea)
        self.scrollBar.setStyleSheet(scrollBarStyle)
        self.scrollArea.setVerticalScrollBar(self.scrollBar)
        self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setFrameShape(QFrame.NoFrame)
        self.layout.addWidget(self.scrollArea)
        self.folders = QWidget()
        self.scrollArea.setWidget(self.folders)
        self.foldersLayout = QVBoxLayout()
        self.foldersLayout.setAlignment(Qt.AlignTop)
        self.folders.setLayout(self.foldersLayout)

        self.buttons = QWidget()
        self.buttonsLayout = QHBoxLayout()
        self.buttons.setLayout(self.buttonsLayout)
        self.layout.addWidget(self.buttons)

        self.add = QPushButton()
        self.add.setText("Add")
        self.add.clicked.connect(self.addFolder)
        self.remove = QPushButton()
        self.remove.setText("Remove")
        self.remove.clicked.connect(self.removeFolder)
        self.cancel = QPushButton()
        self.cancel.setText("Done")
        self.cancel.clicked.connect(self.reject)
        for button in [self.add, self.remove, self.cancel]:
            self.buttonsLayout.addWidget(button)

        self.layout.setContentsMargins(10, 10, 10, 10)
        self.layout.setSpacing(5)
        self.layout.setAlignment(Qt.AlignTop)
        self.setStyleSheet("QDialog {"
                           "    background-color: #141414;"
                           "    border-style: solid;"
                           "    border-width: 8px;"
                           "    border-color: #202020;"
                           "}"
                           "QLabel {"
                           "    background-color: #141414;"
                           "    font-size: 12px"
                           "}")
        self.move(self.mapToGlobal(self.window.pos()) + QPoint(150, 150))

        self.fileDialog = QFileDialog()
        self.fileDialog.setAcceptMode(QFileDialog.AcceptOpen)
        self.fileDialog.setFileMode(QFileDialog.Directory)
        self.fileDialog.setWindowModality(Qt.WindowModal)
        self.fileDialog.Options(QFileDialog.ShowDirsOnly)
        self.fileDialog.fileSelected.connect(self.control.addWatchedFolder)
        self.fileDialog.fileSelected.connect(self.updateFolders)
        self.removeSignal.connect(self.control.removeWatchedFolder)
        for folder in self.control.library.folders:
            field = ClickLabel()
            field.setText(folder)
            field.setFixedHeight(15)
            field.click.connect(self.highlightLabel)
            self.foldersLayout.addWidget(field)

    def highlightLabel(self, label: QLabel) -> None:
        if self.highlightedLabel is not None:
            self.highlightedLabel.setStyleSheet(
                "QLabel {"
                "    background-color: #141414;"
                "}")
            if self.highlightedLabel == label:
                self.highlightedLabel = None
                return
        label.setStyleSheet("QLabel {" "    background-color: #353535;" "}")
        self.highlightedLabel = label

    def addFolder(self) -> None:
        self.fileDialog.show()

    def updateFolders(self, folder: str) -> None:
        field = ClickLabel()
        field.setText(folder.replace("/", "\\"))
        field.setFixedHeight(15)
        field.click.connect(self.highlightLabel)
        self.foldersLayout.addWidget(field)

    def removeFolder(self) -> None:
        if self.highlightedLabel is not None:
            self.foldersLayout.removeWidget(self.highlightedLabel)
            self.removeSignal.emit(self.highlightedLabel.text())
            self.highlightedLabel.deleteLater()
            self.highlightedLabel = None