#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :home.py
@时间    :2022/01/22 09:53:31
@作者    :None
@版本    :1.0
@说明    :主页面
'''


from ui.own.frame_theme import MyFrame
from ui.own.switch_button import SwitchButton
from ui.own.led import Led
from ui.own.palette_theme import set_my_palette
from utils.qt import Qt, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, QGroupBox, QPushButton, QSizePolicy, QComboBox, QSize, QCoreApplication, QMetaObject


class UiEmvHomePage(object):
    def setupUi(self, main):
        main.resize(500, 400)
        main.setMinimumSize(500, 400)
        main.setMaximumSize(500, 400)

        set_my_palette(main)

        self.central_widget = QWidget(main)
        self.main_layout = QHBoxLayout(self.central_widget)
        self.main_layout.setContentsMargins(0, 0, 0, 0)
        self.main_layout.setSpacing(0)

        # 主窗口左侧布局
        self.left_widget = QWidget(self.central_widget)
        # 主窗口左侧采用垂直布局
        self.left_layout = QVBoxLayout(self.left_widget)
        self.left_layout.setContentsMargins(0, 0, 0, 0)
        self.left_layout.setSpacing(0)

        # 数据接口
        self.interface_widget = MyFrame(self.left_widget)
        self.interface_hlayout = QHBoxLayout(self.interface_widget)
        self.interface_hlayout.setContentsMargins(0, 0, 0, 0)
        self.interface_hlayout.setSpacing(0)
        self.groupbox_interface = QGroupBox(self.interface_widget)
        self.interface_vlayout = QVBoxLayout(self.groupbox_interface)
        self.cb_interface = QComboBox(self.groupbox_interface)
        self.cb_interface.addItems(["CAN通讯", "串口通讯"])
        self.interface_vlayout.addWidget(self.cb_interface)
        self.interface_hlayout.addWidget(self.groupbox_interface)
        self.left_layout.addWidget(self.interface_widget)

        # CAN通讯配置
        self.can_interface_widget = MyFrame(self.left_widget)
        self.can_interface_hlayout = QHBoxLayout(self.can_interface_widget)
        self.can_interface_hlayout.setContentsMargins(0, 0, 0, 0)
        self.can_interface_hlayout.setSpacing(0)
        self.groupbox_can_interface = QGroupBox(self.can_interface_widget)
        self.can_interface_glayout = QGridLayout(self.groupbox_can_interface)

        self.lb_can_interface_channel = QLabel(self.groupbox_can_interface)
        self.lb_can_interface_channel.setText("设备索引号：")
        self.can_interface_glayout.addWidget(self.lb_can_interface_channel, 0, 0)
        self.cb_can_interface_channel = QComboBox(self.groupbox_can_interface)
        self.cb_can_interface_channel.addItems([str(i) for i in range(9)])
        self.can_interface_glayout.addWidget(self.cb_can_interface_channel, 0, 1)

        self.lb_can_interface_index = QLabel(self.groupbox_can_interface)
        self.lb_can_interface_index.setText("设备通道号：")
        self.can_interface_glayout.addWidget(self.lb_can_interface_index, 1, 0)
        self.cb_can_interface_index = QComboBox(self.groupbox_can_interface)
        self.cb_can_interface_index.addItems(["0", "1"])
        self.can_interface_glayout.addWidget(self.cb_can_interface_index, 1, 1)

        self.lb_can_interface_baudrate = QLabel(self.groupbox_can_interface)
        self.lb_can_interface_baudrate.setText("CAN波特率：")
        self.can_interface_glayout.addWidget(self.lb_can_interface_baudrate, 2, 0)
        self.cb_can_interface_baudrate = QComboBox(self.groupbox_can_interface)
        self.cb_can_interface_baudrate.addItems(["125Kbps", "250Kbps", "500Kbps"])
        self.cb_can_interface_baudrate.setCurrentText("250Kbps")
        self.can_interface_glayout.addWidget(self.cb_can_interface_baudrate, 2, 1)

        self.can_interface_hlayout.addWidget(self.groupbox_can_interface)
        self.left_layout.addWidget(self.can_interface_widget)

        # 串口通讯配置
        self.com_interface_widget = MyFrame(self.left_widget)
        self.com_interface_hlayout = QHBoxLayout(self.com_interface_widget)
        self.com_interface_hlayout.setContentsMargins(0, 0, 0, 0)
        self.com_interface_hlayout.setSpacing(0)
        self.groupbox_com_interface = QGroupBox(self.interface_widget)
        self.com_interface_glayout = QGridLayout(self.groupbox_com_interface)

        self.lb_com_interface_port = QLabel(self.groupbox_com_interface)
        self.lb_com_interface_port.setText("端口号：")
        self.com_interface_glayout.addWidget(self.lb_com_interface_port, 0, 0)
        self.cb_com_interface_port = QComboBox(self.groupbox_com_interface)
        self.cb_com_interface_port.addItems([str(i + 1) for i in range(9)])
        self.com_interface_glayout.addWidget(self.cb_com_interface_port, 0, 1)

        self.lb_com_interface_baudrate = QLabel(self.groupbox_com_interface)
        self.lb_com_interface_baudrate.setText("波特率：")
        self.com_interface_glayout.addWidget(self.lb_com_interface_baudrate, 1, 0)
        self.cb_com_interface_baudrate = QComboBox(self.groupbox_com_interface)
        self.cb_com_interface_baudrate.addItems(["9600", "115200"])
        self.com_interface_glayout.addWidget(self.cb_com_interface_baudrate, 1, 1)

        self.lb_com_interface_parity = QLabel(self.groupbox_com_interface)
        self.lb_com_interface_parity.setText("校验位：")
        self.com_interface_glayout.addWidget(self.lb_com_interface_parity, 2, 0)
        self.cb_com_interface_parity = QComboBox(self.groupbox_com_interface)
        self.cb_com_interface_parity.addItems(["无校验", "偶校验", "奇校验"])
        self.com_interface_glayout.addWidget(self.cb_com_interface_parity, 2, 1)

        self.lb_com_interface_word_length = QLabel(self.groupbox_com_interface)
        self.lb_com_interface_word_length.setText("数据位：")
        self.com_interface_glayout.addWidget(self.lb_com_interface_word_length, 3, 0)
        self.cb_com_interface_word_length = QComboBox(self.groupbox_com_interface)
        self.cb_com_interface_word_length.addItems(["8", "9"])
        self.com_interface_glayout.addWidget(self.cb_com_interface_word_length, 3, 1)

        self.lb_com_interface_stop = QLabel(self.groupbox_com_interface)
        self.lb_com_interface_stop.setText("停止位：")
        self.com_interface_glayout.addWidget(self.lb_com_interface_stop, 4, 0)
        self.cb_com_interface_stop = QComboBox(self.groupbox_com_interface)
        self.cb_com_interface_stop.addItems(["1", "1.5", "2"])
        self.com_interface_glayout.addWidget(self.cb_com_interface_stop, 4, 1)
        self.groupbox_com_interface.hide()
        self.com_interface_hlayout.addWidget(self.groupbox_com_interface)
        self.left_layout.addWidget(self.com_interface_widget)

        # BMS连接开关
        self.emv_connect_widget = MyFrame(self.left_widget)
        self.emv_connect_layout = QHBoxLayout(self.emv_connect_widget)
        self.emv_connect_layout.setContentsMargins(0, 0, 0, 0)
        self.emv_connect_layout.setSpacing(0)
        self.groupbox_emv_connect = QGroupBox(self.emv_connect_widget)
        self.bms_connect_hlayout = QHBoxLayout(self.groupbox_emv_connect)
        self.emv_switch = SwitchButton(self.groupbox_emv_connect)
        size_policy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        size_policy.setHorizontalStretch(0)
        size_policy.setVerticalStretch(0)
        size_policy.setHeightForWidth(self.emv_switch.sizePolicy().hasHeightForWidth())
        self.emv_switch.setSizePolicy(size_policy)
        self.emv_switch.setMinimumSize(QSize(70, 30))
        self.bms_connect_hlayout.addWidget(self.emv_switch)
        self.emv_connect_layout.addWidget(self.groupbox_emv_connect)
        self.left_layout.addWidget(self.emv_connect_widget)

        # 其他信息
        self.bms_widget = MyFrame(self.left_widget)
        self.bms_layout = QHBoxLayout(self.bms_widget)
        self.bms_layout.setContentsMargins(0, 0, 0, 0)
        self.bms_layout.setSpacing(0)
        self.groupbox_bms = QGroupBox(self.bms_widget)
        self.bms_hlayout = QGridLayout(self.groupbox_bms)
        self.lb_bms = QLabel(self.groupbox_bms)
        self.lb_bms.setText("簇编号：")
        self.bms_hlayout.addWidget(self.lb_bms, 0, 0)
        self.cb_bms = QComboBox(self.groupbox_bms)
        self.cb_bms.addItems(["1", "2", "3", "4", "5", "6", "7", "8", "9"])
        self.bms_hlayout.addWidget(self.cb_bms, 0, 1)
        size_policy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        size_policy.setHorizontalStretch(0)
        size_policy.setVerticalStretch(0)
        size_policy.setHeightForWidth(self.groupbox_bms.sizePolicy().hasHeightForWidth())
        self.groupbox_bms.setSizePolicy(size_policy)
        self.bms_layout.addWidget(self.groupbox_bms)
        self.left_layout.addWidget(self.bms_widget)

        # 其他信息
        self.other_widget = MyFrame(self.left_widget)
        self.other_layout = QHBoxLayout(self.other_widget)
        self.other_layout.setContentsMargins(0, 0, 0, 0)
        self.other_layout.setSpacing(0)
        self.groupbox_other = QGroupBox(self.other_widget)
        size_policy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        size_policy.setHorizontalStretch(0)
        size_policy.setVerticalStretch(0)
        size_policy.setHeightForWidth(self.groupbox_other.sizePolicy().hasHeightForWidth())
        self.groupbox_other.setSizePolicy(size_policy)
        self.other_layout.addWidget(self.groupbox_other)
        self.left_layout.addWidget(self.other_widget)

        self.main_layout.addWidget(self.left_widget, stretch=1)

        self.right_widget = QWidget(self.central_widget)
        # 主窗口右侧采用垂直布局
        self.right_layout = QVBoxLayout(self.right_widget)
        self.right_layout.setContentsMargins(0, 0, 0, 0)
        self.right_layout.setSpacing(0)

        # 数据接口
        self.relay_widget = MyFrame(self.right_widget)
        self.relay_vlayout = QVBoxLayout(self.relay_widget)
        self.relay_vlayout.setContentsMargins(0, 0, 0, 0)
        self.relay_vlayout.setSpacing(0)
        self.groupbox_relay = QGroupBox(self.relay_widget)
        self.groupbox_relay.setTitle("电磁阀控制指令")
        self.relay_glayout = QGridLayout(self.groupbox_relay)

        self.emv_version = QLabel(self.groupbox_relay)
        self.emv_version.setText("电磁阀软件版本号：")
        self.relay_glayout.addWidget(self.emv_version, 0, 0, 1, 1, Qt.AlignCenter)

        self.edt_emv_version = QLabel(self.groupbox_relay)
        self.edt_emv_version.setText("")
        self.relay_glayout.addWidget(self.edt_emv_version, 0, 1, 1, 1, Qt.AlignCenter)

        self.lb_cls = QLabel(self.groupbox_relay)
        self.lb_cls.setText("电磁阀：")
        self.relay_glayout.addWidget(self.lb_cls, 1, 0, 1, 1, Qt.AlignCenter)
        self.cb_cls = QComboBox(self.groupbox_relay)
        self.cb_cls.addItems(["总", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
        self.relay_glayout.addWidget(self.cb_cls, 1, 1, 1, 1)
        self.btn_emv_on = QPushButton(self.groupbox_relay)
        self.btn_emv_on.setText("闭合")
        self.relay_glayout.addWidget(self.btn_emv_on, 2, 0, 1, 1, Qt.AlignCenter)
        self.btn_emv_off = QPushButton(self.groupbox_relay)
        self.btn_emv_off.setText("断开")
        self.relay_glayout.addWidget(self.btn_emv_off, 2, 1, 1, 1, Qt.AlignCenter)

        self.relay_vlayout.addWidget(self.groupbox_relay)
        self.right_layout.addWidget(self.relay_widget)

        # 数据接口
        self.emv_data_widget = MyFrame(self.right_widget)
        self.emv_data_hlayout = QHBoxLayout(self.emv_data_widget)
        self.emv_data_hlayout.setContentsMargins(0, 0, 0, 0)
        self.emv_data_hlayout.setSpacing(0)
        self.groupbox_emv_data = QGroupBox(self.emv_data_widget)
        self.groupbox_emv_data.setTitle("电磁阀状态")
        self.emv_data_glayout = QGridLayout(self.groupbox_emv_data)

        # 总
        self.led_emv_total = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_total, 0, 0, 1, 1, Qt.AlignCenter)
        self.lb_emv_total = QLabel(self.groupbox_emv_data)
        self.lb_emv_total.setText("总")
        self.emv_data_glayout.addWidget(self.lb_emv_total, 1, 0, 1, 1, Qt.AlignCenter)

        # 1
        self.led_emv_1 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_1, 0, 1, 1, 1, Qt.AlignCenter)
        self.lb_emv_1 = QLabel(self.groupbox_emv_data)
        self.lb_emv_1.setText("#1")
        self.emv_data_glayout.addWidget(self.lb_emv_1, 1, 1, 1, 1, Qt.AlignCenter)

        # 2
        self.led_emv_2 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_2, 0, 2, 1, 1, Qt.AlignCenter)
        self.lb_emv_2 = QLabel(self.groupbox_emv_data)
        self.lb_emv_2.setText("#2")
        self.emv_data_glayout.addWidget(self.lb_emv_2, 1, 2, 1, 1, Qt.AlignCenter)

        # 3
        self.led_emv_3 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_3, 0, 3, 1, 1, Qt.AlignCenter)
        self.lb_emv_3 = QLabel(self.groupbox_emv_data)
        self.lb_emv_3.setText("#3")
        self.emv_data_glayout.addWidget(self.lb_emv_3, 1, 3, 1, 1, Qt.AlignCenter)

        # 4
        self.led_emv_4 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_4, 0, 4, 1, 1, Qt.AlignCenter)
        self.lb_emv_4 = QLabel(self.groupbox_emv_data)
        self.lb_emv_4.setText("#4")
        self.emv_data_glayout.addWidget(self.lb_emv_4, 1, 4, 1, 1, Qt.AlignCenter)

        # 5
        self.led_emv_5 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_5, 0, 5, 1, 1, Qt.AlignCenter)
        self.lb_emv_5 = QLabel(self.groupbox_emv_data)
        self.lb_emv_5.setText("#5")
        self.emv_data_glayout.addWidget(self.lb_emv_5, 1, 5, 1, 1, Qt.AlignCenter)

        # 6
        self.led_emv_6 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_6, 2, 1, 1, 1, Qt.AlignCenter)
        self.lb_emv_6 = QLabel(self.groupbox_emv_data)
        self.lb_emv_6.setText("#6")
        self.emv_data_glayout.addWidget(self.lb_emv_6, 3, 1, 1, 1, Qt.AlignCenter)

        # 7
        self.led_emv_7 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_7, 2, 2, 1, 1, Qt.AlignCenter)
        self.lb_emv_7 = QLabel(self.groupbox_emv_data)
        self.lb_emv_7.setText("#7")
        self.emv_data_glayout.addWidget(self.lb_emv_7, 3, 2, 1, 1, Qt.AlignCenter)

        # 8
        self.led_emv_8 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_8, 2, 3, 1, 1, Qt.AlignCenter)
        self.lb_emv_8 = QLabel(self.groupbox_emv_data)
        self.lb_emv_8.setText("#8")
        self.emv_data_glayout.addWidget(self.lb_emv_8, 3, 3, 1, 1, Qt.AlignCenter)

        # 9
        self.led_emv_9 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_9, 2, 4, 1, 1, Qt.AlignCenter)
        self.lb_emv_9 = QLabel(self.groupbox_emv_data)
        self.lb_emv_9.setText("#9")
        self.emv_data_glayout.addWidget(self.lb_emv_9, 3, 4, 1, 1, Qt.AlignCenter)

        # 10
        self.led_emv_10 = Led(self.groupbox_emv_data)
        self.emv_data_glayout.addWidget(self.led_emv_10, 2, 5, 1, 1, Qt.AlignCenter)
        self.lb_emv_10 = QLabel(self.groupbox_emv_data)
        self.lb_emv_10.setText("#10")
        self.emv_data_glayout.addWidget(self.lb_emv_10, 3, 5, 1, 1, Qt.AlignCenter)

        self.emv_data_hlayout.addWidget(self.groupbox_emv_data)
        self.right_layout.addWidget(self.emv_data_widget)
        self.main_layout.addWidget(self.right_widget, stretch=2)

        main.setCentralWidget(self.central_widget)

        self.retranslateUi(main)
        QMetaObject.connectSlotsByName(main)

    def retranslateUi(self, MainWindow):
        _translate = QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "电磁阀控制器"))
