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

from PyQt4.QtGui import QDialog,QDesktopWidget,QPainter,QColor
from PyQt4.QtCore import Qt, QString
from ui.ui_logout import Ui_Dialog
from common import common_run
from os import getenv

class LogoutQss (QDialog, Ui_Dialog):

    def __init__(self, parent=None):
        super(LogoutQss, self).__init__(parent)
        self.setupUi(self)
        self.__borderImagesPath = getenv('WIN_UI_IMAGES_PATH')
        self.setModal(True)
        self.setAttribute(Qt.WA_TranslucentBackground,True)
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint
                            | Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint)
        self.__set_stylesheet()

    def __set_stylesheet(self):
        self.setMaximumHeight(int(common_run.get_height(213.0)))
        self.setMinimumHeight(int(common_run.get_height(213.0)))
        self.setMaximumWidth(int(common_run.get_width(514.0)))
        self.setMinimumWidth(int(common_run.get_width(514.0)))

        self.horizontalLayout.setContentsMargins(int((20.0/1440.0)*common_run.SCREEN_WITH),
                                               0,
                                               int((20.0/1440.0)*common_run.SCREEN_WITH),
                                               0)
        self.horizontalLayout_2.setSpacing(int((10.0/1440.0)*common_run.SCREEN_WITH))
        self.horizontalLayout_2.setContentsMargins(int((20.0/1440.0)*common_run.SCREEN_WITH),
                                               0,
                                               int((20.0/1440.0)*common_run.SCREEN_WITH),
                                               0)

        self.btn_close.setMaximumHeight(int((29.0/1440.0)*common_run.SCREEN_WITH))
        self.btn_close.setMinimumHeight(int((29.0/1440.0)*common_run.SCREEN_WITH))
        self.btn_close.setMaximumWidth(int((21.0/1440.0)*common_run.SCREEN_WITH))
        self.btn_close.setMinimumWidth(int((21.0/1440.0)*common_run.SCREEN_WITH))

        __button_size_height = common_run.SCREEN_HEIGHT * 40.0 / 900.0
        __button_size_with = common_run.SCREEN_WITH * 98.0 / 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_close.setFocusPolicy(Qt.NoFocus)
        self.btn_close.setStyleSheet(QString("border-image: url(%1/settingWindow/close.png);"
                                             "border: 2px;").arg(self.__borderImagesPath))

        __font_size = str(common_run.get_height(14.0))
        self.label_2.setStyleSheet("font: 63 "+__font_size+"pt;")
        self.label.setStyleSheet("font: 63 "+__font_size+"pt;")
        self.widget_2.setStyleSheet("QWidget#widget_2{background-color:rgba(0,0,0,0);}")
        self.widget_3.setStyleSheet("QWidget#widget_3{background-color:rgba(0,0,0,0);}")

        # 确定 按钮样式
        self.btn_ok.setText(u'确定')
        self.btn_ok.setFocusPolicy(Qt.NoFocus)
        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.setFocusPolicy(Qt.NoFocus)
        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))
        )

        screen = QDesktopWidget().screenGeometry()
        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.black)
        __r = int((10.0/1440.0)*common_run.SCREEN_WITH)
        p.drawRoundedRect(-1,-1,self.width() +1,self.height() +1,0,0)


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

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