ToolDialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include "ToolDialog.h"
  2. QEvent::Type SyncControlEvent::m_EventType = QEvent::None;
  3. QEvent::Type SyncHdValueEvent::m_EventType = QEvent::None;
  4. ToolDialogImpl::ToolDialogImpl(QWidget *parent)
  5. : DllToolDialog(parent)
  6. {
  7. ui.setupUi(this);
  8. this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
  9. // connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI())/*, Qt::QueuedConnection*/);
  10. //m_Value = 0;
  11. //m_Value2 = 20;
  12. //m_Value3 = 0;
  13. m_intInput = 10;
  14. m_intOutput = 3;
  15. m_nIndex = 0;
  16. m_nIndex2 = 0;
  17. m_strInput = "strInput";
  18. m_strOutput = "strOutput";
  19. //m_intValuePtr = 200;
  20. m_pInt = nullptr;
  21. // 为导出的测试控件赋值
  22. m_expCheck = ui.expCheck;
  23. m_expCombo = ui.expCombo;
  24. m_expList = ui.expList;
  25. m_expRadio = ui.expRadio;
  26. // m_expText = ui.expText;
  27. m_expButton = ui.expButton;
  28. m_expEdit = ui.expEdit;
  29. // 初始化Combo和List供测试
  30. QStringList comList;
  31. comList << "Com1" << "Com2" << "Com3";
  32. m_expCombo->addItems(comList);
  33. m_expCombo->setCurrentIndex(0);
  34. QStringList testList;
  35. testList << "List1" << "List2" << "List3";
  36. m_expList->insertItems(0, testList);
  37. m_expList->setCurrentRow(0);
  38. // 设置Timer
  39. m_Timer.setInterval(20000);
  40. m_Timer2.setInterval(40000);
  41. connect(&m_Timer, SIGNAL(timeout()), this, SLOT(onTimer()));
  42. connect(&m_Timer2, SIGNAL(timeout()), this, SLOT(onTimer2()));
  43. m_Timer.start();
  44. m_Timer2.start();
  45. // 绑定测试LineEdit变动信号
  46. connect(ui.expEdit, SIGNAL(textChanged(QString)), this, SLOT(OnTestEditChange(QString)));
  47. // 绑定测试ComboBox变动信号
  48. connect(ui.expCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(OnTestComboChange(int)));
  49. // 绑定测试ListBox变动信号
  50. connect(ui.expList, SIGNAL(currentRowChanged(int)), this, SLOT(OnTestListChange(int)));
  51. // 绑定测试RadioBox变动信号
  52. connect(ui.expRadio, SIGNAL(toggled(bool)), this, SLOT(OnTestRadioChange(bool)));
  53. // 绑定测试CheckBox变动信号
  54. connect(ui.expCheck, SIGNAL(stateChanged(int)), this, SLOT(OnTestCheckChange(int)));
  55. on_pushButton_clicked();
  56. //// 模拟延迟
  57. //Utility::qSleep(3000);
  58. }
  59. ToolDialogImpl::~ToolDialogImpl()
  60. {
  61. }
  62. VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
  63. {
  64. int a = 100;
  65. // 测试索引连接,每次+1
  66. m_nIndex++;
  67. if (m_nIndex >= 6)
  68. {
  69. m_nIndex = 0;
  70. }
  71. m_nIndex2++;
  72. if (m_nIndex2 >= 4)
  73. {
  74. m_nIndex2 = 0;
  75. }
  76. //m_Value3 = m_Value + m_Value2;
  77. // qDebug() << "[Dll] ValueTest tool Execute start.";
  78. // 为了测试加一个延时
  79. //Utility::qSleep(2000);
  80. // for test
  81. m_intInput += 10;
  82. m_intOutput = m_intInput + 10;
  83. m_strInput += "p";
  84. m_strOutput = m_strInput + "p";
  85. //n += 10;
  86. //m_strOutput = m_strInput + str;
  87. //str += "a";
  88. //if (m_pInt != nullptr)
  89. //{
  90. // *m_pInt = *m_pInt + 20;
  91. //}
  92. on_pushButton_clicked();
  93. // qDebug() << "[Dll] ValueTest tool Execute finished.";
  94. QDateTime time = QDateTime::currentDateTime();
  95. m_strOutput = time.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
  96. // 测试硬件工具的接口更新(这种方法仅可用于硬件工具)
  97. this->syncHdValueToUi(&m_intOutput);
  98. //// 将变动的变量统一加入到event参数中
  99. //
  100. //SyncValueEvent* valueEvent = new SyncValueEvent();
  101. //// 数值变动1
  102. //SYNC_VALUE syncValue1;
  103. //syncValue1.m_pSrcValue = &m_intOutput;
  104. //syncValue1.m_ValueType = VALUE_TYPE::Type_Int;
  105. //valueEvent->addSyncValue(syncValue1);
  106. //// 数值变动2
  107. //SYNC_VALUE syncValue2;
  108. //syncValue1.m_pSrcValue = &m_strOutput;
  109. //syncValue2.m_ValueType = VALUE_TYPE::Type_String;
  110. //valueEvent->addSyncValue(syncValue2);
  111. //// 发送 value 同步 event
  112. //QCoreApplication::sendEvent(m_pSyncTarget, valueEvent);
  113. return VPEnum::RETURN_VALUE::Success;
  114. }
  115. void ToolDialogImpl::Running(bool bRun)
  116. {
  117. }
  118. bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
  119. {
  120. int paranum;//参数数量
  121. if (bIsOut)//保存参数流程
  122. {
  123. paranum = 1;
  124. ar << paranum;//先保存参数数量
  125. ar << (int)1 << m_intInput;
  126. }
  127. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  128. {
  129. int para;
  130. ar >> paranum;//读取参数数量
  131. for (int i = 0; i < paranum; i++)
  132. {
  133. ar >> para;
  134. switch (para)
  135. {
  136. case 1: ar >> m_intInput; break;
  137. default:
  138. {
  139. qWarning() << "Serialized(In) Error";
  140. return false;
  141. }
  142. break;
  143. }
  144. }
  145. //hwndUnit->getHWndCtrl()->usem_pRoiController(&m_pRoiController);
  146. }
  147. return true;
  148. }
  149. //void ToolDialogImpl::on_UpdateUI()
  150. //{
  151. // // 2021-10-24,Tool execute完毕之后需要向UI同步数值变更(有几个数值变更就写几个)
  152. //
  153. // if (valueCallback != nullptr)
  154. // {
  155. // // 向UI同步
  156. // valueCallback(&m_intOutput, VALUE_TYPE::Type_Int);
  157. //
  158. // valueCallback(&m_strOutput, VALUE_TYPE::Type_String);
  159. //
  160. //
  161. // qDebug() << "ValueTest dll value change [m_intOutput] - " << m_intOutput;
  162. // }
  163. // else
  164. // {
  165. // qDebug() << "ValueTest dll value change [m_intOutput] - " << m_intOutput << ", but valueCallback is nullptr";
  166. // }
  167. //
  168. //}
  169. /// <summary>
  170. /// 确定
  171. /// </summary>
  172. void ToolDialogImpl::on_btnOK_clicked()
  173. {
  174. this->hide();
  175. }
  176. /// <summary>
  177. /// 取消按钮
  178. /// </summary>
  179. void ToolDialogImpl::on_btnCancel_clicked()
  180. {
  181. // 将本工具的恢复到打开工具之前的状态
  182. RecoverData();
  183. }
  184. /// <summary>
  185. /// 测试按钮
  186. /// </summary>
  187. void ToolDialogImpl::on_btnExecute_clicked()
  188. {
  189. QElapsedTimer toolTimer;
  190. toolTimer.start();
  191. // 发送事件
  192. ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
  193. QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
  194. // 统计返回值
  195. VPEnum::RETURN_VALUE ret = pToolEvent->ret;
  196. double nExecTime = toolTimer.elapsed();
  197. QString str;
  198. str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
  199. ui.label_time->setText(str);
  200. str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
  201. ui.label_state->setText(str);
  202. delete pToolEvent;
  203. }
  204. /// <summary>
  205. /// Refresh Button
  206. /// </summary>
  207. void ToolDialogImpl::on_pushButton_clicked()
  208. {
  209. // QMessageBox::information(nullptr, "Value Test", "Refresh Button", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
  210. // 将每个按钮的最新值写入到控件中
  211. ui.editIntInput->setText(QString::number(m_intInput));
  212. ui.editIntOutput->setText(QString::number(m_intOutput));
  213. ui.editStringInput->setText(m_strInput);
  214. ui.editStringOutput->setText(m_strOutput);
  215. //ui.editIntP->setText(QString::asprintf("%0X", m_pInt));
  216. //ui.editIntP2->setText(QString::number(*m_pInt));
  217. if (m_pInt != nullptr)
  218. {
  219. ui.editIntP->setText(QString::asprintf("%0X", m_pInt));
  220. ui.editIntP2->setText(QString::number(*m_pInt));
  221. }
  222. }
  223. /// <summary>
  224. /// Update Button,将界面中的值写入到变量中
  225. /// </summary>
  226. void ToolDialogImpl::on_updateButton_clicked()
  227. {
  228. m_intInput = ui.editIntInput->text().toInt();
  229. m_intOutput = ui.editIntOutput->text().toInt();
  230. m_strInput = ui.editStringInput->text();
  231. m_strOutput = ui.editStringOutput->text();
  232. // *m_pInt = ui.editIntP2->text().toInt();
  233. if (m_pInt != nullptr)
  234. {
  235. *m_pInt = ui.editIntP2->text().toInt();
  236. }
  237. QMessageBox::information(nullptr, "Value Test", "Update ok!");
  238. }
  239. /// <summary>
  240. /// ExpButton设置
  241. /// </summary>
  242. void ToolDialogImpl::on_expButton_clicked()
  243. {
  244. QMessageBox::information(nullptr, "Value Test Dialog", "Export Button!");
  245. }
  246. /// <summary>
  247. /// 发送事件测试
  248. /// </summary>
  249. void ToolDialogImpl::on_btnSendEvent_clicked()
  250. {
  251. if (m_pEventTarget != nullptr)
  252. {
  253. // 2022-9-19MEMO:此处的Event会发送到TaskManager中,以 TRIGGER_TASK_THREAD 的方式执行,
  254. // 然后触发到已经启动并且处于等待状态的Task自动执行
  255. QCoreApplication::sendEvent(m_pEventTarget, &m_eventOut);
  256. vDebug() << "Send event from ValueTest dll...";
  257. }
  258. }
  259. void ToolDialogImpl::OnTestEditChange(const QString& str)
  260. {
  261. // qDebug() << "ValueTest dll OnTestEditChange() - " << str;
  262. if (controlCallback != nullptr)
  263. {
  264. // 向UI同步(回调方式)
  265. // controlCallback(ui.expEdit, UI_SYNC_MSG::EDIT_TEXT_CHANGED);
  266. // 向UI同步 postEvent方式
  267. this->syncControlToUi(ui.expEdit, UI_SYNC_MSG::EDIT_TEXT_CHANGED);
  268. vDebug() << "ValueTest dll OnTestEditChange() - " << str;
  269. }
  270. else
  271. {
  272. vDebug() << "ValueTest dll OnTestEditChange() - " << str << ", but callback is nullptr";
  273. }
  274. }
  275. /// <summary>
  276. /// 测试ComboBox变更消息
  277. /// </summary>
  278. /// <param name="nIndex"></param>
  279. void ToolDialogImpl::OnTestComboChange(const int nIndex)
  280. {
  281. if (controlCallback != nullptr)
  282. {
  283. // 向UI同步(回调方式)
  284. // controlCallback(ui.expCombo, UI_SYNC_MSG::COMBO_SEL_CHANGED);
  285. // 向UI同步 postEvent方式
  286. this->syncControlToUi(ui.expCombo, UI_SYNC_MSG::COMBO_SEL_CHANGED);
  287. vDebug() << "ValueTest dll OnTestComboChange()";
  288. }
  289. else
  290. {
  291. vDebug() << "ValueTest dll OnTestComboChange() , but callback is nullptr";
  292. }
  293. }
  294. /// <summary>
  295. /// 测试ListBox变更消息
  296. /// </summary>
  297. void ToolDialogImpl::OnTestListChange(int nRow)
  298. {
  299. if (controlCallback != nullptr)
  300. {
  301. // 向UI同步(回调方式)
  302. // controlCallback(ui.expList, UI_SYNC_MSG::LIST_SEL_CHANGED);
  303. // 向UI同步 postEvent方式
  304. this->syncControlToUi(ui.expList, UI_SYNC_MSG::LIST_SEL_CHANGED);
  305. vDebug() << "ValueTest dll OnTestListChange() - index : " << nRow;
  306. }
  307. else
  308. {
  309. vDebug() << "ValueTest dll OnTestListChange() , but callback is nullptr";
  310. }
  311. }
  312. /// <summary>
  313. /// 测试RadioBox变更消息
  314. /// </summary>
  315. void ToolDialogImpl::OnTestRadioChange(bool checked)
  316. {
  317. if (controlCallback != nullptr)
  318. {
  319. // 向UI同步(回调方式)
  320. controlCallback(ui.expRadio, UI_SYNC_MSG::RADIOBOX_CHANGED);
  321. // 向UI同步(postEvent方式)
  322. this->syncControlToUi(ui.expRadio, UI_SYNC_MSG::RADIOBOX_CHANGED);
  323. vDebug() << "ValueTest dll OnTestRadioChange() - checked : " << checked;
  324. }
  325. else
  326. {
  327. vDebug() << "ValueTest dll OnTestRadioChange() , but callback is nullptr";
  328. }
  329. }
  330. /// <summary>
  331. /// 测试CheckBox变更消息
  332. /// </summary>
  333. void ToolDialogImpl::OnTestCheckChange(int state)
  334. {
  335. if (controlCallback != nullptr)
  336. {
  337. // 向UI同步(回调方式)
  338. // controlCallback(ui.expCheck, UI_SYNC_MSG::CHECKBOX_CHANGED);
  339. // 向UI同步(postEvent方式)
  340. this->syncControlToUi(ui.expCheck, UI_SYNC_MSG::CHECKBOX_CHANGED);
  341. vDebug() << "ValueTest dll OnTestCheckChange() - state : " << state;
  342. }
  343. else
  344. {
  345. vDebug() << "ValueTest dll OnTestCheckChange() , but callback is nullptr";
  346. }
  347. }
  348. void ToolDialogImpl::onTimer()
  349. {
  350. // 触发工具内部事件供测试
  351. if (m_pEventTarget != nullptr)
  352. {
  353. // 此处的event变量会导致堆栈崩溃问题,临时屏蔽
  354. // 2022-0-19 原因找到了,因为postEvent是会自动释放Event变量,所以自动释放 m_eventOut 的时候会崩溃
  355. //QCoreApplication::postEvent(m_pEventTarget, &m_eventOut);
  356. }
  357. // qDebug() << "[Dll] m_eventOut wakeAll.";
  358. // 激活事件
  359. //m_eventOut.wakeAll();
  360. }
  361. void ToolDialogImpl::onTimer2()
  362. {
  363. // 触发工具内部事件供测试
  364. if (m_pEventTarget != nullptr)
  365. {
  366. // QCoreApplication::postEvent(m_pEventTarget, &m_eventOut2);
  367. }
  368. // qDebug() << "[Dll] m_eventOut2 wakeAll.";
  369. // 激活事件
  370. //m_eventOut2.wakeAll();
  371. }