ToolDialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "ToolDialog.h"
  2. #include <QImage>
  3. #include <QFileInfo>
  4. #include <QFileDialog>
  5. #include <QElapsedTimer>
  6. #include <QGridLayout>
  7. #include "QtConcurrent"
  8. #define TIME_OUT 30
  9. ToolDialogImpl::ToolDialogImpl(QWidget *parent, DllTool* pDllTool)
  10. : DllToolDialog(parent)
  11. //, m_pDllTool(pDllTool)
  12. {
  13. ui.setupUi(this);
  14. this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
  15. connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI()));
  16. // 显示控件初始化
  17. hwndUnit = new HWndUnit(this);
  18. Util::ShowUnitInWidget(ui.Image_widget, hwndUnit);
  19. m_FilePathFinder.m_eFileFrom = e_fileFolder;
  20. ui.rbnPath->setChecked(false);
  21. ui.rbnFile->setChecked(true);
  22. connect(&m_Timer, SIGNAL(timeout()), this, SLOT(onTimer()));
  23. // 列数
  24. ui.tableWidget->setColumnCount(3);
  25. ui.spinFrameRate->setMaximum(500);
  26. ui.spinFrameRate->setMinimum(1);
  27. ui.spinFrameRate->setValue(100);
  28. // 设置表头文字
  29. QStringList headers;
  30. headers
  31. << ("Index")
  32. << ("Name")
  33. << ("Path");
  34. ui.tableWidget->setHorizontalHeaderLabels(headers);
  35. //设置表头字体
  36. QFont font = ui.tableWidget->horizontalHeader()->font();
  37. font.setBold(true);
  38. ui.tableWidget->horizontalHeader()->setFont(font);
  39. // 设置文字左对齐
  40. ui.tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
  41. // 设置为不可编辑
  42. ui.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
  43. // 设置为整行选中模式
  44. ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
  45. ui.tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
  46. // 设置最左侧的序号不用显示
  47. ui.tableWidget->verticalHeader()->setVisible(false);
  48. //设置行高
  49. ui.tableWidget->verticalHeader()->setDefaultSectionSize(10);
  50. //点击表时不对表头行光亮(获取焦点)
  51. ui.tableWidget->horizontalHeader()->setHighlightSections(false);
  52. ui.tableWidget->setColumnWidth(0, 80);
  53. ui.tableWidget->setColumnWidth(1, 150);
  54. ui.tableWidget->setColumnWidth(2, 300);
  55. m_nAcqIndex = 0;
  56. }
  57. ToolDialogImpl::~ToolDialogImpl()
  58. {
  59. }
  60. VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
  61. {
  62. try
  63. {
  64. GetDebugDataPtr()->Clear();
  65. // m_strFileName = m_FilePathFinder.GetOneFilePath();
  66. // if (m_strFileName.isEmpty())
  67. // {
  68. // //qWarning() << "imagePath Error";
  69. // return VPEnum::RETURN_VALUE::Error;
  70. // }
  71. // m_Image.ReadImage(m_strFileName.toStdString().c_str());
  72. // GetDebugDataPtr()->addImage(m_Image);
  73. // GetDebugDataPtr()->addLog("FileName", m_strFileName);
  74. //
  75. // hwndUnit->ShowImage(m_Image);
  76. // hwndUnit->ShowMsg(m_strFileName.toStdString().c_str() );
  77. if (m_ImageLins.size() > 0 )
  78. {
  79. m_Image = m_ImageLins[m_nAcqIndex].image;
  80. m_strPath = m_ImageLins[m_nAcqIndex].strPath;
  81. m_strName = m_ImageLins[m_nAcqIndex].strName;
  82. m_nAcqIndex++;
  83. if (m_nAcqIndex == m_ImageLins.size())
  84. {
  85. m_nAcqIndex = 0;
  86. }
  87. //hwndUnit->ShowMsg(m_nAcqIndex);
  88. hwndUnit->ShowImage(m_Image);
  89. hwndUnit->Refresh();
  90. emit sigUpdateUI();
  91. //qWarning() << m_strName;
  92. }
  93. }
  94. catch (HException& exception)
  95. {
  96. char m_szMsg[2048] = "";
  97. snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s", exception.ErrorCode(),
  98. exception.ProcName().TextA(),
  99. exception.ErrorMessage().TextA());
  100. qWarning() << m_strPouName << "." << m_strName << "ReadImage Execute() Error" << m_szMsg;
  101. }
  102. return VPEnum::RETURN_VALUE::Success;
  103. }
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. /// <param name="bRun"></param>
  108. void ToolDialogImpl::Running(bool bRun)
  109. {
  110. }
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. /// <param name="ar"></param>
  115. /// <param name="bIsOut"></param>
  116. /// <returns></returns>
  117. bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
  118. {
  119. int paranum;//参数数量
  120. if (bIsOut)//保存参数流程
  121. {
  122. paranum = 4;
  123. ar << paranum;//先保存参数数量
  124. ar << (int)1 << (int)m_FilePathFinder.m_eFileFrom;
  125. ar << (int)2 << m_FilePathFinder.m_strFileFolderPath;
  126. ar << (int)3 << m_nAutoAcq;
  127. ar << (int)4 << m_nFrameRate;
  128. }
  129. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  130. {
  131. int nSize1 = 0;
  132. int para;
  133. ar >> paranum;//读取参数数量
  134. for (int i = 0; i < paranum; i++)
  135. {
  136. ar >> para;
  137. switch (para)
  138. {
  139. case 1: ar >> m_eFileFrom; break;
  140. case 2: ar >> m_strFilePath; break;
  141. case 3: ar >> m_nAutoAcq; break;
  142. case 4: ar >> m_nFrameRate; break;
  143. default:
  144. {
  145. qWarning() << "Serialized(In) Error";
  146. return false;
  147. }
  148. break;
  149. }
  150. }
  151. m_FilePathFinder.SetFilePath(m_eFileFrom, m_strFilePath);
  152. onInitUI();
  153. }
  154. return true;
  155. }
  156. void ToolDialogImpl::onInitUI()
  157. {
  158. m_ImageLins.clear();
  159. m_nAcqIndex = 0;
  160. if (m_FilePathFinder.m_eFileFrom == e_fileFolder)
  161. {
  162. ui.rbnPath->setChecked(false);
  163. ui.rbnFile->setChecked(true);
  164. }
  165. else
  166. {
  167. ui.rbnPath->setChecked(true);
  168. ui.rbnFile->setChecked(false);
  169. }
  170. if (m_nAutoAcq > 0)
  171. {
  172. ui.ckbAutoAcq->setChecked(true);
  173. on_ckbAutoAcq_stateChanged(true);
  174. }
  175. ui.spinFrameRate->setValue(m_nFrameRate);
  176. QList<QString> ListPath = m_FilePathFinder.GetAllPath();
  177. int nRowCount = ui.tableWidget->rowCount();
  178. for (int row = 0; row < nRowCount; row++)
  179. {
  180. ui.tableWidget->removeRow(0);
  181. }
  182. for (int i =0; i < ListPath.size(); i++)
  183. {
  184. QString strFilePath = ListPath[i];
  185. QFileInfo FileInfo(strFilePath);
  186. QString strName = FileInfo.fileName();
  187. QString strPath = FileInfo.filePath();
  188. ui.tableWidget->insertRow(ui.tableWidget->rowCount());//增加一行
  189. ui.tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(i)));
  190. ui.tableWidget->setItem(i, 1, new QTableWidgetItem(strName));
  191. ui.tableWidget->setItem(i, 2, new QTableWidgetItem(strPath));
  192. try
  193. {
  194. HImage img;
  195. img.ReadImage(HString::FromUtf8(strPath.toStdString().c_str()));
  196. IMAGE tagImage;
  197. tagImage.image = img;
  198. tagImage.strName = strName;
  199. tagImage.strPath = strPath;
  200. m_ImageLins.push_back(tagImage);
  201. }
  202. catch (HException& exception)
  203. {
  204. char m_szMsg[2048] = "";
  205. snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s", exception.ErrorCode(),
  206. exception.ProcName().TextA(),
  207. exception.ErrorMessage().TextA());
  208. qWarning() << m_strPouName << "." << m_strName << "ReadImage Execute() Error" << m_szMsg;
  209. }
  210. }
  211. }
  212. /// <summary>
  213. /// 确定
  214. /// </summary>
  215. void ToolDialogImpl::on_btnOK_clicked()
  216. {
  217. this->hide();
  218. }
  219. /// <summary>
  220. /// 取消按钮
  221. /// </summary>
  222. void ToolDialogImpl::on_btnCancel_clicked()
  223. {
  224. // 将本工具的恢复到打开工具之前的状态
  225. RecoverData();
  226. }
  227. /// <summary>
  228. /// 测试按钮
  229. /// </summary>
  230. void ToolDialogImpl::on_btnExecute_clicked()
  231. {
  232. QElapsedTimer toolTimer;
  233. toolTimer.start();
  234. // 发送事件
  235. ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
  236. QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
  237. // 统计返回值
  238. VPEnum::RETURN_VALUE ret = pToolEvent->ret;
  239. double nExecTime = toolTimer.elapsed();
  240. QString str;
  241. str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
  242. ui.label_time->setText(str);
  243. str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
  244. ui.label_state->setText(str);
  245. delete pToolEvent;
  246. }
  247. /// <summary>
  248. ///
  249. /// </summary>
  250. void ToolDialogImpl::on_rbnFile_clicked()
  251. {
  252. m_FilePathFinder.m_eFileFrom = E_FileFrom::e_fileFolder;
  253. }
  254. void ToolDialogImpl::on_rbnPath_clicked()
  255. {
  256. m_FilePathFinder.m_eFileFrom = E_FileFrom::e_filePath;
  257. }
  258. void ToolDialogImpl::on_btnReadImage_clicked()
  259. {
  260. if (m_FilePathFinder.m_eFileFrom == E_FileFrom::e_fileFolder)
  261. {
  262. QFileDialog dialog;
  263. dialog.setFileMode(QFileDialog::Directory);
  264. dialog.setOption(QFileDialog::ShowDirsOnly);
  265. QString directory = dialog.getExistingDirectory();
  266. if (directory.toStdString().size() > 3)
  267. {
  268. m_FilePathFinder.SetFilePath(E_FileFrom::e_fileFolder, directory);
  269. }
  270. }
  271. else
  272. {
  273. m_strFileName = QFileDialog::getOpenFileName(
  274. this,
  275. "Open Image",
  276. "C:\\VisionPlus_V3.0\\DemoImage\\",
  277. "Document files (*.jpg *.bmp *.png *.jpeg);;All files(*.*)");
  278. if (!m_strFileName.isNull())
  279. {
  280. m_FilePathFinder.SetFilePath(E_FileFrom::e_filePath, m_strFileName);
  281. }
  282. }
  283. onInitUI();
  284. }
  285. /// <summary>
  286. /// 调整大小的消息中改变表格栏的宽度
  287. /// </summary>
  288. /// <param name="event"></param>
  289. void ToolDialogImpl::resizeEvent(QResizeEvent* event)
  290. {
  291. Q_UNUSED(event);
  292. //
  293. // // qDebug() << "WindowAppVariableTable::resizeEvent";
  294. // int nTotalSize = ui.tableWidget->size().width();
  295. //
  296. // ui.tableWidget->setColumnWidth(0, nTotalSize * 0.05);
  297. // ui.tableWidget->setColumnWidth(1, nTotalSize * 0.25);
  298. // ui.tableWidget->setColumnWidth(2, nTotalSize * 0.7);
  299. //
  300. //
  301. // ui.tableWidget->resize(ui.tableWidget->size());
  302. }
  303. void ToolDialogImpl::on_ckbAutoAcq_stateChanged(int arg1)
  304. {
  305. if (arg1 > 0)
  306. {
  307. m_nFrameRate = ui.spinFrameRate->value();
  308. int nTime = 1000 / m_nFrameRate;
  309. m_Timer.setInterval(nTime);
  310. m_Timer.start();
  311. ui.spinFrameRate->setEnabled(false);
  312. }
  313. else
  314. {
  315. m_Timer.stop();
  316. ui.spinFrameRate->setEnabled(true);
  317. }
  318. m_nAutoAcq = arg1;
  319. }
  320. void ToolDialogImpl::onTimer()
  321. {
  322. // QtConcurrent::run(this, &ToolDialogImpl::Execute);
  323. // on_btnExecute_clicked();
  324. Execute();
  325. }
  326. // double getFps()
  327. // {
  328. // static double fps = 0;
  329. // static double lastTime = QDateTime::currentMSecsSinceEpoch(); // ms
  330. // static double frameCount = 0;
  331. // ++frameCount;
  332. // double curTime = QDateTime::currentMSecsSinceEpoch();
  333. // if (curTime - lastTime > 1000) // 取固定时间间隔为1秒
  334. // {
  335. // fps = frameCount;
  336. // frameCount = 0;
  337. // lastTime = curTime;
  338. // }
  339. // return fps;
  340. // }
  341. double getFps()
  342. {
  343. #include <chrono>
  344. static double fps = 0.0;
  345. static int frameCount = 0;
  346. static auto lastTime = std::chrono::system_clock::now();
  347. static auto curTime = std::chrono::system_clock::now();
  348. curTime = std::chrono::system_clock::now();
  349. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(curTime - lastTime);
  350. double duration_s = double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  351. if (duration_s > 2)//2秒之后开始统计FPS
  352. {
  353. fps = frameCount / duration_s;
  354. frameCount = 0;
  355. lastTime = curTime;
  356. }
  357. ++frameCount;
  358. return fps;
  359. }
  360. void ToolDialogImpl::on_UpdateUI()
  361. {
  362. // 发送工具的实时路径到ui(实例名加接口名)
  363. this->syncHdValueToUi(this->m_strInstanceName, "ImagePath");
  364. this->syncHdValueToUi(this->m_strInstanceName, "ImageName");
  365. this->syncHdValueToUi(this->m_strInstanceName, "AcqIndex");
  366. QCoreApplication::sendEvent(m_pEventTarget, &m_eventOut);
  367. double nFps = getFps();
  368. QString str;
  369. str = QString(" %1 fps").arg(nFps, 0, 'G', 3);
  370. ui.labelFrameRate->setText(str);
  371. }