#coding:utf-8
from PyQt4 import QtGui
from PyQt4 import QtCore
from os import getenv

class AppFloat(QtGui.QLabel):
    """
    悬浮窗界面
    """
    beclicked=QtCore.pyqtSignal()
    def __init__(self,parent=None,left=0,top=0,hasActivities=False):
        QtGui.QLabel.__init__(self,parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.imagePath=getenv("WIN_UI_IMAGES_PATH")
        self.hasActivities=hasActivities
        self.fetchingActivities=False
        self.parentWidth=parent.geometry().width()
        self.parentHeight=parent.geometry().height()
        self.__width=43
        self.__height=44
        self.setGeometry(left,top,self.__width,self.__height)
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        if self.hasActivities:
            self.setStyleSheet("QLabel{border-image: url(%s/appImage/float1.png);background:transparent;}""QLabel:hover{border-image: url(%s/appImage/float2.png);background:transparent;}"%(self.imagePath,self.imagePath))
        else:
            self.setStyleSheet("QLabel{border-image: url(%s/appImage/float1.png);background:transparent;}"%self.imagePath)

        # self.setStyleSheet("QLabel{border-image: url(%s/appImage/float1.png);background:transparent;}"%self.imagePath)
        self.isMove=False
        self.beclicked.connect(lambda: parent.pannel.fetchActivities(caller='float'))
        self.moveSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('moveSignal'),parent.floatMoved)


    def setActiv(self,hasActivities):
        self.hasActivities=hasActivities
        if self.hasActivities:
            self.setStyleSheet("QLabel{border-image: url(%s/appImage/float1.png);background:transparent;}""QLabel:hover{border-image: url(%s/appImage/float2.png);background:transparent;}"%(self.imagePath,self.imagePath))
        else:
            self.setStyleSheet("QLabel{border-image: url(%s/appImage/float1.png);background:transparent;}"%self.imagePath)

    def finishFetch(self):
        self.fetchingActivities=False
        self.setCursor(QtCore.Qt.PointingHandCursor)

    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton and self.hasActivities:
            position=event.globalPos() - self.dragPosition
            if position.x() < 0:
                position.setX(0)
            if position.x() > self.parentWidth-self.__width:
                position.setX(self.parentWidth-self.__width)
            if position.y() < 0:
                position.setY(0)
            if position.y() > self.parentHeight-self.__height:
                position.setY(self.parentHeight-self.__height)
            self.move(position)
            self.isMove=True
            event.accept()
    def mouseReleaseEvent(self, *args, **kwargs):
        """
        响应鼠标点击事件，屏蔽移动时的点击事件
        :param args:
        :param kwargs:
        :return:
        """
        if self.isMove:
            self.isMove=False
            g=self.geometry()
            self.emit(QtCore.SIGNAL('moveSignal'),g.x(),g.y())
            return QtGui.QLabel.mouseReleaseEvent(self, *args, **kwargs)
        if self.hasActivities:
            self.emit(QtCore.SIGNAL("beclicked()"))
            self.setCursor(QtCore.Qt.WaitCursor)
            self.fetchingActivities=True
        return QtGui.QLabel.mouseReleaseEvent(self, *args, **kwargs)

    def enterEvent(self, QEvent):
        if self.fetchingActivities:
            self.setCursor(QtCore.Qt.WaitCursor)
        elif self.hasActivities:
            self.setCursor(QtCore.Qt.PointingHandCursor) # 鼠标指针 变为小手


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