#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：FocusButtonOperation.py
@Description：
@Author：XSH
@Time：2025/12/22 9:05
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from abc                                    import abstractmethod                       #
from PyQt5                                  import Qt                                   #
from AppInterfaces.UiInterface              import UiInterface                          #
from AppInterfaces.globalSlot               import globalSlot, AppSlot                  #
from AppCore.Contexts                       import *                                    #
from AppCore.DBModels                       import *                                    #
from AppCore.Levels                         import *                                    #
from AppUtils.utils                         import *                                    #
from AppSettings.Settings                   import *                                    #

class FocusButtonOperation(Qt.QWidget, UiInterface):
    NextButton      : Qt.QPushButton            #
    PositionButton  : Qt.QPushButton            #
    PrevButton      : Qt.QPushButton            #
    DoneButton      : Qt.QPushButton            #
    RangeButton     : Qt.QPushButton            #
    RangeEdit       : Qt.QDoubleSpinBox         #

    def __init__(self, parent: Qt.QWidget = None):
        super(FocusButtonOperation, self).__init__(parent)
        self.Initialization('FocusButtonOperation', self, parent)


    def initValue(self):
        super(FocusButtonOperation, self).initValue()
        self.initNextButton     ()
        self.initPrevButton     ()
        self.initDoneButton     ()
        self.initPositionButton ()
        self.initRangeButton    ()
        self.initRangeEdit      ()


    def initProperty(self):
        super(FocusButtonOperation, self).initProperty()
        self.NextButton         .setText('下一个　')
        self.PrevButton         .setText('上一个　')
        self.DoneButton         .setText('完成校准')
        self.PositionButton     .setText('当前位置')
        self.RangeButton        .setText('定焦范围')
        self.RangeEdit          .setDecimals(4)
        self.RangeEdit          .setSuffix('mm')
        self.RangeEdit          .setRange(0.2, 2.0)
        self.RangeEdit          .setValue(0.1)
        self.NextButton         .setFixedHeight(35)
        self.PrevButton         .setFixedHeight(35)
        self.DoneButton         .setFixedHeight(35)
        self.PositionButton     .setFixedHeight(35)
        self.RangeButton        .setFixedHeight(35)
        self.RangeEdit          .setFixedHeight(35)
        self.RangeEdit          .setValue(0.6)


    @property
    def focusing_range(self) -> int:
        # 263步是1um 263158 是1mm 131579 0.5mm 2630是10um
        range_limit: float = round(self.RangeEdit.value(), 2) if self.hasRangeEdit else 0.50
        return int(range_limit * 263158)

        pass
    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        main_layout: Qt.QGridLayout = Qt.QGridLayout()
        main_layout.setSpacing(0)
        main_layout.setContentsMargins(5, 5, 5, 5)

        main_layout.addWidget(self.NextButton       , 0, 0  , 1, 2)
        main_layout.addWidget(self.PositionButton   , 0, 2  , 1, 2)
        main_layout.addWidget(self.RangeButton      , 0, 4  , 1, 2)
        main_layout.addWidget(self.RangeEdit        , 0, 6  , 1, 4)
        main_layout.addWidget(self.PrevButton       , 0, 10 , 1, 2)
        main_layout.addWidget(self.DoneButton       , 0, 12 , 1, 2)

        self.setLayout(main_layout)


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

    def initNextButton(self):
        if not self.hasNextButton:
            self.NextButton = Qt.QPushButton()

    def initPrevButton(self):
        if not self.hasPrevButton:
            self.PrevButton = Qt.QPushButton()

    def initDoneButton(self):
        if not self.hasDoneButton:
            self.DoneButton = Qt.QPushButton()

    def initRangeButton    (self):
        if not self.hasRangeButton    :
            self.RangeButton     = Qt.QPushButton()

    def initRangeEdit      (self):
        if not self.hasRangeEdit      :
            self.RangeEdit       = Qt.QDoubleSpinBox()

    @property
    def hasNextButton(self) -> bool:
        return self.has_field('NextButton', Qt.QPushButton)

    @property
    def hasPrevButton(self) -> bool:
        return self.has_field('PrevButton', Qt.QPushButton)

    @property
    def hasDoneButton(self) -> bool:
        return self.has_field('DoneButton', Qt.QPushButton)

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



    @property
    def hasRangeButton     (self) -> bool:
        return self.has_field('RangeButton' , Qt.QPushButton)

    @property
    def hasRangeEdit       (self) -> bool:
        return self.has_field('RangeEdit'   , Qt.QDoubleSpinBox   )