#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：DBOperation.py
@Description：
@Author：XSH
@Time：2025/11/26 13:50
@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
from AppHeadEnd.DBModelViews.DBModelView import DBModelView
from AppHeadEnd.ControllerViews.CameraController import CameraController
from AppCore.DBModels import *
class DBOperation(Qt.QWidget, UiInterface):
    ActionMovesSignal               : Qt.pyqtSignal = Qt.pyqtSignal(object)
    SystemConfigureButton           : Qt.QPushButton    #
    TrayConfigureButton             : Qt.QPushButton    #
    AxisConfigureButton             : Qt.QPushButton    #
    CameraConfigureButton           : Qt.QPushButton    #
    ObjectiveConfigureButton        : Qt.QPushButton    #
    ImageConfigureButton            : Qt.QPushButton    #
    FocusingConfigureButton         : Qt.QPushButton    #
    AnointingConfigureButton        : Qt.QPushButton    #
    CellModelButton                 : Qt.QPushButton    #
    ConvertButton                   : Qt.QPushButton    #
    SystemModelView                 : DBModelView       #
    TrayModelView                   : DBModelView       #
    AxisModelView                   : DBModelView       #
    CameraModelView                 : DBModelView       #
    ObjectiveModelView              : DBModelView       #
    ImageModelView                  : DBModelView       #
    FocusingModelView               : DBModelView       #
    AnointingModelView              : DBModelView       #
    CellModelView                   : DBModelView       #
    CameraView                      : CameraController  #
    CurrentModel                    : Qt.QWidget        #
    StackedLayout                   : Qt.QStackedLayout #
    IsConvert                       : bool
    def __init__(self, parent: Qt.QWidget = None):
        super(DBOperation, self).__init__(parent=parent)
        self.Initialization('CameraController', self, parent)
        self.initWidgetStyle()

    def initWidgetStyle(self):
        self.setStyleSheet(UiUtils.getTransparentStyle())


    def initConnect(self):
        super(DBOperation, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.ConvertButton                   .clicked.connect(slot=self.SlotConvertButton                   , type=TYPE)
        self.SystemConfigureButton           .clicked.connect(slot=self.SlotSystemConfigureButton           , type=TYPE)
        self.TrayConfigureButton             .clicked.connect(slot=self.SlotTrayConfigureButton             , type=TYPE)
        self.AxisConfigureButton             .clicked.connect(slot=self.SlotAxisConfigureButton             , type=TYPE)
        self.CameraConfigureButton           .clicked.connect(slot=self.SlotCameraConfigureButton           , type=TYPE)
        self.ObjectiveConfigureButton        .clicked.connect(slot=self.SlotObjectiveConfigureButton        , type=TYPE)
        self.ImageConfigureButton            .clicked.connect(slot=self.SlotImageConfigureButton            , type=TYPE)
        self.FocusingConfigureButton         .clicked.connect(slot=self.SlotFocusingConfigureButton         , type=TYPE)
        self.AnointingConfigureButton        .clicked.connect(slot=self.SlotAnointingConfigureButton        , type=TYPE)
        self.CellModelButton                 .clicked.connect(slot=self.SlotCellModelButton                 , type=TYPE)
        self.CellModelView.ActionMovesSignal.connect(slot=self.SlotActionMovesSignal, type=TYPE)

    def SlotActionMovesSignal(self, item):
        self.ActionMovesSignal.emit(item)
        AppTimeLoop.processGlobalEvents(100)


    def SlotConvertButton(self):
        if not self.hasCurrentModel:
            self.StackedLayout.setCurrentWidget(self.CameraView)

        else:
            if not self.IsConvert:
                self.StackedLayout.setCurrentWidget(self.CurrentModel)
            else:
                self.StackedLayout.setCurrentWidget(self.CameraView)
            self.IsConvert                   = not self.IsConvert

    def SlotSystemConfigureButton           (self):
        self.StackedLayout.setCurrentWidget(self.SystemModelView         )
        self.CurrentModel = self.SystemModelView       #
        self.IsConvert = True

    def SlotTrayConfigureButton             (self):

        self.StackedLayout.setCurrentWidget(self.TrayModelView           )
        self.CurrentModel = self.TrayModelView         #
        self.IsConvert = True

    def SlotAxisConfigureButton             (self):

        self.StackedLayout.setCurrentWidget(self.AxisModelView           )
        self.CurrentModel = self.AxisModelView         #
        self.IsConvert = True

    def SlotCameraConfigureButton           (self):

        self.StackedLayout.setCurrentWidget(self.CameraModelView         )
        self.CurrentModel = self.CameraModelView       #
        self.IsConvert = True

    def SlotObjectiveConfigureButton        (self):

        self.StackedLayout.setCurrentWidget(self.ObjectiveModelView      )
        self.CurrentModel = self.ObjectiveModelView    #
        self.IsConvert = True

    def SlotImageConfigureButton            (self):

        self.StackedLayout.setCurrentWidget(self.ImageModelView          )
        self.CurrentModel = self.ImageModelView        #
        self.IsConvert = True

    def SlotFocusingConfigureButton         (self):

        self.StackedLayout.setCurrentWidget(self.FocusingModelView       )
        self.CurrentModel = self.FocusingModelView     #
        self.IsConvert = True

    def SlotAnointingConfigureButton        (self):

        self.StackedLayout.setCurrentWidget(self.AnointingModelView      )
        self.CurrentModel = self.AnointingModelView    #
        self.IsConvert = True

    def SlotCellModelButton                 (self):

        self.StackedLayout.setCurrentWidget(self.CellModelView           )
        self.CurrentModel = self.CellModelView         #
        self.IsConvert = True

    def initValue(self):
        super(DBOperation, self).initValue()
        self.IsConvert                   = False
        self.initSystemConfigureButton           ()
        self.initTrayConfigureButton             ()
        self.initAxisConfigureButton             ()
        self.initCameraConfigureButton           ()
        self.initObjectiveConfigureButton        ()
        self.initImageConfigureButton            ()
        self.initFocusingConfigureButton         ()
        self.initAnointingConfigureButton        ()
        self.initCellModelButton                 ()
        self.initConvertButton                   ()
        self.initSystemModelView                 ()
        self.initTrayModelView                   ()
        self.initAxisModelView                   ()
        self.initCameraModelView                 ()
        self.initObjectiveModelView              ()
        self.initImageModelView                  ()
        self.initFocusingModelView               ()
        self.initAnointingModelView              ()
        self.initCellModelView                   ()
        self.initCameraView                      ()
        self.initStackedLayout                   ()


    def initProperty(self):
        super(DBOperation, self).initProperty()
        self.SystemConfigureButton           .setFixedHeight(40)
        self.TrayConfigureButton             .setFixedHeight(40)
        self.AxisConfigureButton             .setFixedHeight(40)
        self.CameraConfigureButton           .setFixedHeight(40)
        self.ObjectiveConfigureButton        .setFixedHeight(40)
        self.ImageConfigureButton            .setFixedHeight(40)
        self.FocusingConfigureButton         .setFixedHeight(40)
        self.AnointingConfigureButton        .setFixedHeight(40)
        self.CellModelButton                 .setFixedHeight(40)
        self.ConvertButton                   .setFixedHeight(40)

        self.SystemConfigureButton           .setFixedWidth(200)
        self.TrayConfigureButton             .setFixedWidth(200)
        self.AxisConfigureButton             .setFixedWidth(200)
        self.CameraConfigureButton           .setFixedWidth(200)
        self.ObjectiveConfigureButton        .setFixedWidth(200)
        self.ImageConfigureButton            .setFixedWidth(200)
        self.FocusingConfigureButton         .setFixedWidth(200)
        self.AnointingConfigureButton        .setFixedWidth(200)
        self.CellModelButton                 .setFixedWidth(200)
        self.ConvertButton                   .setFixedWidth(200)

        self.SystemConfigureButton           .setText('系统配置')
        self.TrayConfigureButton             .setText('托盘参数')
        self.AxisConfigureButton             .setText('电机参数')
        self.CameraConfigureButton           .setText('相机参数')
        self.ObjectiveConfigureButton        .setText('物镜参数')
        self.ImageConfigureButton            .setText('图像参数')
        self.FocusingConfigureButton         .setText('定焦参数')
        self.AnointingConfigureButton        .setText('滴油参数')
        self.CellModelButton                 .setText('核型小图')
        self.ConvertButton                   .setText('画面切换')

        self.SystemConfigureButton           .setStyleSheet(UiUtils.getButtonStyle())
        self.TrayConfigureButton             .setStyleSheet(UiUtils.getButtonStyle())
        self.AxisConfigureButton             .setStyleSheet(UiUtils.getButtonStyle())
        self.CameraConfigureButton           .setStyleSheet(UiUtils.getButtonStyle())
        self.ObjectiveConfigureButton        .setStyleSheet(UiUtils.getButtonStyle())
        self.ImageConfigureButton            .setStyleSheet(UiUtils.getButtonStyle())
        self.FocusingConfigureButton         .setStyleSheet(UiUtils.getButtonStyle())
        self.AnointingConfigureButton        .setStyleSheet(UiUtils.getButtonStyle())
        self.CellModelButton                 .setStyleSheet(UiUtils.getButtonStyle())
        self.ConvertButton                   .setStyleSheet(UiUtils.getButtonStyle())

        self.StackedLayout.addWidget(self.SystemModelView                 )
        self.StackedLayout.addWidget(self.TrayModelView                   )
        self.StackedLayout.addWidget(self.AxisModelView                   )
        self.StackedLayout.addWidget(self.CameraModelView                 )
        self.StackedLayout.addWidget(self.ObjectiveModelView              )
        self.StackedLayout.addWidget(self.ImageModelView                  )
        self.StackedLayout.addWidget(self.FocusingModelView               )
        self.StackedLayout.addWidget(self.AnointingModelView              )
        self.StackedLayout.addWidget(self.CellModelView                   )
        self.StackedLayout.addWidget(self.CameraView                      )
        self.StackedLayout.setCurrentWidget(self.CameraView)
        self.CurrentModel = self.CameraView

        self.SystemModelView     .setFixedSize(1920 // 2 , 1000)
        self.TrayModelView       .setFixedSize(1920 // 2 , 1000)
        self.AxisModelView       .setFixedSize(1920 // 2 , 1000)
        self.CameraModelView     .setFixedSize(1920 // 2 , 1000)
        self.ObjectiveModelView  .setFixedSize(1920 // 2 , 1000)
        self.ImageModelView      .setFixedSize(1920 // 2 , 1000)
        self.FocusingModelView   .setFixedSize(1920 // 2 , 1000)
        self.AnointingModelView  .setFixedSize(1920 // 2 , 1000)
        self.CellModelView       .setFixedSize(1920 // 2 , 1000)

    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        main_layout: Qt.QGridLayout = Qt.QGridLayout()
        main_layout.setSpacing(2)
        main_layout.setContentsMargins(10, 2, 10, 2)

        l_layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        l_layout.setSpacing(2)
        l_layout.setContentsMargins(10, 2, 10, 2)
        l_layout.addWidget(self.ConvertButton                   )
        l_layout.addWidget(self.SystemConfigureButton           )
        l_layout.addWidget(self.TrayConfigureButton             )
        l_layout.addWidget(self.AxisConfigureButton             )
        l_layout.addWidget(self.CameraConfigureButton           )
        l_layout.addWidget(self.ObjectiveConfigureButton        )
        l_layout.addWidget(self.ImageConfigureButton            )
        l_layout.addWidget(self.FocusingConfigureButton         )
        l_layout.addWidget(self.AnointingConfigureButton        )
        l_layout.addWidget(self.CellModelButton                 )










        main_layout.addLayout(l_layout          , 0, 0, -1, 2 , Qt.Qt.AlignTop)
        main_layout.addLayout(self.StackedLayout, 0, 2, -1, -1, Qt.Qt.AlignTop)
        self.setLayout(main_layout)


    @property
    def IsCamera(self) -> bool:
        return self.hasCurrentModel and isinstance(self.CurrentModel, CameraController)

    @property
    def SystemModel                 (self) -> DBModelView:
        return None if not self.hasSystemModelView     else self.SystemModelView           #

    @property
    def TrayModel                   (self) -> DBModelView:
        return None if not self.hasTrayModelView       else self.TrayModelView             #

    @property
    def AxisModel                   (self) -> DBModelView:
        return None if not self.hasAxisModelView       else self.AxisModelView             #

    @property
    def CameraModel                 (self) -> DBModelView:
        return None if not self.hasCameraModelView     else self.CameraModelView           #

    @property
    def ObjectiveModel              (self) -> DBModelView:
        return None if not self.hasObjectiveModelView  else self.ObjectiveModelView        #

    @property
    def ImageModel                  (self) -> DBModelView:
        return None if not self.hasImageModelView      else self.ImageModelView            #

    @property
    def FocusingModel               (self) -> DBModelView:
        return None if not self.hasFocusingModelView   else self.FocusingModelView         #

    @property
    def AnointingModel              (self) -> DBModelView:
        return None if not self.hasAnointingModelView  else self.AnointingModelView        #

    @property
    def CellModel                   (self) -> DBModelView:
        return None if not self.hasCellModelView       else self.CellModelView             #


    def initSystemConfigureButton           (self):
        if not self.hasSystemConfigureButton           :
            self.SystemConfigureButton            = Qt.QPushButton  ()

    def initTrayConfigureButton             (self):
        if not self.hasTrayConfigureButton             :
            self.TrayConfigureButton              = Qt.QPushButton  ()

    def initAxisConfigureButton             (self):
        if not self.hasAxisConfigureButton             :
            self.AxisConfigureButton              = Qt.QPushButton  ()

    def initCameraConfigureButton           (self):
        if not self.hasCameraConfigureButton           :
            self.CameraConfigureButton            = Qt.QPushButton  ()

    def initObjectiveConfigureButton        (self):
        if not self.hasObjectiveConfigureButton        :
            self.ObjectiveConfigureButton         = Qt.QPushButton  ()

    def initImageConfigureButton            (self):
        if not self.hasImageConfigureButton            :
            self.ImageConfigureButton             = Qt.QPushButton  ()

    def initFocusingConfigureButton         (self):
        if not self.hasFocusingConfigureButton         :
            self.FocusingConfigureButton          = Qt.QPushButton  ()

    def initAnointingConfigureButton        (self):
        if not self.hasAnointingConfigureButton        :
            self.AnointingConfigureButton         = Qt.QPushButton  ()

    def initCellModelButton                 (self):
        if not self.hasCellModelButton                 :
            self.CellModelButton                  = Qt.QPushButton  ()
    def initConvertButton(self):
        if not self.hasConvertButton:
            self.ConvertButton = Qt.QPushButton()

    def initSystemModelView                 (self):
        if not self.hasSystemModelView                 :
            self.SystemModelView                  = DBModelView     (False,  SystemConfigModel      (), self)

    def initTrayModelView                   (self):
        if not self.hasTrayModelView                   :
            self.TrayModelView                    = DBModelView     (False,  TrayConfigModel        (), self)

    def initAxisModelView                   (self):
        if not self.hasAxisModelView                   :
            self.AxisModelView                    = DBModelView     (False,  AxisVelocityModel      (), self)

    def initCameraModelView                 (self):
        if not self.hasCameraModelView                 :
            self.CameraModelView                  = DBModelView     (False,  CameraModel            (), self)

    def initObjectiveModelView              (self):
        if not self.hasObjectiveModelView              :
            self.ObjectiveModelView               = DBModelView     (False,  ObjectiveModel         (), self)

    def initImageModelView                  (self):
        if not self.hasImageModelView                  :
            self.ImageModelView                   = DBModelView     (False,  ImageConfigModel       (), self)

    def initFocusingModelView               (self):
        if not self.hasFocusingModelView               :
            self.FocusingModelView                = DBModelView     (False,  FocusingConfigModel    (), self)

    def initAnointingModelView              (self):
        if not self.hasAnointingModelView              :
            self.AnointingModelView               = DBModelView     (False,  AnointingModel         (), self)

    def initCellModelView                   (self):
        if not self.hasCellModelView                   :
            self.CellModelView                    = DBModelView     (True ,  CellModel              (), self)

    def initStackedLayout(self):
        if not self.hasStackedLayout:
            self.StackedLayout = Qt.QStackedLayout()

    def setCurrentView(self, widget):
        self.CurrentModel = widget

    def initCameraView(self):
        if not self.hasCameraView:
            self.CameraView = CameraController()


    @property
    def current_view(self) -> Qt.QWidget:
        return None if not self.hasCurrentModel else self.CurrentModel


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

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

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

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

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

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

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

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

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

    @property
    def hasSystemModelView                 (self) -> bool:
        return hasattr(self, 'SystemModelView'           ) and isinstance(self.SystemModelView                 , DBModelView   )

    @property
    def hasTrayModelView                   (self) -> bool:
        return hasattr(self, 'TrayModelView'             ) and isinstance(self.TrayModelView                   , DBModelView   )

    @property
    def hasAxisModelView                   (self) -> bool:
        return hasattr(self, 'AxisModelView'             ) and isinstance(self.AxisModelView                   , DBModelView   )

    @property
    def hasCameraModelView                 (self) -> bool:
        return hasattr(self, 'CameraModelView'           ) and isinstance(self.CameraModelView                 , DBModelView   )

    @property
    def hasObjectiveModelView              (self) -> bool:
        return hasattr(self, 'ObjectiveModelView'        ) and isinstance(self.ObjectiveModelView              , DBModelView   )

    @property
    def hasImageModelView                  (self) -> bool:
        return hasattr(self, 'ImageModelView'            ) and isinstance(self.ImageModelView                  , DBModelView   )

    @property
    def hasFocusingModelView               (self) -> bool:
        return hasattr(self, 'FocusingModelView'         ) and isinstance(self.FocusingModelView               , DBModelView   )

    @property
    def hasAnointingModelView              (self) -> bool:
        return hasattr(self, 'AnointingModelView'        ) and isinstance(self.AnointingModelView              , DBModelView   )

    @property
    def hasCellModelView                   (self) -> bool:
        return hasattr(self, 'CellModelView'             ) and isinstance(self.CellModelView                   , DBModelView   )

    @property
    def hasCurrentModel  (self) -> bool:
        return hasattr(self, 'CurrentModel'  ) and isinstance(self.CurrentModel , Qt.QWidget)

    @property
    def hasStackedLayout (self) -> bool:
        return hasattr(self, 'StackedLayout' ) and isinstance(self.StackedLayout, Qt.QStackedLayout)

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

    @property
    def hasCameraView(self) -> bool:
        return hasattr(self, 'CameraView') and isinstance(self.CameraView, CameraController)




