#include #include #include #include #include #include #include #include #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 0://中文 sourcePath = appPath.path() + appPath.separator() + "chinese.qm"; break; case 1://英文 sourcePath = appPath.path() + appPath.separator() + "english.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) { MainWidget mainwindow; //初始化主窗口 mainwindow.initMcTypeWindows(); mainwindow.initAllWindows(HMIDecrypt,connectMode); mainwindow.show();//显示窗口 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(); //将文件和文件夹重命名,防止旧程序升级为新程序时因为文件名称不同,造成死机 //重命名或删除mcConfig.ini QString mcconfigfile; mcconfigfile = apppath.path() + apppath.separator() + "mcConfig.ini"; QFile file(mcconfigfile); QString mcconfigfile1; mcconfigfile1 = apppath.path() + apppath.separator() + "mcconfig.ini"; QFile file1(mcconfigfile1); if(file1.exists()) { file.remove();//删除mcConfig.ini } else { if(file.exists()) { QString mcconfigfileCp = apppath.path() + apppath.separator() + "mcconfig.ini"; file.rename(mcconfigfileCp); #ifdef Q_OS_LINUX system("sync"); #endif } } //重命名或删除lotConfig.ini QString lotconfigfile; lotconfigfile = apppath.path() + apppath.separator() + "lotConfig.ini"; file.setFileName(lotconfigfile); QString lotconfigfile1; lotconfigfile1 = apppath.path() + apppath.separator() + "lotconfig.ini"; file1.setFileName(lotconfigfile1); if(file1.exists()) { file.remove();//删除lotConfig.ini } else { if(file.exists()) { QString lotconfigfileCp = apppath.path() + apppath.separator() + "lotconfig.ini"; file.rename(lotconfigfileCp); #ifdef Q_OS_LINUX system("sync"); #endif } } //如果目录下有oprt文件夹,删除此文件夹 #ifdef Q_OS_LINUX QString oprtPath; oprtPath = apppath.path() + apppath.separator() + "oprt"; QDir oprtDir(oprtPath); if(oprtDir.exists()) { system("rm -rf /storage/oprt/oprt"); system("sync"); } #endif //改变花样路径名称并删除fcg文件 QString dirPath = apppath.absolutePath() + apppath.separator() + "patternfiles"; QDir dataFilesDir(dirPath); if(dataFilesDir.exists()) { //删除文件 QDir dir(dirPath);//项目中所保存的路径 dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);//设置过滤 QFileInfoList fileList = dir.entryInfoList(QStringList()<<"*.fcg");// 获取.fcg的文件列表 foreach (QFileInfo file,fileList)//遍历文件信息 { if (file.isFile()) //是文件,删除 { file.dir().remove(file.fileName()); } else //递归删除 { // deleteDir(file.absoluteFilePath()); } } QString dirPathCp = apppath.absolutePath() + apppath.separator() + PATTERNPATH; dataFilesDir.rename(dirPath,dirPathCp); #ifdef Q_OS_LINUX system("sync"); #endif } //日志文件记录 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; s16 connectMode = 0; s16 gatewayConnectMode = 0; if(!iniFile.exists())//配置文件不存在 { g_pSettings->writeToIniFile("HMI/resolution",resolution1006); //上次关机时间 g_pSettings->writeToIniFile("DateTime/second",0); //写入机头断线次数 g_pSettings->writeToIniFile("HeadBreakLineNum/head",0); g_pSettings->writeToIniFile("HMI/connectMode",0);//连接方式(通讯方式,网口或串口) g_pSettings->writeToIniFile("HMI/passwordOne",PASSWORD_ONE); g_pSettings->writeToIniFile("HMI/machineType",MACHINE_PUNCH); g_pSettings->writeToIniFile("HMI/productType",PRODUCT_PUNCH_SINGLEHEAD); g_pSettings->writeToIniFile("HMI/connectMode",0);//连接方式(通讯方式,网口或串口) g_pSettings->writeToIniFile("HMI/commonbtn1",FUN_PATTERNSELECT);//快捷按钮1的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn2",FUN_SETSTARTPOINT);//快捷按钮2的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn3",FUN_BACKSTARTPOINT);//快捷按钮3的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn4",FUN_CHECKFRAME);//快捷按钮4的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn5",FUN_FORWARDORBACK);//快捷按钮5的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn6",FUN_PUNCHMANUALCHANGECOLOR);//快捷按钮6的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn7",FUN_PUNCHSPINDLEJOG);//快捷按钮7的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn8",FUN_BACKWORKPOINT);//快捷按钮8的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn9",FUN_ALLTOZERO);//快捷按钮9的功能索引 g_pSettings->writeToIniFile("HMI/commonbtn10",FUN_SINGLEPUNCH);//快捷按钮10的功能索引 g_pSettings->writeToIniFile("HMI/machineType",MACHINE_PUNCH); g_pSettings->writeToIniFile("HMI/productType",PRODUCT_PUNCH_SINGLEHEAD); g_pSettings->writeToIniFile("HMI/wifi",0);//是否有wifi g_pSettings->writeToIniFile("HMI/language",chinese); g_pSettings->writeToIniFile("HMI/waterMark",1);//0:主背景图不带水印 1:主背景图带水印 g_pSettings->writeToIniFile("HMI/paraSort",1);//0:参数不分类显示 1:参数分类显示 g_pSettings->writeToIniFile("HMI/debugMode",nodebugMode);//界面调试模式 g_pSettings->writeToIniFile("IOT/gatewayConnectMode",0);//网关连接方式(连接方式,网口或串口) g_pSettings->writeToIniFile("IOT/rackNumber",0);//机架号 g_pSettings->writeToIniFile("Pattern/name",""); g_pSettings->writeToIniFile("AUFUS/zeroXcoordinate",90000);//自动定起始点 g_pSettings->writeToIniFile("AUFUS/highSpeed",0);//高低速 //冲孔针杆 g_pSettings->writeToIniFile("PunchNeedle/number",1); g_pSettings->writeToIniFile("PunchNeedle/1_coloridx",1); //缝纫针杆 g_pSettings->writeToIniFile("SewNeedle/number",1); g_pSettings->writeToIniFile("SewNeedle/1_coloridx",1); //绣花针杆 g_pSettings->writeToIniFile("EmbNeedle/number",1); g_pSettings->writeToIniFile("EmbNeedle/1_coloridx",1); g_emResolut = resolution1006; language = chinese; g_passwordOne = PASSWORD_ONE; g_emDebugMode = nodebugMode; } 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());//单位为秒 } QString g_emResolutStr = "HMI/resolution"; if(g_pSettings->ifKeyExists(g_emResolutStr) == true) { g_emResolut = (Resolution)(g_pSettings->readFromIniFile("HMI/resolution").toInt()); if(g_emResolut > resolution1006) { g_emResolut = resolution1006; } } else { g_emResolut = resolution1006; } language = g_pSettings->readFromIniFile("HMI/language").toInt(); if(language > english) { language = chinese; } g_emDebugMode =(DebugMode)(g_pSettings->readFromIniFile("HMI/debugMode").toInt());//如果存在配置文件,并且有这个key就读配置文件里的调试模式的值 connectMode = g_pSettings->readFromIniFile("HMI/connectMode").toInt(); g_emMacType = (MachineType)(g_pSettings->readFromIniFile("HMI/machineType").toInt()); if(g_emMacType > MACHINE_TEMPLATE_SEW) { g_emMacType = MACHINE_PUNCH_AUTOPOS; } g_emProductType = (ProductType)(g_pSettings->readFromIniFile("HMI/productType").toInt()); if(g_emProductType > PRODUCT_NULL) { g_emProductType = PRODUCT_NULL; } gatewayConnectMode = g_pSettings->readFromIniFile("IOT/gatewayConnectMode").toInt(); } //自动定位冲孔机,针杆数固定为6 if(g_emMacType == MACHINE_PUNCH_AUTOPOS) { g_pSettings->writeToIniFile("PunchNeedle/number",6); for(int i = 0 ; i < 6; i++) { QString str = "PunchNeedle/"+QString::number(i+1)+"_coloridx"; if(g_pSettings->ifKeyExists(str) == false) { g_pSettings->writeToIniFile(str,i); } } } //自动获取屏幕分辨率 #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 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,0); int waterMark = g_pSettings->readFromIniFile("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(); } //下位机通讯 g_pMachine = NULL; 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 - 1) && i < (EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM)) { int punchIdx = i + 1 - EMB_NEEDLE_NUM; idxStr = "PunchNeedle/"+QString::number(punchIdx)+"_coloridx"; } //缝纫针杆 else if(i == EMB_NEEDLE_NUM + PUNCH_NEEDLE_NUM) { int sewIdx = i + 1 - EMB_NEEDLE_NUM - PUNCH_NEEDLE_NUM; idxStr = "SewNeedle/"+QString::number(sewIdx)+"_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); } } Qt::Alignment alignment = Qt::AlignHCenter|Qt::AlignBottom; //设置一个格式 QString str = QCoreApplication::translate("GLOBAL", "Init windows...");//初始化窗体... splash->showMessage(str,alignment,Qt::red); 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; delete g_pMachine; delete g_pCurEmbData; return 0; }