# coding=utf-8
from view.setting.personalizedsetting import Ui_Form
from model.setting.personalizedset import PersonalizedSet
from PyQt4.QtCore import SIGNAL, QString
from common.infohintdialog import InfoHintDialog
from PyQt4.Qt import QWidget

 
class PersonalizedSetting(QWidget, Ui_Form):
    def __init__(self):
        super(PersonalizedSetting, self).__init__()
        self.setupUi(self)
        self.setStyleSheet(QString("QComboBox{border:1px groove rgb(170,214,252);height:25;}"))
        self.groupBox_4.setHidden(True)#把关机退出全屏那一项去掉
        self.personalizedSet=PersonalizedSet()
        self.updateVmSereenState()#更新配置是否关机退出虚拟机全屏函数
        self.resolutionAutoAdjust()#分辨率自适应初始化

        self.connect(self.radioButtonResolutionStart, SIGNAL("clicked()"), self.on_radioButtonOpenAutoAdjustClicked)
        self.connect(self.radioButtonResolutionStop, SIGNAL("clicked()"), self.on_radioButtonCloseAutoAdjustClicked)
        self.connect(self.radioButtonOpenSereenFun,SIGNAL("clicked()"),self.radioButtonOpenSereen_clicked)
        self.connect(self.radioButtonCloseSereenFun,SIGNAL("clicked()"),self.radioButtonCloseSereen_clicked)

    def resolutionAutoAdjust(self):#分辨率自适应初始化函数
        action = self.personalizedSet.getResolutionAutoAdjustType()
        if (action == "open"):
            self.radioButtonResolutionStart.setChecked(True)
            self.personalizedSet.ctrlResolutionAutoAdjust("open")
        elif action == "close":
            self.radioButtonResolutionStop.setChecked(True)
            self.personalizedSet.ctrlResolutionAutoAdjust("close")
        else:
            self.personalizedSet.ctrlResolutionAutoAdjust("open")
            self.radioButtonResolutionStart.setChecked(True)
    
    def radioButtonOpenSereen_clicked(self):
        self.personalizedSet.setVmScreenState("yes")
        self.updateVmSereenState()

    def radioButtonCloseSereen_clicked(self):
        self.personalizedSet.setVmScreenState("no")
        self.updateVmSereenState()

    def updateVmSereenState(self):
        retv=self.personalizedSet.updateVmScreenState()
        if(retv!=""):
            if(retv=="yes"):
                self.radioButtonOpenSereenFun.setChecked(True)
            elif(retv=="no"):
                self.radioButtonCloseSereenFun.setChecked(True)
        else:
            self.radioButtonOpenSereenFun.setChecked(True)
            self.personalizedSet.setVmScreenState("yes")

    def on_radioButtonOpenAutoAdjustClicked(self):  # 分辨率自适应打开
        res = self.personalizedSet.ctrlResolutionAutoAdjust("open")
        if(-1 == res):
            InfoHintDialog(u'打开分辨率自适应文件失败！').exec_()
            return
        elif(-2 == res):
            InfoHintDialog(u'设置环境变量SMEP_SEND_MONITOR_CONFIG失败！').exec_()
        else:
            InfoHintDialog(u'成功启用分辨率自适应').exec_()
        return

    def on_radioButtonCloseAutoAdjustClicked(self):#分辨率自适应关闭
        res = self.personalizedSet.ctrlResolutionAutoAdjust("close")
        if(-1 == res):
            InfoHintDialog(u'打开分辨率自适应文件失败！').exec_()
        else:
            InfoHintDialog(u'成功关闭分辨率自适应').exec_()
        return
