#include <QApplication>
#include <QFontDatabase>
#include <QTranslator>
#include <QTextCodec>
#include <QSplashScreen>
#include <QResource>
#include <QDir>
#include <QSharedMemory>

#define _IN_MAIN_CPP

#include "main.h"
#include "mctype/patternsingleboard/mainUI/mainwidget.h"

QString switchLanguage(int language,QDir appPath)
{
    QString sourcePath,targetPath;
    targetPath = appPath.path() + appPath.separator() + "language.qm";
    switch (language)
    {
    case chinese://中文
        sourcePath = appPath.path() + appPath.separator() + "chinese.qm";
        break;
    case english://英文
        sourcePath = appPath.path() + appPath.separator() + "english.qm";
        break;
    case spanish://西班牙文
        sourcePath = appPath.path() + appPath.separator() + "spanish.qm";
        break;
    case bengal://孟加拉文
        sourcePath = appPath.path() + appPath.separator() + "bengal.qm";
        break;
//    case 4://土耳其文
//        sourcePath = appPath.path() + appPath.separator() + "turkey.qm";
//        break;
    case portugal://葡萄牙语
        sourcePath = appPath.path() + appPath.separator() + "portugal.qm";
        break;
    case french://法语
        sourcePath = appPath.path() + appPath.separator() + "french.qm";
        break;
    case arabic://阿拉伯语
        sourcePath = appPath.path() + appPath.separator() + "arabic.qm";
        break;
    case ukrainian://乌克兰语
        sourcePath = appPath.path() + appPath.separator() + "ukrainian.qm";
        break;
    case korean://韩语
        sourcePath = appPath.path() + appPath.separator() + "korean.qm";
        break;
    case russian://俄语
        sourcePath = appPath.path() + appPath.separator() + "russian.qm";
        break;
    case uzbek://乌兹别克语
        sourcePath = appPath.path() + appPath.separator() + "uzbek.qm";
        break;
    case vietnamese://越南文
        sourcePath = appPath.path() + appPath.separator() + "vietnamese.qm";
        break;
    case turkey://土耳其文
        sourcePath = appPath.path() + appPath.separator() + "turkey.qm";
        break;
    default://中文
        sourcePath = appPath.path() + appPath.separator() + "chinese.qm";
        break;
    }

    QFile::remove(targetPath);
    QFile::copy(sourcePath,targetPath);
#ifdef Q_OS_LINUX
    system("sync");
#endif
    return targetPath;
}

void setMcMainWindow(QApplication &app, QSplashScreen *splash, s16 HMIDecrypt, s16 connectMode)
{
    //创建主窗口
    if (g_emMacType == MACHINE_EMB ||               //绣花机
            g_emMacType == QUIMACHINE_EMB )               //绗绣机
    {
        MainWidget mainwindow;
        //初始化主窗口
        mainwindow.initMcTypeWindows();
        mainwindow.initAllWindows(HMIDecrypt,connectMode);
        mainwindow.show();//显示窗口
        mainwindow.setWindowIcon(QIcon(":/images/1024x600/rp.png"));
        splash->finish(NULL);
        app.exec();//开始执行应用程序
    }
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);//此处必须用QApplication，否则QSplashScreen不能使用
    QSharedMemory shared_memory;
    shared_memory.setKey(QString("main_window"));//设置固定共享内存段的key值
    if(shared_memory.attach())   //尝试将进程附加到该共享内存段,避免windows下重开程序
    {
        return 0;
    }

#ifdef Q_OS_LINUX
    QApplication::setOverrideCursor(Qt::BlankCursor);
