#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：CameraOperation.py
@Description：
@Author：XSH
@Time：2025/12/18 14:38
@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 AppHeadEnd.Commons.OperationViews.CameraAreaOperation  import CameraAreaOperation                  #



class CameraOperation(Qt.QScrollArea, UiInterface):
    AxisMoveSignal      : Qt.pyqtSignal = Qt.pyqtSignal(object, object)
    MicroscopeMoveSignal: Qt.pyqtSignal = Qt.pyqtSignal(object)
    AreaFrame       : CameraAreaOperation       #
    LastPoint       : Qt.QPoint                 #
    CameraUiSignal  : Qt.pyqtSignal             = Qt.pyqtSignal(object, object)  #
    Offset_Z        : int
    def __init__(self, parent: Qt.QWidget = None):
        super(CameraOperation, self).__init__(parent)
        self.Initialization('CameraOperation', self, parent)


    def initValue(self):
        super(CameraOperation, self).initValue()
        self.initAreaFrame()

    def initConnect(self):
        super(CameraOperation, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.CameraUiSignal.connect(slot=self.SlotCameraUiSignal, type=TYPE)
        self.area_frame.AxisMoveSignal.connect(self.AxisMoveSignal, TYPE)
        self.area_frame.MicroscopeMoveSignal.connect(self.MicroscopeMoveSignal, TYPE)

    def SlotCameraUiSignal(self, info, name):
        if self.hasAreaFrame:
            self.area_frame.CameraUiSignal.emit(info, name)


    def loadWindowIcon(self):
        if not self.has_WindowIcon:
            import sys
            info: Qt.QFileInfo = Qt.QFileInfo(AppUtils.toSourceFile("Icon/showUi.png"))
            self.WindowIcon = Qt.QIcon(Qt.QPixmap(info.absoluteFilePath()))

    def initProperty(self):
        super(CameraOperation, self).initProperty()
        self.setFixedSize(1000, 700)




    def mousePressEvent(self, ev: Qt.QMouseEvent) -> None:
        self.Offset_Z = None
        self.MicroscopeMoveSignal.emit(self.Offset_Z)
        return super(CameraOperation, self).mousePressEvent(ev)

    def mouseReleaseEvent(self, ev: Qt.QMouseEvent) -> None:
        self.Offset_Z = None
        self.MicroscopeMoveSignal.emit(self.Offset_Z)
        return super(CameraOperation, self).mouseReleaseEvent(ev)

    def wheelEvent(self, ev: Qt.QWheelEvent) -> None:
        if self.hasOffset_Z:
            self.Offset_Z += ev.angleDelta().y()
        else:
            self.Offset_Z = ev.angleDelta().y()
        self.MicroscopeMoveSignal.emit(self.Offset_Z)
        ev.accept()


    def initLayouts(self):
        if self.hasWindowLayouts:
            return

        main_layout: Qt.QGridLayout = Qt.QGridLayout()
        main_layout.setSpacing(1)
        main_layout.setContentsMargins(50, 50, 50, 50)
        # main_layout.setAlignment(Qt.Qt.AlignCenter)
        # main_layout.setAlignment(Qt.Qt.AlignTop | Qt.Qt.AlignLeft)
        main_layout.addWidget(self.AreaFrame, 0, 0, -1, -1)
        self.setLayout(main_layout)
        self.setWidget(self.AreaFrame)

    def initAreaFrame(self):
        if not self.hasAreaFrame:
            self.AreaFrame = CameraAreaOperation()


    @property
    def hasAreaFrame(self) -> bool:
        return hasattr(self, 'AreaFrame') and isinstance(self.AreaFrame, CameraAreaOperation)

    @property
    def area_frame(self) -> CameraAreaOperation:
        return None if not self.hasAreaFrame else self.AreaFrame

    @property
    def hasOffset_Z(self) -> bool:
        return self.has_field('Offset_Z', (int, float))

    @property
    def offset_z(self) -> int:
        return 0 if not self.hasOffset_Z else self.Offset_Z
