#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：DrawCameraView.py\n
 @DateTime: 2025/7/17 17:28\n
 @Author  ：xsh
"""

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
class DrawCameraView(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(DrawCameraView, 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(DrawCameraView, self).initValue()
        self.initIcons()

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

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

    def initConnect(self):
        super(DrawCameraView, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        try:
            AppSlot.ClearImageListSignal.connect(slot=self.SlotClearImageListSignal, type=TYPE)
            if self.receivers(self.CameraUiSignal) <= 0:
                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 initCameraUiSignal(self):
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        try:
            AppSlot.CameraUiSignal.connect(slot=self.slot_CameraUiSignal, type=TYPE)

        except Exception as e:
            AppUtils.print_Exception(e)

    def deleCameraUiSignal(self):

        try:
            index: int = AppSlot.metaObject().indexOfMethod('CameraUiSignal')
            if index > -1:
                AppSlot.CameraUiSignal.disconnect(self.slot_CameraUiSignal)
        except Exception as e:
            AppUtils.print_Exception(e)


    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(DrawCameraView, 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 paintEvent(self, ev: Qt.QPaintEvent) -> None:
        painter = Qt.QPainter(self)
        src_size: Qt.QSize = self.size()
        if self.hasImage:
            # 将图片缩放到适合窗口大小
            scaled_image = self.Image.scaled(src_size, Qt.Qt.IgnoreAspectRatio, Qt.Qt.SmoothTransformation)
        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)
        # 计算居中位置
        # 绘制图片
        painter.drawImage(0, 0, scaled_image)
        return super(DrawCameraView, self).paintEvent(ev)
