#coding:utf-8

from PyQt4.QtCore import QString
from PyQt4.QtCore import Qt, SIGNAL, QRect
from PyQt4.QtGui import QDialog
from PyQt4.QtGui import QPainter
from PyQt4.QtGui import QPixmap
from PyQt4.QtGui import QVBoxLayout

from base.infohintdialog import InfoHintDialog
from setting.centresetting import CentreSetting
from setting.dominmapsetting import DominMapSetting
from setting.getnetinfo import NetCtl
import setting.globalfunc
from setting.loginsetting import LoginSetting
from setting.systempropertysetting import SystemSetting
from setting.tabtip import TabWidget
from setting.terminalstylesetting import TerminalStyleSetting
from setting.usbtransersetting import UsbTranserSetting
from setting.waitdlg import WaitingWidget
from setting.wirelessnetworksetting import WirelessNetworkSetting
from setting.wirenetworksetting import WireNetworkSetting


class SettingWidget(QDialog):
       
    def __init__(self,parent = None):
        super(SettingWidget,self).__init__(parent)
        self.setFixedSize(670,600)
        self.setWindowTitle(u'设置平台')
    
        self.netCtl = NetCtl()
        self.waitDlg = WaitingWidget()
        self.infoHint = InfoHintDialog()
        
        self.setWindowFlags(Qt.FramelessWindowHint|Qt.Dialog)
        self.tabWidget = TabWidget()
        
        self.tabWidget.centreSetting = CentreSetting()
        self.tabWidget.dominMapSetting = DominMapSetting()
        self.tabWidget.loginSetting = LoginSetting()
        self.tabWidget.systemSetting = SystemSetting()
        self.tabWidget.terminalStyleSetting = TerminalStyleSetting()
        self.tabWidget.usbTranserSetting = UsbTranserSetting()
        self.tabWidget.wirelessNetworkSetting = WirelessNetworkSetting()
        self.tabWidget.wireNetworkSetting = WireNetworkSetting()
        
        self.tabWidget.addTab(self.tabWidget.centreSetting, u"平台设置")
        self.tabWidget.addTab(self.tabWidget.wireNetworkSetting, u"网络配置")
        self.tabWidget.addTab(self.tabWidget.systemSetting, u"个性化设置")
        self.tabWidget.addTab(self.tabWidget.dominMapSetting, u"域名映射")
        self.tabWidget.addTab(self.tabWidget.usbTranserSetting, u"USB透传")

        self.connect(self.tabWidget.centreSetting, SIGNAL("closetabwidget"),self.close)
        self.connect(self.tabWidget.dominMapSetting, SIGNAL("closetabwidget"),self.close)
        self.connect(self.tabWidget.loginSetting, SIGNAL("closetabwidget"),self.close)
        self.connect(self.tabWidget.systemSetting, SIGNAL("closetabwidget"),self.close)
        self.connect(self.tabWidget.systemSetting, SIGNAL("setscreenover"),self.resetScreen)
        self.connect(self.tabWidget.usbTranserSetting, SIGNAL("closetabwidget"),self.close)
        self.connect(self.tabWidget.wirelessNetworkSetting, SIGNAL("closetabwidget"),self.close)
        self.connect(self.tabWidget.wireNetworkSetting, SIGNAL("closetabwidget"),self.close)
        
        self.connect(self.tabWidget.wireNetworkSetting, SIGNAL("refresh_center_net"),self.refreshCenterNet)
        self.connect(self.tabWidget.centreSetting, SIGNAL("centerRestartNetworkSignal"),self.showWireRestartNetworkInfo)
        self.connect(self.tabWidget.wireNetworkSetting, SIGNAL("wireRestartNetworkSignal"),self.showCenterRestartNetworkInfo)
        
        self.connect(self.tabWidget.centreSetting, SIGNAL("updatedomain"),self.updateDomain)
        self.connect(self.tabWidget.centreSetting, SIGNAL("updatecenter"),self.updateCenter)
        self.connect(self.tabWidget.centreSetting, SIGNAL("editupdatecenter"),self.updateHostCenter)
        self.connect(self.tabWidget.centreSetting, SIGNAL("registerterminal"),self.registerTerminal)
        
        self.mainLayout = QVBoxLayout()
        self.mainLayout.addSpacing(33)
        self.mainLayout.addWidget(self.tabWidget)
        self.mainLayout.setMargin(0)
        self.setLayout(self.mainLayout)
        self.mousePressed = False
    def resetScreen(self):
        self.emit(SIGNAL("setscreenover"))
    def slotSetDesktopNet(self):
        self.tabWidget.wireNetworkSetting.refreshNetCard()
        
    def slotSetSystemNet(self):
        
        self.tabWidget.wireNetworkSetting.refreshNetCard()
        
    def slotSetTerminalNet(self):
        
        if self.tabWidget.wireNetworkSetting.netCardComboBox.count()>=2:
            return
        self.tabWidget.wireNetworkSetting.netCardComboBox.addItem("breth0")
    def registerTerminal(self,registerid):
        self.emit(SIGNAL("registerterminal"),registerid)    
    def updateHostCenter(self):
        self.tabWidget.dominMapSetting.freshHostCenterList()
        self.emit(SIGNAL("editupdatecenter"),"true")
    def updateDomain(self):
        print ("updateDomain")
        self.emit(SIGNAL("updatedomain"))
    def updateCenter(self,flag):
        self.tabWidget.dominMapSetting.freshHostCenterList()
        self.tabWidget.dominMapSetting.autoUpdateDomain()
        self.emit(SIGNAL("updatecenter"),flag)

    def showWireRestartNetworkInfo(self,result):
        if (result == 0):
            self.waitDlg.close()
            #得到当前的网络信息
            curNetInfo = self.netCtl.getCurrentNetworkInfo()
            #读取网络地址信息
            netAddressList = self.netCtl.getNetAddressList()
            num = len(netAddressList)
            if( 0 == num ):
                return
            #刷新表格和平台网络中的下拉列表内容
            for i in range(num):
                self.tabWidget.wireNetworkSetting.settwIPListRowData(i, netAddressList[i], curNetInfo)
                
            self.infoHint.setHintInfo(u"网络启动成功!")
            self.infoHint.show()
            self.infoHint.move(setting.globalfunc.moveToCenter(self.infoHint.width(),self.infoHint.height()))
        elif (result == 1000):
            self.waitDlg.setTip(u"重启网络中...")
            self.waitDlg.show()
            self.waitDlg.move(setting.globalfunc.moveToCenter(self.waitDlg.width(), self.waitDlg.height()))
            return
        elif (result == 1):
            return
        elif (result == -1):
            self.waitDlg.close()
            self.infoHint.setHintInfo(u"网络启动失败!")
            self.infoHint.show()
            self.infoHint.move(setting.globalfunc.moveToCenter(self.infoHint.width(),self.infoHint.height()))
            return
        
    def refreshSetingWidget(self):
        self.tabWidget.centreSetting.refreshAutoNet()
        self.tabWidget.centreSetting.refreshComboNet()
        self.tabWidget.centreSetting.refreshNetworkTable()
        self.tabWidget.wireNetworkSetting.refreshNetCard()
        self.tabWidget.wireNetworkSetting.refreshNetWidget()
        self.tabWidget.wirelessNetworkSetting.refreshWidget()
        self.tabWidget.loginSetting.refreshWidget()
        self.tabWidget.systemSetting.refreshWidget()
        self.tabWidget.dominMapSetting.refreshTable()
        self.tabWidget.dominMapSetting.freshHostCenterList()
        self.tabWidget.usbTranserSetting.refreshWidget()
        self.tabWidget.terminalStyleSetting.initTerminalVM()
    def setAllFocus(self):
        self.tabWidget.centreSetting.lineEditName.setFocus()
        self.tabWidget.wireNetworkSetting.lineEditNetwork.setFocus()
        self.tabWidget.wirelessNetworkSetting.lineEditUserName.setFocus()
        self.tabWidget.loginSetting.lineEditUserName.setFocus()
        self.tabWidget.systemSetting.ratioConnect.setFocus()
        self.tabWidget.dominMapSetting.lineEditDomain.setFocus()
        self.tabWidget.usbTranserSetting.checkUsb.setFocus()
        self.tabWidget.terminalStyleSetting.desktopVare.setFocus()
    def showCenterRestartNetworkInfo(self,result):
        if (result == 0):
            self.emit(SIGNAL("updatecenter"),"true")
            self.emit(SIGNAL("updatedomain"))
            self.tabWidget.centreSetting.refreshComboNet()
            
    def refreshCenterNet(self,num,netAddressList):
        self.tabWidget.centreSetting.refreshComboNet()
        
    def cancel(self):   
        self.accept()
        
    def mouseMoveEvent(self,event):
        if self.mousePressed:
            self.move(self.pos() + event.pos() - self.currentPos)   
        self.default = QString()
    def mousePressEvent(self,event):
        if event.buttons() == Qt.LeftButton:
            self.currentPos = event.pos()
            self.mousePressed = True
    def mouseReleaseEvent(self,event):
        if event.buttons() == Qt.LeftButton:
            self.mousePressed = False
    def paintEvent(self,event):
        painter = QPainter(self)
       
        


        painter.save()
        pix = QPixmap("../images/settop.png")
        painter.drawPixmap(QRect(0,0,800,33),pix)
        painter.restore()
