# -*- coding: utf-8 -*-  

from PyQt4.QtCore import QTextCodec, QFile, QString
from PyQt4.QtGui import QApplication
from mainframe import MainFrame
import sys
import os
import globalvariable
import uuid
import common
from storeinfoparser import StoreInfoParser
from logrecord import LogRecord
import globalfunc
import time
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf-8"))

reload(sys)
sys.setdefaultencoding('utf-8')
import warnings
warnings.filterwarnings("ignore")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    #获取程序运行时的环境类型
    globalvariable.PROGRAM_RUNNING_TYPE = globalfunc.getProgramRunningEnvType()
    infoContent = u" 运行环境类型为开发类型" if globalvariable.PROGRAM_RUNNING_TYPE == common.DEVELOPED_ENV_TYPE \
            else u" 运行环境类型为执行类型"
    LogRecord.instance().logger.info(infoContent)

    #获取学生端的名称
    if StoreInfoParser.instance().getTerminalName() is not None:
        terminalName = StoreInfoParser.instance().getTerminalName()
        globalvariable.TERMINAL_NAME = str(terminalName)


    LogRecord.instance().logger.info(u"进入主函数")  
    #加载QSS样式表
    qss= QFile("clouds.qss")
    qss.open(QFile.ReadOnly)
    app.setStyleSheet(QString(qss.readAll()))
    qss.close()
    
    # #设置屏幕分辨率
    resolutionValue = StoreInfoParser.instance().getResolutionValue()
    if resolutionValue is None:
        resolutionValue = common.defaultResolution
    cmd = "xrandr -s %s" % resolutionValue
    if os.system(cmd) != 0:
        LogRecord.instance().logger.info(u"设置分辨率值%s失败" % resolutionValue)
        if os.system("xrandr -s 1366x768") != 0:
            LogRecord.instance().logger.info(u"设置分辨率值1366x768失败" )
            if os.system("xrandr -s 1368x768") != 0:
                LogRecord.instance().logger.info(u"设置分辨率值1368x768失败" )
                if os.system("xrandr -s 1440x900") != 0:
                    LogRecord.instance().logger.info(u"设置分辨率值1440x900失败" )
                    if os.system("xrandr -s 1280x720") != 0:
                        LogRecord.instance().logger.info(u"设置分辨率值1280x720失败")
                    else:
                        StoreInfoParser.instance().setResolutionValue("1280x720")
                else:
                    StoreInfoParser.instance().setResolutionValue("1440x900")
            else:
                StoreInfoParser.instance().setResolutionValue("1368x768")
        else:
            StoreInfoParser.instance().setResolutionValue("1366x768")
    else:
        StoreInfoParser.instance().setResolutionValue(resolutionValue)
    
    #关闭屏保
    os.system("xset dpms 0 0 0")
    os.system("xset s 0 0")
    #os.system("setenforce 0")
       
    window = MainFrame()
    window.show()
    #window.repaint()
    #window.setPosition()
    
    #time.sleep(5)
    app.exec_()
