Quilting-lw/main.cpp

286 lines
10 KiB
C++
Raw Permalink Normal View History

2026-01-23 08:37:18 +00:00
#include <QApplication>
#include <QFontDatabase>
#include <QTranslator>
#include <QTextCodec>
#include <QSplashScreen>
#include <QResource>
#include <QDir>
#include <QSharedMemory>
#include <QGraphicsProxyWidget>
#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 ukrainian://乌克兰文
sourcePath = appPath.path() + appPath.separator() + "ukrainian.qm";
break;
case french://法文
sourcePath = appPath.path() + appPath.separator() + "france.qm";
break;
case russian://俄语
sourcePath = appPath.path() + appPath.separator() + "russian.qm";
break;
case german://德文
sourcePath = appPath.path() + appPath.separator() + "german.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();//显示窗口
//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;
}
//支持中文编码
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
//文件路径
QDir apppath(qApp->applicationDirPath());
qDebug()<<"apppath:"<<apppath.path();
//配置文件路径
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())//配置文件不存在
{
QMap<QString, QVariant> settings;
settings["Pattern/name"] = "";
settings["HMI/resolution"] = resolution1006;
settings["HMI/theme"] = theme1;
settings["HMI/language"] = chinese;
settings["HMI/FrontSize"] = FONTSIZE;
settings["HMI/machineType"] = MACHINE_HIGHSPEEDSINGLEQUI;
settings["HMI/productType"] = PRODUCT_NULL;
settings["HMI/debugMode"] = nodebugMode;//界面调试模式
settings["HMI/waterMark"] = 1;//0:主背景图不带水印 1:主背景图带水印
settings["HMI/paraSort"] = 0;//0:参数不分类显示 1:参数分类显示
settings["HMI/connectMode"] = 0;//连接方式(通讯方式,网口或串口)
settings["HMI/passwordOne"] = g_passwordOne;
settings["IOT/gatewayConnectMode"] = 0;//网关连接方式(连接方式,网口或串口)
settings["IOT/rackNumber"] = 0;//机架号
settings["DateTime/second"] = 0;
settings["HeadBreakLineNum"] = 0;//写入机头断线次数
settings["HMI/splitStep"] = 1;//是否分割针步
settings["MaxSpeedOfMainSewWork"] = 0;//主轴缝纫工作最高转速
g_pSettings->writeToIniFile(settings);
g_emResolut = resolution1006;
g_emTheme = theme1;
language = chinese;
g_emDebugMode = nodebugMode;
g_emMacType = MACHINE_HIGHSPEEDSINGLEQUI;
g_emProductType = PRODUCT_NULL;
g_passwordOne = PASSWORD_ONE;
g_fontsize = FONTSIZE;
}
else
{
int frontSize = g_pSettings->readFromIniFile("HMI/FrontSize").toInt();
if((8 > frontSize) || (frontSize > 18))
{
g_pSettings->writeOneToIniFile("HMI/FrontSize",FONTSIZE);
g_fontsize = FONTSIZE;
}
g_fontsize = g_pSettings->readFromIniFile("HMI/FrontSize").toInt();
g_emResolut = (Resolution)(g_pSettings->readFromIniFile("HMI/resolution").toInt());
g_emTheme = (Theme)(g_pSettings->readFromIniFile("HMI/theme").toInt());
//这个情况是存在配置文件的时候很可能是只有DateTime/second读不到主题就是0图片加载不出来二级密码也会没有
//为了防止配置文件里没有debugMode时升级黑屏因为界面调试模式是后来新加的之前版本的配置文件里没有兼容之前的版本 -rq
QString debugModeStr = "HMI/debugMode";
if(g_pSettings->ifKeyExists(debugModeStr) == true)
{
g_emDebugMode =(DebugMode)(g_pSettings->readFromIniFile("HMI/debugMode").toInt());//如果存在配置文件并且有这个key就读配置文件里的调试模式的值
}
else
{
g_pSettings->writeOneToIniFile("HMI/debugMode",nodebugMode);//界面调试模式
g_emDebugMode = nodebugMode;
}
language = g_pSettings->readFromIniFile("HMI/language").toInt();
g_emDebugMode =(DebugMode)(g_pSettings->readFromIniFile("HMI/debugMode").toInt());//如果存在配置文件,就读配置文件里的调试模式的值
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();
g_passwordOne = g_pSettings->readFromIniFile("HMI/passwordOne").toString();
if(g_passwordOne.isEmpty())
g_passwordOne = PASSWORD_ONE;
gatewayConnectMode = g_pSettings->readFromIniFile("IOT/gatewayConnectMode").toInt();
g_xCorrectionFactor = 1.0;
g_yCorrectionFactor = 1.0;
}
//自动获取屏幕分辨率
#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;
}
else if(width == 800 && height == 480)
{
g_emResolut = resolution0804;
}
#endif
//多语言翻译
QString languageFile = switchLanguage(language,apppath);
qDebug()<<"languageFile:"<<languageFile;
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 == resolution0804)
{
splash->setPixmap(QPixmap(":/images/startlogo0804.png"));
}
else if(g_emResolut == resolution1910)
{
splash->setPixmap(QPixmap(":/images/startlogo1910.png"));
}
QFont font;
font.setPixelSize(20);
splash->setFont(font);
//splash->move(0,0);
splash->move(0+g_mainWidgetPos.x(),0+g_mainWidgetPos.y());
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();
#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;
}