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

from AppScrollPannel import AppScrollPannel
from logger.logrecord import LogRecord


class AppPannelWidget(QtGui.QWidget):
    """
    展示应用的面板
    """
    def __init__(self,parent=None,appUser=None):
        QtGui.QWidget.__init__(self,parent)
        self.user=appUser
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.scrollPannel=None
        self.desktop=QtGui.QApplication.desktop().screenGeometry()
        self.refresh()
        #响应修改分辨率
        self.timer=QtCore.QTimer(self)
        self.connect(self.timer,QtCore.SIGNAL('timeout()'),self.checkResolution)
        self.timer.start(1000)

        self.backInfoSignal=QtCore.pyqtSignal()
        self.connect(self,QtCore.SIGNAL('backInfoSignal'),self.send_appbackInfo)

        self.hideRDPSignal=QtCore.pyqtSignal()
        self.showRDPSignal=QtCore.pyqtSignal()

    def checkResolution(self):
        """
        响应修改分辨率
        :return:
        """
        if self.desktop != QtGui.QApplication.desktop().screenGeometry():
            self.desktop = QtGui.QApplication.desktop().screenGeometry()
            LogRecord.instance().logger.debug('修改分辨率:%d,%d'%(self.desktop.width(),self.desktop.height()))
            self.refresh()

    # 根据分辨率　调整窗口大小
    def refresh(self):
        if self.scrollPannel != None:#响应修改分辨率
            self.disconnect(self,QtCore.SIGNAL('hideRDPSignal'),self.scrollPannel,QtCore.SIGNAL('hideRDPSignal'))
            self.disconnect(self,QtCore.SIGNAL('showRDPSignal'),self.scrollPannel,QtCore.SIGNAL('showRDPSignal'))
            self.scrollPannel.close()
            if (self.user.getRdpState() == 'connected') or (self.user.getRdpState() == 'connecting'):
                self.user.setRdpState('disconnected')

        self.setGeometry(0,0,self.desktop.width(),self.desktop.height()-76) # 设置大小
        if self.user != None:
            LogRecord.instance().logger.debug('加载应用面板，用户名：%s'%self.user.getName())
        self.scrollPannel = AppScrollPannel(self,self.user)
        self.connect(self,QtCore.SIGNAL('hideRDPSignal'),self.scrollPannel,QtCore.SIGNAL('hideRDPSignal'))
        self.connect(self,QtCore.SIGNAL('showRDPSignal'),self.scrollPannel,QtCore.SIGNAL('showRDPSignal'))
        self.scrollPannel.show()

    def send_appbackInfo(self,code,description):
        self.emit(QtCore.SIGNAL('appbackInfo'),code,description)
        if code == '5':
            self.refresh()