#coding=utf-8
from PyQt4.QtGui import QWidget, QPushButton, QPainter, QPixmap, QDialog, QAction, QKeySequence
from PyQt4.QtCore import Qt, QPoint, QRect, SIGNAL, QEvent
from login import LoginDlg
from vmmainwindow import VMMainWindow
from common.messagebox import MessageBox
from common.infohintdialog import InfoHintDialog
from common.globalfun import moveWidgetToScreenCenter
from logwidget import LogWidget
import os

class MainWindow(QWidget):  
    def __init__(self,parent=None):  
        super(MainWindow,self).__init__(parent)
        
        self.setWindowTitle(u'安全云桌面')
        self.setWindowFlags(Qt.CustomizeWindowHint | Qt.FramelessWindowHint)

        self.initSubWidget()
        
        self.bandSignalSlot()
    
    def initSubWidget(self):
        """
            初始化子控件
        """
        self.loginDlg = LoginDlg(self)
        self.loginDlg.installEventFilter(self)
        
        self.vmWidget = VMMainWindow(self)
        self.vmWidget.setVisible(False)
        self.vmWidget.installEventFilter(self)
        
        self.shutDownBtn = QPushButton(self)
        self.shutDownBtn.setObjectName("shutDownBtn")
        self.shutDownBtn.setFixedSize(60,70)
        self.shutDownBtn.setToolTip(u"关机")
        
        self.rebootBtn = QPushButton(self)
        self.rebootBtn.setObjectName("rebootBtn")
        self.rebootBtn.setFixedSize(60,70)
        self.rebootBtn.setToolTip(u"重新启动")
        
        self.logWidget = LogWidget()
        self.logWidget.hide()
        self.actionShowLogWidget = QAction(self)
        self.actionHideLogWidget = QAction(self)
        self.actionShowLogWidget.setShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_B))
        self.actionHideLogWidget.setShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_C))
        self.addAction(self.actionShowLogWidget)
        self.addAction(self.actionHideLogWidget)
        self.connect(self.actionShowLogWidget,SIGNAL("triggered()"),self.showLogWidget)
        self.connect(self.actionHideLogWidget,SIGNAL("triggered()"),self.hideLogWidget)
        
        
    def showLogWidget(self):
        self.logWidget.show()
        self.logWidget.startThread()
        self.logWidget.move((self.width()-self.logWidget.width())/2,(self.height()-self.logWidget.height())/2)
    
    
    def hideLogWidget(self):
        self.logWidget.cancel()
        self.logWidget.close()
        

    def bandSignalSlot(self):
        self.connect(self.loginDlg, SIGNAL("authSuccess"),self.slotShowVMList)
        self.connect(self.vmWidget, SIGNAL("logout"),self.slotLogout)
        self.connect(self.rebootBtn, SIGNAL("clicked()"),self.slotReboot)
        self.connect(self.shutDownBtn, SIGNAL("clicked()"),self.slogShutdown)
        self.connect(self.loginDlg, SIGNAL("moveWidgetToScreenCenter"),self.adjustPosition)
        self.connect(self.vmWidget, SIGNAL("moveWidgetToScreenCenter"),self.adjustPosition)
        
    def adjustPosition(self):
        moveWidgetToScreenCenter(self.loginDlg, checkMulMonitor=False)
        moveWidgetToScreenCenter(self.vmWidget, checkMulMonitor=False)
        
    def paintEvent(self,event):
        self.shutDownBtn.move(QPoint(30,self.height() - 100))
        self.rebootBtn.move(QPoint(100,self.height() - 100))
        
        painter = QPainter(self)
        painter.save()
        pix = QPixmap("../images/background.png")
        painter.drawPixmap(QRect(0,0,self.width(),self.height()),pix)
        painter.restore()
        
    def checkAutoLogin(self):
        if self.loginDlg.checkHasAutoLoginUser():
            self.loginDlg.slotLogin()
        
    def slotShowVMList(self, vmBaseInfoList):
        print "success login"
        self.vmWidget.setVisible(True)
        self.loginDlg.setVisible(False)
        self.vmWidget.updateVMList(vmBaseInfoList)
        
    def slotLogout(self):
        self.vmWidget.setVisible(False)
        self.loginDlg.initCenterList()
        self.loginDlg.setVisible(True)
        self.loginDlg.resetWidgetInfo()
        
        
    def eventFilter(self, obj, event):
        if obj is self.vmWidget and event.type() == QEvent.HoverMove:
            self.vmWidget.showFlavorDetail(event.pos())
            
        return QWidget.eventFilter(self, obj, event);
  
        
    def slotReboot(self):
        if (MessageBox(u"确定要重新启动吗？").exec_() == QDialog.Accepted):
            if os.system("reboot") == 0:
                return
            else:
                InfoHintDialog(u"重启失败！").exec_()
        return
      
    def slogShutdown(self):
        if (MessageBox(u"确定要关机吗？").exec_() == QDialog.Accepted):
            if os.system('init 0') == 0:
                return
            else:
                InfoHintDialog(u"重启失败！").exec_()
                
        return 
            
        