HM-SPMS/product/src/sys/sys_startup/ButtonForm.cpp
2025-08-02 22:32:58 +08:00

312 lines
7.4 KiB
C++

#if defined(WIN32)
#include <Windows.h>
#endif
#include "ButtonForm.h"
#include "ui_ButtonForm.h"
#include "DataMng.h"
#include "MonitorCommon.h"
#include "DogCheck.h"
#include "DbCheck.h"
#include "SetupFunc.h"
#include "AutoStart.h"
#include <QToolButton>
#include <QIcon>
#include <QProcess>
#include <QHBoxLayout>
#include <QString>
#include "pub_utility_api/FileUtil.h"
#include "../include/sys/sys_login_api/CLoginDlg.h"
using namespace sys_startup;
ButtonForm::ButtonForm(int mode,bool isZh, QWidget *parent):
QWidget(parent),
ui(new Ui::ButtonForm),
m_mode(mode),
m_bIsZh(isZh)
{
ui->setupUi(this);
}
ButtonForm::~ButtonForm()
{
delete ui;
}
//void ButtonForm::setTitleHide(bool hide)
//{
// ui->label->setHidden(hide);
//}
void ButtonForm::addToolButtons(QVector<QString> &buttonVec)
{
QHBoxLayout *layOut = new QHBoxLayout(ui->widget);
layOut->setMargin(0);
if(m_mode == EN_SYS_STARTUP_PAGE1)
{
ui->frame->setObjectName("frame_page1");
layOut->addStretch();
}else
{
ui->frame->setObjectName("frame_page3");
}
for(int index(0);index<buttonVec.size();index++)
{
QString buttonName = buttonVec.at(index);
SButtonInfo buttonInfo ;
if(DataMng::instance()->getButtonInfoByName(buttonName,buttonInfo) != true)
{
continue;
}
QToolButton *toolBtn = new QToolButton(this);
toolBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
//QIcon icon = QIcon(buttonInfo.m_icon);
//QSize size(45,45);
QSize size(20,45);
toolBtn->setIconSize(size);
toolBtn->setMaximumHeight(30);
toolBtn->setMinimumHeight(30);
toolBtn->setMaximumWidth(100);
toolBtn->setMinimumWidth(80);
//toolBtn->setIcon(icon);
toolBtn->setText(m_bIsZh?buttonInfo.m_zh:buttonInfo.m_en);
#ifdef Q_PROCESSOR_ARM_64
toolBtn->setEnabled(buttonInfo.m_enable);
#endif
connect(toolBtn,&QToolButton::clicked,[=](){
executeTask(buttonInfo.m_cmd,buttonInfo.m_param);
});
if(buttonInfo.m_cmd =="SysCtrl")
{
connect(this, &ButtonForm::sigEnableStatus, [=](bool enabled) {
toolBtn->setEnabled(enabled);
});
}
layOut->addWidget(toolBtn);
}
if(m_mode == EN_SYS_STARTUP_PAGE3)
{
layOut->addStretch();
}
}
//void ButtonForm::setTitleText(const QString &title)
//{
// ui->label->setText(title);
//}
void ButtonForm::executeTask(QString cmd,QString param)
{
if(cmd =="SysCtrl")
{
emit sigButtonClick();
return ;
}
if(cmd =="DogCheck")
{
dogCheckDlg();
return ;
}
if(cmd =="DbCheck")
{
dbCheckDlg();
return ;
}
if(cmd =="AutoStart")
{
autoStartDlg();
return ;
}
if(cmd == "sys_command.bat")
{
#ifdef Q_OS_WIN
WinExec("sys_command.bat", SW_NORMAL);
#else
QProcess::startDetached("./sys_command.sh");
#endif
return ;
}
if(cmd.contains("ReportEdit"))
{
CLoginDlg dlg(this);
QString perm ="FUNC_NOM_RPT_EDIT";
dlg.setCheckPerm(perm);
dlg.setOkButtonText(tr("验证"));
dlg.setTitle(tr("权限验证"));
int res = dlg.exec();
if(res != QDialog::Accepted)
{
return;
}
}
if(cmd == "sys_command-platform.bat")
{
#ifdef Q_OS_WIN
WinExec("sys_command-platform.bat", SW_NORMAL);
#else
QProcess::startDetached("./sys_command-platform.sh");
#endif
return ;
}
if(cmd.endsWith(".exe"))
{
const std::string strFileName =
cmd.remove(cmd.size()-4,4).toStdString() + iot_public::CFileUtil::getProcSuffix();
std::string strPath = std::move( iot_public::CFileUtil::getPathOfBinFile(strFileName));
if(strPath.empty())
{
//< 也许配置的是外部程序,不在我们的路径中
strPath = strFileName;
}
if(param.isEmpty())
{
QProcess::startDetached(strPath.c_str());
}else
{
QString pathParam = QString("%1 %2").arg(strPath.c_str()).arg(param);
QProcess::startDetached(pathParam);
}
}
else if(cmd.endsWith(".bat"))
{
#ifdef Q_OS_WIN
const std::string strFileName = std::move(cmd.toStdString());
std::string strPath = std::move( iot_public::CFileUtil::getPathOfBinFile(strFileName));
if(strPath.empty())
{
//< 也许配置的是外部脚本,不在我们的路径中
strPath = strFileName;
}
system(strPath.c_str());
#else
const std::string strFileName = cmd.remove(cmd.size()-4,4).toStdString() + ".sh";
std::string strPath = std::move( iot_public::CFileUtil::getPathOfBinFile(strFileName));
if(strPath.empty())
{
//< 也许配置的是外部脚本,不在我们的路径中
strPath = strFileName;
}
QProcess::startDetached(QString("bash -c '%1'").arg(strPath.c_str()));
#endif
}
}
void ButtonForm::autoStartDlg()
{
QString strSysErr;
QString strHmiErr;
bool isHmi = false;
bool isSys = false;
isSys = iot_sys::isSysService(strSysErr);
#ifdef OS_WINDOWS
isHmi = iot_sys::isSysStartupVbsExist();
#endif
#ifdef OS_LINUX
isHmi = iot_sys::isHmiAutoStart(strHmiErr);
#endif
AutoStart *dlg =new AutoStart(isHmi,isSys,this);
int res =dlg->exec();
if(res == QDialog::Accepted)
{
QString text;
if(isSys != dlg->isSysAutoStart())
{
setSysAuto(!isSys,text);
}
if(isHmi != dlg->isHmiAutoStart())
{
setHmiAuto(!isHmi,text);
}
emit sigLogText(text);
}
delete dlg;
dlg =NULL;
}
void ButtonForm::dogCheckDlg()
{
DogCheck dogCheck;
dogCheck.exec();
}
void ButtonForm::dbCheckDlg()
{
DbCheck dbCheck;
dbCheck.exec();
}
void ButtonForm::setSysAuto(bool enable, QString &text)
{
QString strErr;
if(enable==true)
{
if(iot_sys::regSysService(strErr))
{
text+=tr("设置系统开机自启动成功...");
}
else
{
text+=tr("设置系统开机自启动失败...错误:%1").arg(strErr);
}
}
else if(enable==false)
{
if(iot_sys::unregSysService(strErr))
{
text+=tr("注销系统开机自启动成功...");
}
else
{
text+=tr("注销系统开机自启动失败......错误:%1").arg(strErr);
}
}
}
void ButtonForm::setHmiAuto(bool enable,QString &text)
{
QString strErr;
if(enable==true)
{
#ifdef OS_WINDOWS
if (iot_sys::setSysStartupAutoStartByVbs(strErr))
#endif
#ifdef OS_LINUX
if (iot_sys::setHmiAutoStart(strErr))
#endif
{
text+=tr("设置HMI开机自启动成功...");
}
else
{
text+=tr("设置HMI开机自启动失败...错误:%1").arg(strErr);
}
}
else if(enable==false)
{
#ifdef OS_WINDOWS
if (iot_sys::unsetSysStartupAutoStartByVbs(strErr))
#endif
#ifdef OS_LINUX
if (iot_sys::unsetHmiAutoStart(strErr))
#endif
{
text+=tr("注销HMI开机自启动成功...");
}
else
{
text+=tr("注销HMI开机自启动失败...错误:%1").arg(strErr);
}
}
}