# coding:utf-8

from os import getenv
from PyQt4.QtGui import QWidget, QPushButton, QLabel, QToolTip, QCursor
from PyQt4.QtCore import SIGNAL, Qt, QString, pyqtSlot, QPoint

from ui.ui_winDesktopUnit import Ui_WinDesktopUnit
from PyQt4 import QtCore
from common import common_run

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s


# 系统类型
class VmOsType(object):
    WINXP = "windows_xp"

    WIN_2003 = "windows_2003"
    WIN_2003X64 = "windows_2003x64"

    WIN_2008 = "windows_2008"
    WIN_2008X64 = "windows_2008x64"

    WIN_7 = "windows_7"
    WIN_7X64 = "windows_7x64"

    OTHER = "other"
    OTHER_LINUX = "other_linux"
    RHEL_3 = "rhel_3"
    RHEL_3X64 = "rhel_3x64"
    RHEL_4 = "rhel_4"
    RHEL_4X64 = "rhel_4x64"
    RHEL_5 = "rhel_5"
    RHEL_5X64 = "rhel_5x64"

    RHEL_6X64 = "rhel_6x64"

    UBUNTU_12_04 = "ubuntu_12_04"
    UBUNTU_13_04 = "ubuntu_13_04"

    FREEBSD = "freebsd"

    OTHER_PPC64 = "other_ppc64"
    OTHER_LINUX_PPC64 = "other_linux_ppc64"
    RHEL_6_PPC64 = "rhel_6_ppc64"

    rhelList = [RHEL_3, RHEL_3X64, RHEL_4, RHEL_4X64, RHEL_5, RHEL_5X64, RHEL_6X64]


# class VmPushbutton(QPushButton):  # 开关 虚拟机
#     def __init__(self, parent=None):
#         super(VmPushbutton, self).__init__(parent)
#         self.setEnabled(True)
#         self.setGeometry(QtCore.QRect(2, 193, 245, 77))
#
#         self.setMinimumSize(QtCore.QSize(160, 53))
#         self.setMaximumSize(QtCore.QSize(254, 77))
#         self.setMouseTracking(False)
#         self.setText(_fromUtf8(""))
#         self.setCheckable(False)
#         self.setAutoRepeat(False)
#         self.setAutoExclusive(False)
#         self.setAutoDefault(False)
#         self.setDefault(False)
#         self.setFlat(False)
#         self.setObjectName(_fromUtf8("osPowerOff"))
#
#     def enterEvent(self, QEvent):
#         self.emit(SIGNAL("enterVmPushbutton"))
#
#     def leaveEvent(self, QEvent):
#         self.emit(SIGNAL("leaveVmPushbutton"))


# class VmImage(QLabel):  # 虚拟系统图片
#     def __init__(self, parent=None):
#         super(VmImage, self).__init__(parent)
#         self.setGeometry(QtCore.QRect(38, 20, 123, 123))
#         self.setText(_fromUtf8(""))
#         self.setObjectName(_fromUtf8("osImage"))
#
#         self.__osImagName = ""
#
#     def enterEvent(self, QEvent):
#         self.emit(SIGNAL("enterVmImage"))
#
#     def leaveEvent(self, QEvent):
#         self.emit(SIGNAL("leaveVmImage"))

class Tmp_label_2(QLabel):
    def __init__(self,parent = None):
        super(Tmp_label_2, self).__init__(parent)
        self.update_style_sheet()

    def update_style_sheet(self):
        __font_size = str(common_run.get_height(10.0))
        self.setStyleSheet("font: 63 "+__font_size+"pt;color:rgb(255,255,255)")

    def enterEvent(self, ee):
        return QToolTip.showText(QCursor().pos(),common_run.tip_vm_name)


