#coding:utf-8
'''
Created on Sep 3, 2014
@author: root
'''

from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QCheckBox
from PyQt4.QtGui import QGroupBox
from PyQt4.QtGui import QPushButton
from PyQt4.QtGui import QWidget, QLabel, QHBoxLayout, QVBoxLayout

from base.infohintdialog import InfoHintDialog
from setting.logrecord import LogRecord
from setting.usbset import UsbSet


class UsbTranserSetting(QWidget):
       
    def __init__(self,parent = None):
        super(UsbTranserSetting,self).__init__(parent)
       
        self.usbSet = UsbSet()
        
        usbGroupBox = QGroupBox()
        usbGroupBox.setTitle(u"USB设备透传")
        self.checkUsb = QCheckBox(u"透传已经接入USB设备")
        
        self.tipLabel = QLabel()
        self.tipLabel.setText(u"说明：默认情况下透传已经接入USB设备")
        vLayout = QVBoxLayout()
        vLayout.setMargin(20)
        vLayout.addWidget(self.checkUsb)
        vLayout.addWidget(self.tipLabel)
        myVLayout = QVBoxLayout()
        myVLayout.addSpacing(30)
        myVLayout.addLayout(vLayout)
        myVLayout.addSpacing(30)
        usbGroupBox.setLayout(myVLayout)
        
        self.closeButton = QPushButton(u"关闭")
        self.closeButton.setObjectName("settingDlgBtn")
        self.closeButton.setFixedSize(75,27)
        self.closeButton.setFlat(True)
        closeButtonLayout = QHBoxLayout()
        closeButtonLayout.addStretch()
        closeButtonLayout.addWidget(self.closeButton)
        
        self.mainLayout = QVBoxLayout(self)
        self.mainLayout.setMargin(100)
        self.mainLayout.addWidget(usbGroupBox)
        self.mainLayout.addSpacing(100)
        self.mainLayout.addLayout(closeButtonLayout)
        
        self.refreshWidget()
        
        self.connect(self.closeButton, SIGNAL("clicked()"),self.closeTabWidget)
        self.connect(self.checkUsb, SIGNAL("clicked()"),self.on_checkBox_UsbSet_clicked)

        
    def refreshWidget(self):
        LogRecord.instance().logger.info(u"初始化设置")
        usbset = self.usbSet.getUsbSet()
        if("forbusb" == usbset):
            self.checkUsb.setChecked(False)
        elif("autousb" == usbset):
            self.checkUsb.setChecked(True)
        else:
            #将其设置为默认的禁止
            policy = "autousb"
            self.usbSet.setUsb(policy)
            self.checkUsb.setChecked(True)
    
    def on_checkBox_UsbSet_clicked(self):
        LogRecord.instance().logger.info(u"当按钮被点击")
        #允许透传已接入usb设备
        if(self.checkUsb.isChecked()):
            #选择允许透传已接入设备
            content = "autousb"
            LogRecord.instance().logger.info(u"设置文件")
            res = self.usbSet.setUsb(content)
            if (0 == res):
                
                InfoHintDialog(u"已成功设置为允许自动透传已接入USB设备").exec_()
            else:
               
                InfoHintDialog(u"设置失败").exec_()
        else:
            #取消自动透传
            content = "forbusb"
            res = self.usbSet.setUsb(content)
            if (0 == res):
                
                InfoHintDialog(u"已取消自动透传已接入USB设备").exec_()
            else:
                
                InfoHintDialog(u"设置失败").exec_()
        return
    
    def closeTabWidget(self):
        self.emit(SIGNAL("closetabwidget"))        
    
        