#coding:utf-8
from PyQt4 import QtCore
from PyQt4 import QtGui
class AppName(QtGui.QLabel):
    """
    显示应用的名字
    """
    def __init__(self,parent=None,name_=''):
        QtGui.QLabel.__init__(self,parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.__name=name_
        font=QtGui.QFont()
        font.setPointSize(12)
        self.setFont(font)
        self.setStyleSheet('QLabel{border-image: url();color: rgb(255, 255, 255);background:transparent;}')
        self.setAlignment(QtCore.Qt.AlignCenter)
        self.setWordWrap(True)
        self.showName(name_.decode('utf-8'))
        # self.setToolTip(self.__name.decode('utf-8'))

    def showName(self,appName):
        name=''
        fm=QtGui.QFontMetrics(self.font())
        # print self.nameLabel.geometry()
        limitWidth=2*self.width()-20
        width=fm.width(appName)
        lenth=len(appName)
        # print 'appName: ' + str(fm.width(' ...'))
        if width > limitWidth:
            for i in range(1,lenth):
                name=appName[0:i]
                width=fm.width(name)
                if width > limitWidth:
                    name+=' ...'
                    break
        else:
            name=appName

        self.setText(name)
        self.installEventFilter(self)


    def eventFilter(self, obj, event):
        eve=QtCore.QEvent.HoverEnter
        if event.type() == eve:
            QtGui.QToolTip.showText(QtGui.QCursor().pos(),self.__name.decode('utf-8'))
        return QtGui.QLabel.eventFilter(self,obj,event)

    # def enterEvent(self, QEvent):
    #     self.setToolTip(self.__name.decode('utf-8'))