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

from AppPannelBackground import AppPannelBackground
from AppFloat import AppFloat
from AppFloatLabel import AppFloatLabel
from logger.logrecord import LogRecord


class AppScrollPannel(QtGui.QScrollArea):
    def __init__(self,parent=None,appUser=None):
        QtGui.QScrollArea.__init__(self,parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setFrameStyle(QtGui.QFrame.NoFrame)
        self.vScrollBar=QtGui.QScrollBar(self)
        self.vScrollBar.setObjectName('appSBar')
        self.vScrollBar.setStyleSheet('#appSBar{background-color: rgba(255,255,255,43);width: 15px;}')
        self.setVerticalScrollBar(self.vScrollBar)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setGeometry(0,0,parent.geometry().width(),parent.geometry().height())
        # self.setStyleSheet('QScrollArea{border-image: url();}')
        self.setStyleSheet('QScrollArea{border-image: url();background:transparent;}')
        # self.setStyleSheet('QScrollArea{border-image: url();background-color: rgb(9,26,35);}')
#         self.setBackgroundRole(QtGui.QPalette.Dark)
#         self.setMaximumSize(parent.geometry().width(), parent.geometry().height())
#         AppPannel(self)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        # self.appPannel=AppPannel(self)
        # self.setWidget(self.appPannel)
        # self.appPannel.refreshApp()
        # self.setWidget(AppPannel(self))
        self.pannelBackground=AppPannelBackground(self,appUser)
        self.setWidget(self.pannelBackground)
        self.pannel=self.pannelBackground.pannel
        self.floatLeft=self.geometry().width()-64
        self.floatTop=20
        hasActivities=appUser.getHasActs()
        self.floatWnd=AppFloat(self,self.floatLeft,self.floatTop,hasActivities)
        self.floatLabel=AppFloatLabel(self)
        self.enableFloatSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('enableFloatSignal'),self.floatWnd.setActiv)
        self.floatSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('floatSignal'),self.floatLabel.showActivities)
        self.finishFetchSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('finishFetchSignal'),self.floatWnd.finishFetch)
        self.alterSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('alterSignal'),self.alterActivity)
        self.backInfoSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('backInfoSignal'),parent,QtCore.SIGNAL('backInfoSignal'))
        self.hideRDPSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('hideRDPSignal'),self.pannel,QtCore.SIGNAL('hideRDPSignal'))
        self.showRDPSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('showRDPSignal'),self.pannel,QtCore.SIGNAL('showRDPSignal'))

    # @QtCore.pyqtSlot()
    def floatMoved(self,x_move=0,y_move=0):
        self.floatLeft=x_move
        self.floatTop=y_move
        self.hideFloatLabel()

    # @QtCore.pyqtSlot()
    def showFloatLabel(self,activDic):
        self.emit(QtCore.SIGNAL('finishFetchSignal'))
        self.floatLabel.showActivities(activDic,self.floatLeft-300,self.floatTop+30)

    def hideFloatLabel(self):
        self.floatLabel.hideFloatLabel()

    def alterActivity(self,id,type):
        try:
            self.pannel.alterActivity(id,type)
        except Exception,err:
            LogRecord.instance().logger.exception("应用面板已经被删除！不能调用应用面板方法！")


    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            self.hideFloatLabel()
            event.accept()