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

class WaitingLabel(QtGui.QLabel):
    def __init__(self,movieGifPaht = None,parent=None):
        QtGui.QLabel.__init__(self,parent)
        # super(WaitingLabel,self).__init__(parent)
        # self.setModal(True)
        # self.setAttribute(QtCore.Qt.WA_DeleteOnClose)  # 关闭释放内存
        # self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
        # self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.WindowStaysOnTopHint)

        if movieGifPaht == None:
            self.__imagePath=os.getenv('WIN_UI_IMAGES_PATH')
        else:
            self.__imagePath = movieGifPaht

        self.setStyleSheet(
            "QLabel{background-color:rgba(0,0,0,100);}")

        self.refresh(parent)


    def refresh(self,parent):

        if parent != None:
            self.__width    = parent.width()
            self.__height   = parent.height()
            self.setGeometry(0,0,self.__width,self.__height)
        else:
            self.desktop = QtGui.QApplication.desktop()
            self.__width    = self.desktop.width()
            self.__height   = self.desktop.height()

        self.__waitWidth=self.__height//3
        self.__waitHeight=self.__waitWidth
        self.backLabel=QtGui.QLabel(self)
        self.backLabel.setGeometry(0,0,self.__width,self.__height)
        self.backLabel.setStyleSheet("QLabel{background-color:rgba(0,0,0,100);}")
        self.waitingLabel=QtGui.QLabel(self.backLabel)
        self.waitingLabel.setGeometry((self.__width-self.__waitWidth)//2,
                                      (self.__height-self.__waitHeight)//2,
                                      self.__waitWidth,self.__waitHeight)
        self.gifLabel=QtGui.QLabel(self.waitingLabel)
        self.gifLabel.setGeometry(self.__waitWidth//4,
                                  self.__waitHeight//8,
                                  self.__waitWidth*1//2,
                                  self.__waitHeight*1//2)
        self.gifLabel.setScaledContents(True)
        self.gifLabel.setStyleSheet("QLabel{background:transparent;}")
        self.gifLabel.setPixmap(QtGui.QPixmap("%s/waiting.gif"%self.__imagePath))
        self.movie=QtGui.QMovie("%s/waiting.gif"%self.__imagePath)
        # self.movie.start()
        self.connect(self.movie,QtCore.SIGNAL("frameChanged(int)"),self.iconChanged)
        self.msgLabel=QtGui.QLabel(self.waitingLabel)

        self.msgLabel.setGeometry(0,
                                  self.__waitHeight*3//4,
                                  self.__waitWidth,
                                  self.__waitHeight//4)
        self.msgLabel.setStyleSheet(
            "QLabel{background:transparent;color:rgba(255,255,255,255);"
                    "font: 63 16pt \"Ubuntu\";"
            "}")
        self.msgLabel.setAlignment(QtCore.Qt.AlignCenter)

    def keyPressEvent(self, QKeyEvent):
        """
        屏蔽键盘事件
        :param QKeyEvent:
        :return:
        """
        pass

    def iconChanged(self,index):
        """
        用于显示gif动画
        :param index:
        :return:
        """
        self.gifLabel.setPixmap(self.movie.currentPixmap())

    def showMsg(self,message):
        """
        显示等待信息
        :param message:等待信息
        :return:
        """
        # self.lower()
        self.msgLabel.raise_()
        self.msgLabel.setText(message)


    def lower(self):
        self.movie.stop()
        self.hide()
        super(WaitingLabel,self).lower()

    def raise_(self):
        self.show()
        self.movie.start()
        super(WaitingLabel,self).raise_()

    # def close(self):
    #     self.movie.stop()
    #     super(WaitingDialog,self).close()

    # def showFullScreen(self):
    #     super(WaitingDialog,self).showFullScreen()
    #     self.movie.start()

    # def exec_(self):
    #     self.movie.start()
    #     super(WaitingDialog,self).exec_()

    # def __del__(self):
    #     super(WaitingDialog,self).__del__()
    #     #self.movie.stop()
    #     # del self.movie

if __name__ == "__main__":
    import sys
    os.environ["WIN_UI_IMAGES_PATH"]="../../images"
    app = QtGui.QApplication(sys.argv)
    from PyQt4.QtCore import  QTextCodec,QString
    QTextCodec.setCodecForCStrings(QTextCodec.codecForLocale())  # 支持中文
    from PyQt4.QtGui import QWidget
    win = QWidget()
    win.resize(280,300)
    waiting = WaitingLabel(None,win)
    waiting.showMsg(QString("开机中..."))
    waiting.raise_()
    win.show()

    print "xxxx"
    sys.exit(app.exec_())

