# coding:utf-8
from PyQt4 import QtCore
from PyQt4 import QtGui
# from urwid.signals import emit_signal
import time
from AppObject import AppObject
from AppIcon import AppIcon
from AppName import AppName
from os import getenv

from logger.logrecord import LogRecord
class AppLabel(QtGui.QLabel):
    """
    展示应用的面板
    """
    beclicked=QtCore.pyqtSignal()
    def __init__(self,parent=None,appObj=None,left=0,top=0):
        QtGui.QLabel.__init__(self,parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.imagePath=getenv("WIN_UI_IMAGES_PATH")
        self.lastClicked=0
        # self.setGeometry(left,top,164,167)
#         sideLength=parent.geometry().width()//5
#         self.setGeometry(0,0,sideLength,sideLength) 
#         self.setGeometry((parent.geometry().width()-sideLength)//2,(parent.geometry().height()-sideLength)//2,sideLength,sideLength) 
#         self.pannel=QtGui.QLabel(self)
#         self.pannel.setGeometry(self.geometry())
#         self.pannel.setStyleSheet('QLabel{border-image: url();background-color:blue;}')
#         self.setText(text)
#         self.setStyleSheet('''QLabel{border-image: url(images/appFrame1.png);}''')
        if appObj == None:
            # print '应用对象为空'.decode('utf-8')
            LogRecord.instance().logger.debug('应用对象为空!')
            self.setText('应用对象为空'.decode('utf-8'))
        elif isinstance(appObj, AppObject):
            # self.gridLayout=QtGui.QGridLayout(self)
            # self.gridLayout.setSpacing(0)
            # self.gridLayout.setContentsMargins(20,20,20,20)
            iconName=appObj.getAppIcon()
            # print iconName
            if iconName != '':
                iconLabel=AppIcon(self,iconName)
            else:
                LogRecord.instance().logger.debug('应用图标为空!')
                iconLabel=AppIcon(self)
            iconLabel.setGeometry(50,33,64,64)
#             iconLabel.setFrameStyle(QtGui.QFrame.Panel+QtGui.QFrame.Sunken) 
#             iconLabel.setMinimumSize(48, 48)
#             iconLabel.setAlignment(QtCore.Qt.AlignCenter)
            # self.gridLayout.addWidget(iconLabel,0,1,6,6)
            nameLabel=AppName(self,appObj.getAppName())
            nameLabel.setGeometry(15,107,134,40)
#             nameLabel.setFrameStyle(QtGui.QFrame.Panel+QtGui.QFrame.Sunken) 
#             nameLabel.setMinimumSize(64, 16)
            nameLabel.setAlignment(QtCore.Qt.AlignCenter)
#             self.gridLayout.addWidget(nameLabel,6,0,2,8)
            


        else:
            LogRecord.instance().logger.debug(str(type(appObj)))
#         self.setStyleSheet('QLabel{border-image: url();background-color:bisque;}')
#         self.setStyleSheet('QLabel{border-image: url();}')
        #self.setStyleSheet('''QLabel:hover{border-image: url(images/appFrame1.png);}''')
        self.setStyleSheet('QLabel{border-image: url();}'+
                           'QLabel:hover{border-image: url(%s/appImage/appFrame1.png);}'%self.imagePath)
        self.beclicked.connect(lambda: parent.startApp(appObj.getAppPosition()))
#         self.connect(self, QtCore.SIGNAL('clicked()'),parent,parent.startApp(0))
    def mousePressEvent(self, QMouseEvent):
        if QMouseEvent.button() == QtCore.Qt.LeftButton:
            self.setStyleSheet('QLabel{border-image: url(%s/appImage/appFrame2.png);}'%self.imagePath)
    def mouseReleaseEvent(self, QMouseEvent):
        if QMouseEvent.button() == QtCore.Qt.LeftButton:
            if time.time() - self.lastClicked > 0.5:
                self.emit(QtCore.SIGNAL("beclicked()"))
                self.lastClicked=time.time()
                self.setStyleSheet('QLabel{border-image: url();}'+
                            'QLabel:hover{border-image: url(%s/appImage/appFrame1.png);}'%self.imagePath)

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


    def leaveEvent(self, QEvent):
        self.setCursor(QtCore.Qt.ArrowCursor)        #鼠标指针 变为箭头
    # def mouseReleaseEvent(self, *args, **kwargs):
    #     if time.clock() - self.lastClicked > 0.5:
    #         self.emit(QtCore.SIGNAL("beclicked()"))
    #         self.lastClicked=time.clock()
    #     self.setStyleSheet('QLabel{border-image: url();}'+
    #                        'QLabel:hover{border-image: url(%s/appImage/appFrame1.png);}'%self.imagePath)
    #     return QtGui.QLabel.mouseReleaseEvent(self, *args, **kwargs)