#include "DialogNewTask.h"
#include "WindowAppMdiFrame.h"
#include "TaskManager.h"
#include "PouManager.h"
#include "GvlManager.h"
DialogNewTask::DialogNewTask(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
// 对话框初始化
initUI();
}
DialogNewTask::~DialogNewTask()
{
}
///
/// 对话框初始化
///
void DialogNewTask::initUI()
{
this->setWindowTitle(("Add New Task"));
this->setAttribute(Qt::WA_QuitOnClose);
this->setWindowModality(Qt::ApplicationModal);
ui.editName->setText(("Task1"));
// 初始化界面中的任务分类下拉框(目前是三个分类)
ui.comboType->addItem("系统内置");
ui.comboType->addItem("硬件组态");
ui.comboType->addItem("变量触发");
// 设置默认选项
ui.comboType->setCurrentIndex(0);
this->onTaskTypeComboChanged(0);
// 初始化Priority的ComboBox
for (int i = 0; i < VPGlobal::emPriority.keyCount(); i++)
{
ui.comboPriority->addItem(VPGlobal::emPriority.key(i));
}
ui.comboPriority->setCurrentIndex(3);
ui.comboPriority->setStyleSheet("QComboBox{combobox-popup:0;}");
// 设置界面默认参数
// ui.editPriority->setText(QString::number(m_TaskOption.execParams.nPriority));
ui.editInterval->setText(QString::number(m_TaskOption.execParams.nPostDelay));
// 设置如下三个输入框只能输入数字
QRegExp regExp("[0-9]+$");
ui.editWatchdog->setValidator(new QRegExpValidator(regExp, this));
ui.editTimeOut->setValidator(new QRegExpValidator(regExp, this));
ui.editInterval->setValidator(new QRegExpValidator(regExp, this));
// 设置看门狗
ui.checkWatchdog->setChecked(m_TaskOption.execParams.bWatchDog);
ui.editWatchdog->setText(QString::number(m_TaskOption.execParams.nWatchDog));
if (!m_TaskOption.execParams.bWatchDog)
{
ui.editWatchdog->setEnabled(false);
}
ui.editTimeOut->setText(QString::number(m_TaskOption.timeOut));
// 槽函数
connect(ui.createButton, SIGNAL(clicked()), this, SLOT(onButtonCreateClicked()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
connect(ui.checkWatchdog, SIGNAL(stateChanged(int)), this, SLOT(onWatchdogCheckboxChanged(int)));
connect(ui.comboType, SIGNAL(currentIndexChanged(int)), this, SLOT(onTaskTypeComboChanged(int)));
// 设置固定窗体大小
this->setFixedSize(495, 500);
//this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
}
/////
///// 初始化界面中的Event列表
/////
//void DialogNewTask::initTaskModeList()
//{
// // 搜集系统中的Event,展示到列表中
//
// // 系统事件
// for (EVENT * pEvent : g_pTaskManager->m_sysModes)
// {
// ui.comboMode->addItem(pEvent->strName);
// }
//
// // 硬件事件
// POU* pHdwPou = g_pPouManager->getPouByName(GROUP_NAME_HARDWARE);
// if (pHdwPou != nullptr)
// {
// QMap pHdwEvents = g_pPouManager->getPouByName(GROUP_NAME_HARDWARE)->getAllEvents();
//
// for (EVENT * pEvent : pHdwEvents)
// {
// ui.comboMode->addItem(pEvent->strFullName);
// }
// }
//
// ui.comboMode->setCurrentIndex(0);
//}
///
/// Create按钮
///
void DialogNewTask::onButtonCreateClicked()
{
// 获取用户输入并检查是否有效
// Task名称
QString taskName = ui.editName->text();
if (taskName.isEmpty())
{
Utility::VPCriticalMessageBox(("The task name is invalid!"));
return;
}
if (VPGlobal::getMdiFrame()->findSubView(taskName) != nullptr)
{
Utility::VPCriticalMessageBox(("The task name is duplicated!"));
return;
}
// 优先级
int nPriority = ui.comboPriority->currentIndex();
// 间隔
int nPostDelay = ui.editInterval->text().toInt();
if (nPostDelay < 0)
{
Utility::VPCriticalMessageBox("The interval must >0 .");
return;
}
// 看门狗
bool bWatch = ui.checkWatchdog->isChecked();
int nWatchDog = ui.editWatchdog->text().toInt();
if (bWatch && nWatchDog <= 0)
{
Utility::VPCriticalMessageBox("The watchdog must >0 .");
return;
}
// 保存用户输入的内容
m_TaskOption.strName = taskName;
m_TaskOption.execParams.nPriority = (VPEnum::PRIORITY)nPriority;
m_TaskOption.execParams.nPostDelay = nPostDelay;
// 2022-9-1 增加了任务模式的分类
m_TaskOption.modeType = static_cast(ui.comboType->currentIndex());
m_TaskOption.strModeName =ui.comboMode->currentText();
m_TaskOption.execParams.bWatchDog = bWatch;
m_TaskOption.execParams.nWatchDog = nWatchDog;
// 2021-9-29 修复了之前使用的toInt()在release模式下数值错误导致event未等待的问题
m_TaskOption.timeOut = ui.editTimeOut->text().toULong();
// 根据Mode设置Event
m_TaskOption.bindEventByMode();
this->accept();
}
///
/// 当任务类别下拉框变动时,同步变动下面的任务类型分类
///
///
void DialogNewTask::onTaskTypeComboChanged(int index)
{
// 先清空然后重新添加
ui.comboMode->clear();
TASK_MODE_TYPE modeType = static_cast(index);
// 0. 系统内置
if (modeType == TASK_MODE_TYPE::MODE_SYS)
{
for (EVENT * pEvent : g_pTaskManager->m_sysEvents)
{
ui.comboMode->addItem(pEvent->strName);
}
}
// 1. 硬件组态
else if (modeType == TASK_MODE_TYPE::MODE_HDW)
{
POU* pHdwPou = g_pPouManager->getPouByName(GROUP_NAME_HARDWARE);
if (pHdwPou != nullptr)
{
QMap pHdwEvents = g_pPouManager->getPouByName(GROUP_NAME_HARDWARE)->getAllEvents();
for (EVENT* pEvent : pHdwEvents)
{
ui.comboMode->addItem(pEvent->strFullName);
}
}
}
// 2. 变量触发(目前是所有带SyncValueEvent的)
else if (modeType == TASK_MODE_TYPE::MODE_VAL)
{
// 获取所有具有Event触发特性的变量
QHash eventVars = g_pGvlManager->getAllEventVariables();
QHashIterator it(eventVars);
while (it.hasNext())
{
ui.comboMode->addItem(it.next().key());
}
}
// Error,不应该执行到这里
else
{
}
}
///
/// 看门狗的Checkbox切换时需要切换下面的编辑框是否可用
///
///
void DialogNewTask::onWatchdogCheckboxChanged(int state)
{
if (state == Qt::Checked)
{
ui.editWatchdog->setEnabled(true);
}
else
{
ui.editWatchdog->setEnabled(false);
}
}
///
///
///
///
void DialogNewTask::setDefaultName(QString strName)
{
ui.editName->setText(strName);
}