# coding:utf-8
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import Qt
from AppToolLabel import AppToolLabel
from os import getenv
from logger.logrecord import LogRecord
class AppTool(QtGui.QX11EmbedWidget):
    """
    工具栏主窗口
    """
    def __init__(self,parent=None,rdpWidth_=1024,pannel_=None):
        QtGui.QX11EmbedWidget.__init__(self,parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.imagePath=getenv("WIN_UI_IMAGES_PATH")
        self.pannel=pannel_
        self.isDown=False
        self.rdpWidth=rdpWidth_        
        self.wndWidth=172
        self.wndHeight=70
        self.left=(self.rdpWidth-self.wndWidth)//2
        self.setWindowFlags(QtCore.Qt.Dialog and QtCore.Qt.CustomizeWindowHint and QtCore.Qt.FramelessWindowHint)
        # self.setAttribute(QtCore.Qt.WA_NoSystemBackground,False)
        # self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
        # self.setStyleSheet("QWidget{background:transparent;}")
        # pal=QtGui.QPalette()
        # pal.setColor(QtGui.QPalette.Background, QtGui.QColor(0,0,0,0))
        # self.setPalette(pal)
        # self.setWindowOpacity(0.5)
        self.setGeometry(self.left,0,self.wndWidth,self.wndHeight)
        # rect=QtCore.QRectF(0,0,self.wndWidth,self.wndHeight)
        # path=QtGui.QPainterPath()
        # path.addRoundRect(rect,50)
        # polygon= path.toFillPolygon().toPolygon()
        # region=QtGui.QRegion(polygon)
        # self.setMask(region)
        self.setObjectName('tool')
        self.setStyleSheet("#tool{background:black;}")
        self.maskImage=QtGui.QPixmap("%s/appImage/tool_mask.png"%self.imagePath)
        # self.palette().setBrush(self.backgroundRole(),QtGui.QBrush(self.maskImage))
        self.setMask(self.maskImage.createMaskFromColor(QtCore.Qt.white))
        self.toolLabel=AppToolLabel(self)
        self.hide()
        self.connect(self.toolLabel.toolActivites,QtCore.SIGNAL('clicked()'),self,QtCore.SLOT('fetchActivities()'))
        self.connect(self.toolLabel.toolShow,QtCore.SIGNAL('clicked()'),self,QtCore.SLOT('showClicked()'))
        self.connect(self.toolLabel.toolHome,QtCore.SIGNAL('clicked()'),self,QtCore.SLOT('toHome()'))
        self.showSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('showSignal()'),self,QtCore.SLOT('showSlot()'))

    def setParentWnd(self,parentWnd=0):
        try:
            LogRecord.instance().logger.debug("RDP窗口为："+str(parentWnd))
            self.embedInto(parentWnd)
        except Exception,err:
            LogRecord.instance().logger.exception("挂载工具栏失败！")

    def enterBottom(self):
        if not self.isDown:
           self.showDown()
    @QtCore.pyqtSlot()
    def showDown(self):
        self.isDown=True
        self.move(self.left,0)
        self.emit(QtCore.SIGNAL('showSignal()'))
        # self.show()

    @QtCore.pyqtSlot()
    def showUp(self):
        self.isDown=False
        self.move(self.left,-self.wndHeight+19)
        self.emit(QtCore.SIGNAL('showSignal()'))
        # self.show()

    @QtCore.pyqtSlot()
    def showClicked(self):
        if self.isDown:
            self.showUp()
        else:
            self.showDown()

    @QtCore.pyqtSlot()
    def showSlot(self):
        self.show()
    @QtCore.pyqtSlot()
    def toHome(self):
        try:
            self.pannel.backHome()
        except Exception,err:
            LogRecord.instance().logger.exception("返回主界面异常！")

    @QtCore.pyqtSlot()
    def fetchActivities(self):
        try:
            self.pannel.fetchActivities('tool')
        except Exception,err:
            LogRecord.instance().logger.exception("应用面板已经被删除！不能调用应用面板方法！")


    # def paintEvent(self, *args, **kwargs):
    #     painter=QtGui.QPainter(self)
    #     painter.setCompositionMode(QtGui.QPainter.CompositionMode_Clear)
    #     painter.fillRect(0,0,self.wndWidth,self.wndHeight,QtCore.Qt.SolidPattern )
    #     print 'paint transparent!'
    #     return QtGui.QX11EmbedWidget.paintEvent(self, *args, **kwargs)
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    appTool=AppTool()
    appTool.show()
#     notify.showMessage('通知')  
    sys.exit(app.exec_())   