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

from logger.logrecord import LogRecord

class WaitingDialog(QtGui.QDialog):
    def __init__(self,movieGifPaht = None,parent=None):
        QtGui.QDialog.__init__(self,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 | QtCore.Qt.FramelessWindowHint )
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        if movieGifPaht == None:
            self.__imagePath=os.getenv('WIN_UI_IMAGES_PATH')
        else:
            self.__imagePath = movieGifPaht
        self.backLabel=QtGui.QLabel(self)
        self.waitingLabel=QtGui.QLabel(self.backLabel)
        self.gifLabel=QtGui.QLabel(self.waitingLabel)
        self.movie=QtGui.QMovie("%s/waiting.gif"%self.__imagePath)
        self.msgLabel=QtGui.QLabel(self.waitingLabel)

        self.connect(self,QtCore.SIGNAL('showMsgSignal'),self.msgLabel.setText)

    def __getResolution(self):
        cmd = "xrandr | grep \"^  .*x.*\*\" | sed  \"s/^\ \ \ //g\" | sed \"s/\ .*$//\""
        try:
            f = os.popen(cmd, "r")
            Resolution = f.readline().strip('\n')  # 去掉\n
            f.close()
            width,height=Resolution.split('x',1)
            self.__width=int(width)
            self.__height=int(height)
            return
        except Exception:
            desktop=QtGui.QApplication.desktop()
            self.__width=desktop.width()
            self.__height=desktop.height()
            return


    def refresh(self):
        # self.desktop=QtGui.QApplication.desktop()
        # self.__width=self.desktop.width()
        # self.__height=self.desktop.height()
        self.__getResolution()
        LogRecord.instance().logger.debug(str(self.__width)+','+str(self.__height))
        self.__waitWidth=self.__height//6
        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*1//4)
        fontsize=(self.__width*10/1366)
        self.msgLabel.setStyleSheet(
            "QLabel{background:transparent;color:rgba(255,255,255,255);"+
                    "font: %dpt \"Ubuntu\";"%fontsize+
            "}")
        self.msgLabel.setAlignment(QtCore.Qt.AlignHCenter)
        self.msgLabel.setWordWrap(True)

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

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

    def showMsg(self,message):
        """
        显示等待信息
        :param message:等待信息
        :return:
        """
        self.emit(QtCore.SIGNAL('showMsgSignal'),message)
        # self.msgLabel.setText(message)

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

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

    # 软件 升级 显示  临时
    def showFullScreen_solfWareUpdate(self):
        self.refresh()
        self.msgLabel.setStyleSheet(
            "QLabel{background:transparent;color:rgba(255,255,255,255);"
            "font: 12pt \"Ubuntu\";"
            "}")
        super(WaitingDialog,self).showFullScreen()
        self.movie.start()

    def show(self):
        self.refresh()
        super(WaitingDialog,self).show()
        self.movie.start()

    def exec_(self):
        self.refresh()
        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())  # 支持中文
    waiting=WaitingDialog()
    # waiting.showMsg(QString("开机中...\n f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f"))
    waiting.showMsg(QString("虚拟机开机虚拟机开机虚拟机开机虚拟机开机虚拟机开机"))
    waiting.showFullScreen()

    # waiting.close()
    # waiting.exec_()

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

