#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：PositionController.py
@Description：
@Author：XSH
@Time：2025/11/17 17:57
@Department：研发部
@Copyright：©2025 乐辰生物
"""
import sys
import os
from PyQt5                      import Qt
from AppSettings.Settings       import *
from AppInterfaces.utils        import *
from AppCore.Levels             import *
from AppUtils.UiUtils           import UiUtils
from AppUtils.utils                     import *
from AppInterfaces.UiInterface          import UiInterface

class  PositionController(Qt.QWidget, UiInterface):
    InfoChamberNumber   : Qt.QPushButton                #
    EditChamberNumber   : Qt.QSpinBox                   #
    InfoTrayNumber      : Qt.QPushButton                #
    EditTrayNumber      : Qt.QSpinBox                   #
    InfoXPosition       : Qt.QPushButton                #
    EditXPosition       : Qt.QDoubleSpinBox             #
    InfoYPosition       : Qt.QPushButton                #
    EditYPosition       : Qt.QDoubleSpinBox             #
    InfoZPosition       : Qt.QPushButton                #
    EditZPosition       : Qt.QDoubleSpinBox             #
    InfoHPosition       : Qt.QPushButton                #
    InfoMPosition       : Qt.QPushButton                #
    EditHPosition       : Qt.QDoubleSpinBox             #
    EditMPosition       : Qt.QDoubleSpinBox             #
    InfoObjective       : Qt.QPushButton                #
    EditObjective       : Qt.QSpinBox                   #
    PositionButton      : Qt.QPushButton                #
    def __init__(self, parent: Qt.QWidget = None):
        super(PositionController, self).__init__(parent=parent)
        self.Initialization('PositionController', self, parent)
        self.initWidgetStyle()

    def initWidgetStyle(self):
        from AppUtils.UiUtils import UiUtils
        self.setStyleSheet(UiUtils.getTransparentStyle())


    def initValue(self):
        super(PositionController, self).initValue()
        self.initInfoChamberNumber   ()
        self.initEditChamberNumber   ()
        self.initInfoXPosition       ()
        self.initEditXPosition       ()
        self.initInfoYPosition       ()
        self.initEditYPosition       ()
        self.initInfoZPosition       ()
        self.initEditZPosition       ()
        self.initInfoHPosition       ()
        self.initInfoMPosition       ()
        self.initEditHPosition       ()
        self.initEditMPosition       ()
        self.initInfoObjective       ()
        self.initEditObjective       ()
        self.initInfoTrayNumber      ()
        self.initEditTrayNumber      ()

    def initProperty(self):
        super(PositionController, self).initProperty()
        self.InfoChamberNumber   .setText("片仓序号　")
        self.InfoXPosition       .setText('Ｘ轴位置　')
        self.InfoYPosition       .setText('Ｙ轴位置　')
        self.InfoZPosition       .setText('Ｚ轴位置　')
        self.InfoHPosition       .setText('挂钩位置　')
        self.InfoMPosition       .setText('显微镜位置')
        self.InfoObjective       .setText('物镜类型　')
        self.InfoTrayNumber      .setText('玻片序号　')
        self.EditChamberNumber   .setRange(1, 24)
        self.EditXPosition       .setRange(0.0, 155.0)
        self.EditYPosition       .setRange(0.0, 155.0)
        self.EditZPosition       .setRange(0.0, 155.0)
        self.EditHPosition       .setRange(0.0, 2000000.0)
        self.EditMPosition       .setRange(0.0, 25000000.0)
        self.EditObjective       .setRange(1, 7 )
        self.EditTrayNumber      .setRange(1, 5 )
        self.EditChamberNumber   .setSingleStep(1           )
        self.EditXPosition       .setSingleStep(0.01        )
        self.EditYPosition       .setSingleStep(0.01        )
        self.EditZPosition       .setSingleStep(0.01        )
        self.EditHPosition       .setSingleStep(0.01        )
        self.EditMPosition       .setSingleStep(500000.0   )
        self.EditObjective       .setSingleStep(1           )
        self.EditTrayNumber      .setSingleStep(1           )


        self.InfoTrayNumber      .setFixedHeight(40)
        self.EditTrayNumber      .setFixedHeight(40)
        self.InfoChamberNumber   .setFixedHeight(40)
        self.EditChamberNumber   .setFixedHeight(40)
        self.InfoXPosition       .setFixedHeight(40)
        self.EditXPosition       .setFixedHeight(40)
        self.InfoYPosition       .setFixedHeight(40)
        self.EditYPosition       .setFixedHeight(40)
        self.InfoZPosition       .setFixedHeight(40)
        self.EditZPosition       .setFixedHeight(40)
        self.InfoHPosition       .setFixedHeight(40)
        self.InfoMPosition       .setFixedHeight(40)
        self.EditHPosition       .setFixedHeight(40)
        self.EditMPosition       .setFixedHeight(40)
        self.InfoObjective       .setFixedHeight(40)
        self.EditObjective       .setFixedHeight(40)

    def getChamberNumber   (self) -> int    :
        if self.hasEditChamberNumber   :
            return int    (self.EditChamberNumber   .value())
        return int    (1)

    def getTrayNumber(self) -> int:
        if self.hasEditTrayNumber:
            return int(self.EditTrayNumber.value())
        return int (1)

    def getXPosition       (self) -> float  :
        if self.hasEditXPosition       :
            return float  (self.EditXPosition       .value())
        return float  (0)

    def getYPosition       (self) -> float  :
        if self.hasEditYPosition       :
            return float  (self.EditYPosition       .value())
        return float  (0)

    def getZPosition       (self) -> float  :
        if self.hasEditZPosition       :
            return float  (self.EditZPosition       .value())
        return float  (0)

    def getHPosition       (self) -> float  :
        if self.hasEditHPosition       :
            return float  (self.EditHPosition       .value())
        return float  (0)

    def getMPosition       (self) -> float  :
        if self.hasEditMPosition       :
            return float  (self.EditMPosition       .value())
        return float  (0)

    def getObjective       (self) -> int    :
        if self.hasEditObjective       :
            return int    (self.EditObjective       .value())
        return int    (0)

    def setChamberNumber   (self, value: (int, float)):
        if isinstance(value, (int, float)) and self.hasEditChamberNumber   :
            self.EditChamberNumber   .setValue(int    (value))

    def setXPosition       (self, value: (int, float)):
        if isinstance(value, (int, float)) and self.hasEditXPosition       :
            self.EditXPosition       .setValue(float  (value))

    def setYPosition       (self, value: (int, float)):
        if isinstance(value, (int, float)) and self.hasEditYPosition       :
            self.EditYPosition       .setValue(float  (value))

    def setZPosition       (self, value: (int, float)):
        if isinstance(value, (int, float)) and self.hasEditZPosition       :
            self.EditZPosition       .setValue(float  (value))

    def setHPosition       (self, value: (int, float)):
        if isinstance(value, (int, float)) and self.hasEditHPosition       :
            self.EditHPosition       .setValue(float  (value))

    def setMPosition       (self, value: (int, float)):
        if isinstance(value, (int, float)) and self.hasEditMPosition       :
            self.EditMPosition       .setValue(float  (value))

    def setObjective       (self, value: (int, float)):
        if isinstance(value, (int, float)) and self.hasEditObjective       :
            self.EditObjective       .setValue(int    (value))


    def initLayouts(self):
        super(PositionController, self).initLayouts()
        if self.hasWindowLayouts:
            return
        main_layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        main_layout.setSpacing(2)
        main_layout.setContentsMargins(2, 2, 2, 2)
        row1: Qt.QHBoxLayout = Qt.QHBoxLayout()
        row1.setSpacing(2)
        row1.setContentsMargins(2, 2, 2, 2)

        row2: Qt.QHBoxLayout = Qt.QHBoxLayout()
        row2.setSpacing(2)
        row2.setContentsMargins(2, 2, 2, 2)
        row1.addWidget(self.InfoChamberNumber   , 1)
        row1.addWidget(self.EditChamberNumber   , 1)
        row1.addWidget(self.InfoTrayNumber      , 1)
        row1.addWidget(self.EditTrayNumber      , 1)
        row1.addWidget(self.InfoXPosition       , 1)
        row1.addWidget(self.EditXPosition       , 1)
        row1.addWidget(self.InfoYPosition       , 1)
        row1.addWidget(self.EditYPosition       , 1)


        row2.addWidget(self.InfoZPosition       , 1)
        row2.addWidget(self.EditZPosition       , 1)
        row2.addWidget(self.InfoMPosition       , 1)
        row2.addWidget(self.EditMPosition       , 1)
        row2.addWidget(self.InfoObjective       , 1)
        row2.addWidget(self.EditObjective       , 1)
        row2.addWidget(self.InfoHPosition       , 1)
        row2.addWidget(self.EditHPosition       , 1)



        main_layout.addLayout(row1)
        main_layout.addLayout(row2)
        self.setLayout(main_layout)




    def initInfoChamberNumber   (self):
        if not self.hasInfoChamberNumber   :
            self.InfoChamberNumber    = Qt.QPushButton     (self)

    def initEditChamberNumber   (self):
        if not self.hasEditChamberNumber   :
            self.EditChamberNumber    = Qt.QSpinBox        (self)

    def initInfoXPosition       (self):
        if not self.hasInfoXPosition       :
            self.InfoXPosition        = Qt.QPushButton     (self)

    def initEditXPosition       (self):
        if not self.hasEditXPosition       :
            self.EditXPosition        = Qt.QDoubleSpinBox  (self)

    def initInfoYPosition       (self):
        if not self.hasInfoYPosition       :
            self.InfoYPosition        = Qt.QPushButton     (self)

    def initEditYPosition       (self):
        if not self.hasEditYPosition       :
            self.EditYPosition        = Qt.QDoubleSpinBox  (self)

    def initInfoZPosition       (self):
        if not self.hasInfoZPosition       :
            self.InfoZPosition        = Qt.QPushButton     (self)

    def initEditZPosition       (self):
        if not self.hasEditZPosition       :
            self.EditZPosition        = Qt.QDoubleSpinBox  (self)

    def initInfoHPosition       (self):
        if not self.hasInfoHPosition       :
            self.InfoHPosition        = Qt.QPushButton     (self)

    def initInfoMPosition       (self):
        if not self.hasInfoMPosition       :
            self.InfoMPosition        = Qt.QPushButton     (self)

    def initEditHPosition       (self):
        if not self.hasEditHPosition       :
            self.EditHPosition        = Qt.QDoubleSpinBox  (self)

    def initEditMPosition       (self):
        if not self.hasEditMPosition       :
            self.EditMPosition        = Qt.QDoubleSpinBox  (self)

    def initInfoObjective       (self):
        if not self.hasInfoObjective       :
            self.InfoObjective        = Qt.QPushButton     (self)

    def initEditObjective       (self):
        if not self.hasEditObjective       :
            self.EditObjective        = Qt.QSpinBox        (self)

    def initPositionButton      (self):
        if not self.hasPositionButton      :
            self.PositionButton       = Qt.QPushButton     (self)

    def initInfoTrayNumber(self):
        if not self.hasInfoTrayNumber:
            self.InfoTrayNumber = Qt.QPushButton(self)
    def initEditTrayNumber(self):
        if not self.hasEditTrayNumber:
            self.EditTrayNumber = Qt.QSpinBox(self)

    @property
    def hasInfoChamberNumber   (self) -> bool:
        return hasattr(self, 'InfoChamberNumber'  ) and isinstance(self.InfoChamberNumber   , Qt.QPushButton    )

    @property
    def hasEditChamberNumber   (self) -> bool:
        return hasattr(self, 'EditChamberNumber'  ) and isinstance(self.EditChamberNumber   , Qt.QSpinBox       )

    @property
    def hasInfoXPosition       (self) -> bool:
        return hasattr(self, 'InfoXPosition'      ) and isinstance(self.InfoXPosition       , Qt.QPushButton    )

    @property
    def hasEditXPosition       (self) -> bool:
        return hasattr(self, 'EditXPosition'      ) and isinstance(self.EditXPosition       , Qt.QDoubleSpinBox )

    @property
    def hasInfoYPosition       (self) -> bool:
        return hasattr(self, 'InfoYPosition'      ) and isinstance(self.InfoYPosition       , Qt.QPushButton    )

    @property
    def hasEditYPosition       (self) -> bool:
        return hasattr(self, 'EditYPosition'      ) and isinstance(self.EditYPosition       , Qt.QDoubleSpinBox )

    @property
    def hasInfoZPosition       (self) -> bool:
        return hasattr(self, 'InfoZPosition'      ) and isinstance(self.InfoZPosition       , Qt.QPushButton    )

    @property
    def hasEditZPosition       (self) -> bool:
        return hasattr(self, 'EditZPosition'      ) and isinstance(self.EditZPosition       , Qt.QDoubleSpinBox )

    @property
    def hasInfoHPosition       (self) -> bool:
        return hasattr(self, 'InfoHPosition'      ) and isinstance(self.InfoHPosition       , Qt.QPushButton    )

    @property
    def hasInfoMPosition       (self) -> bool:
        return hasattr(self, 'InfoMPosition'      ) and isinstance(self.InfoMPosition       , Qt.QPushButton    )

    @property
    def hasEditHPosition       (self) -> bool:
        return hasattr(self, 'EditHPosition'      ) and isinstance(self.EditHPosition       , Qt.QDoubleSpinBox )

    @property
    def hasEditMPosition       (self) -> bool:
        return hasattr(self, 'EditMPosition'      ) and isinstance(self.EditMPosition       , Qt.QDoubleSpinBox )

    @property
    def hasInfoObjective       (self) -> bool:
        return hasattr(self, 'InfoObjective'      ) and isinstance(self.InfoObjective       , Qt.QPushButton    )

    @property
    def hasEditObjective       (self) -> bool:
        return hasattr(self, 'EditObjective'      ) and isinstance(self.EditObjective       , Qt.QSpinBox       )

    @property
    def hasPositionButton      (self) -> bool:
        return hasattr(self, 'PositionButton'     ) and isinstance(self.PositionButton      , Qt.QPushButton    )

    @property
    def hasInfoTrayNumber(self) -> bool:
        return hasattr(self, 'InfoTrayNumber') and isinstance(self.InfoTrayNumber, Qt.QPushButton   )

    @property
    def hasEditTrayNumber(self) -> bool:
        return hasattr(self, 'EditTrayNumber') and isinstance(self.EditTrayNumber, Qt.QSpinBox      )