#endif

    //支持中文编码
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    //文件路径
    QDir apppath(qApp->applicationDirPath());
    g_pSettings = new Settings();

    //日志文件记录
    for(s16 i = TYPE_ERROR; i <= TYPE_BREAK; i++)
    {
        QString csvfile;
        if(i == TYPE_ERROR)
        {
            csvfile = apppath.path() + apppath.separator() + CSV_ERROR;
        }
        else if(i == TYPE_BREAK)
        {
            csvfile = apppath.path() + apppath.separator() + CSV_BREAK;
        }

        // 使用时间格式进行csv文件命名
        //m_strFilePath = strDir + "/" + QString("csv%1.csv").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
        // 判断文件是否不存在
        QFile fileCSV(csvfile);
        if (!fileCSV.exists())
        {
            QFile file(csvfile);
            if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
            {
                QTextStream in(&file);
                QString strText("");
                // 文件不存在，第一次，给他写个列表名字，这样csv文件打开时候查看的时候就比较清晰
                strText = QString("DateTime,") + QString("Info");
                in << strText << '\n';
                file.close();
            }
        }
        g_pSettings->clearToCsv(i);
    }

    //配置文件路径
    QString configfile;
    configfile = apppath.path() + apppath.separator() + "config.ini";

    //读入配置
    //g_pSettings = new Settings();
    g_pSettings->loadSetting(configfile);
    QFile iniFile(configfile);
    int language = 0;
    int fontsize = 0;
    s16 connectMode = 0;
    s16 gatewayConnectMode = 0;

    if(!iniFile.exists())//配置文件不存在
    {
        if(RESOLUTIO_TYPE == 1)
        {
            g_pSettings->writeToIniFile("HMI/resolution",resolution1910);
        }
        else if (RESOLUTIO_TYPE == 2)
        {
            g_pSettings->writeToIniFile("HMI/resolution",resolution1006);
        }
        g_pSettings->writeToIniFile("HMI/theme",theme1);

        g_pSettings->writeToIniFile("HMI/machineType",MACHINE_EMB);
        g_pSettings->writeToIniFile("HMI/productType",PRODUCT_EMB_EMB);

        g_pSettings->writeToIniFile("HMI/connectMode",0);//连接方式(通讯方式，网口或串口)
        g_pSettings->writeToIniFile("HMI/passwordOne",PASSWORD_ONE);

        g_pSettings->writeToIniFile("HMI/commonbtn1",FUN_PATTERNSELECT);//快捷按钮1的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn2",FUN_SETSTARTEMBPOINT);//快捷按钮2的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn3",FUN_BACKSTARTEMBPOINT);//快捷按钮3的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn4",FUN_CHECKFRAME);//快捷按钮4的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn5",FUN_BACKWORKPOINT);//快捷按钮5的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn6",FUN_NEEDLERODPOSITION);//快捷按钮6的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn7",FUN_BACKOFFSETPOINT);//快捷按钮7的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn8",FUN_SPINDLEJOG);//快捷按钮8的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn9",FUN_MANUALTRIM);//快捷按钮9的功能索引
        g_pSettings->writeToIniFile("HMI/commonbtn10",FUN_MANUALCHANGECOLOR);//快捷按钮10的功能索引

        //绣花针杆
        g_pSettings->writeToIniFile("EmbNeedle/number",9);
        g_pSettings->writeToIniFile("EmbNeedle/1_coloridx",0);

        //亮片颜色设置
        g_pSettings->writeToIniFile("SequinColor/number",2);
        g_pSettings->writeToIniFile("SequinColor/1_coloridx",0);

        //散珠颜色设置
        g_pSettings->writeToIniFile("BeadColor/number",1);
        g_pSettings->writeToIniFile("BeadColor/1_coloridx",0);

        //毛巾针杆设置
        g_pSettings->writeToIniFile("TowelNeedle/number",6);
        g_pSettings->writeToIniFile("TowelNeedle/1_coloridx",0);

        //缠绕针杆设置    //默认是9个针杆
        g_pSettings->writeToIniFile("CoilNeedle/number",9);
        g_pSettings->writeToIniFile("CoilNeedle/1_coloridx",0);

        //锯齿针杆设置    //默认是6个针杆
        g_pSettings->writeToIniFile("SawtoothNeedle/number",6);
        g_pSettings->writeToIniFile("SawtoothNeedle/1_coloridx",0);

        //盘带针杆设置     //默认是2个针杆
        g_pSettings->writeToIniFile("RibbonNeedle/number",2);
        g_pSettings->writeToIniFile("RibbonNeedle/1_coloridx",0);

        //激光针杆设置     //默认是1个针杆
        g_pSettings->writeToIniFile("LaserNeedle/number",10);
        g_pSettings->writeToIniFile("LaserNeedle/1_coloridx",0);

        //上次关机时间
        g_pSettings->writeToIniFile("DateTime/second",0);

        if(RESOLUTIO_TYPE == 1)
        {
            g_emResolut = resolution1910;
        }
        else if (RESOLUTIO_TYPE == 2)
        {
            g_emResolut = resolution1006;
        }

        g_emTheme = theme1;
        g_emMacType = MACHINE_EMB;
        g_emProductType = PRODUCT_EMB_EMB;
        g_passwordOne =  PASSWORD_ONE;
    }
    else//存在配置文件，很可能是只有DateTime/second
    {
        int date =   g_pSettings->readFromIniFile("DateTime/second").toInt();
        if(date == 0)
        {
            // 获取系统当前时间
            QDateTime dateTime = QDateTime::currentDateTime();
            g_pSettings->writeToIniFile(("DateTime/second"),dateTime.toTime_t());//单位为秒
        }

        g_passwordOne = g_pSettings->readFromIniFile("HMI/passwordOne").toString();//先从配置文件里读290810
        if(g_passwordOne == 0)//如果是0
        {
            g_passwordOne = PASSWORD_ONE;//就等于290810
            g_pSettings->writeToIniFile("HMI/passwordOne",g_passwordOne);//然后写入配置文件
        }
        else//如果不是0
        {
            g_passwordOne = g_pSettings->readFromIniFile("HMI/passwordOne").toString();//就等于配置文件里的
        }

        g_emTheme = (Theme)(g_pSettings->readFromIniFile("HMI/theme").toInt());
        if(g_emTheme == 0 )// 不存在主题 ，就写成主题1，为了防止灰屏
        {
            g_pSettings->writeToIniFile("HMI/theme",theme1);
            g_emTheme = theme1;
        }

        QString g_emResolutStr = "HMI/resolution";
        if(g_pSettings->ifKeyExists(g_emResolutStr) == true)
        {
            g_emResolut = (Resolution)(g_pSettings->readFromIniFile("HMI/resolution").toInt());
        }
        else
        {
            if(RESOLUTIO_TYPE == 1)
            {
                g_pSettings->writeToIniFile("HMI/resolution",resolution1910);
                g_emResolut = resolution1910;
            }
            else if (RESOLUTIO_TYPE == 2)
            {
                g_pSettings->writeToIniFile("HMI/resolution",resolution1006);
                g_emResolut = resolution1006;
            }
        }

        g_emMacType = (MachineType)(g_pSettings->readFromIniFile("HMI/machineType").toInt());
        g_emProductType = (ProductType)(g_pSettings->readFromIniFile("HMI/productType").toInt());
        connectMode = g_pSettings->readFromIniFile("HMI/connectMode").toInt();
    }

    //各机头针杆断线信息
    QString breakFilePath = apppath.path() + apppath.separator() + BREAKINFOFILE;
    QFile breakFile(breakFilePath);
    if(!breakFile.exists())
    {
        if(!breakFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        {
            qDebug() << "open breakFile fail when wirte";
        }
        else
        {
            QByteArray arr;
            arr.clear();
            arr.resize(HEADNUM*NEEDLENUM*sizeof(s32)*3);//乘3是包括平绣、毛巾、缠绕三种
            memset(arr.data(),0,arr.length());
            breakFile.write(arr);
            breakFile.close();
        }
    }

    //自动获取屏幕分辨率
#ifdef Q_OS_LINUX
    QDesktopWidget * desktop = QApplication::desktop();
    QRect screenRect = desktop->screenGeometry();
    int width = screenRect.width();
    int height = screenRect.height();
    if(width == 1920 && height == 1080)
    {
        g_emResolut = resolution1910;
    }
    else if(width == 1024 && height == 600)
    {
        g_emResolut = resolution1006;
    }
#endif

    QString HMIConfigfile;
    HMIConfigfile = apppath.path() + apppath.separator() + "HMIConfig.ini";

    //读入配置
    //   g_pSettings = new Settings();
    g_pSettings->loadHMISetting(HMIConfigfile);

    QFile HMIFile(HMIConfigfile);
    if(!HMIFile.exists())//不存在
    {
        g_pSettings->writeToInHMIiFile("Pattern/name","");
        g_pSettings->writeToInHMIiFile("HMI/language",chinese);
        g_pSettings->writeToInHMIiFile("HMI/fontsize",normal);//字体大小
        g_pSettings->writeToInHMIiFile("HMI/debugMode",nodebugMode);//界面调试模式
        g_pSettings->writeToInHMIiFile("HMI/combineEmb",0);//0:无双头组合绣功能 1:有双头组合绣功能
        g_pSettings->writeToInHMIiFile("HMI/threeCombineEmb",0);//0:无三头组合绣功能 1:有三头组合绣功能
        g_pSettings->writeToInHMIiFile("HMI/headEmb",0);//0:无隔头绣功能 1:有隔头绣功能
        g_pSettings->writeToInHMIiFile("HMI/laser",0);//0:无激光功能 1:有激光功能       -rq
        g_pSettings->writeToInHMIiFile("HMI/shuttleChange",0);//0:无自动换梭功能 1:有自动换梭功能       -rq
        g_pSettings->writeToInHMIiFile("HMI/trackropeEmb",0);//0:无跟踪绳绣功能 1:有跟踪绳绣功能
        g_pSettings->writeToInHMIiFile("HMI/stepDrive",0);//0:无步进驱动功能 1:有步进驱动功能
        g_pSettings->writeToInHMIiFile("HMI/camColorChange",0);//0:无凸轮换色功能 1:有凸轮换色功能
        g_pSettings->writeToInHMIiFile("HMI/bead",0);//0:无散珠功能 1:有散珠功能
        g_pSettings->writeToInHMIiFile("HMI/sequin",0);//0:无亮片功能 1:有亮片功能
        g_pSettings->writeToInHMIiFile("HMI/towel",0);//0:无毛巾功能 1:有毛巾功能
        g_pSettings->writeToInHMIiFile("HMI/coil",0);//0:无缠绕功能 1:有缠绕功能
        g_pSettings->writeToInHMIiFile("HMI/simpleTowel",0);//0:无简易毛巾功能 1:有简易毛巾功能
        g_pSettings->writeToInHMIiFile("HMI/foot",0);//0:无独立压脚 1:有独立压脚
        g_pSettings->writeToInHMIiFile("HMI/waterMark",1);//0:主背景图不带水印 1:主背景图带水印
        g_pSettings->writeToInHMIiFile("HMI/paraSort",1);//0:参数不分类显示 1:参数分类显示
        g_pSettings->writeToInHMIiFile("HMI/wifi",0);//0:不带wifi 1:带wifi
        g_pSettings->writeToInHMIiFile("HMI/gradient",0);//0:3D显示 1:2D显示
        g_pSettings->writeToInHMIiFile("HMI/fileMode",0);//0:ds16 1:ds8

        //针步补偿，默认值是0.2mm
        //g_pSettings->writeToIniFile("PatternPara/stepCompensation",20);
        g_pSettings->writeToInHMIiFile("PatternPara/xCompensation",20);
        g_pSettings->writeToInHMIiFile("PatternPara/yCompensation",20);
        //g_pSettings->writeToInHMIiFile("PatternPara/lockNeedlesNum",2);     // 锁针次数
        g_pSettings->writeToInHMIiFile("PatternPara/lockNeedlesStepNum",1);  // 锁针步数
        g_pSettings->writeToInHMIiFile("PatternPara/lockNeedlesStepLength",100);   // 锁针针步长度
        g_pSettings->writeToInHMIiFile("PatternPara/minStep",0);   // 过滤针步长度

        g_pSettings->writeToInHMIiFile("IOT/gatewayConnectMode",0);//网关连接方式(连接方式，网口或串口)
        //g_pSettings->writeToInHMIiFile("IOT/rackNumber",0);//机架号

        language = chinese;
        fontsize = normal;
        g_emDebugMode = nodebugMode;
    }
    else
    {
        //为了防止配置文件里没有debugMode时升级黑屏，因为界面调试模式是后来新加的，之前版本的配置文件里没有，兼容之前的版本  -rq
        QString debugModeStr = "HMI/debugMode";
        if(g_pSettings->ifHMIKeyExists(debugModeStr) == true)
        {
            g_emDebugMode =(DebugMode)(g_pSettings->readFromInHMIiFile("HMI/debugMode").toInt());//如果存在配置文件，并且有这个key就读配置文件里的调试模式的值
        }
        else
        {
            g_pSettings->writeToIniFile("HMI/debugMode",nodebugMode);//界面调试模式
            g_emDebugMode = nodebugMode;
        }
        QString stepDriveStr = "HMI/stepDrive";
        if(g_pSettings->ifHMIKeyExists(stepDriveStr) == true)
        {
            int stepDrive = g_pSettings->readFromInHMIiFile("HMI/stepDrive").toInt();//如果存在配置文件，并且有这个key就读配置文件里的值
            g_pSettings->writeToInHMIiFile("HMI/stepDrive",stepDrive);
        }
        else
        {
            g_pSettings->writeToInHMIiFile("HMI/stepDrive",0);
        }

        language = g_pSettings->readFromInHMIiFile("HMI/language").toInt();
        fontsize =  g_pSettings->readFromInHMIiFile("HMI/fontsize").toInt();
        gatewayConnectMode = g_pSettings->readFromInHMIiFile("IOT/gatewayConnectMode").toInt();
    }

    //多语言翻译
    QString languageFile = switchLanguage(language,apppath);
    QTranslator translator;
    translator.load(languageFile);
    app.installTranslator(&translator);

    //资源文件
    QString resourcefile = apppath.path() + apppath.separator() + "nxcui.rcc";
    QResource::registerResource(resourcefile);

    //启动画面
    QSplashScreen *splash = new QSplashScreen;
    if(g_emResolut == resolution1006)
    {
        splash->setPixmap(QPixmap(":/images/startlogo1006.png"));
    }
    else if(g_emResolut == resolution1910)
    {
        splash->setPixmap(QPixmap(":/images/startlogo1910.png"));
    }

    QFont font;
    font.setPixelSize(20);
    splash->setFont(font);
    splash->move(0+g_mainWidgetPos.x(),0+g_mainWidgetPos.y());
    int waterMark = g_pSettings->readFromInHMIiFile("HMI/waterMark").toInt();//是否带水印
    if(waterMark==1)//带logo
    {
        splash->show();//显示启动画面
    }

    //检测界面解密的配置文件是否存在
    //界面解密配置文件路径
    s16 HMIDecrypt = 1;//1代表已授权解密过，0代表未授权解密过
    QString HMIDecryptConfigfile;
    HMIDecryptConfigfile = apppath.path() + apppath.separator() + "HMIDecryptConfig.ini";
    QFile HMIDecryptIniFile(HMIDecryptConfigfile);
    if(!HMIDecryptIniFile.exists())//不存在界面解密的文件(已发机器),可与主板建立连接
    {
        HMIDecrypt = 1;
    }
    else//存在界面解密的文件,判断是否解密过来决定是否与主板建立连接
    {
        QSettings iniSetting(HMIDecryptConfigfile, QSettings::IniFormat);
        HMIDecrypt = iniSetting.value("HMIDecrypt/ifdecrypt").toInt();
    }

    //下位机通讯
    QString mcConfigfile;
    mcConfigfile = apppath.path() + apppath.separator() + "mcconfig.ini";

    g_pMachine = new Machine();   //创建下位机通讯
    g_pMachine->initConnectMode(connectMode);//初始化通讯连接方式
    g_pMachine->setComportName("");
    g_pMachine->setConfigFileName(mcConfigfile);//配置文件
    if(HMIDecrypt != 0)
    {
        g_pMachine->startCommunication();//启动线程
    }

    //物联网客户端与mqtt网关服务器建立连接
    g_pLotMachine = NULL;
    QString lotConfigfile;
    lotConfigfile = apppath.path() + apppath.separator() + "lotconfig.ini";
    g_pLotMachine = new LotMachine();   //创建网关通讯
    g_pLotMachine->initConnectMode(gatewayConnectMode);//初始化通讯连接方式
    g_pLotMachine->setComportName("");
    g_pLotMachine->setConfigFileName(lotConfigfile);
    g_pLotMachine->startCommunication();

    g_pCurEmbData = new EmbData();
    //初始化颜色
    g_pCurEmbData->initColor(g_pSettings->getColorRgbArray().size(),(QRgb*)g_pSettings->getColorRgbArray().data());
    //设置颜色列表(针杆色序表)
    for(int i = 0; i < TOTAL_NEEDLE_NUM; i++)
    {
        QString idxStr;
        idxStr.clear();
        //绣花针杆
        if(i >= 0 && i <= EMB_NEEDLE_NUM - 1)
        {
            idxStr = "EmbNeedle/"+QString::number(i+1)+"_coloridx";
        }
        //盘带针杆
        else if(i >= (EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM - 1) &&  i <=(EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM + RIBBON_NEEDLE_NUM- 1))
        {
            idxStr = "RibbonNeedle/"+QString::number(i+1)+"_coloridx";
        }
        //毛巾针杆
        else if(i >= (EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM + RIBBON_NEEDLE_NUM - 1) &&  i <=(EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM+RIBBON_NEEDLE_NUM+TOWEL_NEEDLE_NUM  - 1))
        {
            idxStr = "TowelNeedle/"+QString::number(i+1)+"_coloridx";
        }
        //链式针杆
        else if(i >= (EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM + RIBBON_NEEDLE_NUM +TOWEL_NEEDLE_NUM - 1) &&  i <=(EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM+RIBBON_NEEDLE_NUM+TOWEL_NEEDLE_NUM+ CHAIN_NEEDLE_NUM - 1))
        {
            idxStr = "TowelNeedle/"+QString::number(i+1)+"_coloridx";
        }
        //缠绕针杆
        else if(i >= (EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM + RIBBON_NEEDLE_NUM+TOWEL_NEEDLE_NUM+CHAIN_NEEDLE_NUM - 1) &&  i <=(EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM+RIBBON_NEEDLE_NUM+TOWEL_NEEDLE_NUM+ CHAIN_NEEDLE_NUM +COIL_NEEDLE_NUM - 1))
        {
            idxStr = "CoilNeedle/"+QString::number(i+1)+"_coloridx";
        }
        //锯齿针杆
        else if(i >= (EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM+RIBBON_NEEDLE_NUM+TOWEL_NEEDLE_NUM+ CHAIN_NEEDLE_NUM +COIL_NEEDLE_NUM - 1) &&  i <=(EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM+RIBBON_NEEDLE_NUM+TOWEL_NEEDLE_NUM+ CHAIN_NEEDLE_NUM +COIL_NEEDLE_NUM+REV2_NEEDLE_NUM+SAWTOOTH_NEEDLE_NUM - 1))
        {
            idxStr = "SawtoothNeedle/"+QString::number(i+1)+"_coloridx";
        }
        //激光针杆
        else if(i >= (EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM+RIBBON_NEEDLE_NUM+TOWEL_NEEDLE_NUM+ CHAIN_NEEDLE_NUM +COIL_NEEDLE_NUM+REV2_NEEDLE_NUM+SAWTOOTH_NEEDLE_NUM - 1) &&  i <=(EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM + SEW_NEEDLE_NUM+RIBBON_NEEDLE_NUM+TOWEL_NEEDLE_NUM+ CHAIN_NEEDLE_NUM +COIL_NEEDLE_NUM+REV2_NEEDLE_NUM+SAWTOOTH_NEEDLE_NUM+REV3_NEEDLE_NUM+LASER_NEEDLE_NUM - 1))
        {
            idxStr = "LaserNeedle/"+QString::number(i+1)+"_coloridx";
        }
        if(g_pSettings->ifKeyExists(idxStr) == true)
        {
            int idx = g_pSettings->readFromIniFile(idxStr).toInt();
            g_pCurEmbData->setNeedleColorTable(i,idx);
        }
        else
        {
            g_pCurEmbData->setNeedleColorTable(i,i);
        }
    }

    //设置亮片颜色列表
    for(int i = 0; i < SEQUIN_NUM; i++)
    {
        QString idxStr = "SequinColor/"+QString::number(i+1)+"_coloridx";

        if(g_pSettings->ifKeyExists(idxStr) == true)
        {
            int idx = g_pSettings->readFromIniFile(idxStr).toInt();
            g_pCurEmbData->setSequinColorTable(i,idx);
        }
        else
        {
            g_pCurEmbData->setSequinColorTable(i,i);
        }
    }

    //设置散珠颜色列表
    for(int i = 0; i < BEAD_NUM; i++)
    {
        QString idxStr = "BeadColor/"+QString::number(i+1)+"_coloridx";

        if(g_pSettings->ifKeyExists(idxStr) == true)
        {
            int idx = g_pSettings->readFromIniFile(idxStr).toInt();
            g_pCurEmbData->setBeadColorTable(i,idx);
        }
        else
        {
            g_pCurEmbData->setBeadColorTable(i,i);
        }
    }

    Qt::Alignment alignment = Qt::AlignHCenter|Qt::AlignBottom; //设置一个格式
    QString str = QCoreApplication::translate("GLOBAL", "Init windows...");//初始化窗体...
    splash->showMessage(str,alignment,Qt::red);

    if (fontsize == 1)//如果是加大字体
    {
        if(g_emResolut == resolution1006)
        {
            fontBold_1 = fontBold_4;//二级界面主标题字体
            fontNormal_1 = fontNormal_4;//1024x600常规字体加大
            fontNormal_6 = fontNormal_7;//显示花样信息的字体
            fontNormal_10 = fontNormal_11;//断线机头字体
            fontNormal_12 = fontNormal_12;//1024x600分辨率  花样选择界面花样名称
            fontNormal_3 = fontNormal_3;//调试信息
            fontNormal_4 = fontNormal_4;
        }
        else
        {
            fontBold_1 = fontBold_1;
            fontNormal_1 = fontNormal_9;
            fontNormal_6 = fontNormal_9; //1920x1080分辨率 的所有字体
            fontNormal_10 =  fontNormal_10;
            fontNormal_12 = fontNormal_1;
            fontNormal_3 = fontNormal_6;//调试信息
            fontNormal_4 = fontNormal_9;
        }
    }
    else if (fontsize == 0)//如果是正常字体
    {
        if(g_emResolut == resolution1006)
        {
            fontBold_1 = fontBold_5;
            fontNormal_1 = fontNormal_8;//1024x600常规字体和显示花样字体一样大,都是fontNormal_8(正常)
            fontNormal_6 = fontNormal_8;
            fontNormal_10 = fontNormal_11;//绣花机断线机头字体
            fontNormal_9 = fontNormal_9;//绗绣机断线机头字体
            fontNormal_12 = fontNormal_12;//1024x600分辨率  花样选择界面花样名称
            fontNormal_3 = fontNormal_8;//调试信息
            fontNormal_4 = fontNormal_12;
        }
        else
        {
            fontBold_1 = fontBold_1;
            fontNormal_1 = fontNormal_1;
            fontNormal_6 = fontNormal_1;  //1920x1080分辨率 的所有字体
            fontNormal_10 =  fontNormal_10;
            fontNormal_9 = fontNormal_11;//绗绣机断线机头字体
            fontNormal_12 = fontNormal_1;
            fontNormal_3 = fontNormal_3;//调试信息
            fontNormal_4 = fontNormal_4;
        }
    }

#ifdef Q_OS_WIN
    //win下防止多个程序打开
    if(shared_memory.create(1)) //创建1byte的共享内存段
    {
        setMcMainWindow(app,splash,HMIDecrypt,connectMode);
    }
#endif

#ifdef Q_OS_LINUX
    setMcMainWindow(app,splash,HMIDecrypt,connectMode);
#endif

    delete splash;
    delete g_pSettings;

    if(g_pMachine != NULL)
    {
        delete g_pMachine;
    }

    delete g_pCurEmbData;
    return 0;
}
