#coding:utf-8
from PyQt4 import QtCore
from PyQt4 import QtGui
from os import getenv
class AppActivItemShow(QtGui.QPushButton):
    """

    """
    def __init__(self,parent=None,appName="",iconName="default-app.png"):
        QtGui.QPushButton.__init__(self,parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.imagePath=getenv("WIN_UI_IMAGES_PATH")
        self.__name=appName
        self.nameLabel=QtGui.QLabel(self)
        self.setGeometry(4,5,parent.geometry().width()-30,parent.geometry().height()-10)
        self.nameLabel.setGeometry(66,3,self.width()-66,48)
        self.nameLabel.setStyleSheet("QLabel{color:rgb(255,255,255);background:transparent;}")
        # self.nameLabel.setToolTip(appName)
        self.nameLabel.setText(self.dealWithName(appName))
        # font=QtGui.QFont()
        # font.setPointSize(14)
        # self.nameLabel.setFont(font)
        self.iconLabel=QtGui.QLabel(self)
        self.iconLabel.setGeometry(7,0,48,48)
        self.iconLabel.setStyleSheet("QLabel{border-image: url(%s/appImage/itemImage/%s);background:transparent;}"%(self.imagePath,iconName))

        # self.setStyleSheet("QPushButton{background-color: rgba(255,255,255,43);}""QPushButton:hover{background-color: rgba(255,255,255,56);}")
        # self.setStyleSheet("QPushButton{text-align : left;color:rgb(255,255,255)}")
        self.setFlat(True)
        self.nameLabel.installEventFilter(self)

    def dealWithName(self,appName):
        """
        限制应用窗口名字显示宽度
        :param appName:应该显示的应用的名字
        :return:实际显示的名字
        """
        name=''
        fm=QtGui.QFontMetrics(self.nameLabel.font())
        # print self.nameLabel.geometry()
        limitWidth=self.nameLabel.width()-20
        width=fm.width(appName)
        lenth=len(appName)
        # print 'appName: ' + str(fm.width(' ...'))
        if width > limitWidth:
            for i in range(15,lenth):
                name=appName[0:i]
                width=fm.width(name)
                if width > limitWidth:
                    name+=' ...'
                    break
        else:
            name=appName
        return name
    def eventFilter(self, obj, event):
        eve=QtCore.QEvent.HoverEnter
        if event.type() == eve:
            print eve
            QtGui.QToolTip.showText(QtGui.QCursor().pos(),self.__name.decode('utf-8'))
        return QtGui.QLabel.eventFilter(self,obj,event)
