ToolDialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #include "ToolDialog.h"
  2. #include <QImage>
  3. #include <QFileInfo>
  4. #include <QFileDialog>
  5. #include <QElapsedTimer>
  6. #include <QGridLayout>
  7. #include "toolinterface.h"
  8. #include "Tools/ThresholdTool.h"
  9. #include "Tools/DynthresholdTool.h"
  10. #include "Tools/MeanfilterTool.h"
  11. #include "Tools/MedianfilterTool.h"
  12. #include "Tools/GaussfilterTool.h"
  13. #include "Tools/GraydilationTool.h"
  14. #include "Tools/GrayerosionTool.h"
  15. #include "Tools/EmphasizeTool.h"
  16. #include "Tools/IlluminatetoolTool.h"
  17. #include "Tools/LightTool.h"
  18. #include "Tools/GrayopenTool.h"
  19. #include "Tools/GraycloseTool.h"
  20. #include "Tools/InvertcolorTool.h"
  21. #include "Tools/ImageconvertTool.h"
  22. #include "Tools/MirrorTool.h"
  23. #include "Tools/RotateTool.h"
  24. #include "Tools/ChangesizeTool.h"
  25. ToolDialogImpl::ToolDialogImpl(QWidget *parent, DllTool* pDllTool)
  26. : DllToolDialog(parent)
  27. {
  28. ui.setupUi(this);
  29. this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
  30. connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI()));
  31. connect(ui.tableWidget, SIGNAL(sigClick(bool) ), this, SLOT(clickSlot(bool) ));
  32. // 显示控件初始化
  33. hwndUnit = new HWndUnit(this);
  34. Util::ShowUnitInWidget(ui.Image_widget, hwndUnit);
  35. // hwndUnit->getHWndCtrl()->useROIController(&roiController);
  36. // connect(&roiController, SIGNAL(ROIChange(const ViewMessage)), this, SLOT(on_ROIChange(const ViewMessage)));
  37. //
  38. //
  39. // ROIRect1* roi = new ROIRect1();
  40. // roi->setTitle("搜索区域");
  41. // roiController.setROISign(ROIOperation::Negative);
  42. // roiController.setROIShape(roi);
  43. // roiController.mouseDownAction(500, 500);
  44. QString strPath = QCoreApplication::applicationDirPath() + "/ocv1.bmp";
  45. try
  46. {
  47. m_Image.ReadImage(strPath.toStdString().c_str());
  48. hwndUnit->ShowImage(m_Image);
  49. hwndUnit->Refresh(false);
  50. }
  51. catch (...)
  52. {
  53. }
  54. //////////////////////////////////////////////////////////////////////////
  55. ui.checkBox->setChecked(true);
  56. //ui.tableWidget->setMouseTracking(true);
  57. QMenu* menu = new QMenu();
  58. QMenu* binarization = menu->addMenu("二值化");
  59. binarization->addAction("二值化", this, SLOT(ActionSlot()));
  60. binarization->addAction("均值二值化", this, SLOT(ActionSlot()));
  61. QMenu* filter = menu->addMenu("滤波");
  62. filter->addAction("均值滤波", this, SLOT(ActionSlot()));
  63. filter->addAction("中值滤波", this, SLOT(ActionSlot()));
  64. filter->addAction("高斯滤波", this, SLOT(ActionSlot()));
  65. QMenu* orphological = menu->addMenu("形态学运算");
  66. orphological->addAction("灰度膨胀", this, SLOT(ActionSlot()));
  67. orphological->addAction("灰度腐蚀", this, SLOT(ActionSlot()));
  68. QMenu* imageenhance = menu->addMenu("图像增强");
  69. imageenhance->addAction("锐化", this, SLOT(ActionSlot()));
  70. imageenhance->addAction("对比度", this, SLOT(ActionSlot()));
  71. imageenhance->addAction("亮度调节", this, SLOT(ActionSlot()));
  72. imageenhance->addAction("灰度开运算", this, SLOT(ActionSlot()));
  73. imageenhance->addAction("灰度闭运算", this, SLOT(ActionSlot()));
  74. QMenu* imageadjust = menu->addMenu("图像调整");
  75. imageadjust->addAction("反色", this, SLOT(ActionSlot()));
  76. imageadjust->addAction("彩色转灰", this, SLOT(ActionSlot()));
  77. imageadjust->addAction("图像镜像", this, SLOT(ActionSlot()));
  78. imageadjust->addAction("图像旋转", this, SLOT(ActionSlot()));
  79. imageadjust->addAction("深度转彩色", this, SLOT(ActionSlot()));
  80. imageadjust->addAction("修改图像尺寸", this, SLOT(ActionSlot()));
  81. imageadjust->addAction("深度转灰度", this, SLOT(ActionSlot()));
  82. ui.btnAdd->setMenu(menu);
  83. }
  84. ToolDialogImpl::~ToolDialogImpl()
  85. {
  86. }
  87. VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
  88. {
  89. HObject ImageTemp;
  90. ImageTemp = m_Image;
  91. hwndUnit->ShowImage(m_Image);
  92. for (int i = 0; i < m_LinkProcess.size(); i++)
  93. {
  94. if (m_LinkProcess[i].p_dlg != nullptr && m_LinkProcess[i].isrun == true)
  95. {
  96. m_LinkProcess[i].p_dlg->setValue(ImageTemp);
  97. m_LinkProcess[i].p_dlg->Execute();
  98. ImageTemp = m_LinkProcess[i].p_dlg->GetResult();
  99. }
  100. }
  101. hwndUnit->ShowObj(ImageTemp, false);
  102. hwndUnit->Refresh();
  103. m_ImageOut = ImageTemp;
  104. emit sigUpdateUI();
  105. return VPEnum::RETURN_VALUE::Success;
  106. }
  107. /// <summary>
  108. ///
  109. /// </summary>
  110. /// <param name="bRun"></param>
  111. void ToolDialogImpl::Running(bool bRun)
  112. {
  113. }
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. /// <param name="ar"></param>
  118. /// <param name="bIsOut"></param>
  119. /// <returns></returns>
  120. bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
  121. {
  122. int paranum;//参数数量
  123. if (bIsOut)//保存参数流程
  124. {
  125. paranum = 1;
  126. ar << paranum;//先保存参数数量
  127. ar << (int)1 << m_Image;
  128. paranum = 2;//以下参数的数量
  129. ar << paranum;//先保存参数数量
  130. ar << (UINT)m_LinkProcess.size();
  131. for (UINT i = 0; i < m_LinkProcess.size(); i++)
  132. {
  133. LINKCTRL& link = m_LinkProcess[i];
  134. ar << (int)1 << link.ToolName;
  135. ar << (int)2 << link.ToolID;
  136. }
  137. // 保存工具内的参数
  138. for (UINT i = 0; i < m_LinkProcess.size(); i++)
  139. {
  140. m_LinkProcess[i].p_dlg->Serialized(ar, true);
  141. }
  142. }
  143. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  144. {
  145. m_bLoading = true;
  146. int nSize1 = 0;
  147. int para;
  148. ar >> paranum;//读取参数数量
  149. for (int i = 0; i < paranum; i++)
  150. {
  151. ar >> para;
  152. switch (para)
  153. {
  154. case 1: ar >> m_Image; break;
  155. default:
  156. {
  157. qWarning() << "Serialized(In) Error" ;
  158. return false;
  159. }
  160. break;
  161. }
  162. }
  163. {
  164. m_LinkProcess.clear();
  165. ui.tableWidget->clear();
  166. for (int i = ui.stackedWidget->count(); i >= 0; i--)
  167. {
  168. QWidget* widget = ui.stackedWidget->widget(i);
  169. ui.stackedWidget->removeWidget(widget);
  170. }
  171. }
  172. UINT LinkSize = 0;
  173. ar >> paranum;//读取参数数量
  174. ar >> LinkSize;
  175. for (UINT i = 0; i < LinkSize; i++)
  176. {
  177. int nToolID = 0;
  178. QString strToolName;
  179. int nPpara;
  180. for (int j = 0; j < paranum; j++)
  181. {
  182. ar >> nPpara;
  183. switch (nPpara)
  184. {
  185. case 1: ar >> strToolName; break;
  186. case 2: ar >> nToolID; break;
  187. default:
  188. return false;
  189. }
  190. }
  191. AddTool(nToolID);
  192. }
  193. // 加载工具内的参数
  194. for (UINT i = 0; i < m_LinkProcess.size(); i++)
  195. {
  196. m_LinkProcess[i].p_dlg->Serialized(ar, false);
  197. }
  198. m_bLoading = false;
  199. }
  200. return true;
  201. }
  202. void ToolDialogImpl::on_UpdateUI()
  203. {
  204. }
  205. /// <summary>
  206. /// 确定
  207. /// </summary>
  208. void ToolDialogImpl::on_btnOK_clicked()
  209. {
  210. this->hide();
  211. }
  212. /// <summary>
  213. /// 取消按钮
  214. /// </summary>
  215. void ToolDialogImpl::on_btnCancel_clicked()
  216. {
  217. // 将本工具的恢复到打开工具之前的状态
  218. RecoverData();
  219. }
  220. /// <summary>
  221. /// 测试按钮
  222. /// </summary>
  223. void ToolDialogImpl::on_btnExecute_clicked()
  224. {
  225. QElapsedTimer toolTimer;
  226. toolTimer.start();
  227. // 发送事件
  228. ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
  229. QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
  230. // 统计返回值
  231. VPEnum::RETURN_VALUE ret = pToolEvent->ret;
  232. double nExecTime = toolTimer.elapsed();
  233. QString str;
  234. str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
  235. ui.label_time->setText(str);
  236. str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
  237. ui.label_state->setText(str);
  238. delete pToolEvent;
  239. }
  240. void ToolDialogImpl::ActionSlot()
  241. {
  242. if (m_bLoading)
  243. {
  244. return;
  245. }
  246. QAction* action = (QAction*)sender();
  247. if (action->text() == "二值化")
  248. {
  249. AddTool(0);
  250. }
  251. if (action->text() == "均值二值化")
  252. {
  253. AddTool(1);
  254. }
  255. if (action->text() == "均值滤波")
  256. {
  257. AddTool(2);
  258. }
  259. if (action->text() == "中值滤波")
  260. {
  261. AddTool(3);
  262. }
  263. if (action->text() == "高斯滤波")
  264. {
  265. AddTool(4);
  266. }
  267. if (action->text() == "灰度膨胀")
  268. {
  269. AddTool(5);
  270. }
  271. if (action->text() == "灰度腐蚀")
  272. {
  273. AddTool(6);
  274. }
  275. if (action->text() == "锐化")
  276. {
  277. AddTool(7);
  278. }
  279. if (action->text() == "对比度")
  280. {
  281. AddTool(8);
  282. }
  283. if (action->text() == "亮度调节")
  284. {
  285. AddTool(9);
  286. }
  287. if (action->text() == "灰度开运算")
  288. {
  289. AddTool(10);
  290. }
  291. if (action->text() == "灰度闭运算")
  292. {
  293. AddTool(11);
  294. }
  295. if (action->text() == "反色")
  296. {
  297. AddTool(12);
  298. }
  299. if (action->text() == "彩色转灰")
  300. {
  301. AddTool(13);
  302. }
  303. if (action->text() == "图像镜像")
  304. {
  305. AddTool(14);
  306. }
  307. if (action->text() == "图像旋转")
  308. {
  309. AddTool(15);
  310. }
  311. if (action->text() == "修改图像尺寸")
  312. {
  313. AddTool(17);
  314. }
  315. }
  316. void ToolDialogImpl::clickSlot(bool ok)
  317. {
  318. if (ui.tableWidget->currentRow() != -1)
  319. {
  320. int nRow = ui.tableWidget->currentRow();
  321. LINKCTRL aLinkCtrl = m_LinkProcess[nRow];
  322. aLinkCtrl.isrun = ok;
  323. }
  324. }
  325. void ToolDialogImpl::AddTool(int nToolID)
  326. {
  327. if (nToolID == -1)
  328. {
  329. return;
  330. }
  331. ToolInterface* pDlg;
  332. LINKCTRL aLinkCtrl;
  333. QString strToolName;
  334. if (nToolID == 0)
  335. {
  336. strToolName = "二值化";
  337. pDlg = new ThresholdTool();
  338. }
  339. else if (nToolID == 1)
  340. {
  341. strToolName = "均值二值化";
  342. pDlg = new dynthreshold();
  343. }
  344. else if (nToolID == 2)
  345. {
  346. strToolName = "均值滤波";
  347. pDlg = new meanfilter();
  348. }
  349. else if (nToolID == 3)
  350. {
  351. strToolName = "中值滤波";
  352. pDlg = new medianfilter();
  353. }
  354. else if (nToolID == 4)
  355. {
  356. strToolName = "高斯滤波";
  357. pDlg = new gaussfilter();
  358. }
  359. else if (nToolID == 5)
  360. {
  361. strToolName = "灰度膨胀";
  362. pDlg = new graydilation();
  363. }
  364. else if (nToolID == 6)
  365. {
  366. strToolName = "灰度腐蚀";
  367. pDlg = new grayerosion();
  368. }
  369. else if (nToolID == 7)
  370. {
  371. strToolName = "锐化";
  372. pDlg = new emphasizetool();
  373. }
  374. else if (nToolID == 8)
  375. {
  376. strToolName = "对比度";
  377. pDlg = new illuminatetool();
  378. }
  379. else if (nToolID == 9)
  380. {
  381. strToolName = "亮度调节";
  382. pDlg = new lighttool();
  383. }
  384. else if (nToolID == 10)
  385. {
  386. strToolName = "灰度开运算";
  387. pDlg = new grayopen();
  388. }
  389. else if (nToolID == 11)
  390. {
  391. strToolName = "灰度闭运算";
  392. pDlg = new grayclose();
  393. }
  394. else if (nToolID == 12)
  395. {
  396. strToolName = "反色";
  397. pDlg = new invertcolor();
  398. }
  399. else if (nToolID == 13)
  400. {
  401. strToolName = "彩色转灰";
  402. pDlg = new imageconverttool();
  403. }
  404. else if (nToolID == 14)
  405. {
  406. strToolName = "图像镜像";
  407. pDlg = new mirrortool();
  408. }
  409. else if (nToolID == 15)
  410. {
  411. strToolName = "图像旋转";
  412. pDlg = new rotatetool();
  413. }
  414. else if (nToolID == 17)
  415. {
  416. strToolName = "修改图像尺寸";
  417. pDlg = new changesize();
  418. }
  419. connect(pDlg, SIGNAL(updateParameter(QString)), this, SLOT(UpdateUI(QString)));
  420. ui.tableWidget->addTool(m_LinkProcess.size(), strToolName, "null"/*pDlg->ShowParameter()*/);
  421. ui.tableWidget->selectRow(m_LinkProcess.size());
  422. ui.stackedWidget->insertWidget(m_LinkProcess.size(), pDlg);
  423. ui.stackedWidget->setCurrentIndex(m_LinkProcess.size() );
  424. aLinkCtrl.ToolID = nToolID;
  425. aLinkCtrl.p_dlg = pDlg;
  426. aLinkCtrl.ToolName = strToolName;
  427. aLinkCtrl.isrun = true;
  428. aLinkCtrl.nUi = m_LinkProcess.size();
  429. m_LinkProcess.push_back(aLinkCtrl);
  430. if (ui.checkBox->isChecked())
  431. {
  432. Execute();
  433. }
  434. }
  435. void ToolDialogImpl::UpdateUI(QString name)
  436. {
  437. if (ui.checkBox->isChecked())
  438. {
  439. ui.tableWidget->setItem(ui.tableWidget->currentRow(), 2, new QTableWidgetItem(name));
  440. ui.tableWidget->item(ui.tableWidget->currentRow(), 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  441. try
  442. {
  443. Execute();
  444. }
  445. catch (...)
  446. {
  447. qWarning() << "Error";
  448. }
  449. }
  450. }
  451. void ToolDialogImpl::on_tableWidget_clicked(const QModelIndex& index)
  452. {
  453. int nRow = index.row();
  454. LINKCTRL& aLink1 = m_LinkProcess.at(nRow);
  455. ui.stackedWidget->setCurrentIndex(aLink1.nUi);
  456. }
  457. void ToolDialogImpl::on_btnDel_clicked()
  458. {
  459. }
  460. void ToolDialogImpl::SwapTwoRow(int selectRow, int targetRow)
  461. {
  462. QString text1 = ui.tableWidget->item(selectRow, 1)->text();
  463. QString text2 = ui.tableWidget->item(selectRow, 2)->text();
  464. QString text3 = ui.tableWidget->item(targetRow, 1)->text();
  465. QString text4 = ui.tableWidget->item(targetRow, 2)->text();
  466. ui.tableWidget->setItem(selectRow, 1, new QTableWidgetItem(text3));
  467. ui.tableWidget->setItem(selectRow, 2, new QTableWidgetItem(text4));
  468. ui.tableWidget->setItem(targetRow, 1, new QTableWidgetItem(text1));
  469. ui.tableWidget->setItem(targetRow, 2, new QTableWidgetItem(text2));
  470. ui.tableWidget->item(selectRow, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  471. ui.tableWidget->item(selectRow, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  472. ui.tableWidget->item(targetRow, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  473. ui.tableWidget->item(targetRow, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  474. ui.tableWidget->selectRow(targetRow);
  475. }
  476. void ToolDialogImpl::on_btnMoveUp_clicked()
  477. {
  478. if (ui.tableWidget->currentRow() != -1 && ui.tableWidget->currentRow() > 0)
  479. {
  480. int nRow = ui.tableWidget->currentRow();
  481. swap(m_LinkProcess.at(nRow), m_LinkProcess.at(nRow - 1));//向上交换处理模块位置
  482. LINKCTRL aLink1 = m_LinkProcess.at(nRow -1);
  483. ui.stackedWidget->setCurrentIndex(aLink1.nUi);
  484. this->SwapTwoRow(nRow, nRow - 1);
  485. }
  486. }
  487. void ToolDialogImpl::on_btnMoveDown_clicked()
  488. {
  489. int a = ui.tableWidget->currentRow();
  490. int b = ui.tableWidget->rowCount();
  491. if (ui.tableWidget->currentRow() != -1 && ui.tableWidget->currentRow() < ui.tableWidget->rowCount()-1)
  492. {
  493. int nRow = ui.tableWidget->currentRow();
  494. swap(m_LinkProcess.at(nRow), m_LinkProcess.at(nRow + 1));//向下交换处理模块位置
  495. LINKCTRL aLink1 = m_LinkProcess.at(nRow + 1);
  496. ui.stackedWidget->setCurrentIndex(aLink1.nUi);
  497. this->SwapTwoRow(nRow, nRow + 1);
  498. }
  499. }