ToolDialog.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "ToolDialog.h"
  2. #include <QImage>
  3. #include <QFileInfo>
  4. #include <QFileDialog>
  5. #include <QElapsedTimer>
  6. #include <QGridLayout>
  7. #include <QCloseEvent>
  8. ToolDialogImpl::ToolDialogImpl(QWidget* parent)
  9. : DllToolDialog(parent)
  10. {
  11. ui.setupUi(this);
  12. this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
  13. connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI()));
  14. // 显示控件初始化
  15. hwndUnit = new HWndUnit(this);
  16. Util::ShowUnitInWidget(ui.Image_widget, hwndUnit);
  17. hwndUnit->getHWndCtrl()->useROIController(&m_RoiController);
  18. connect(&m_RoiController, SIGNAL(ROIChange(const ViewMessage)), this, SLOT(on_ROIChange(const ViewMessage)));
  19. connect(ui.spinBox_H, SIGNAL(valueChanged(int)), this, SLOT(on_setValue(int)));
  20. connect(ui.spinBox_W, SIGNAL(valueChanged(int)), this, SLOT(on_setValue(int)));
  21. ROIPoint* pRoiPoint = new ROIPoint();
  22. pRoiPoint->setTitle("Brush");
  23. pRoiPoint->setHandleWidth(100);
  24. //m_pRoiController->setROISign(ROIOperation::Negative);
  25. m_RoiController.setROIShape(pRoiPoint);
  26. m_RoiController.mouseDownAction(500, 400);
  27. m_nROI_W = 512;
  28. m_nROI_H = 512;
  29. }
  30. ToolDialogImpl::~ToolDialogImpl()
  31. {
  32. }
  33. VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
  34. {
  35. m_nROI_W = ui.spinBox_W->value();
  36. m_nROI_H = ui.spinBox_H->value();
  37. try
  38. {
  39. GenRectangle2(&m_hoROI, hv_roiDate[0], hv_roiDate[1], 0, m_nROI_W, m_nROI_H);
  40. hwndUnit->ShowImage(m_Image);
  41. hwndUnit->ShowObj(m_hoROI, QColor(128, 222, 0));
  42. ReduceDomain(m_Image, m_hoROI, &m_OutImage);
  43. m_CroppingImage = m_OutImage.CropDomain();
  44. hwndUnit->Refresh();
  45. }
  46. catch (HException& exception)
  47. {
  48. char m_szMsg[2048] = "";
  49. snprintf(m_szMsg, sizeof(m_szMsg),"Error #%u in %s: %s\n", exception.ErrorCode(),
  50. exception.ProcName().TextA(),
  51. exception.ErrorMessage().TextA());
  52. qWarning() << "Execute() Error";
  53. }
  54. emit sigUpdateUI();
  55. return VPEnum::RETURN_VALUE::Success;
  56. }
  57. void ToolDialogImpl::on_ROIChange(const ViewMessage& sign)
  58. {
  59. switch (sign)
  60. {
  61. case ViewMessage::MovingROI:
  62. {
  63. }break;
  64. case ViewMessage::UpdateROI:
  65. {
  66. ROI* roi = m_RoiController.getActiveROI();
  67. if (roi == nullptr)
  68. {
  69. return;
  70. }
  71. hv_roiDate = roi->getROIData();
  72. try
  73. {
  74. GenRectangle2(&m_hoROI, hv_roiDate[0], hv_roiDate[1], 0, m_nROI_W, m_nROI_H);
  75. }
  76. catch (...)
  77. {
  78. }
  79. Execute();
  80. }
  81. break;
  82. case ViewMessage::CreatedROI:
  83. {
  84. ROI* roi = m_RoiController.getActiveROI();
  85. if (roi == nullptr)
  86. {
  87. return;
  88. }
  89. hv_roiDate = roi->getROIData();
  90. try
  91. {
  92. GenRectangle2(&m_hoROI, hv_roiDate[0], hv_roiDate[1], 0, m_nROI_W, m_nROI_H);
  93. }
  94. catch (...)
  95. {
  96. }
  97. }
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. void ToolDialogImpl::Running(bool bRun)
  104. {
  105. }
  106. void ToolDialogImpl::timerEvent(QTimerEvent* event)
  107. {
  108. }
  109. bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
  110. {
  111. int paranum;//参数数量
  112. if (bIsOut)//保存参数流程
  113. {
  114. paranum = 5;
  115. ar << paranum;//先保存参数数量
  116. ar << (int)1 << hv_roiDate;
  117. ar << (int)2 << m_RoiController;
  118. ar << (int)3 << m_hoROI;
  119. ar << (int)4 << (int)ui.spinBox_W->value();
  120. ar << (int)5 << (int)ui.spinBox_H->value();
  121. }
  122. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  123. {
  124. int nSize1 = 0;
  125. int para;
  126. ar >> paranum;//读取参数数量
  127. for (int i = 0; i < paranum; i++)
  128. {
  129. ar >> para;
  130. switch (para)
  131. {
  132. case 1: ar >> hv_roiDate; break;
  133. case 2: ar >> m_RoiController; break;
  134. case 3: ar >> m_hoROI; break;
  135. case 4: ar >> m_nROI_W; break;
  136. case 5: ar >> m_nROI_H; break;
  137. default:
  138. {
  139. qWarning() << "Serialized(In) Error";
  140. return false;
  141. }
  142. break;
  143. }
  144. }
  145. on_UpdateUI();
  146. }
  147. return true;
  148. }
  149. void ToolDialogImpl::on_UpdateUI()
  150. {
  151. int x = hv_roiDate[1].D();
  152. int y = hv_roiDate[0].D();
  153. ui.spinBox_X->setValue(x);
  154. ui.spinBox_Y->setValue(y);
  155. ui.spinBox_W->setValue(m_nROI_W);
  156. ui.spinBox_H->setValue(m_nROI_H);
  157. }
  158. /// <summary>
  159. /// 确定
  160. /// </summary>
  161. void ToolDialogImpl::on_btnOK_clicked()
  162. {
  163. this->hide();
  164. }
  165. /// <summary>
  166. /// 取消按钮
  167. /// </summary>
  168. void ToolDialogImpl::on_btnCancel_clicked()
  169. {
  170. // 将本工具的恢复到打开工具之前的状态
  171. RecoverData();
  172. }
  173. /// <summary>
  174. /// 测试按钮
  175. /// </summary>
  176. void ToolDialogImpl::on_btnExecute_clicked()
  177. {
  178. QElapsedTimer toolTimer;
  179. toolTimer.start();
  180. // 发送事件
  181. ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
  182. QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
  183. // 统计返回值
  184. VPEnum::RETURN_VALUE ret = pToolEvent->ret;
  185. double nExecTime = toolTimer.elapsed();
  186. QString str;
  187. str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
  188. ui.label_time->setText(str);
  189. str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
  190. ui.label_state->setText(str);
  191. delete pToolEvent;
  192. }
  193. void ToolDialogImpl::on_setValue(int)
  194. {
  195. if (this->isVisible())
  196. {
  197. Execute();
  198. }
  199. }