# coding:utf-8

from os import getenv
from PyQt4.QtGui import QWidget, QPixmap, QPalette, QBrush, QDesktopWidget, QApplication
from PyQt4.QtCore import Qt, QString, SIGNAL
from ui.ui_winLogin import Ui_winLoginForm
from common import common_run, base_fun

try:
    _encoding = QApplication.UnicodeUTF8


    def _translate(context, text, disambig):
        return QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QApplication.translate(context, text, disambig)


class WinLoginQss(QWidget, Ui_winLoginForm):
    def __init__(self, parent=None):
        super(WinLoginQss, self).__init__(parent)

        self.__borderImagesPath = getenv('WIN_UI_IMAGES_PATH')  #
        self.setupUi(self)
        self.__setitstyle()

        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnBottomHint |  Qt.Widget)
        self.centerNameList = []  # 平台名称 列表
        self.centerInfoList = []

        self.__band_signal_slot_2()
        # self.comboBoxCenter.hide()  # 先隐藏，待删除
        self.pushButtonrem.setFocusPolicy(Qt.NoFocus)
        self.pushButtonauto.setFocusPolicy(Qt.NoFocus)
    def setstylesheet_2(self):  # 分辨率样式改变
        self.__setitstyle()

    def init_act_size(self):
        self.pushButtonclose.setHidden(True)
        self.winLogo.setMinimumWidth(int(common_run.get_width(128.0)))
        self.winLogo.setMinimumHeight(int((42.0 / 900.0) * common_run.SCREEN_HEIGHT))
        self.winLogo.setMaximumWidth((128.0 / 1440.0) * common_run.SCREEN_WITH)
        self.winLogo.setMaximumHeight((42.0 / 900.0) * common_run.SCREEN_WITH)
        self.btn_setting.setMinimumWidth((31.0 / 1440.0) * common_run.SCREEN_WITH)
        self.btn_setting.setMinimumHeight((31.0 / 900.0) * common_run.SCREEN_HEIGHT)
        self.btn_setting.setMaximumWidth((31.0 / 1440.0) * common_run.SCREEN_WITH)
        self.btn_setting.setMaximumHeight((31.0 / 900.0) * common_run.SCREEN_HEIGHT)
        self.horizontalLayout_4.setContentsMargins((22.0 / 1440.0) * common_run.SCREEN_WITH,
                                                   (14.0 / 900.0) * common_run.SCREEN_HEIGHT,
                                                   (22.0 / 1440.0) * common_run.SCREEN_WITH,
                                                   0)

        __font_size = str(int(18.0 * (common_run.SCREEN_HEIGHT / 900.0) - 1))
        self.widget.setStyleSheet("QLineEdit{font: 63 " + __font_size + "pt \"Ubuntu\";"
                                                                        "color: rgba(9, 26, 35, 102);"
                                                                        "background-color:grba(255,255,255,0);"
                                                                        "border:1px  groove  grba(255,255,255,0);}")

        __font_size = str(int(20.0 * (common_run.SCREEN_HEIGHT / 900.0) - 1))
        self.widget_3.setStyleSheet("QLabel{font: 63 " + __font_size + "pt \"Ubuntu\";"
                                                                       "color: rgb(194, 217, 255);}")

        __button_size = int(24.0 * (common_run.SCREEN_HEIGHT / 900.0) - 1)
        self.pushButtonrem.setMaximumWidth(__button_size)
        self.pushButtonrem.setMinimumWidth(__button_size)
        self.pushButtonrem.setMaximumHeight(__button_size)
        self.pushButtonrem.setMinimumHeight(__button_size)
        self.pushButtonauto.setMaximumWidth(__button_size)
        self.pushButtonauto.setMinimumWidth(__button_size)
        self.pushButtonauto.setMaximumHeight(__button_size)
        self.pushButtonauto.setMinimumHeight(__button_size)

        __font_size = str(int(common_run.get_height(17.0)))
        self.currentVersion.setStyleSheet(QString.fromUtf8("font: " + __font_size + "pt \"ubuntu\";"
                                                                                    "color: rgba(255, 255, 255,153);"))

        self.label_info.setStyleSheet(QString.fromUtf8("font: " + __font_size + "pt \"ubuntu\";"
                                                                                "color: rgba(255, 255, 255,150);"))

        # self.setStyleSheet("QWidget#winLoginForm{background-color: rgb(9, 26, 35);}")
        # 设置背景图片

        screen = QDesktopWidget().screenGeometry()  # 设置全屏
        self.resize(screen.width(), screen.height())

        jpeg = QPixmap(self.__borderImagesPath + "/winVirturlList/background_down.png").scaled(
            self.size())  # scaled适应窗口大小
        palette1 = QPalette(self)
        palette1.setBrush(self.backgroundRole(), QBrush(jpeg))
        self.setPalette(palette1)
        self.setAutoFillBackground(True)

        __font_size = str(int(common_run.get_height(15.0)))
        self.label_2.setStyleSheet("color: rgba(255, 255, 255,153);"
                                   "font :" + __font_size + "pt")
        self.label_3.setStyleSheet("color: rgba(255, 255, 255,153);"
                                   "font :" + __font_size + "pt")

    def show_full_screen(self):
        self.setstylesheet_2()
        self.init_act_size()
        self.showFullScreen()

    def __setitstyle(self):
        __font_size = str(int(common_run.get_height(13.0)))
        self.lineEditUserName.setStyleSheet("color:rgb(90,90,90);font: " + __font_size + "pt")
        self.lineEditPassword.setStyleSheet("color:rgb(90,90,90);font: " + __font_size + "pt")
        self.lineEdit_domain.setStyleSheet("color:rgb(90,90,90);font: " + __font_size + "pt")

        self.lineEditUserName.setContextMenuPolicy(Qt.NoContextMenu) # 无右键快捷键
        self.lineEditPassword.setContextMenuPolicy(Qt.NoContextMenu) # 无右键快捷键
        self.lineEdit_domain.setContextMenuPolicy(Qt.NoContextMenu) # 无右键快捷键

        # 公司 logo
        self.winLogo.setStyleSheet(QString("QLabel {border: 2px;"
                                           "border-image:url(%1/loginWindow/login/clounds.png);border: 4px;}"
                                           ).arg(str(self.__borderImagesPath))
                                   )
        # 用户输入背景框
        self.inputBackGround.setStyleSheet(QString("QWidget#inputBackGround {border: 2px;"
                                                   "border-image:url(%1/loginWindow/login/inputBackGround.png);border: 4px;}"
                                                   ).arg(str(self.__borderImagesPath))
                                           )

        self.label_username_image.setStyleSheet(QString(""
                                                        "border-image:url(%1/loginWindow/login/username.png);"
                                                        ).arg(self.__borderImagesPath))
        self.label_username_image.setMaximumWidth(int(common_run.get_width(32.0)))
        self.label_username_image.setMinimumWidth(int(common_run.get_width(32.0)))
        self.label_username_image.setMaximumHeight(int(common_run.get_height(38.0)))
        self.label_username_image.setMinimumHeight(int(common_run.get_height(38.0)))

        self.label_password_image.setStyleSheet(QString(""
                                                        "border-image:url(%1/loginWindow/login/password.png);"
                                                        ).arg(self.__borderImagesPath))
        self.label_password_image.setMaximumWidth(int(common_run.get_width(32.0)))
        self.label_password_image.setMinimumWidth(int(common_run.get_width(32.0)))
        self.label_password_image.setMaximumHeight(int(common_run.get_height(38.0)))
        self.label_password_image.setMinimumHeight(int(common_run.get_height(38.0)))

        self.label_domain_image.setStyleSheet(QString(""
                                                      "border-image:url(%1/loginWindow/login/domain.png);"
                                                      ).arg(self.__borderImagesPath))
        self.label_domain_image.setMaximumWidth(int(common_run.get_width(32.0)))
        self.label_domain_image.setMinimumWidth(int(common_run.get_width(32.0)))
        self.label_domain_image.setMaximumHeight(int(common_run.get_height(38.0)))
        self.label_domain_image.setMinimumHeight(int(common_run.get_height(38.0)))

        self.verticalLayout_11.setContentsMargins(int(common_run.get_width(15)),
                                                  int(common_run.get_width(2)),
                                                  int(common_run.get_height(10)),
                                                  int(common_run.get_height(2)))
        self.verticalLayout_11.setSpacing(common_run.get_height(6))
        self.horizontalLayout_8.setContentsMargins(0,
                                                   int(common_run.get_height(10)),
                                                   int(common_run.get_height(18.0)),
                                                   0)

        self.verticalLayout_10.setContentsMargins(0,
                                                  0,
                                                  0,
                                                  int(common_run.get_height(10.0)))

        # 设置按钮 样式图片
        self.btn_setting.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/loginWindow/login/set.png);border: 2px;}"
            "QPushButton:hover{border-image:url(%1/loginWindow/login/set_hov.png);border: 2px;}"
            "QPushButton:pressed{border-image: url(%1/loginWindow/login/set_pre.png);border: 2px;}"
        ).arg(str(self.__borderImagesPath))
                                       )
        # 去除设置按钮焦点
        self.btn_setting.setFocusPolicy(Qt.NoFocus)
        # 记住密码
        self.pushButtonrem.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/check.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        # 自动登录
        self.pushButtonauto.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/check.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        # 登录按钮 样式图片
        __font_size = str(int(18.0 * (common_run.SCREEN_HEIGHT / 900.0) - 1))
        self.pushButtonLogin.setStyleSheet(
            QString("QPushButton{font: " + __font_size + "pt \"Ubuntu\";}"
                                                         "QPushButton{border-image: url(%1/loginWindow/login/login.png);border: 2px;}"
                                                         "QPushButton:hover{border-image:url(%1/loginWindow/login/login_hov.png);border: 2px;}"
                                                         "QPushButton:pressed{border-image: url(%1/loginWindow/login/login_pre.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )

        # 设置 选择平台下拉框  comBoxCenter样式
        # __font_size = str(int(18.0 * (common_run.SCREEN_HEIGHT / 900.0)))
        # self.comboBoxCenter.setStyleSheet(QString(""
        #                                           "color: rgb(255,255,255)"))
        #
        # self.comboBoxCenter.setStyleSheet(QString("QComboBox:!editable {"
        #                                           "font: 63 " + __font_size + "pt \"Ubuntu\";"
        #                                           "border:0px;"
        #                                           "}"
        #                                           "QComboBox::drop-down:!editable {"
        #                                           "image: url(%1/loginWindow/login/drop_down.png)16;"
        #                                           "subcontrol-position: center right;"
        #                                                   "}").arg(str(self.__borderImagesPath))
        #                                   )

        # self.pushButtonauto.setFocusPolicy(Qt.NoFocus)
        # self.pushButtonrem.setFocusPolicy(Qt.NoFocus)
        self.lineEdit_domain.setFocusPolicy(Qt.NoFocus)

        # if self.comboBoxCenter.currentText() == "":
        #     self.set_combox_centerdown_img()
        # else:
        #     self.set_combox_centerclear_img()

        self.pushButton_cho.setFocusPolicy(Qt.NoFocus)
        self.pushButton_cho.setMaximumWidth(int(common_run.get_width(15.0)))
        self.pushButton_cho.setMinimumWidth(int(common_run.get_width(15.0)))
        self.pushButton_cho.setMaximumHeight(int(common_run.get_height(14.0)))
        self.pushButton_cho.setMaximumHeight(int(common_run.get_height(14.0)))
        self.pushButton_cho.setFlat(True)
        self.pushButton_cho.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/choose_down.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        self.lineEdit_domain.setReadOnly(True)

    def __band_signal_slot_2(self):
        """add by pbc """
        self.connect(self.pushButton_cho, SIGNAL("clicked()"), self.choose_center_list)

    def choose_center_list(self):
        if len(self.centerInfoList) == 0:
            v = u'无平台'
            __temp_value = "<html><head/><body><p align=\"center\"><span style=\" color:#ff0000;\">" + v + "</p></body></html>"
            self.label_info.setText(_translate("Form", __temp_value, None))
            base_fun.eventloop_timer(2)
            self.label_info.setText("")
            return
        self.choose_center(self.centerNameList)  # 添加平台名称

    # ----------------- add by jiang_xmin 20160528--------------
    # 设置平台名称, 子对象 继承，重新实现
    def setcentName(self, name,index):
        print "父对象打印: ", name,index
        pass

    # ----------------- add by jiang_xmin end-------------------

    def upload_center_name(self,name,index):
        print "upload_center_name",name,index
        self.lineEdit_domain.setText(name)

        # --------- add by jiang_xmin 20160528-------
        try:
            self.setcentName(str(name),index)
        except Exception as e:
            pass
        # -------------- add by jiang_xmin end--------

        self._ChooseCenterDialog.close()
        del self._ChooseCenterDialog  # 释放 self._ChooseCenterDialog

    def choose_center(self, value_list):
        """add by pbc"""
        from uiWindow.ui_qss.base_ui.center_choose_dialog import ChooseCenterDialog
        self.pushButton_cho.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/choose_up.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        self._ChooseCenterDialog = ChooseCenterDialog(self.inputBackGround)
        __x = common_run.SCREEN_WITH / 2 - self.widget.width() / 2
        __y = common_run.SCREEN_HEIGHT * (55.0 / 89.0) - (
        self.widget.height() * (129.0 / 332.0) + common_run.get_height(8.0))
        self._ChooseCenterDialog.move(__x, __y)



        self.connect(self._ChooseCenterDialog, SIGNAL("upload_center_name"), self.upload_center_name)
        # _current_value = self.lineEdit_domain.text()
        temp_w = self.inputBackGround.width()
        self._ChooseCenterDialog.show_t(temp_w, value_list, self.defaultIndex)

        self.pushButton_cho.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/choose_down.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )

    # def comboxCenterActiivated(self, value):
    #     if self.comboBoxCenter.currentText() == "":
    #         self.set_combox_centerdown_img()
    #     else:
    #         self.set_combox_centerclear_img()

    # def comboxDamainActiivated(self, value):
    #     print value
    #     if self.comboBoxDamain.currentText() == "":
    #         self.set_combox_domaindown_img()
    #     else:
    #         self.set_combox_domainclear_img()

    # def set_combox_centerdown_img(self):
    #     return 0
    #
    # def set_combox_domaindown_img(self):
    #     return 0
    # #
    # def set_combox_centerclear_img(self):
    #     return 0

    # def set_combox_domainclear_img(self):
    #     return 0

    def set_rembername_checked_img(self):
        print "记住密码"
        try:
            self.pushButtonrem.setStyleSheet(
                QString("QPushButton{border-image: url(%1/loginWindow/login/check_pre.png);border: 2px;}").arg(
                    str(self.__borderImagesPath))
            )
        except Exception as e:
            print e.message
        finally:
            print " set_rembername_checked_img  ", self.__borderImagesPath

        return 0

    def set_rembername_notchecked_img(self):
        """create by wmz on 2015.11.06 for : set pushButtonrem not check"""
        print "取消记住密码"
        self.pushButtonrem.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/check.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        return 0

    def set_autologin_checked_img(self):
        """create by wmz on 2015.11.06 for : set pushButtonauto  check"""
        print "自动登陆"
        self.pushButtonauto.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/check_pre.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        return 0

    def set_autologin_notchecked_img(self):
        """create by wmz on 2015.11.06 for : set pushButtonauto not check"""
        print "取消自动登陆"
        self.pushButtonauto.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/check.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        return 0

    def setbutton_clicked_style(self):
        self.pushButtonLoginSet.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/setting_pre.png);border: 2px;}"
                    "QPushButton:hover{border-image:url(%1/loginWindow/login/setting_pre_hov.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        return 0

    def setbutton_noclick_style(self):
        self.pushButtonLoginSet.setStyleSheet(
            QString("QPushButton{border-image: url(%1/loginWindow/login/setting.png);border: 2px;}"
                    "QPushButton:hover{border-image:url(%1/loginWindow/login/setting_hov.png);border: 2px;}").arg(
                str(self.__borderImagesPath))
        )
        return 0


###############################
#   单元测试
##############################
if __name__ == "__main__":
    from PyQt4 import QtGui
    from os import environ

    environ["WIN_UI_IMAGES_PATH"] = "../../../images"
    app = QtGui.QApplication([])
    win = WinLoginQss()
    win.show()
    app.exec_()
