# -*- coding: utf-8 -*-

from os import getenv
from PyQt4.QtGui import QWidget, QAbstractItemView, QFrame
from PyQt4.QtCore import QString,Qt
import PyQt4.QtCore
from ui.ui_centerSet import Ui_Form
from common import common_run

class CenterSetQss(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(CenterSetQss, self).__init__(parent)
        self.setupUi(self)
        self.__borderImagesPath =  getenv('WIN_UI_IMAGES_PATH')    # 读取环境变量
        self.setAttribute(Qt.WA_DeleteOnClose) # 当窗口关闭时释放内存
        self.setitsStyle()

    def update_screen_show(self):
        self.setitsStyle()

    def setitsStyle(self):
        self.tableWidget.setSelectionMode(QAbstractItemView.SingleSelection)
        self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.buttonclick = False
        self.fontword = self.tableWidget.horizontalHeader().font()
        self.tableWidget.horizontalHeader().setFont(self.fontword)
        self.tableWidget.setHorizontalHeaderLabels([u'名称', u'IP', u'域名'])
        self.tableWidget.setShowGrid(False)
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableWidget.horizontalHeader().setStretchLastSection(True)
        self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.tableWidget.setFrameShape(QFrame.NoFrame)
        self.tableWidget.horizontalHeader().setFixedHeight(common_run.get_height(40.0))
        self.tableWidget.horizontalHeader().resizeSection(0, common_run.get_width(170.0))
        self.tableWidget.horizontalHeader().resizeSection(1, common_run.get_width(230.0))
        self.tableWidget.verticalHeader().setHidden(True)

        self.verticalLayout_3.setContentsMargins(int(common_run.get_width(60.0)),
                                               int(common_run.get_width(20.0)),
                                               int(common_run.get_width(60.0)),
                                               int(common_run.get_width(20.0)))

        self.btn_add.setMinimumWidth((74.0/1440.0)*common_run.SCREEN_WITH)
        self.btn_add.setMinimumHeight((31.0/900.0)*common_run.SCREEN_HEIGHT)
        self.btn_delete.setMinimumWidth((74.0/1440.0)*common_run.SCREEN_WITH)
        self.btn_delete.setMinimumHeight((31.0/900.0)*common_run.SCREEN_HEIGHT)
        self.verticalLayout_2.setSpacing((30.0/900.0)*common_run.SCREEN_HEIGHT)
        self.horizontalLayout.setSpacing((10.0/900.0)*common_run.SCREEN_HEIGHT)

        __font_size = str(int(common_run.get_width(11.0)))
        self.tableWidget.setStyleSheet("font: 63 "+__font_size+"pt ")
        self.lineEdit_port.setStyleSheet("font: 63 "+__font_size+"pt ")
        self.lineEdit_ip.setStyleSheet("font: 63 "+__font_size+"pt")

        self.lineEdit_ip.setStyleSheet("border:1px groove rgb(200,200,200);")
        self.lineEdit_port.setStyleSheet("border:1px groove rgb(200,200,200);")

        self.tableWidget.horizontalHeader().setStyleSheet(
            "QHeaderView:section{background-color:#e6e6e6;color:rgb(206,210,213);padding-left: 0px;}"
            "QHeaderView:section{font: "+__font_size+"pt;}"
            "QHeaderView:section{color:rgb(0,1,2);border:0px groove rgb(200,200,200);}"
        )

        # self.btn_delete.setStyleSheet(
        #     QString("QPushButton:hover{background-image:url(%1/settingWindow/pushbutton_hov.png);border: 2px;}"
        #     "QPushButton:pressed{border-image:url(%1/settingWindow/pushbutton_pre.png);border: 2px;}"
        #     "QPushButton{border-image:url(%1/settingWindow/pushbutton.png);border: 2px;}"
        #     "QPushButton{font: 75 "+__font_size+"pt \"ubuntu\";}"
        #     "QPushButton{color: rgb(255, 255, 255);}").arg(str(self.__borderImagesPath))
        # )

        self.btn_delete.setText(u'删除')
        self.btn_delete.setStyleSheet(
             QString("QPushButton{color: rgb(255,255,255);font: 63 "+__font_size+"pt;border-image: url(%1/settingWindow/btn_backgound1.png);border: 2px;}"
            "QPushButton:hover{border-image:url(%1/settingWindow/btn_backgound2.png);border: 2px;}"
            "QPushButton:pressed{border-image: url(%1/settingWindow/btn_backgound3.png);border: 2px;}").arg(str(self.__borderImagesPath))
        )

        self.btn_add.setText(u'添加')
        self.btn_add.setStyleSheet(
             QString("QPushButton{color: rgb(255,255,255);font: 63 "+__font_size+"pt;border-image: url(%1/settingWindow/btn_backgound1.png);border: 2px;}"
            "QPushButton:hover{border-image:url(%1/settingWindow/btn_backgound2.png);border: 2px;}"
            "QPushButton:pressed{border-image: url(%1/settingWindow/btn_backgound3.png);border: 2px;}").arg(str(self.__borderImagesPath))
        )

        # self.btn_add.setStyleSheet(
        #     QString("QPushButton:hover{background-image:url(%1/settingWindow/pushbutton_hov.png);border: 2px;}"
        #     "QPushButton:pressed{border-image:url(%1/settingWindow/pushbutton_pre.png);border: 2px;}"
        #     "QPushButton{border-image:url(%1/settingWindow/pushbutton.png);border: 2px;}"
        #     "QPushButton{font: 75 "+__font_size+"pt \"ubuntu\";}"
        #     "QPushButton{color: rgb(255, 255, 255);}").arg(str(self.__borderImagesPath))
        # )

    @PyQt4.QtCore.pyqtSlot()
    def show_centername(self):
        if self.lineEditCenterName.text() != '\0':
            self.lineEditCenterName.setStyleSheet(QString("QLineEdit{border: 2px;"
                                                  "font: 9pt \"宋体\";\n"
                                                  "border-image: url(%1/settingWindow/settingViewcenter_clear.png);"
                                                  "color: rgb(149,149,149)"
                                                  "}").arg(str(self.__borderImagesPath))
                                                  )
        else:
            self.lineEditCenterName.setStyleSheet(QString("QLineEdit{border: 2px;"
                                                  "font: 9pt \"宋体\";\n"
                                                  "border-image: url(%1/settingWindow/settingViewcenter_name.png);"
                                                  "color: rgb(149,149,149)"
                                                  "}").arg(str(self.__borderImagesPath))
                                                  )

    @PyQt4.QtCore.pyqtSlot()
    def show_IP(self):
        if self.lineEditCentreIp.text() != '\0':
            self.lineEditCentreIp.setStyleSheet(QString("QLineEdit{border: 2px;"
                                                "font: 9pt \"宋体\";\n"
                                                "border-image: url(%1/settingWindow/settingViewcenter_clear.png);"
                                                "color: rgb(149,149,149)"
                                                "}").arg(str(self.__borderImagesPath))
                                                )
        else:
            self.lineEditCentreIp.setStyleSheet(QString("QLineEdit{border: 2px;"
                                                "font: 9pt \"宋体\";\n"
                                                "border-image: url(%1/settingWindow/settingViewcenter_IP.png);"
                                                "color: rgb(149,149,149)"
                                                "}").arg(str(self.__borderImagesPath))
                                                )

    @PyQt4.QtCore.pyqtSlot()
    def setupwidget(self):
        if self.buttonclick == False:
            self.widget.show()
            self.pushButton_addcenter.setStyleSheet(
                QString("QPushButton{background-image:url(%1/settingWindow/add_center_up.png);border: 2px;}"
                "QPushButton{font: 75 11pt \"ubuntu\";}").arg(str(self.__borderImagesPath))
            )
            self.buttonclick = True
        else:
            self.pushButton_addcenter.setStyleSheet(
                QString("QPushButton{background-image:url(%1/settingWindow/add_center_down);border: 2px;}"
                "QPushButton{font: 75 11pt \"ubuntu\";}").arg(str(self.__borderImagesPath))
            )
            self.widget.hide()
            self.buttonclick = False


#############################
## 测试用例
###########################
if __name__ == "__main__":
    import sys
    from os import  environ
    reload(sys)
    sys.setdefaultencoding('utf-8')
    from PyQt4 import QtGui
    app = QtGui.QApplication([])

    environ["WIN_UI_IMAGES_PATH"]="../../../images3"
    logindlg = CenterSetQss()
    logindlg.show()

    app.exec_()