#!/usr/bin/python
#coding=utf-8

from ui_qss.autoStartDesktopSetQss import AutoStartDesktopSetQss
from PyQt4.QtGui import QCheckBox,QLabel, QTableWidgetItem
from PyQt4.QtCore import SIGNAL, QString, QPoint
from userInfoConfFile.userInfoConf import UserInfoConfFile
from common import common_run

class AutoStartDesktopSet(AutoStartDesktopSetQss):
    def __init__(self,vmInfoList=None):
        super(AutoStartDesktopSet,self).__init__()
        self.tableWidget.setShowGrid(False)
        self.__vmInfoList = vmInfoList
        self.__vmId = ""
        self.__init_vmid = ""
        self.upate_list_statu(self.__vmInfoList)

    # add by pbc for changed this function  -top
    def set_auto_desktop_func(self):
        conf = UserInfoConfFile()
        conf.setDefaultVM(self.__vmId)

    def set_init_auto_desktop_func(self):
        conf = UserInfoConfFile()
        conf.setDefaultVM(self.__init_vmid)
    # add by pbc for changed this function  -end

    def upload_checklist_statu(self,bool):
        if bool == True:
            for i in range(self.tableWidget.rowCount()):
                self.tableWidget.cellWidget(i,1).setChecked(False)
            pbt = self.sender()
            vmIndexRow = self.tableWidget.indexAt(QPoint((pbt.frameGeometry().x()), pbt.frameGeometry().y()))
            currentIndex = vmIndexRow.row()
            self.tableWidget.cellWidget(currentIndex,1).setChecked(True)
            self.__vmId = QTableWidgetItem(self.tableWidget.item(currentIndex,2)).text()
        else:
            self.__vmId = ""

    def upate_list_statu(self,vmInfoList):
        self.tableWidget.clear()
        if vmInfoList != None :
            _columnWidth=int(common_run.SCREEN_WITH * 480 / 1440.0)

            self.tableWidget.setColumnCount(3)
            self.tableWidget.setColumnHidden(2,True)
            lenth = len(vmInfoList)
            self.tableWidget.setRowCount(lenth)
            for i in range(len(vmInfoList)):
                vmName = vmInfoList[i]["name"]
                vmId   = vmInfoList[i]["vmId"]
                _checkbox = QCheckBox()
                conf = UserInfoConfFile()
                if vmId == conf.getDefaultVM():
                    self.__init_vmid = vmId
                    _checkbox.setChecked(True)
                self.tableWidget.setCellWidget(i,0,QLabel("           " + vmName))
                self.tableWidget.setCellWidget(i,1,_checkbox)
                #add by pbc for changed -top
                self.tableWidget.setItem(i,2,QTableWidgetItem(QString(vmId)))
                self.connect(_checkbox,SIGNAL("clicked(bool)"),self.upload_checklist_statu)
                #add by pbc for changed -top
                self.tableWidget.setColumnWidth(0,_columnWidth)
        else:
            pass











###############################
#   单元测试
##############################
if __name__ == "__main__":
    from PyQt4 import QtGui
    from PyQt4.QtCore import QTextCodec
    from os import environ
    environ["WIN_UI_IMAGES_PATH"]="../../../images"
    environ["MCOS_CONFIG_FILE"] = "./userInfoConfFile/centerset.conf" # 配置文件路径

    app = QtGui.QApplication([])
    QTextCodec.setCodecForCStrings(QTextCodec.codecForLocale())     # 支持中文

    vmInfo = {"name"    : "xp",
              "vmId"    : "123456",
              "osType"  : "REDHAT_64",
              "osStatus": "down",
              "vmIp"    : "192.168.137.1",
              "vmPort"  : "8888",
              "vmPassWd": "123456"
              }
    vmInfo2 = { "name"    : "win7",
                "vmId": "5555",
               "osType": "WINXP_32",
               "osStatus": "down",
               "vmIp": "192.168.137.1",
               "vmPort": "8888",
               "vmPassWd": "123456"
               }
    vmInfo3 = { "name"    : "win8",
            "vmId": "77777",
           "osType": "WIN_8",
           "osStatus": "down",
           "vmIp": "192.168.137.1",
           "vmPort": "8888",
           "vmPassWd": "123456"
           }
    vmInfoList = [vmInfo,vmInfo2,vmInfo3]

    win = AutoStartDesktopSet(vmInfoList)
    win.show()
    app.exec_()