DialogNewTask.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "DialogNewTask.h"
  2. #include "WindowAppMdiFrame.h"
  3. #include "TaskManager.h"
  4. #include "PouManager.h"
  5. #include "GvlManager.h"
  6. DialogNewTask::DialogNewTask(QWidget *parent)
  7. : QDialog(parent)
  8. {
  9. ui.setupUi(this);
  10. // 对话框初始化
  11. initUI();
  12. }
  13. DialogNewTask::~DialogNewTask()
  14. {
  15. }
  16. /// <summary>
  17. /// 对话框初始化
  18. /// </summary>
  19. void DialogNewTask::initUI()
  20. {
  21. this->setWindowTitle(("Add New Task"));
  22. this->setAttribute(Qt::WA_QuitOnClose);
  23. this->setWindowModality(Qt::ApplicationModal);
  24. ui.editName->setText(("Task1"));
  25. // 初始化界面中的任务分类下拉框(目前是三个分类)
  26. ui.comboType->addItem("系统内置");
  27. ui.comboType->addItem("硬件组态");
  28. ui.comboType->addItem("变量触发");
  29. // 设置默认选项
  30. ui.comboType->setCurrentIndex(0);
  31. this->onTaskTypeComboChanged(0);
  32. // 初始化Priority的ComboBox
  33. for (int i = 0; i < VPGlobal::emPriority.keyCount(); i++)
  34. {
  35. ui.comboPriority->addItem(VPGlobal::emPriority.key(i));
  36. }
  37. ui.comboPriority->setCurrentIndex(3);
  38. ui.comboPriority->setStyleSheet("QComboBox{combobox-popup:0;}");
  39. // 设置界面默认参数
  40. // ui.editPriority->setText(QString::number(m_TaskOption.execParams.nPriority));
  41. ui.editInterval->setText(QString::number(m_TaskOption.execParams.nPostDelay));
  42. // 设置如下三个输入框只能输入数字
  43. QRegExp regExp("[0-9]+$");
  44. ui.editWatchdog->setValidator(new QRegExpValidator(regExp, this));
  45. ui.editTimeOut->setValidator(new QRegExpValidator(regExp, this));
  46. ui.editInterval->setValidator(new QRegExpValidator(regExp, this));
  47. // 设置看门狗
  48. ui.checkWatchdog->setChecked(m_TaskOption.execParams.bWatchDog);
  49. ui.editWatchdog->setText(QString::number(m_TaskOption.execParams.nWatchDog));
  50. if (!m_TaskOption.execParams.bWatchDog)
  51. {
  52. ui.editWatchdog->setEnabled(false);
  53. }
  54. ui.editTimeOut->setText(QString::number(m_TaskOption.timeOut));
  55. // 槽函数
  56. connect(ui.createButton, SIGNAL(clicked()), this, SLOT(onButtonCreateClicked()));
  57. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
  58. connect(ui.checkWatchdog, SIGNAL(stateChanged(int)), this, SLOT(onWatchdogCheckboxChanged(int)));
  59. connect(ui.comboType, SIGNAL(currentIndexChanged(int)), this, SLOT(onTaskTypeComboChanged(int)));
  60. // 设置固定窗体大小
  61. this->setFixedSize(495, 500);
  62. //this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
  63. }
  64. ///// <summary>
  65. ///// 初始化界面中的Event列表
  66. ///// </summary>
  67. //void DialogNewTask::initTaskModeList()
  68. //{
  69. // // 搜集系统中的Event,展示到列表中
  70. //
  71. // // 系统事件
  72. // for (EVENT * pEvent : g_pTaskManager->m_sysModes)
  73. // {
  74. // ui.comboMode->addItem(pEvent->strName);
  75. // }
  76. //
  77. // // 硬件事件
  78. // POU* pHdwPou = g_pPouManager->getPouByName(GROUP_NAME_HARDWARE);
  79. // if (pHdwPou != nullptr)
  80. // {
  81. // QMap<QString, EVENT*> pHdwEvents = g_pPouManager->getPouByName(GROUP_NAME_HARDWARE)->getAllEvents();
  82. //
  83. // for (EVENT * pEvent : pHdwEvents)
  84. // {
  85. // ui.comboMode->addItem(pEvent->strFullName);
  86. // }
  87. // }
  88. //
  89. // ui.comboMode->setCurrentIndex(0);
  90. //}
  91. /// <summary>
  92. /// Create按钮
  93. /// </summary>
  94. void DialogNewTask::onButtonCreateClicked()
  95. {
  96. // 获取用户输入并检查是否有效
  97. // Task名称
  98. QString taskName = ui.editName->text();
  99. if (taskName.isEmpty())
  100. {
  101. Utility::VPCriticalMessageBox(("The task name is invalid!"));
  102. return;
  103. }
  104. if (VPGlobal::getMdiFrame()->findSubView(taskName) != nullptr)
  105. {
  106. Utility::VPCriticalMessageBox(("The task name is duplicated!"));
  107. return;
  108. }
  109. // 优先级
  110. int nPriority = ui.comboPriority->currentIndex();
  111. // 间隔
  112. int nPostDelay = ui.editInterval->text().toInt();
  113. if (nPostDelay < 0)
  114. {
  115. Utility::VPCriticalMessageBox("The interval must >0 .");
  116. return;
  117. }
  118. // 看门狗
  119. bool bWatch = ui.checkWatchdog->isChecked();
  120. int nWatchDog = ui.editWatchdog->text().toInt();
  121. if (bWatch && nWatchDog <= 0)
  122. {
  123. Utility::VPCriticalMessageBox("The watchdog must >0 .");
  124. return;
  125. }
  126. // 保存用户输入的内容
  127. m_TaskOption.strName = taskName;
  128. m_TaskOption.execParams.nPriority = (VPEnum::PRIORITY)nPriority;
  129. m_TaskOption.execParams.nPostDelay = nPostDelay;
  130. // 2022-9-1 增加了任务模式的分类
  131. m_TaskOption.modeType = static_cast<TASK_MODE_TYPE>(ui.comboType->currentIndex());
  132. m_TaskOption.strModeName =ui.comboMode->currentText();
  133. m_TaskOption.execParams.bWatchDog = bWatch;
  134. m_TaskOption.execParams.nWatchDog = nWatchDog;
  135. // 2021-9-29 修复了之前使用的toInt()在release模式下数值错误导致event未等待的问题
  136. m_TaskOption.timeOut = ui.editTimeOut->text().toULong();
  137. // 根据Mode设置Event
  138. m_TaskOption.bindEventByMode();
  139. this->accept();
  140. }
  141. /// <summary>
  142. /// 当任务类别下拉框变动时,同步变动下面的任务类型分类
  143. /// </summary>
  144. /// <param name="index"></param>
  145. void DialogNewTask::onTaskTypeComboChanged(int index)
  146. {
  147. // 先清空然后重新添加
  148. ui.comboMode->clear();
  149. TASK_MODE_TYPE modeType = static_cast<TASK_MODE_TYPE>(index);
  150. // 0. 系统内置
  151. if (modeType == TASK_MODE_TYPE::MODE_SYS)
  152. {
  153. for (EVENT * pEvent : g_pTaskManager->m_sysEvents)
  154. {
  155. ui.comboMode->addItem(pEvent->strName);
  156. }
  157. }
  158. // 1. 硬件组态
  159. else if (modeType == TASK_MODE_TYPE::MODE_HDW)
  160. {
  161. POU* pHdwPou = g_pPouManager->getPouByName(GROUP_NAME_HARDWARE);
  162. if (pHdwPou != nullptr)
  163. {
  164. QMap<QString, EVENT*> pHdwEvents = g_pPouManager->getPouByName(GROUP_NAME_HARDWARE)->getAllEvents();
  165. for (EVENT* pEvent : pHdwEvents)
  166. {
  167. ui.comboMode->addItem(pEvent->strFullName);
  168. }
  169. }
  170. }
  171. // 2. 变量触发(目前是所有带SyncValueEvent的)
  172. else if (modeType == TASK_MODE_TYPE::MODE_VAL)
  173. {
  174. // 获取所有具有Event触发特性的变量
  175. QHash<QString, VARIABLE*> eventVars = g_pGvlManager->getAllEventVariables();
  176. QHashIterator<QString, VARIABLE*> it(eventVars);
  177. while (it.hasNext())
  178. {
  179. ui.comboMode->addItem(it.next().key());
  180. }
  181. }
  182. // Error,不应该执行到这里
  183. else
  184. {
  185. }
  186. }
  187. /// <summary>
  188. /// 看门狗的Checkbox切换时需要切换下面的编辑框是否可用
  189. /// </summary>
  190. /// <param name="state"></param>
  191. void DialogNewTask::onWatchdogCheckboxChanged(int state)
  192. {
  193. if (state == Qt::Checked)
  194. {
  195. ui.editWatchdog->setEnabled(true);
  196. }
  197. else
  198. {
  199. ui.editWatchdog->setEnabled(false);
  200. }
  201. }
  202. /// <summary>
  203. ///
  204. /// </summary>
  205. /// <param name="nNunber"></param>
  206. void DialogNewTask::setDefaultName(QString strName)
  207. {
  208. ui.editName->setText(strName);
  209. }