QHalconWindow.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. #include "QHalconWindow.h"
  2. #include "ROIController.h"
  3. #include <QDebug>
  4. #include<QWheelEvent>
  5. #ifdef _WIN32
  6. #include <Exception>
  7. #endif
  8. #include<HalconCpp.h>
  9. #include "HWndMessage.h"
  10. #include <QtWidgets/QLabel>
  11. #include<QFileDialog>
  12. #include<QMessageBox>
  13. #include "HImage.h"
  14. #include"Util.h"
  15. #include "ROI.h"
  16. #include"ROICircle.h"
  17. #include"ROIRect1.h"
  18. #include"ROICircularArc.h"
  19. #include"ROIRect2.h"
  20. #include"ROILine.h"
  21. #include<QThread>
  22. #include <QCoreApplication>
  23. #include <QDateTime>
  24. using namespace std;
  25. using namespace HalconCpp;
  26. bool inMeasureLine = false;
  27. //构造函数
  28. QHalconWindow::QHalconWindow(QWidget* parent)
  29. : QWidget(parent)
  30. {
  31. resize(400, 300);
  32. setGeometry(QRect(30, 30, 400, 300));
  33. setMouseTracking(true);
  34. resultShow = ResultShow::AllObject;
  35. //show();
  36. OpenWindow();
  37. SetSystem("int_zooming", "true");
  38. SetSystem("border_shape_models", "false");
  39. SetSystem("tsp_width", 6000);
  40. SetSystem("tsp_height", 6000);
  41. backguoundColor = "#696969";
  42. windowWidth = size().width();
  43. windowHeight = size().height();
  44. showMode = ShowMode::IncludeROI;
  45. imageHeight = 400;
  46. imageWidth = 300;
  47. zoomWndFactor = (double)imageWidth / this->width();
  48. InitUserUI();
  49. roiManager = nullptr;
  50. firstEnter = true;
  51. }
  52. /**
  53. * @brief QHalconWindow::~QHalconWindow 析构函数
  54. */
  55. QHalconWindow::~QHalconWindow(void)
  56. {
  57. using namespace HalconCpp;
  58. CloseWindow(winID);
  59. // delete roiManager;
  60. qDebug() << " CloseWindow(winID);";
  61. }
  62. void QHalconWindow::refresh()
  63. {
  64. try
  65. {
  66. using namespace HalconCpp;
  67. SetSystem("flush_graphic", "false");
  68. SetDraw(winID, "margin");
  69. ClearWindow(winID);
  70. mGC.stateOfSettings.clear();
  71. //Util::notify(GBK("对象个数%1").arg(count));
  72. //m_Mutex.lock();
  73. int count = HObjList.count();
  74. for (int i = 0; i < count; i++)
  75. {
  76. if (i > 0 && resultShow == ResultShow::Image)
  77. {
  78. break;
  79. }
  80. HObject objShow = HObjList[i].getHobj();
  81. HWndMessage messageShow = HObjList[i].getMessage();
  82. if (objShow.IsInitialized())
  83. {
  84. this->mGC.applyContext(winID, HObjList[i].getGC());
  85. DispObj(objShow, winID);
  86. }
  87. else if (messageShow.IsInitialized())
  88. {
  89. messageShow.dispMessage(winID, "image", ((double)imageWidth / this->width()) / zoomWndFactor);
  90. }
  91. }
  92. //m_Mutex.unlock();
  93. if (roiManager != nullptr && (showMode == ShowMode::IncludeROI))
  94. {
  95. int dat = imageWidth / 300;
  96. if (dat < 5)
  97. {
  98. dat = 5;
  99. }
  100. roiManager->paintData(getWindowID(), dat, zoomWndFactor);
  101. }
  102. SetSystem("flush_graphic", "true");
  103. //获取当前显示信息
  104. HTuple hv_Red, hv_Green, hv_Blue;
  105. HTuple hv_lineWidth;
  106. GetRgb(winID, &hv_Red, &hv_Green, &hv_Blue);
  107. GetLineWidth(winID, &hv_lineWidth);
  108. HTuple hv_Draw;
  109. GetDraw(winID, &hv_Draw);
  110. if (showCross == false)
  111. {
  112. SetColor(winID, backguoundColor.toStdString().c_str());
  113. DispLine(winID, -100.0, -100.0, -101.0, -101.0);
  114. }
  115. else
  116. {
  117. SetDraw(winID, "margin");
  118. SetLineWidth(winID, 1);//设置线宽
  119. SetLineStyle(winID, HTuple());
  120. SetColor(winID, "green");//十字架显示颜色
  121. double CrossCol = (double)imageWidth / 2,
  122. CrossRow = (double)imageHeight / 2;
  123. //竖线
  124. HTuple row, col;
  125. row[0] = 0;
  126. row[1] = CrossRow - 50;
  127. col[0] = CrossCol;
  128. col[1] = CrossCol;
  129. DispPolygon(winID, row, col);
  130. row[0] = CrossRow + 50;
  131. row[1] = imageHeight;
  132. col[0] = CrossCol;
  133. col[1] = CrossCol;
  134. DispPolygon(winID, row, col);
  135. //中心点
  136. row[0] = CrossRow - 2;
  137. row[1] = CrossRow + 2;
  138. col[0] = CrossCol;
  139. col[1] = CrossCol;
  140. DispPolygon(winID, row, col);
  141. row[0] = CrossRow;
  142. row[1] = CrossRow;
  143. col[0] = CrossCol - 2;
  144. col[1] = CrossCol + 2;
  145. DispPolygon(winID, row, col);
  146. //横线
  147. row[0] = CrossRow;
  148. row[1] = CrossRow;
  149. col[0] = 0;
  150. col[1] = CrossCol - 50;
  151. DispPolygon(winID, row, col);
  152. row[0] = CrossRow;
  153. row[1] = CrossRow;
  154. col[0] = CrossCol + 50;
  155. col[1] = imageWidth;
  156. DispPolygon(winID, row, col);
  157. }
  158. //恢复窗口显示信息
  159. SetRgb(winID, hv_Red, hv_Green, hv_Blue);
  160. SetLineWidth(winID, hv_lineWidth);
  161. SetDraw(winID, hv_Draw);
  162. // SetColor(WindowID(),"black");
  163. // DispLine(WindowID(),-100.0, -100.0, -101.0, -101.0);
  164. }
  165. catch (HalconCpp::HException& exception)
  166. {
  167. char m_szMsg[2048] = "";
  168. snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s\n", exception.ErrorCode(),
  169. exception.ProcName().TextA(),
  170. exception.ErrorMessage().TextA());
  171. qWarning() << m_szMsg;
  172. }
  173. }
  174. void QHalconWindow::addIconicVar(HImage img)
  175. {
  176. using namespace HalconCpp;
  177. if (img.IsInitialized() == false)
  178. {
  179. return;
  180. }
  181. double r = 0, c = 0;
  182. Hlong h = 0, w = 0;
  183. Hlong area;
  184. //当前使用重载来判断是否图像,如果是hobject的图像,无法进入此处重载
  185. try
  186. {
  187. area = img.GetDomain().AreaCenter(&r, &c);
  188. img.GetImageSize(&w, &h);
  189. //面积=长*宽 表示确实是图片
  190. if (area == (w * h))
  191. {
  192. //添加图像就显示所有obj
  193. resultShow = ResultShow::AllObject;
  194. //HObjList.clear();
  195. if ((h != imageHeight) || (w != imageWidth))
  196. {
  197. imageHeight = h;
  198. imageWidth = w;
  199. resetWindow();
  200. }
  201. }//if
  202. originalImage = img;
  203. if (img.IsInitialized())
  204. {
  205. HObject ho_object(img);
  206. addIconicVar(ho_object);
  207. }
  208. // HObject ho_object = static_cast<HObject>(img);
  209. // addIconicVar(ho_object);
  210. }
  211. catch (HalconCpp::HException& exception)
  212. {
  213. char m_szMsg[2048] = "";
  214. snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s\n", exception.ErrorCode(),
  215. exception.ProcName().TextA(),
  216. exception.ErrorMessage().TextA());
  217. qWarning() << m_szMsg;
  218. }
  219. }
  220. void QHalconWindow::addIconicVar(HObject obj)
  221. {
  222. using namespace HalconCpp;
  223. if (obj.IsInitialized() == false)
  224. {
  225. return;
  226. }
  227. //m_Mutex.lock();
  228. //属性复制
  229. HObjectEntry entry(obj, mGC.graphicalSettings);
  230. HObjList.append(entry);
  231. if (HObjList.count() > MAX_NUM_OBJ_LIST)
  232. {
  233. HObjList.removeAt(1);
  234. }
  235. //m_Mutex.unlock();
  236. }
  237. void QHalconWindow::addMessage(HWndMessage message)
  238. {
  239. HObjectEntry entry(message);
  240. //m_Mutex.lock();
  241. HObjList.append(entry);
  242. if (HObjList.count() > MAX_NUM_OBJ_LIST)
  243. {
  244. HObjList.removeAt(1);
  245. }
  246. //m_Mutex.unlock();
  247. }
  248. void QHalconWindow::useROIController(ROIController* rC)
  249. {
  250. roiManager = rC;
  251. rC->setViewController(this);
  252. rC->setROISign(ROIOperation::Positive);
  253. }
  254. void QHalconWindow::resetWindow()
  255. {
  256. // setImagePart(0, 0, ImgRow2, ImgCol2);
  257. QRect rect = this->geometry();
  258. //判断行缩放还是列缩放
  259. double scaleC = (double)imageWidth / rect.width();
  260. double scaleR = (double)imageHeight / rect.height();
  261. double w, h;
  262. if (scaleC < scaleR)
  263. {
  264. h = rect.height() * scaleR;
  265. w = rect.width() * scaleR;
  266. ImgRow1 = 0;
  267. ImgCol1 = (imageWidth - w) / 2.0;
  268. }
  269. else {
  270. h = rect.height() * scaleC;
  271. w = rect.width() * scaleC;
  272. ImgRow1 = (imageHeight - h) / 2.0;
  273. ImgCol1 = 0;
  274. }
  275. ImgRow2 = ImgRow1 + h - 1;
  276. ImgCol2 = ImgCol1 + w - 1;
  277. zoomWndFactor = w / rect.width();
  278. SetPart(winID, ImgRow1, ImgCol1, ImgRow2, ImgCol2);
  279. // SetPart(WindowID(), 0,0,imageHeight-1,imageWidth-1);
  280. }
  281. void QHalconWindow::clearWindow()
  282. {
  283. HObjList.clear();
  284. refresh();
  285. }
  286. void QHalconWindow::changeGraphicSettings(const QString& mode, const HTuple& val)
  287. {
  288. this->mGC.setAttribute(mode, val);
  289. }
  290. void QHalconWindow::setResultShow(const ResultShow& resultShow)
  291. {
  292. this->resultShow = resultShow;
  293. refresh();
  294. }
  295. void QHalconWindow::setShowMode(const ShowMode& showMode)
  296. {
  297. this->showMode = showMode;
  298. refresh();
  299. }
  300. void QHalconWindow::on_actDispFit()
  301. {
  302. resetWindow();
  303. refresh();
  304. }
  305. void QHalconWindow::on_actSaveImage()
  306. {
  307. if (HObjList.count() < 1)
  308. {
  309. //QMessageBox::information(this, GBK("提示"), GBK("无图像数据,无法保存"));
  310. return;
  311. }
  312. QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
  313. QString strFileName = "Image_" + QDateTime::currentDateTime().toString("[MM-dd_hh_mm_ss_zzz]");
  314. QString strPath = runPath + "//..//PrjImage//" ;
  315. QDir dir(strPath);
  316. if (!dir.exists())
  317. {
  318. dir.mkdir(strPath);
  319. }
  320. QString fileName = QFileDialog::getSaveFileName(this,
  321. GBK("另存为"),
  322. strPath + strFileName,
  323. GBK("图像文件 (*.bmp)"));
  324. if (!fileName.isNull())
  325. {
  326. try
  327. {
  328. HTuple path = HTuple(fileName.toLocal8Bit().toStdString().c_str());
  329. if (originalImage.IsInitialized())
  330. {
  331. WriteImage(originalImage, "bmp", 0, path);
  332. //QMessageBox::information(this, GBK("提示"), GBK("图像保存完成"));
  333. }
  334. else
  335. {
  336. QMessageBox::information(this, GBK("提示"), GBK("图像未初始化"));
  337. }
  338. }
  339. catch (HException&)
  340. {
  341. QMessageBox::information(this, GBK("提示"), GBK("操作异常,请检查图像数据及文件路径"));
  342. return;
  343. }
  344. //fileName是文件名
  345. }
  346. }
  347. void QHalconWindow::on_actDumpWindow()
  348. {
  349. QString fileName = QFileDialog::getSaveFileName(this,
  350. GBK("截图另存为"),
  351. "",
  352. GBK("图像 (*.bmp)"));
  353. if (!fileName.isNull())
  354. {
  355. try
  356. {
  357. HTuple path = HTuple(fileName.toLocal8Bit().toStdString().c_str());
  358. DumpWindow(winID, "bmp", path);
  359. QMessageBox::information(this, GBK("提示"), GBK("截图保存完成"));
  360. }
  361. catch (HException&)
  362. {
  363. QMessageBox::information(this, GBK("提示"), GBK("文件路径异常,保存失败"));
  364. return;
  365. }
  366. //fileName是文
  367. }
  368. }
  369. void QHalconWindow::on_actShowCross()
  370. {
  371. showCross = !showCross;
  372. refresh();
  373. }
  374. void QHalconWindow::on_actDelActiveRoi()
  375. {
  376. if (roiManager == Q_NULLPTR) return;
  377. roiManager->removeActive();
  378. }
  379. void QHalconWindow::on_actDelAllRoi()
  380. {
  381. if (roiManager == Q_NULLPTR) return;
  382. roiManager->reset();
  383. }
  384. void QHalconWindow::on_actSetRoiAdd()
  385. {
  386. if (roiManager == Q_NULLPTR) return;
  387. roiManager->setROISign(ROIOperation::Positive);
  388. }
  389. void QHalconWindow::on_actSetRoiSub()
  390. {
  391. if (roiManager == Q_NULLPTR) return;
  392. roiManager->setROISign(ROIOperation::Negative);
  393. }
  394. void QHalconWindow::on_actGetDistance()
  395. {
  396. HWndMessage message = HWndMessage(HTuple(QString("鼠标点击两个位置,单击鼠标右键完成。").toStdString().c_str()), 20, 20, 20, "green");
  397. message.dispMessage(winID, "window", 1);
  398. inMeasureLine = true;
  399. // viewPort.ContextMenuStrip = null;
  400. HTuple r1, c1, r2, c2;
  401. HTuple dd;
  402. //获取当前显示信息
  403. HTuple hv_Red, hv_Green, hv_Blue;
  404. GetRgb(winID, &hv_Red, &hv_Green, &hv_Blue);
  405. HTuple hv_lineWidth;
  406. GetLineWidth(winID, &hv_lineWidth);
  407. HTuple hv_Draw;
  408. GetDraw(winID, &hv_Draw);
  409. SetLineWidth(winID, 1);//设置线宽
  410. SetLineStyle(winID, HTuple());
  411. SetColor(winID, "green");//画点的颜色
  412. DrawLine(winID, &r1, &c1, &r2, &c2);
  413. DispLine(winID, r1, c1, r2, c2);
  414. //恢复窗口显示信息
  415. SetRgb(winID, hv_Red, hv_Green, hv_Blue);
  416. SetLineWidth(winID, hv_lineWidth);
  417. SetDraw(winID, hv_Draw);
  418. DistancePp(r1, c1, r2, c2, &dd);
  419. double dr = abs(r2.D() - r1.D());
  420. double dc = abs(c2.D() - c1.D());
  421. QMessageBox::information(this, GBK("提示"), QString(GBK("直线长度:%1 px\nx轴距离:%2 px\ny轴距离:%3 px")).arg(dd.D(), 0, 'f', 2)
  422. .arg(dc, 0, 'f', 2)
  423. .arg(dr, 0, 'f', 2));
  424. // MessageBox.Show(string.Format("直线距离%1 px\rx轴距离%2 px\ry轴距离%3 px", dd.D, dc, dr), "结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
  425. //hv_MenuStrip.Visible = true;
  426. //viewPort.ContextMenuStrip = hv_MenuStrip;
  427. inMeasureLine = false;
  428. refresh();
  429. }
  430. void QHalconWindow::on_actShowImageOnly()
  431. {
  432. if (resultShow == ResultShow::AllObject)
  433. {
  434. resultShow = ResultShow::Image;
  435. }
  436. else
  437. {
  438. resultShow = ResultShow::AllObject;
  439. }
  440. refresh();
  441. }
  442. void QHalconWindow::on_actCreateCircle()
  443. {
  444. if (roiManager == NULL)
  445. {
  446. return;
  447. }
  448. ROICircle* roi = new ROICircle();
  449. roiManager->setROISign(ROIOperation::Negative);
  450. roiManager->setROIShape(roi);
  451. }
  452. void QHalconWindow::on_actCreateCircleArc()
  453. {
  454. if (roiManager == NULL)
  455. {
  456. return;
  457. }
  458. ROICircularArc* roi = new ROICircularArc();
  459. roiManager->setROISign(ROIOperation::Negative);
  460. roiManager->setROIShape(roi);
  461. }
  462. void QHalconWindow::on_actCreateLine()
  463. {
  464. if (roiManager == NULL)
  465. {
  466. return;
  467. }
  468. ROILine* roi = new ROILine();
  469. roiManager->setROISign(ROIOperation::Negative);
  470. roiManager->setROIShape(roi);
  471. }
  472. void QHalconWindow::on_actCreateRect1()
  473. {
  474. if (roiManager == NULL)
  475. {
  476. return;
  477. }
  478. ROIRect1* roi = new ROIRect1();
  479. roiManager->setROISign(ROIOperation::Negative);
  480. roiManager->setROIShape(roi);
  481. }
  482. void QHalconWindow::on_actCreateRect2()
  483. {
  484. if (roiManager == NULL)
  485. {
  486. return;
  487. }
  488. ROIRect2* roi = new ROIRect2();
  489. roiManager->setROISign(ROIOperation::Negative);
  490. roiManager->setROIShape(roi);
  491. }
  492. // Open a HALCON window inside the QHalconWindow widget
  493. void QHalconWindow::OpenWindow(void)
  494. {
  495. using namespace HalconCpp;
  496. #ifdef Q_WS_X11
  497. // In Qt versions older version 3.3, it is necessary to temporarily
  498. // change the SubstructureRedirectMask attribute of a Qt widget
  499. // in order to be able to open a HALCON window inside the widget
  500. XWindowAttributes attr;
  501. #if QT_VERSION >= 0x040000
  502. XGetWindowAttributes(x11Info().display(), winId(), &attr);
  503. if (attr.your_event_mask & SubstructureRedirectMask)
  504. XSelectInput(x11Info().display(), winId(),
  505. attr.your_event_mask & ~SubstructureRedirectMask);
  506. XFlush(x11Info().display());
  507. XSync(x11Info().display(), False);
  508. #else
  509. XGetWindowAttributes(x11Display(), winId(), &attr);
  510. if (attr.your_event_mask & SubstructureRedirectMask)
  511. XSelectInput(x11Display(), winId(),
  512. attr.your_event_mask & ~SubstructureRedirectMask);
  513. XFlush(x11Display());
  514. XSync(x11Display(), False);
  515. #endif
  516. #endif
  517. SetWindowAttr("border_width", 0);
  518. SetCheck("~father");
  519. // Open a HALCON window with dummy width and height
  520. // it will be resized soon
  521. HalconCpp::OpenWindow(0, 0, 100, 100, (Hlong)winId(), "visible", "", &winID);
  522. // HalconCpp::OpenWindow(0,0,100,100, (Hlong)this,"invisible","",&winID);
  523. SetCheck("father");
  524. #ifdef Q_WS_X11
  525. // Reset widget attributes to previous settings (see above comment)
  526. #if QT_VERSION >= 0x040000
  527. if (attr.your_event_mask & SubstructureRedirectMask)
  528. XSelectInput(x11Info().display(), winId(), attr.your_event_mask);
  529. #else
  530. if (attr.your_event_mask & SubstructureRedirectMask)
  531. XSelectInput(x11Display(), winId(), attr.your_event_mask);
  532. #endif
  533. #endif
  534. }
  535. void QHalconWindow::DispImageZoom(HTuple mode, HTuple Mouse_row, HTuple Mouse_col)
  536. {
  537. GetPart(winID, &current_beginRow, &current_beginCol, &current_endRow, &current_endCol);
  538. if (mode > 0) // 放大图像
  539. {
  540. zoom_beginRow = (int)(current_beginRow.D() + (Mouse_row.D() - current_beginRow.D()) * 0.3);
  541. zoom_beginCol = (int)(current_beginCol.D() + (Mouse_col.D() - current_beginCol.D()) * 0.3);
  542. zoom_endRow = (int)(current_endRow.D() - (current_endRow.D() - Mouse_row.D()) * 0.3);
  543. zoom_endCol = (int)(current_endCol.D() - (current_endCol.D() - Mouse_col.D()) * 0.3);
  544. }
  545. else // 缩小图像
  546. {
  547. zoom_beginRow = (int)(Mouse_row.D() - (Mouse_row.D() - current_beginRow.D()) / 0.7);
  548. zoom_beginCol = (int)(Mouse_col.D() - (Mouse_col.D() - current_beginCol.D()) / 0.7);
  549. zoom_endRow = (int)(Mouse_row.D() + (current_endRow.D() - Mouse_row.D()) / 0.7);
  550. zoom_endCol = (int)(Mouse_col.D() + (current_endCol.D() - Mouse_col.D()) / 0.7);
  551. }
  552. QRect rect = this->geometry();
  553. int hw_width = rect.width();
  554. int hw_height = rect.height();
  555. bool _isOutOfArea = true;
  556. bool _isOutOfSize = true;
  557. bool _isOutOfPixel = true; //避免像素过大
  558. if (zoom_endRow == zoom_beginRow || zoom_endCol == zoom_beginCol)
  559. {
  560. return;
  561. }
  562. _isOutOfArea = zoom_beginRow >= imageHeight || zoom_endRow <= 0 || zoom_beginCol >= imageWidth || zoom_endCol < 0;
  563. _isOutOfSize = (zoom_endRow - zoom_beginRow) > imageHeight * 20 || (zoom_endCol - zoom_beginCol) > imageWidth * 20;
  564. _isOutOfPixel = hw_height / (zoom_endRow - zoom_beginRow) > 500 || hw_width / (zoom_endCol - zoom_beginCol) > 500;
  565. if (_isOutOfArea || _isOutOfSize || _isOutOfPixel)
  566. {
  567. return;
  568. }
  569. SetPaint(winID, "default");
  570. SetPart(winID, zoom_beginRow, zoom_beginCol,
  571. zoom_endRow, zoom_beginCol +
  572. (zoom_endRow - zoom_beginRow) * hw_width / hw_height);
  573. int w = (zoom_endRow - zoom_beginRow) * hw_width / hw_height;
  574. int w0 = current_endCol.D() - current_beginCol.D();
  575. double scale = (double)w / w0;
  576. zoomWndFactor *= scale;
  577. refresh();
  578. }
  579. void QHalconWindow::InitUserUI()
  580. {
  581. actDispImageOnly = new QAction(this);
  582. actDispFit = new QAction(this);
  583. actShowCross = new QAction(this);
  584. actSaveImage = new QAction(this);
  585. actDumpWindow = new QAction(this);
  586. actDispImageOnly->setText((GBK("显示原图/所有")));
  587. actDispFit->setText(GBK("适应图像显示"));
  588. actShowCross->setText(GBK("显示/隐藏十字架"));
  589. actSaveImage->setText(GBK("保存图像"));
  590. actDumpWindow->setText(GBK("截图另存"));
  591. connect(actDispFit, SIGNAL(triggered(bool)), this, SLOT(on_actDispFit()));
  592. connect(actSaveImage, SIGNAL(triggered(bool)), this, SLOT(on_actSaveImage()));
  593. connect(actDumpWindow, SIGNAL(triggered(bool)), this, SLOT(on_actDumpWindow()));
  594. connect(actShowCross, SIGNAL(triggered(bool)), this, SLOT(on_actShowCross()));
  595. }
  596. void QHalconWindow::moveImage(double motionX, double motionY)
  597. {
  598. GetPart(winID, &current_beginRow, &current_beginCol, &current_endRow, &current_endCol);
  599. ImgRow1 = current_beginRow - motionY;
  600. ImgRow2 = current_endRow - motionY;
  601. ImgCol1 = current_beginCol - motionX;
  602. ImgCol2 = current_endCol - motionX;
  603. SetPart(winID, ImgRow1, ImgCol1,
  604. ImgRow2, ImgCol2);
  605. refresh();
  606. }
  607. // Resize the HALCON window whenever the QHalconWindow widget is resized
  608. void QHalconWindow::resizeEvent(QResizeEvent*)
  609. {
  610. using namespace HalconCpp;
  611. #ifdef Q_WS_X11
  612. // See comment in ::OpenWindow()
  613. XWindowAttributes attr;
  614. #if QT_VERSION >= 0x040000
  615. XGetWindowAttributes(x11Info().display(), winId(), &attr);
  616. if (attr.your_event_mask & SubstructureRedirectMask)
  617. XSelectInput(x11Info().display(), winId(),
  618. attr.your_event_mask & ~SubstructureRedirectMask);
  619. XFlush(x11Info().display());
  620. XSync(x11Info().display(), False);
  621. #else
  622. XGetWindowAttributes(x11Display(), winId(), &attr);
  623. if (attr.your_event_mask & SubstructureRedirectMask)
  624. XSelectInput(x11Display(), winId(),
  625. attr.your_event_mask & ~SubstructureRedirectMask);
  626. XFlush(x11Display());
  627. XSync(x11Display(), False);
  628. #endif
  629. #endif
  630. // Set the HALCON window to its new size.
  631. SetWindowExtents(winID, 0, 0, rect().width(), rect().height() );
  632. #ifdef Q_WS_X11
  633. // See comment in ::OpenWindow()
  634. #if QT_VERSION >= 0x040000
  635. if (attr.your_event_mask & SubstructureRedirectMask)
  636. XSelectInput(x11Info().display(), winId(), attr.your_event_mask);
  637. #else
  638. if (attr.your_event_mask & SubstructureRedirectMask)
  639. XSelectInput(x11Display(), winId(), attr.your_event_mask);
  640. #endif
  641. #endif
  642. }
  643. void QHalconWindow::mousePressEvent(QMouseEvent*)
  644. {
  645. if (inMeasureLine)return;
  646. mousePressed = true;
  647. HTuple state;
  648. HTuple x, y;
  649. try
  650. {
  651. GetMpositionSubPix(winID, &y, &x, &state);
  652. }
  653. catch (HException&)
  654. {
  655. return;
  656. }
  657. int activeROIidx = -1;
  658. if (roiManager != Q_NULLPTR && (showMode == ShowMode::IncludeROI))
  659. {
  660. activeROIidx = roiManager->mouseDownAction(x.D(), y.D());
  661. }
  662. if (activeROIidx == -1)
  663. {
  664. startX = x;
  665. startY = y;
  666. }
  667. }
  668. void QHalconWindow::wheelEvent(QWheelEvent* event)
  669. {
  670. if (inMeasureLine)return;
  671. using namespace HalconCpp;
  672. HTuple Row, Column, Button;
  673. try
  674. {
  675. GetMpositionSubPix(winID, &Row, &Column, &Button);
  676. }
  677. catch (HException&)
  678. {
  679. return;
  680. }
  681. HTuple mode = 1;
  682. if (event->delta() > 0)
  683. {
  684. mode = 1;
  685. }
  686. else
  687. {
  688. mode = -1;
  689. }
  690. DispImageZoom(mode, Row, Column);
  691. }
  692. void QHalconWindow::mouseReleaseEvent(QMouseEvent*)
  693. {
  694. if (inMeasureLine)return;
  695. mousePressed = false;
  696. if (roiManager != NULL
  697. && (roiManager->getActiveROIidx() != -1)
  698. && (showMode == ShowMode::IncludeROI))
  699. {
  700. roiManager->tiggerROINotifySignals(ViewMessage::UpdateROI);
  701. }
  702. }
  703. void QHalconWindow::mouseMoveEvent(QMouseEvent*)
  704. {
  705. if (inMeasureLine)return;
  706. // qDebug()<<"mouse move";
  707. double motionX, motionY;
  708. if (HObjList.count() < 1)
  709. {
  710. return;
  711. }
  712. HTuple currX, currY, state;
  713. try
  714. {
  715. GetMpositionSubPix(winID, &currY, &currX, &state);
  716. if (originalImage.IsInitialized())
  717. {
  718. QString str_value = "";
  719. QString str_position = "";
  720. bool _isXOut = true, _isYOut = true;
  721. int channel_count;
  722. QString str_imgSize = QString("%1*%2").arg(imageWidth).arg(imageHeight);
  723. channel_count = originalImage.CountChannels();
  724. str_position = QString("|%1*%2").arg((int)currX.D()).arg((int)currY.D());;
  725. _isXOut = (currX.D() < 0 || currX.D() >= imageWidth);
  726. _isYOut = (currY.D() < 0 || currY.D() >= imageHeight);
  727. if (!_isXOut && !_isYOut)
  728. {
  729. if ((int)channel_count == 1)
  730. {
  731. double grayVal;
  732. grayVal = originalImage.GetGrayval((int)currY.D(), (int)currX.D());
  733. str_value = QString("|%1").arg((int)grayVal);
  734. }
  735. else if ((int)channel_count == 3)
  736. {
  737. double grayValRed, grayValGreen, grayValBlue;
  738. HImage _RedChannel, _GreenChannel, _BlueChannel;
  739. _RedChannel = originalImage.AccessChannel(1);
  740. _GreenChannel = originalImage.AccessChannel(2);
  741. _BlueChannel = originalImage.AccessChannel(3);
  742. grayValRed = _RedChannel.GetGrayval((int)currY.D(), (int)currX.D());
  743. grayValGreen = _GreenChannel.GetGrayval((int)currY.D(), (int)currX.D());
  744. grayValBlue = _BlueChannel.GetGrayval((int)currY.D(), (int)currX.D());
  745. str_value = QString("|%1,%2,%3").arg((int)grayValRed).arg((int)grayValGreen).arg((int)grayValBlue);
  746. }
  747. else
  748. {
  749. str_value = "";
  750. }
  751. }
  752. QString MousePosMessage = str_imgSize + str_position + str_value;
  753. emit mouseMoveStatus(MousePosMessage);
  754. }
  755. if (!mousePressed)
  756. {
  757. return;
  758. }
  759. if (roiManager != Q_NULLPTR &&
  760. (roiManager->getActiveROIidx() != -1) &&
  761. (showMode == ShowMode::IncludeROI))
  762. {
  763. roiManager->mouseMoveAction(currX.D(), currY.D());
  764. }
  765. else
  766. {
  767. //qDebug()<<"xx.D():"<<xx.;
  768. motionX = ((currX.D() - startX));
  769. motionY = ((currY.D() - startY));
  770. if (((int)motionX != 0) || ((int)motionY != 0))
  771. {
  772. moveImage(motionX, motionY);
  773. startX = currX.D() - motionX;
  774. startY = currY.D() - motionY;
  775. }
  776. }
  777. }
  778. catch (HOperatorException&)
  779. {
  780. return;
  781. }
  782. catch (HException&)
  783. {
  784. return;
  785. }
  786. // HImage *img=&(HObjList[0].HObj);
  787. }
  788. void QHalconWindow::contextMenuEvent(QContextMenuEvent* event)
  789. {
  790. popMenu = new QMenu(this);
  791. //清除原有菜单
  792. popMenu->clear();
  793. popMenu->addAction(actDispFit);
  794. popMenu->addSeparator();
  795. popMenu->addAction(actDispImageOnly);
  796. popMenu->addAction(actShowCross);
  797. popMenu->addSeparator();
  798. popMenu->addAction(actSaveImage);
  799. popMenu->addAction(actDumpWindow);
  800. //菜单出现的位置为当前鼠标的位置
  801. popMenu->exec(event->globalPos());
  802. delete popMenu;
  803. popMenu = NULL;
  804. }
  805. void QHalconWindow::enterEvent(QEvent*)
  806. {
  807. // if(firstEnter)
  808. // {
  809. // firstEnter=false;
  810. // refresh();
  811. // }
  812. }
  813. void QHalconWindow::showEvent(QShowEvent*)
  814. {
  815. refresh();
  816. }
  817. void QHalconWindow::setBackgroundColor(QString color)
  818. {
  819. backguoundColor = color;
  820. SetWindowParam(winID, "background_color", color.toStdString().c_str());
  821. }
  822. void QHalconWindow::on_actDispImageOnly()
  823. {
  824. if (resultShow == ResultShow::Image)
  825. {
  826. setResultShow(ResultShow::AllObject);
  827. }
  828. else
  829. {
  830. setResultShow(ResultShow::Image);
  831. }
  832. }