class WinDesktopUnitQss(QWidget, Ui_WinDesktopUnit):
    def __init__(self, parent=None):
        super(WinDesktopUnitQss, self).__init__(parent)

        self.__borderImagesPath = getenv('WIN_UI_IMAGES_PATH')  #
        self.setupUi(self)
        self.setAttribute(Qt.WA_DeleteOnClose)  # 当窗口关闭时释放内存

        self.background_label.lower()
        self.__setitstyle()
        self.mouse_hove_label = QLabel(self)
        self.vm_name_temp2 = ""

    #  ------------------------ API ------------------------

    # 设定 系统图标
    def setOsImage(self, osType):
        if osType == VmOsType.WINXP:
            osImagName = "windows_xp.png"
            self.__osImagName ="windows_xp"

        elif osType == VmOsType.WIN_2003 or osType == VmOsType.WIN_2003X64:  # windows_2003
            osImagName = "windows_2003"
            self.__osImagName ="windows_2003"

        elif osType == VmOsType.WIN_2008 or osType == VmOsType.WIN_2008X64:  # windows_2008
            osImagName = "windows_2008.png"
            self.__osImagName ="windows_2008"

        elif osType == VmOsType.WIN_7 or osType == VmOsType.WIN_7X64:  # windows_7
            osImagName = "windows_7"
            self.__osImagName ="windows_7"

        elif osType in VmOsType.rhelList:
            osImagName = "redhat.png"
            self.__osImagName = "redhat"
        else:
            osImagName = "win_default.png"
            self.__osImagName = "win_default"

        self.background_label.setStyleSheet(
            QString("QLabel {border: 2px;"
                    "border-image: url(%1/os_image/%2);border: 4px;}").arg(str(self.__borderImagesPath)).arg(
                str(osImagName))
        )

    #windows_7_gray.png

    def setOsImage_ON(self):
        self.background_label.setStyleSheet(
            QString("border-image:url(%1/os_image/%2)").
                arg(self.__borderImagesPath).
                arg(self.__osImagName+".png"))

    def setOsImage_OFF(self):
        self.background_label.setStyleSheet(
            QString("border-image:url(%1/os_image/%2)").
                arg(self.__borderImagesPath).
                arg(self.__osImagName+"_gray.png"))

    # ------------------------ API End------------------------

    # def enterEvent(self, e):
    #     self.background_hover()
    #     print "enter this ...."
    #
    # def leaveEvent(self, e):
    #     self.background_border()
    #     print "leave this ...."

    def enterEvent(self, QEvent):
        print "enterevent ...."
        self.background_hover()
        self.setCursor(Qt.PointingHandCursor) # 鼠标指针 变为小手
        p = QPoint()
        p.setX(self.x())
        p.setY(self.y()+self.height()+common_run.get_height(30.0))
        QToolTip.showText(p,self.vm_name_temp2)

    def leaveEvent(self, QEvent):
        print "leaveevent ...."
        self.background_press()
        self.setCursor(Qt.ArrowCursor)        #鼠标指针 变为箭头


    #  ------------------ 私有成员 -----------------------------

    def background_border(self):
        pass
        # self.background_label.setStyleSheet("color: rgba(9, 26, 35, 0)")
        # self.mouse_hove_label.setGeometry(0,0,self.width(),self.height())
        # self.mouse_hove_label.setStyleSheet(QString("QLabel {border: 2px;"
        #                                             "border-image:url(%1/os_image/vmBackground_hover.png);border: 4px;}"
        #                                             ).arg(str(self.__borderImagesPath))
        #                                     )

    def background_hover(self):
        self.mouse_hove_label.setGeometry(0,0,self.width(),self.height())
        self.mouse_hove_label.setStyleSheet(QString("QLabel {border: 2px;"
                                                    "border-image:url(%1/os_image/vmBackground_hover.png);border: 4px;}"
                                                    ).arg(str(self.__borderImagesPath))
                                            )


    def background_press(self):
        self.mouse_hove_label.setGeometry(0,0,self.width(),self.height())
        self.mouse_hove_label.setStyleSheet(QString(""))

    # ----------

    def update_style_function(self):
        self.__setitstyle()


    def __setitstyle(self):
        from common import common_run
        #add by pbc for init size -top
        self.setMinimumWidth(common_run.get_width(200.0))
        self.setMaximumWidth(common_run.get_width(200.0))
        self.setMinimumHeight(common_run.get_width(210.0))
        self.setMaximumHeight(common_run.get_width(210.0))
        self.verticalLayout.setContentsMargins(common_run.get_width(30)
                                               ,common_run.get_width(20)
                                               ,common_run.get_width(30)
                                               ,common_run.get_width(20))
        #add by pbc for init size -end

        __font_size = str(int(common_run.get_width(16.0)))

        self.setStyleSheet(
            QString("background-color: rgba(9,26,35,0);")
        )

        self.osName.setStyleSheet(
            QString("QLabel {border: 2px;"
                    "color:rgb(255,255,255);"
                    "font: 56 "+__font_size+"pt \"Ubuntu\";}")
        )

    #  ------------------ 私有成员 end-----------------------------


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

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