HM-SPMS/product/src/tools/workBench/ButtonForm.cpp

355 lines
8.7 KiB
C++
Raw Normal View History

2025-03-13 15:21:23 +08:00

#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 "InputDialog.h"
#include <QToolButton>
#include <QIcon>
#include <QProcess>
#include <QHBoxLayout>
#include <QString>
#include <QScrollArea>
#include "pub_utility_api/FileUtil.h"
#include "../include/sys/sys_login_api/CLoginDlg.h"
using namespace workBench;
ButtonForm::ButtonForm(int mode, QString language ,QWidget *parent):
QWidget(parent),
ui(new Ui::ButtonForm),
m_mode(mode),
m_language(language)
{
ui->setupUi(this);
}
ButtonForm::~ButtonForm()
{
delete ui;
}
void ButtonForm::setTitleHide(bool hide)
{
ui->label->setHidden(hide);
}
void ButtonForm::addToolButtons(QVector<QString> &buttonVec ,int groupIndex)
{
// QHBoxLayout *layOut = new QHBoxLayout(ui->widget);
// layOut->setMargin(0);
// layOut->setSpacing(30);
QWidget *scrollWidget = new QWidget(this);
QHBoxLayout *scrollLayout = new QHBoxLayout(scrollWidget);
scrollLayout->setContentsMargins(0, 0, 0, 0);
//scrollLayout->setSpacing(30);
QScrollArea *scrollArea = new QScrollArea(ui->widget);
scrollArea->setWidget(scrollWidget);
scrollArea->setWidgetResizable(true);
if(m_mode == EN_SYS_STARTUP_PAGE1)
{
ui->frame->setObjectName("frame_page1");
}else
{
ui->frame->setObjectName("frame_page3");
//scrollLayout->setContentsMargins(30, 0, 0, 0);
}
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(scrollWidget);
toolBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
QIcon icon = QIcon(buttonInfo.m_icon);
QSize size(45,45);
toolBtn->setIconSize(size);
toolBtn->setMaximumHeight(80);
toolBtn->setMinimumHeight(80);
toolBtn->setMaximumWidth(80);
toolBtn->setMinimumWidth(80);
toolBtn->setIcon(icon);
toolBtn->setText(buttonInfo.m_desc);
if( "custom" == buttonInfo.m_param)
{
toolBtn->setObjectName("customButton");
toolBtn->setProperty("groupIndex" , groupIndex);
}
connect(toolBtn,&QToolButton::clicked, this ,[this , toolBtn ,buttonInfo](){
executeTask(buttonInfo.m_cmd,buttonInfo.m_param , toolBtn);
});
scrollLayout->addWidget(toolBtn);
}
if(m_mode == EN_SYS_STARTUP_PAGE3)
{
scrollLayout->addStretch();
scrollLayout->setSpacing(30);
}
ui->widget->setLayout(new QVBoxLayout());
ui->widget->layout()->addWidget(scrollArea);
}
void ButtonForm::setTitleText(const QString &title)
{
ui->label->setText(title);
}
void ButtonForm::executeTask(QString cmd,QString param , QToolButton *btn)
{
if(cmd =="DogCheck")
{
dogCheckDlg();
return ;
}
if(cmd =="DbCheck")
{
dbCheckDlg();
return ;
}
if(cmd =="AutoStart")
{
autoStartDlg();
return ;
}
if( cmd == "Custom")
{
customTools(btn);
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(this);
dogCheck.exec();
}
void ButtonForm::dbCheckDlg()
{
DbCheck dbCheck(this);
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);
}
}
}
void ButtonForm::customTools( QToolButton *btn )
{
QString name = "";
QString picturePath = "";
QString exePath = "";
int groupIndex = -1;
InputDialog *dialog = new InputDialog(this);
if( QDialog::Accepted == dialog->exec())
{
dialog->getDataInput(name , picturePath , exePath);
//修改json文件
workBench::SButtonInfo buttonInfo;
buttonInfo.m_desc = name;
buttonInfo.m_icon = picturePath;
buttonInfo.m_cmd = exePath;
//获取哪一个自定义按钮
groupIndex = btn->property("groupIndex").toInt();
DataMng::instance()->addPage3ButtonInfo(groupIndex ,buttonInfo);
//修改成功后更改控件
emit sigUpdataButton(EN_SYS_STARTUP_PAGE3);
}
delete dialog;
dialog = NULL;
}