#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :bms_home.py
@时间    :2021/12/09 13:33:56
@作者    :None
@版本    :1.0
@说明    :主页面逻辑
'''


from ui.home import UiPcsHomePage
from utils.qt import QMainWindow, Qt, QStyle, QMessageBox, QGuiApplication, Signal


class Win_Pcs_Home(UiPcsHomePage, QMainWindow):
    # 跳转连接页面
    interface_signal = Signal(int)
    connect_signal = Signal()
    disconnect_signal = Signal()

    def __init__(self, parent=None):
        super(Win_Pcs_Home, self).__init__(parent)
        self.setupUi(self)

        self.cb_interface.currentIndexChanged.connect(self._interface_chose)
        self.pcs_switch.checkedChanged.connect(self._get_state)

    # 居中显示

    def center(self):
        # 新方法
        self.setGeometry(
            QStyle.alignedRect(
                Qt.LeftToRight,
                Qt.AlignCenter,
                self.size(),
                QGuiApplication.primaryScreen().availableGeometry(),
            ),
        )

    def _interface_chose(self):
        self.interface_signal.emit(self.cb_interface.currentIndex())

    # BMS连接
    def _get_state(self, checked):
        if checked:
            self.connect_signal.emit()
        else:
            self.disconnect_signal.emit()

    def can_connect_error(self):
        QMessageBox.critical(self, "失败！", "CAN 连接失败！")
        self.pcs_switch.state = False
        return

    def no_can_device(self):
        QMessageBox.critical(self, "失败！", "无CAN 设备！")
        self.pcs_switch.state = False
        return

    def com_connect_error(self):
        QMessageBox.critical(self, "失败！", "串口连接失败！")
        self.pcs_switch.state = False
        return

    def no_com_device(self):
        QMessageBox.critical(self, "失败！", "无法连接此串口设备！")
        self.pcs_switch.state = False
        return
