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

from AppActivItemShow import AppActivItemShow
from AppActivItemClose import AppActivItemClose
from logger.logrecord import LogRecord


class AppActivItem(QtGui.QLabel):
    """

    """
    exe2Icon={'calc.exe':'calc.png',
              'wordpad.exe':'wordpad.png',
              'notepad.exe':'notepad.png',
              'mspaint.exe':'mspaint.png',
              'wmplayer.exe':'wmplayer.png',
              'iexplore.exe':'iexplore.png',
              'EXCEL.EXE':'EXCEL.png',
              'AcroRd32.exe':'AcroRd32.png',
              'WINWORD.EXE':'WINWORD.png',
              'POWERPNT.EXE':'POWERPNT.png',
              'MSACCESS.EXE':'MSACCESS.png',
              'INFOPATH.EXE':'INFOPATH.png',
              'OIS.EXE':'OIS.png',
              'ONENOTE.EXE':'ONENOTE.png',
              'OUTLOOK.EXE':'OUTLOOK.png',
              'MSPUB.EXE':'MSPUB.png',
              'GROOVE.EXE':'GROOVE.png',
              'MSTORE.EXE':'MSTORE.png',
              'wps.exe':'wps.png',
              'wpp.exe':'wpp.png',
              'et.exe':'et.png',
              'chrome.exe':'chrome.png',
              'firefox.exe':'firefox.png',
              'HaoZip.exe':'HaoZip.png',
              'WinRAR.exe':'WinRAR.png',
              'StormPlayer.exe':'StormPlayer.png',
              '360se.exe':'360se.png',
              }
    tail2exe={
        '计算器'.decode('utf-8'):'calc.exe',
        '- Windows Internet Explorer'.decode('utf-8'):'iexplore.exe',
        '- 记事本'.decode('utf-8'):'notepad.exe',
        '- 写字板'.decode('utf-8'):'wordpad.exe',
        '- 画图'.decode('utf-8'):'mspaint.exe',
        'Windows Media Player'.decode('utf-8'):'wmplayer.exe',
        'Microsoft Excel -'.decode('utf-8'):'EXCEL.EXE',
        '- Adobe Reader'.decode('utf-8'):'AcroRd32.exe',
        'Adobe Reader'.decode('utf-8'):'AcroRd32.exe',
        '- Microsoft Word'.decode('utf-8'):'WINWORD.EXE',
        '- Microsoft PowerPoint'.decode('utf-8'):'POWERPNT.EXE',
        'Microsoft Access'.decode('utf-8'):'MSACCESS.EXE',
        'Microsoft InfoPath'.decode('utf-8'):'INFOPATH.EXE',
        'Microsoft Office 2010'.decode('utf-8'):'OIS.EXE',
        '- Microsoft OneNote'.decode('utf-8'):'ONENOTE.EXE',
        '- Microsoft Outlook'.decode('utf-8'):'OUTLOOK.EXE',
        'Microsoft Publisher'.decode('utf-8'):'MSPUB.EXE',
        'Microsoft SharePoint Workspace'.decode('utf-8'):'GROOVE.EXE',
        '- Microsoft 剪辑管理器'.decode('utf-8'):'MSTORE.EXE',
        '- WPS 文字'.decode('utf-8'):'wps.exe',
        '- WPS 演示'.decode('utf-8'):'wpp.exe',
        '- WPS 表格'.decode('utf-8'):'et.exe',
        '- Google Chrome'.decode('utf-8'):'chrome.exe',
        '- Mozilla Firefox'.decode('utf-8'):'firefox.exe',
        '- 2345好压'.decode('utf-8'):'HaoZip.exe',
        '- WinRAR'.decode('utf-8'):'WinRAR.exe',
        '暴风影音'.decode('utf-8'):'StormPlayer.exe',
        '(来源：暴高清)'.decode('utf-8'):'StormPlayer.exe',
        '- 360安全浏览器 7.1'.decode('utf-8'):'360se.exe',
    }
    def __init__(self,parent=None,id=0,text='',number=0):
        QtGui.QLabel.__init__(self,parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.width_=parent.geometry().width()
        self.id_=id
        self.height_=parent.stripHeight
        self.top_=number*self.height_
        # self.setText(text.decode('GBK'))
        # self.setFrameStyle(QtGui.QFrame.HLine)
        # self.setFrameShape(QtGui.QFrame.HLine)
        self.setObjectName('item')
        self.setStyleSheet("#item{background-color: rgba(255,255,255,43);}")
        self.setGeometry(0,self.top_+1,self.width_,self.height_-1)
        self.backLabel=QtGui.QLabel(self)
        self.backLabel.setGeometry(4,5,self.width_-8,self.height_-10)
        # self.backLabel.setObjectName('itemBack')
        self.backLabel.setStyleSheet("QLabel{background-color: rgba(255,255,255,0);}""QLabel:hover{background-color: rgba(255,255,255,56);}")
        iconName="default-app.png"
        appName=text.decode('GBK')
        self.name=appName
        for tail in AppActivItem.tail2exe.keys():
            if appName.endswith(tail):
                try:
                    iconName=AppActivItem.exe2Icon[AppActivItem.tail2exe[tail]]
                except Exception,ex:
                    # print ex
                    pass
            elif appName.startswith(tail):
                try:
                    iconName=AppActivItem.exe2Icon[AppActivItem.tail2exe[tail]]
                except Exception,ex:
                    # print ex
                    pass
        self.backLabel.nameButton=AppActivItemShow(self.backLabel,appName=text.decode('GBK'),iconName=iconName)
        # self.backLabel.nameButton.setText(text.decode('GBK'))
        self.backLabel.closeButton=AppActivItemClose(self.backLabel)
        self.alterSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('alterSignal'),parent,QtCore.SIGNAL('alterSignal'))
        self.connect(self.backLabel.nameButton,QtCore.SIGNAL('clicked()'),self.activate)
        self.connect(self.backLabel.closeButton,QtCore.SIGNAL('clicked()'),self.alterClose)

    @QtCore.pyqtSlot()
    def activate(self):
        print 'clicked activate item!'+self.id_
        LogRecord.instance().logger.debug("激活窗口："+self.name)
        self.emit(QtCore.SIGNAL('alterSignal'),self.id_,'activate')

    @QtCore.pyqtSlot()
    def alterClose(self):
        LogRecord.instance().logger.debug("关闭窗口："+self.name)
        self.emit(QtCore.SIGNAL('alterSignal'),self.id_,'close')

