# coding:utf-8

from os import getenv
from PyQt4 import QtGui
from PyQt4.QtGui import QWidget,QPalette,QPixmap,QBrush,QDesktopWidget
from PyQt4.QtCore import Qt, QString,QEvent
from ui.ui_winVirturlList import Ui_Form
from common import common_run

class WinVirturlListQss(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(WinVirturlListQss, self).__init__(parent)
        self.__borderImagesPath = getenv('WIN_UI_IMAGES_PATH') #
        self.setupUi(self)
        self.setWindowFlags(Qt.CustomizeWindowHint | Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_DeleteOnClose) # 当窗口关闭时释放内存
        self.btn_user_old.setHidden(True)
        self.__set_stylesheet()

    def set_stylesheet_2(self):
        self.__set_stylesheet()

    def __set_stylesheet(self):
        self.label_user_name_sign.setMaximumWidth(common_run.get_width(36.0))
        self.label_user_name_sign.setMinimumWidth(common_run.get_width(36.0))
        self.desktopGridLayout.setSpacing(common_run.get_width(20.0))
        self.desktopGridLayout.setContentsMargins(common_run.get_width(160),
                                                  common_run.get_height(100),
                                                  common_run.get_width(160),
                                                  common_run.get_height(180))
        __font_size = str(common_run.get_height(10.0))
        self.label_user_name_sign.setStyleSheet("font: 63 "+__font_size+"pt;color:rgb(255,255,255)")

        self.winVirturlist_up.setStyleSheet(QString( "QWidget#winVirturlist_up"
                                     "{ background-color: rgba(9,26,35,127);"
                                       "}" )
                                    )
        self.setStyleSheet(QString( "QWidget#stackedWidget,"
                                    "QWidget#osDesktop,"
                                    "QWidget#scrollAreaWidgetContents,"
                                    "QWidget#scrollArea,"
                                    "QWidget#desktopGridLayout"
                                    "{ background-color: rgba(9,26,35,0);"
                                       "border-image:url(%1/winVirturlList/background_down.png);border: 2px;"
                                       "}" ).arg(str(self.__borderImagesPath))
                                    )
        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)

        # self.scrollArea.setStyleSheet('border-image: url();background-color: rgb(9,26,35);')
        self.horizontalLayout.setContentsMargins(int((20.0/1440.0)*common_run.SCREEN_WITH),
                                                   int((10.0/900.0)*common_run.SCREEN_HEIGHT),
                                                   int((20.0/1440.0)*common_run.SCREEN_WITH),
                                                   0)
        self.horizontalLayout.setSpacing(int((40.0/1440.0)*common_run.SCREEN_WITH))
        self.verticalLayout_4.setContentsMargins(0,
                                                 0,
                                                   int((40.0/1440.0)*common_run.SCREEN_WITH),

                                                   0)

        self.winApp.setFocusPolicy(Qt.NoFocus)
        self.winApp.setMaximumHeight(int(common_run.get_height(44.0)))
        self.winApp.setMinimumHeight(int(common_run.get_height(44.0)))
        self.winApp.setMaximumWidth(int(common_run.get_width(108.0)))
        self.winApp.setMinimumWidth(int(common_run.get_width(108.0)))

        self.winDesktop.setFocusPolicy(Qt.NoFocus)
        self.winDesktop.setMaximumHeight(int(common_run.get_height(44.0)))
        self.winDesktop.setMinimumHeight(int(common_run.get_height(44.0)))
        self.winDesktop.setMaximumWidth(int(common_run.get_width(108.0)))
        self.winDesktop.setMinimumWidth(int(common_run.get_width(108.0)))

        self.btn_setting.setMaximumHeight(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_setting.setMinimumHeight(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_setting.setMaximumWidth(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_setting.setMinimumWidth(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_logout.setMaximumHeight(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_logout.setMinimumHeight(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_logout.setMaximumWidth(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_logout.setMinimumWidth(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_user.setMaximumHeight(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_user.setMinimumHeight(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_user.setMaximumWidth(int((30.0/900.0)*common_run.SCREEN_HEIGHT))
        self.btn_user.setMinimumWidth(int((30.0/900.0)*common_run.SCREEN_HEIGHT))

        # 设置 桌面 按钮样式
        self.winDesktop.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/winVirturlList/desktop1.png);border: 2px;}"
            "QPushButton:hover,QPushButton:pressed,QPushButton:focus{"
                    "border-image: url(%1/winVirturlList/desktop2.png);border: 2px;"
                    "}"
             ).arg(str(self.__borderImagesPath))
        )

        # 设置 应用 按钮样式
        # self.winApp.setFocusPolicy(Qt.NoFocus)
        self.winApp.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/winVirturlList/app1.png);border: 2px;}"
            "QPushButton:hover,QPushButton:pressed,QPushButton:focus{"
                "border-image:url(%1/winVirturlList/app2.png);border: 2px;"
            "}").arg(str(self.__borderImagesPath))
        )
        # 设置按钮
        self.btn_setting.setFocusPolicy(Qt.NoFocus)
        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_logout.setFocusPolicy(Qt.NoFocus)
        self.btn_logout.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/winVirturlList/logout1.png);border: 2px;}"
            "QPushButton:hover{border-image:url(%1/winVirturlList/logout2.png);border: 2px;}"
            "QPushButton:pressed{border-image: url(%1/winVirturlList/logout3.png);border: 2px;}"
                     ).arg(str(self.__borderImagesPath))
        )
        # 注销按钮
        self.btn_user.setFocusPolicy(Qt.NoFocus)
        self.btn_user.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/winVirturlList/userName1.png);border: 2px;}"
            "QPushButton:hover{border-image:url(%1/winVirturlList/userName2.png);border: 2px;}"
            "QPushButton:pressed{border-image: url(%1/winVirturlList/userName3.png);border: 2px;}"
                     ).arg(str(self.__borderImagesPath))
        )

        #按钮提示
        self.btn_user.installEventFilter(self)
        self.btn_logout.installEventFilter(self)
        self.btn_setting.installEventFilter(self)
        self.label_user_name_sign.installEventFilter(self)

    #按钮提示
    def eventFilter(self, obj, event):
        eve=QEvent.HoverEnter
        if event.type() == eve:
            objName=str(obj.objectName())
            if (objName == 'btn_user'):
                QtGui.QToolTip.showText(QtGui.QCursor().pos(),u'修改密码')
            elif (objName == 'btn_logout'):
                QtGui.QToolTip.showText(QtGui.QCursor().pos(),u'注销')
            elif (objName == 'btn_setting'):
                QtGui.QToolTip.showText(QtGui.QCursor().pos(),u'设置')
            # elif (objName == 'label_user_name_sign'):
            #     QtGui.QToolTip.showText(QtGui.QCursor().pos(),u'注销')
        return QtGui.QWidget.eventFilter(self,obj,event)


    def select_winapp(self):

        self.winApp.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/winVirturlList/app2.png);border: 2px;}"
            "QPushButton:hover,QPushButton:pressed,QPushButton:focus{"
                "border-image:url(%1/winVirturlList/app2.png);border: 2px;"
            "}").arg(str(self.__borderImagesPath))
        )

        self.winDesktop.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/winVirturlList/desktop1.png);border: 2px;}"
            "QPushButton:hover,QPushButton:pressed,QPushButton:focus{"
                    "border-image: url(%1/winVirturlList/desktop2.png);border: 2px;"
                    "}"
             ).arg(str(self.__borderImagesPath))
        )
    def select_windesktop(self):

        self.winApp.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/winVirturlList/app1.png);border: 2px;}"
            "QPushButton:hover,QPushButton:pressed,QPushButton:focus{"
                "border-image:url(%1/winVirturlList/app2.png);border: 2px;"
            "}").arg(str(self.__borderImagesPath))
        )

        self.winDesktop.setStyleSheet(QString(
            "QPushButton{border-image: url(%1/winVirturlList/desktop2.png);border: 2px;}"
            "QPushButton:hover,QPushButton:pressed,QPushButton:focus{"
                    "border-image: url(%1/winVirturlList/desktop2.png);border: 2px;"
                    "}"
             ).arg(str(self.__borderImagesPath))
        )
    def bandSignalSlot(self):
         pass



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