# coding=utf-8
""" create by pbc on 2016.05.17 for change ui .
"""
import os
from PyQt4.QtCore import QSettings, QString

SCREEN_WITH = 1440
SCREEN_HEIGHT = 900
CURRENT_SCREENRES = ""
vm_colum = 4
vmInfoList = None
userInfo = None

s_userInfo=None
s_CenterIP=None
s_vmInfoList=None
s_agentInfo=None
s_appInfoList=None

user_information = None
tip_label_name = ""
tip_vm_name = ""


# setting for usb -top
#其中　0代表spice协议 　1代表network协议    22代表本地选择自动
usb_status = "network_ip"
usb_settings = QSettings("/mcos/usb_set.ini", QSettings.IniFormat)
def get_usb_set_statu():
    ret = usb_settings.value("usb_set").toString()
    return ret

def set_usb_set_statu(v):
    usb_settings.setValue("usb_set", QString(v))
# setting for usb -top



def get_screen_resolutionlist():
    screenResolution = []
    cmd = "xrandr | grep \"^  .*x\" | sed  \"s/^\ \ \ //g\" | sed \"s/\ .*$//\""
    try:
        f = os.popen(cmd, "r")
        while True:
            x = f.readline().strip('\n')  # 去掉\n
            if x == "":
                break
            screenResolution.append(x)
        f.close()
        screenResolution.reverse()
    except:
        return ["1024x768"]

    # add by pbc on 2016.05.25 for get screen list -top
    new_screenResolution = []
    for item in screenResolution:
        item_w_h_list = str(item).split("x")
        if len(item_w_h_list) == 2:
            if int(item_w_h_list[0]) >= 800 and int(item_w_h_list[1]) >= 600:
                new_screenResolution.append(item)
    return new_screenResolution
    # add by pbc on 2016.05.25 for get screen list -end


# 获取当前分辨率
def getCurScreenResolution():
    cmd = "xrandr | grep \"^  .*x.*\*\" | sed  \"s/^\ \ \ //g\" | sed \"s/\ .*$//\""
    try:
        import os
        f = os.popen(cmd, "r")
        Resolution = f.readline().strip('\n')  # 去掉\n
        f.close()

    except Exception:
        raise Exception("getScreenResolution err")
    return Resolution

def getScreenWidth():
    return int(str(getCurScreenResolution()).split("x")[0])

def getScreenHeight():
    return int(str(getCurScreenResolution()).split("x")[1])



def get_width(v):
    __desktop_with = SCREEN_WITH
    return __desktop_with * v / 1440.0

def get_height(v):
    __desktop_height = SCREEN_HEIGHT
    return __desktop_height * v / 900.0


# def get_vmlist_colum():
    # #add by pbc for changed count -top
    # for i in range(6):
    #     if i >1:
    #         with_tmp = i* 200 + get_width(400)+ (i-1)* get_width(30.0)
    #         print with_tmp
    #         if with_tmp > SCREEN_WITH:
    #             _column = i-1
    #             return _column
    # #add by pbc for changed count -top

