#!/usr/bin/python
#coding=utf8

from PyQt4.QtGui import QDesktopWidget, QPainter, QColor, QPalette
from PyQt4.QtCore import Qt, QString
from os import getenv
from PyQt4.QtGui import QDialog
from ui.ui_setting import Ui_Form
from log_dialog_qss import LogoutQss
from common import common_run
from PyQt4 import QtCore, QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate_qss(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate_qss(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class SettingQss (QDialog, Ui_Form):
    def __init__(self, parent=None):
        super(SettingQss, self).__init__(parent)

        self.setupUi(self)
        self.__borderImagesPath = getenv('WIN_UI_IMAGES_PATH') # 读取环境变量 图片路径
        self.setWindowFlags( Qt.FramelessWindowHint )# 无标题栏 | 置top
        self.btn_close.setFocusPolicy(Qt.NoFocus)
        self.setAttribute(Qt.WA_TranslucentBackground,True)

        self._LogoutQss = LogoutQss()
        self.verticalLayout_log_mode.addWidget(self._LogoutQss)

        self.__setitstyle() # 设置样式

        self.setModal(True) # 设置模态窗口
        self.setAttribute(Qt.WA_DeleteOnClose) # 当窗口关闭时释放内存z

        # 使窗口位于屏幕 中央
        screen = QDesktopWidget().screenGeometry()
        self.move((screen.width() - self.width() ) / 2,(screen.height() - self.height()) / 2)

    def update_screen_show(self):
        self.__setitstyle()

    def update_current_button_style(self):
        try:
            __font_size = str(int(common_run.SCREEN_HEIGHT * 14.0 / 900.0))
            __current_style = "QPushButton{background-color:#a3c3ce;border-style:flat;font: 63 "+__font_size+"pt \"ubuntu\";text-align : left;}"
            self.__set_pushbutton_style()
            if self.stackedWidget.currentIndex() == 0:
                self.centersetting.setStyleSheet(__current_style)
            elif self.stackedWidget.currentIndex() == 1:
                self.networkseting.setStyleSheet(__current_style)
            elif self.stackedWidget.currentIndex() == 2:
                self.screenResolutionSetting.setStyleSheet(__current_style)
            elif self.stackedWidget.currentIndex() == 3:
                self.autoStartDesktopSetting.setStyleSheet(__current_style)
            elif self.stackedWidget.currentIndex() == 4:
                self.pushButton_log.setStyleSheet(__current_style)
            else:
                self.__set_pushbutton_style()
        except:
            pass


    def __set_pushbutton_style(self):
        __font_size = str(int(common_run.SCREEN_HEIGHT * 14.0 / 900.0))
        self.autoStartDesktopSetting.setFocusPolicy(Qt.NoFocus)
        self.autoStartDesktopSetting.setStyleSheet(QString(u"QPushButton{border-style:flat;font: 63 "+__font_size+"pt \"ubuntu\";text-align : left;}"
                                                u"QPushButton:hover{border-style:flat;background-color:#a3c3ce;}"
                                                ))
        self.screenResolutionSetting.setFocusPolicy(Qt.NoFocus)
        self.screenResolutionSetting.setStyleSheet(QString(u"QPushButton{border-style:flat;font: 63 "+__font_size+"pt \"ubuntu\";text-align : left;}"
                                                u"QPushButton:hover{border-style:flat;background-color:#a3c3ce;}"
                                                ))
        self.centersetting.setFocusPolicy(Qt.NoFocus)
        self.centersetting.setStyleSheet(QString(u"QPushButton{border-style:flat;font: 63 "+__font_size+"pt \"ubuntu\";text-align : left;}"
                                                u"QPushButton:hover{border-style:flat;background-color:#a3c3ce;}"
                                                ))
        self.networkseting.setFocusPolicy(Qt.NoFocus)
        self.networkseting.setStyleSheet(QString(u"QPushButton{border-style:flat;font: 63 "+__font_size+"pt \"ubuntu\";text-align : left;}"
                                                u"QPushButton:hover{border-style:flat;background-color:#a3c3ce;}"
                                                ))
        self.pushButton_log.setFocusPolicy(Qt.NoFocus)
        self.pushButton_log.setStyleSheet(QString(u"QPushButton{border-style:flat;font: 63 "+__font_size+"pt \"ubuntu\";text-align : left;}"
                                                u"QPushButton:hover{border-style:flat;background-color:#a3c3ce;}"
                                                ))


    def __setitstyle(self):
        # 使窗口位于屏幕 中央
        screen = QDesktopWidget().screenGeometry()

        self.widget_top.setStyleSheet("QWidget#widget_top{background-color:rgba(230,230,230,255);}")
        self.widget_botton.setStyleSheet("QWidget#widget_botton{background-color:rgba(230,230,230,255);}")
        self.widget.setStyleSheet("QWidget#widget{background-color:#f2f2f2;}")
        self.stackedWidget.setStyleSheet("background-color:#ffffff;")
        __width = common_run.get_width(850.0)
        __height = common_run.get_height(513.0)
        self.resize(int(__width),int(__height))

        __btn_close_height = common_run.SCREEN_HEIGHT * 45.0/900.0
        self.btn_close.setMaximumHeight(int(__btn_close_height))
        self.btn_close.setMinimumHeight(int(__btn_close_height))
        self.btn_close.setMaximumWidth(int(__btn_close_height*48.0/45.0))
        self.btn_close.setMinimumWidth(int(__btn_close_height*48.0/45.0))
        self.btn_close.setStyleSheet(
             QString("QPushButton{border-image: url(%1/settingWindow/closeX1.png);border: 2px;}"
            "QPushButton:hover{border-image:url(%1/settingWindow/closeX1.png);border: 2px;}"
            "QPushButton:pressed{border-image: url(%1/settingWindow/closeX1.png);border: 2px;}").arg(str(self.__borderImagesPath))
        )

        __font_size = str(int(common_run.SCREEN_HEIGHT * 14.0 / 900.0))
        self.horizontalLayout_4.setContentsMargins(0,
                                                   0,
                                                   (21.0/1440.0)*common_run.SCREEN_WITH,
                                                   0)

        self.horizontalLayout_2.setContentsMargins((21.0/1440.0)*common_run.SCREEN_WITH,
                                                   0,
                                                   (5.0/1440.0)*common_run.SCREEN_WITH,
                                                   0)

        self.horizontalLayout_4.setSpacing((18.0/1440.0)*common_run.SCREEN_WITH)

        __button_size_height = common_run.SCREEN_HEIGHT * 36.0 / 900.0
        __button_size_with = common_run.SCREEN_WITH * 88.2 / 1440.0
        self.btn_cancle.setMinimumHeight(int(__button_size_height))
        self.btn_cancle.setMinimumWidth(int(__button_size_with))
        self.btn_cancle.setMaximumHeight(int(__button_size_height))
        self.btn_cancle.setMaximumWidth(int(__button_size_with))
        self.btn_ok.setMinimumHeight(int(__button_size_height))
        self.btn_ok.setMinimumWidth(int(__button_size_with))
        self.btn_ok.setMaximumHeight(int(__button_size_height))
        self.btn_ok.setMaximumWidth(int(__button_size_with))
        self.btn_yes.setMinimumHeight(int(__button_size_height))
        self.btn_yes.setMinimumWidth(int(__button_size_with))
        self.btn_yes.setMaximumHeight(int(__button_size_height))
        self.btn_yes.setMaximumWidth(int(__button_size_with))

        self.label.setText(_translate_qss("Form", "<html><head/><body><p align=\"center\"><span style=\" font-size:"+__font_size+"pt;\">  设 置</span></p></body></html>", None))

        self.__set_pushbutton_style()

        # 确定 按钮样式
        self.btn_ok.setText(u'确定')
        self.btn_ok.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_cancle.setText(u'取消')
        self.btn_cancle.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))
        )
        # yingyong 按钮样式
        self.btn_yes.setText(u'应用')
        self.btn_yes.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_close.setStyleSheet(
             QString("QPushButton{border-image: url(%1/settingWindow/closeX1.png);border: 2px;}"
            "QPushButton:hover{border-image:url(%1/settingWindow/closeX1.png);border: 2px;}"
            "QPushButton:pressed{border-image: url(%1/settingWindow/closeX1.png);border: 2px;}").arg(str(self.__borderImagesPath))
        )


        # self.btn_up_log = QPushButton()
        # self.label_info = QLabel(u'上传日志')
        self.verticalLayout_log_mode.setContentsMargins(common_run.get_width(40.0),
                                                        common_run.get_height(20.0),
                                                        0,
                                                        0)

        self._LogoutQss.horizontalLayout.setSpacing(common_run.get_width(15.0))
        self._LogoutQss.btn_up_log.setFocusPolicy(Qt.NoFocus)
        self._LogoutQss.btn_up_log.setText(u'上 传')
        self._LogoutQss.set_lable_log_text(u'是否向云交付平台上传日志')

        self._LogoutQss.btn_up_log.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._LogoutQss.btn_up_log.setMinimumWidth(common_run.get_width(100.0))
        self._LogoutQss.btn_up_log.setMinimumHeight(common_run.get_height(36.0))
        self._LogoutQss.btn_up_log.setMaximumWidth(common_run.get_width(100.0))
        self._LogoutQss.btn_up_log.setMaximumHeight(common_run.get_height(36.0))

        self.networkseting.setFocusPolicy(Qt.NoFocus)
        self.move((screen.width() - self.width() ) / 2,(screen.height() - self.height()) / 2)


    # def paintEvent(self, event):
    #     p = QPainter(self)
    #     p.setBrush(QColor(230,230,230,255))
    #     p.setPen(Qt.white)
    #     __r = int((11.0/1440.0)*common_run.SCREEN_WITH)
    #     p.drawRoundedRect(-1,-1,self.width()+1,self.height()+1,__r,__r)
        # self.setStyleSheet(QString("border-image:url(%1/base_bg.png)").arg(str(self.__borderImagesPath)))

###############################
#   单元测试
##############################
if __name__ == "__main__":
    from PyQt4 import QtGui
    from os import environ
    environ["WIN_UI_IMAGES_PATH"]="../../../images3"
    app = QtGui.QApplication([])
    win = SettingQss()
    win.show()
    app.exec_()
