#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName   : CameraConfigView.py
@Account    : Administrator
@Time       : 2025/9/13 8:57
@Author     : xsh
@Description: 
"""
import sys
from PyQt5 import Qt
from AppInterfaces.UiInterface import UiInterface, AppUiProperty
from AppCore.DBModels import *
from AppInterfaces.globalSlot import globalSlot, AppSlot
from AppUtils.AppUtils import AppUtils
from AppSettings.SystemSetting import SystemConfig, SystemSetting
class CameraConfigView(Qt.QWidget, UiInterface):
    IOLocker        : Qt.QReadWriteLock = Qt.QReadWriteLock()
    CameraUiSignal  : Qt.pyqtSignal = Qt.pyqtSignal(object, object)
    Image           : Qt.QImage
    _Icons          : Qt.QImage

    def __init__(self, parent: Qt.QWidget = None):
        super(CameraConfigView, self).__init__(parent=parent)
        self.Initialization("显示图片界面", self, parent)
        self.flushFrame()
        self.initStyleSheet()

    def initStyleSheet(self):
        from AppUtils.UiUtils import UiUtils
        self.setStyleSheet(UiUtils.getWidgetStyle())

    def initValue(self):
        super(CameraConfigView, self).initValue()
        self.initIcons()

    def flushFrame(self):
        super(CameraConfigView, self).flushFrame()
        self.Image = None
        self.initIcons()
        self.repaint()

    def initLayouts(self):
        super(CameraConfigView, self).initLayouts()

    def initConnect(self):
        super(CameraConfigView, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        try:
            self.CameraUiSignal.connect(slot=self.SlotCameraUiSignal, type=TYPE)
        except Exception as e:
            AppUtils.print_Exception(e)
        # self.initCameraUiSignal()

    def SlotClearImageListSignal(self, info, name):
        self.Image = None
        self.repaint()

    def deleConnect(self):
        try:
            if self.receivers(self.CameraUiSignal) > 0:
                self.CameraUiSignal.disconnect(self.SlotCameraUiSignal)
        except Exception as e:
            AppUtils.print_Exception(e)

    def slot_CameraUiSignal(self, info: object, image: Qt.QImage):
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        if isinstance(info, Qt.QImage):
            self.Image = Qt.QImage(info)
        elif isinstance(image, Qt.QImage):
            self.Image = Qt.QImage(image)
        else:
            self.Image = None
        self.repaint()
    def SlotCameraUiSignal(self, info: object, image: Qt.QImage):
        Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        if isinstance(info, Qt.QImage):
            self.Image = Qt.QImage(info)
        elif isinstance(image, Qt.QImage):
            self.Image = Qt.QImage(image)
        else:
            self.Image = None
        self.repaint()

    def initProperty   (self):
        super(CameraConfigView, self).initProperty()
        w: int
        h: int
        w, h = ImageConfigModel.DrawImageSize()
        self.setFixedSize(w, h)

    @property
    def hasImage(self) -> bool:
        return hasattr(self, 'Image') and isinstance(self.Image, Qt.QImage) and not self.Image.isNull()

    @property
    def hasIcons(self) -> bool:
        return hasattr(self, 'Icons') and isinstance(self.Icons, Qt.QImage) and not self.Icons.isNull()

    def initIcons(self):
        if not self.hasIcons:
            self._Icons = Qt.QImage(f"{Qt.QFileInfo(sys.argv[0]).absolutePath()}/Resource/Image/karyotype3.png")

    def initImage(self):
        if not self.hasIcons:
            self.Image = Qt.QImage(f"{Qt.QFileInfo(sys.argv[0]).absolutePath()}/Resource/Image/karyotype3.png")


    def drawImage(self) -> Qt.QPixmap:
        src_size: Qt.QSize = self.size()
        isDraw: bool = False
        if self.hasImage:
            # 将图片缩放到适合窗口大小
            scaled_image = self.Image.scaled(src_size, Qt.Qt.IgnoreAspectRatio, Qt.Qt.SmoothTransformation)
            isDraw = True
        elif self.hasIcons:
            scaled_image = self._Icons.scaled(src_size, Qt.Qt.IgnoreAspectRatio, Qt.Qt.SmoothTransformation)
        else:
            self.initIcons()
            scaled_image = self._Icons.scaled(src_size, Qt.Qt.IgnoreAspectRatio, Qt.Qt.SmoothTransformation)
        pixmap: Qt.QPixmap = Qt.QPixmap.fromImage(scaled_image)
        painter: Qt.QPainter = Qt.QPainter(pixmap)
        if SystemConfig.isDebugMode() and isDraw:
            painter.setPen(Qt.QPen(Qt.Qt.red, 2))              # 设置画笔颜色为红色，宽度为2
            painter.drawLine(0                  , 0                 , 0                 , src_size.height() )
            painter.drawLine(0                  , 0                 , src_size.width()  , 0                 )
            painter.drawLine(0                  , src_size.height() , src_size.width()  , src_size.height() )
            painter.drawLine(src_size.width()   , 0                 , src_size.width()  , src_size.height() )
            # 绘制垂直线
            painter.drawLine(src_size.width() / 2, 0, src_size.width() / 2, src_size.height())
            # 绘制水平线
            painter.drawLine(0, src_size.height() / 2, src_size.width(), src_size.height() / 2)

        return pixmap

    def paintEvent(self, ev: Qt.QPaintEvent) -> None:
        painter = Qt.QPainter(self)
        pixmap: Qt.QPixmap = self.drawImage()
        # 计算居中位置
        # 绘制图片
        painter.drawPixmap(0, 0, pixmap)
        return super(CameraConfigView, self).paintEvent(ev)