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

from PyQt4.QtGui import QDialog,QDesktopWidget, QPainter, QColor, QFrame
from ui.ui_winDebug import Ui_Form
from os import getenv
from PyQt4.QtCore import Qt, QString
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 WinDebugQss (QDialog, Ui_Form):

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

        self.setupUi(self)
        self.__borderImagesPath = getenv('WIN_UI_IMAGES_PATH')
        self.__borderImagesPath = getenv('WIN_UI_IMAGES_PATH')
        self.setWindowFlags( Qt.FramelessWindowHint )# 无标题栏 | 置top
        self.setAttribute(Qt.WA_TranslucentBackground,True)
        self.__setitstyle()

    def update_current_pushbutton_style(self):
        try:
            self.__set_pushbutton_style()
            __font_size = str(int(common_run.SCREEN_HEIGHT * 14.0 / 900.0))
            __current_button_style = "QPushButton{background-color:#a3c3ce;border-style:flat;font: 63 "+__font_size+"pt \"ubuntu\";text-align : left;}"
            if self.stackedWidget.currentIndex() == 0:
                self.externDevice.setStyleSheet(__current_button_style)
            elif self.stackedWidget.currentIndex() == 1:
                self.networkCheck.setStyleSheet(__current_button_style)
            elif self.stackedWidget.currentIndex() == 2:
                self.muiltScreen.setStyleSheet(__current_button_style)
            elif self.stackedWidget.currentIndex() == 3:
                self.pushButton_usb_set.setStyleSheet(__current_button_style)
        except:
            pass

    def __set_pushbutton_style(self):
        __font_size = str(int(common_run.SCREEN_HEIGHT * 14.0 / 900.0))
        self.externDevice.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.networkCheck.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.muiltScreen.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_usb_set.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;}"
                                                ))

        __indicator_size = int(common_run.get_height(16.0))
        self.radioButton_auto_usb.setFocusPolicy(Qt.NoFocus)
        self.radioButton_auto_usb.setStyleSheet(QString("QRadioButton{font:"+__font_size+"pt;}"
                                       "QRadioButton:indicator:unchecked{image: url(%1/debugWindow/radio1.png);width:%2px;height:%2px;}"
                                       "QRadioButton:indicator:checked{image: url(%1/debugWindow/radio2.png);width:%2px;height:%2px;}"
                              ).arg( str(self.__borderImagesPath),str(__indicator_size)))

        self.radioButton_network_usb.setFocusPolicy(Qt.NoFocus)
        self.radioButton_network_usb.setStyleSheet(QString("QRadioButton{font:"+__font_size+"pt;}"
                                       "QRadioButton:indicator:unchecked{image: url(%1/debugWindow/radio1.png);width:%2px;height:%2px;}"
                                       "QRadioButton:indicator:checked{image: url(%1/debugWindow/radio2.png);width:%2px;height:%2px;}"
                              ).arg( str(self.__borderImagesPath),str(__indicator_size)))

        self.radioButton_virt_usb.setFocusPolicy(Qt.NoFocus)
        self.radioButton_virt_usb.setStyleSheet(QString("QRadioButton{font:"+__font_size+"pt;}"
                                       "QRadioButton:indicator:unchecked{image: url(%1/debugWindow/radio1.png);width:%2px;height:%2px;}"
                                       "QRadioButton:indicator:checked{image: url(%1/debugWindow/radio2.png);width:%2px;height:%2px;}"
                              ).arg( str(self.__borderImagesPath),str(__indicator_size)))


        self.groupBox.setStyleSheet(QString("broder:1px groove #626970;font:%1pt").arg(str(__indicator_size)))

        self.gridLayout_usb_status_set.setContentsMargins(common_run.get_height(60),
                                                             common_run.get_height(20),
                                                             common_run.get_height(60),
                                                             common_run.get_height(150))

        self.verticalLayout_usb_statu_set.setContentsMargins(common_run.get_width(20),
                                                             0,
                                                             0,
                                                             0)



    def __setitstyle(self):
        # 使窗口位于屏幕 中央
        screen = QDesktopWidget().screenGeometry()
        self.setStyleSheet("background-color:#e6e6e6;")
        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))
        self.networkCheck.setFocusPolicy(Qt.NoFocus)
        self.muiltScreen.setFocusPolicy(Qt.NoFocus)
        self.externDevice.setFocusPolicy(Qt.NoFocus)
        self.pushButton_usb_set.setFocusPolicy(Qt.NoFocus)
        __btn_close_height = common_run.get_height(45.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.setFocusPolicy(Qt.NoFocus)
        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,
                                                   common_run.get_width(21.0)
                                                   ,0)

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

        self.horizontalLayout_2.setContentsMargins(int(common_run.get_width(21.0)),
                                                   0,
                                                   int(common_run.get_width(8.0)),
                                                   0)

        __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.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)

if __name__ == "__main__":
    from PyQt4 import QtGui
    from os import environ
    environ["WIN_UI_IMAGES_PATH"]="../../../images"
    app = QtGui.QApplication([])
    win = WinDebugQss()
    win.show()
    app.exec_()