QuiltingHMI/main.cpp

307 lines
11 KiB
C++
Raw Normal View History

2024-02-06 07:10:48 +00:00
#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 0://中文
sourcePath = appPath.path() + appPath.separator() + "chinese.qm";
break;
case 1://英文
sourcePath = appPath.path() + appPath.separator() + "english.qm";
break;
case 2://西班牙文
sourcePath = appPath.path() + appPath.separator() + "spanish.qm";
break;
case 3://孟加拉文
sourcePath = appPath.path() + appPath.separator() + "bengal.qm";
break;
case 4://土耳其文
sourcePath = appPath.path() + appPath.separator() + "turkey.qm";
break;
case 5://丹麦文
sourcePath = appPath.path() + appPath.separator() + "denmark.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;
}
#ifdef Q_OS_LINUX
QApplication::setOverrideCursor(Qt::BlankCursor);
#endif
//支持中文编码
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
//文件路径
QDir apppath(qApp->applicationDirPath());
//配置文件路径
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())//配置文件不存在
{
g_pSettings->writeToIniFile("Pattern/name","");
g_pSettings->writeToIniFile("HMI/resolution",resolution1006);
g_pSettings->writeToIniFile("HMI/theme",theme1);
g_pSettings->writeToIniFile("HMI/language",chinese);
g_pSettings->writeToIniFile("HMI/fontsize",normal);//字体大小
g_pSettings->writeToIniFile("HMI/machineType",MACHINE_HIGHSPEEDSINGLEQUI);
g_pSettings->writeToIniFile("HMI/productType",PRODUCT_NULL);
g_pSettings->writeToIniFile("HMI/debugMode",nodebugMode);//界面调试模式
g_pSettings->writeToIniFile("HMI/waterMark",1);//0:主背景图不带水印 1:主背景图带水印
g_pSettings->writeToIniFile("HMI/paraSort",0);//0:参数不分类显示 1:参数分类显示
g_pSettings->writeToIniFile("HMI/connectMode",0);//连接方式(通讯方式,网口或串口)
g_pSettings->writeToIniFile("HMI/passwordOne",g_passwordOne);
g_pSettings->writeToIniFile("IOT/gatewayConnectMode",0);//网关连接方式(连接方式,网口或串口)
g_pSettings->writeToIniFile("IOT/rackNumber",0);//机架号
g_pSettings->writeToIniFile("DateTime/second",0);
//写入机头断线次数
g_pSettings->writeToIniFile("HeadBreakLineNum",0);
g_emResolut = resolution1006;
g_emTheme = theme1;
language = chinese;
fontsize = normal;
g_emDebugMode = nodebugMode;
g_emMacType = MACHINE_HIGHSPEEDSINGLEQUI;
g_emProductType = PRODUCT_NULL;
g_passwordOne = PASSWORD_ONE;
}
else
{
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->writeToIniFile("HMI/debugMode",nodebugMode);//界面调试模式
g_emDebugMode = nodebugMode;
}
language = g_pSettings->readFromIniFile("HMI/language").toInt();
fontsize = g_pSettings->readFromIniFile("HMI/fontsize").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();
}
//自动获取屏幕分辨率
#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();
if (fontsize == 1)//如果是加大字体
{
if(g_emResolut == resolution1006)
{
fontBold_1 = fontBold_4;//二级界面主标题字体
fontNormal_1 = fontNormal_4;//1024x600常规字体加大
fontNormal_2 = fontNormal_5;//800x480字体加大
fontNormal_6 = fontNormal_7;//显示花样信息的字体
fontNormal_10 = fontNormal_11;//断线机头字体
fontNormal_12 = fontNormal_12;//1024x600分辨率 花样选择界面花样名称
}
else
{
fontBold_1 = fontBold_1;
fontNormal_1 = fontNormal_9;
fontNormal_6 = fontNormal_9; //1920x1080分辨率 的所有字体
fontNormal_10 = fontNormal_10;
fontNormal_12 = fontNormal_1;
}
}
else if (fontsize == 0)//如果是正常字体
{
if(g_emResolut == resolution1006 )
{
fontBold_1 = fontBold_5;
fontNormal_1 = fontNormal_8;//1024x600常规字体和显示花样字体一样大,都是fontNormal_8(正常)
fontNormal_2 = fontNormal_6;//800x480字体
fontNormal_6 = fontNormal_8;
fontNormal_10 = fontNormal_11;//断线机头字体
fontNormal_12 = fontNormal_12;//1024x600分辨率 花样选择界面花样名称
}
else
{
fontBold_1 = fontBold_1;
fontNormal_1 = fontNormal_1;
fontNormal_6 = fontNormal_1; //1920x1080分辨率 的所有字体
fontNormal_10 = fontNormal_10;
fontNormal_12 = fontNormal_1;
}
}
#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;
}