123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- #include "ToolDialog.h"
- #include <QImage>
- #include <QFileInfo>
- #include <QFileDialog>
- #include <QElapsedTimer>
- #include <QGridLayout>
- #include "QtConcurrent"
- #define TIME_OUT 30
- ToolDialogImpl::ToolDialogImpl(QWidget *parent, DllTool* pDllTool)
- : DllToolDialog(parent)
- //, m_pDllTool(pDllTool)
- {
- ui.setupUi(this);
- this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
- connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI()));
-
-
- // 显示控件初始化
- hwndUnit = new HWndUnit(this);
- Util::ShowUnitInWidget(ui.Image_widget, hwndUnit);
- m_FilePathFinder.m_eFileFrom = e_fileFolder;
-
- ui.rbnPath->setChecked(false);
- ui.rbnFile->setChecked(true);
- connect(&m_Timer, SIGNAL(timeout()), this, SLOT(onTimer()));
- // 列数
- ui.tableWidget->setColumnCount(3);
- ui.spinFrameRate->setMaximum(500);
- ui.spinFrameRate->setMinimum(1);
- ui.spinFrameRate->setValue(100);
- // 设置表头文字
- QStringList headers;
- headers
- << ("Index")
- << ("Name")
- << ("Path");
- ui.tableWidget->setHorizontalHeaderLabels(headers);
- //设置表头字体
- QFont font = ui.tableWidget->horizontalHeader()->font();
- font.setBold(true);
- ui.tableWidget->horizontalHeader()->setFont(font);
- // 设置文字左对齐
- ui.tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
- // 设置为不可编辑
- ui.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
- // 设置为整行选中模式
- ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
- ui.tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
- // 设置最左侧的序号不用显示
- ui.tableWidget->verticalHeader()->setVisible(false);
- //设置行高
- ui.tableWidget->verticalHeader()->setDefaultSectionSize(10);
- //点击表时不对表头行光亮(获取焦点)
- ui.tableWidget->horizontalHeader()->setHighlightSections(false);
- ui.tableWidget->setColumnWidth(0, 80);
- ui.tableWidget->setColumnWidth(1, 150);
- ui.tableWidget->setColumnWidth(2, 300);
- m_nAcqIndex = 0;
- }
- ToolDialogImpl::~ToolDialogImpl()
- {
- }
- VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
- {
- try
- {
- GetDebugDataPtr()->Clear();
- // m_strFileName = m_FilePathFinder.GetOneFilePath();
- // if (m_strFileName.isEmpty())
- // {
- // //qWarning() << "imagePath Error";
- // return VPEnum::RETURN_VALUE::Error;
- // }
- // m_Image.ReadImage(m_strFileName.toStdString().c_str());
- // GetDebugDataPtr()->addImage(m_Image);
- // GetDebugDataPtr()->addLog("FileName", m_strFileName);
- //
- // hwndUnit->ShowImage(m_Image);
- // hwndUnit->ShowMsg(m_strFileName.toStdString().c_str() );
- if (m_ImageLins.size() > 0 )
- {
- m_Image = m_ImageLins[m_nAcqIndex].image;
- m_strPath = m_ImageLins[m_nAcqIndex].strPath;
- m_strName = m_ImageLins[m_nAcqIndex].strName;
- m_nAcqIndex++;
- if (m_nAcqIndex == m_ImageLins.size())
- {
- m_nAcqIndex = 0;
- }
- //hwndUnit->ShowMsg(m_nAcqIndex);
- hwndUnit->ShowImage(m_Image);
- hwndUnit->Refresh();
- emit sigUpdateUI();
- //qWarning() << m_strName;
- }
- }
- catch (HException& exception)
- {
- char m_szMsg[2048] = "";
- snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s", exception.ErrorCode(),
- exception.ProcName().TextA(),
- exception.ErrorMessage().TextA());
- qWarning() << m_strPouName << "." << m_strName << "ReadImage Execute() Error" << m_szMsg;
- }
-
- return VPEnum::RETURN_VALUE::Success;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="bRun"></param>
- void ToolDialogImpl::Running(bool bRun)
- {
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="ar"></param>
- /// <param name="bIsOut"></param>
- /// <returns></returns>
- bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
- {
- int paranum;//参数数量
- if (bIsOut)//保存参数流程
- {
- paranum = 4;
- ar << paranum;//先保存参数数量
- ar << (int)1 << (int)m_FilePathFinder.m_eFileFrom;
- ar << (int)2 << m_FilePathFinder.m_strFileFolderPath;
- ar << (int)3 << m_nAutoAcq;
- ar << (int)4 << m_nFrameRate;
- }
- else//加载参数流程,参数加载顺序一定要跟保存顺序一致
- {
- int nSize1 = 0;
- int para;
- ar >> paranum;//读取参数数量
- for (int i = 0; i < paranum; i++)
- {
- ar >> para;
- switch (para)
- {
- case 1: ar >> m_eFileFrom; break;
- case 2: ar >> m_strFilePath; break;
- case 3: ar >> m_nAutoAcq; break;
- case 4: ar >> m_nFrameRate; break;
- default:
- {
- qWarning() << "Serialized(In) Error";
- return false;
- }
- break;
- }
- }
-
- m_FilePathFinder.SetFilePath(m_eFileFrom, m_strFilePath);
- onInitUI();
- }
- return true;
- }
- void ToolDialogImpl::onInitUI()
- {
- m_ImageLins.clear();
- m_nAcqIndex = 0;
- if (m_FilePathFinder.m_eFileFrom == e_fileFolder)
- {
- ui.rbnPath->setChecked(false);
- ui.rbnFile->setChecked(true);
- }
- else
- {
- ui.rbnPath->setChecked(true);
- ui.rbnFile->setChecked(false);
- }
-
- if (m_nAutoAcq > 0)
- {
- ui.ckbAutoAcq->setChecked(true);
- on_ckbAutoAcq_stateChanged(true);
- }
- ui.spinFrameRate->setValue(m_nFrameRate);
- QList<QString> ListPath = m_FilePathFinder.GetAllPath();
- int nRowCount = ui.tableWidget->rowCount();
- for (int row = 0; row < nRowCount; row++)
- {
- ui.tableWidget->removeRow(0);
- }
- for (int i =0; i < ListPath.size(); i++)
- {
- QString strFilePath = ListPath[i];
- QFileInfo FileInfo(strFilePath);
- QString strName = FileInfo.fileName();
- QString strPath = FileInfo.filePath();
- ui.tableWidget->insertRow(ui.tableWidget->rowCount());//增加一行
- ui.tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(i)));
- ui.tableWidget->setItem(i, 1, new QTableWidgetItem(strName));
- ui.tableWidget->setItem(i, 2, new QTableWidgetItem(strPath));
- try
- {
-
- HImage img;
- img.ReadImage(HString::FromUtf8(strPath.toStdString().c_str()));
- IMAGE tagImage;
- tagImage.image = img;
- tagImage.strName = strName;
- tagImage.strPath = strPath;
- m_ImageLins.push_back(tagImage);
- }
- catch (HException& exception)
- {
- char m_szMsg[2048] = "";
- snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s", exception.ErrorCode(),
- exception.ProcName().TextA(),
- exception.ErrorMessage().TextA());
- qWarning() << m_strPouName << "." << m_strName << "ReadImage Execute() Error" << m_szMsg;
- }
- }
- }
- /// <summary>
- /// 确定
- /// </summary>
- void ToolDialogImpl::on_btnOK_clicked()
- {
- this->hide();
- }
- /// <summary>
- /// 取消按钮
- /// </summary>
- void ToolDialogImpl::on_btnCancel_clicked()
- {
- // 将本工具的恢复到打开工具之前的状态
- RecoverData();
- }
- /// <summary>
- /// 测试按钮
- /// </summary>
- void ToolDialogImpl::on_btnExecute_clicked()
- {
- QElapsedTimer toolTimer;
- toolTimer.start();
- // 发送事件
- ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
- QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
- // 统计返回值
- VPEnum::RETURN_VALUE ret = pToolEvent->ret;
- double nExecTime = toolTimer.elapsed();
- QString str;
- str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
- ui.label_time->setText(str);
- str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
- ui.label_state->setText(str);
- delete pToolEvent;
- }
- /// <summary>
- ///
- /// </summary>
- void ToolDialogImpl::on_rbnFile_clicked()
- {
- m_FilePathFinder.m_eFileFrom = E_FileFrom::e_fileFolder;
- }
- void ToolDialogImpl::on_rbnPath_clicked()
- {
- m_FilePathFinder.m_eFileFrom = E_FileFrom::e_filePath;
- }
- void ToolDialogImpl::on_btnReadImage_clicked()
- {
- if (m_FilePathFinder.m_eFileFrom == E_FileFrom::e_fileFolder)
- {
- QFileDialog dialog;
- dialog.setFileMode(QFileDialog::Directory);
- dialog.setOption(QFileDialog::ShowDirsOnly);
- QString directory = dialog.getExistingDirectory();
- if (directory.toStdString().size() > 3)
- {
- m_FilePathFinder.SetFilePath(E_FileFrom::e_fileFolder, directory);
- }
- }
- else
- {
- m_strFileName = QFileDialog::getOpenFileName(
- this,
- "Open Image",
- "C:\\VisionPlus_V3.0\\DemoImage\\",
- "Document files (*.jpg *.bmp *.png *.jpeg);;All files(*.*)");
- if (!m_strFileName.isNull())
- {
- m_FilePathFinder.SetFilePath(E_FileFrom::e_filePath, m_strFileName);
- }
- }
- onInitUI();
- }
- /// <summary>
- /// 调整大小的消息中改变表格栏的宽度
- /// </summary>
- /// <param name="event"></param>
- void ToolDialogImpl::resizeEvent(QResizeEvent* event)
- {
- Q_UNUSED(event);
- //
- // // qDebug() << "WindowAppVariableTable::resizeEvent";
- // int nTotalSize = ui.tableWidget->size().width();
- //
- // ui.tableWidget->setColumnWidth(0, nTotalSize * 0.05);
- // ui.tableWidget->setColumnWidth(1, nTotalSize * 0.25);
- // ui.tableWidget->setColumnWidth(2, nTotalSize * 0.7);
- //
- //
- // ui.tableWidget->resize(ui.tableWidget->size());
- }
- void ToolDialogImpl::on_ckbAutoAcq_stateChanged(int arg1)
- {
- if (arg1 > 0)
- {
- m_nFrameRate = ui.spinFrameRate->value();
- int nTime = 1000 / m_nFrameRate;
- m_Timer.setInterval(nTime);
- m_Timer.start();
- ui.spinFrameRate->setEnabled(false);
- }
- else
- {
- m_Timer.stop();
- ui.spinFrameRate->setEnabled(true);
- }
- m_nAutoAcq = arg1;
- }
- void ToolDialogImpl::onTimer()
- {
- // QtConcurrent::run(this, &ToolDialogImpl::Execute);
- // on_btnExecute_clicked();
- Execute();
- }
- // double getFps()
- // {
- // static double fps = 0;
- // static double lastTime = QDateTime::currentMSecsSinceEpoch(); // ms
- // static double frameCount = 0;
- // ++frameCount;
- // double curTime = QDateTime::currentMSecsSinceEpoch();
- // if (curTime - lastTime > 1000) // 取固定时间间隔为1秒
- // {
- // fps = frameCount;
- // frameCount = 0;
- // lastTime = curTime;
- // }
- // return fps;
- // }
- double getFps()
- {
- #include <chrono>
- static double fps = 0.0;
- static int frameCount = 0;
- static auto lastTime = std::chrono::system_clock::now();
- static auto curTime = std::chrono::system_clock::now();
- curTime = std::chrono::system_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>(curTime - lastTime);
- double duration_s = double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
- if (duration_s > 2)//2秒之后开始统计FPS
- {
- fps = frameCount / duration_s;
- frameCount = 0;
- lastTime = curTime;
- }
- ++frameCount;
- return fps;
- }
- void ToolDialogImpl::on_UpdateUI()
- {
- // 发送工具的实时路径到ui(实例名加接口名)
- this->syncHdValueToUi(this->m_strInstanceName, "ImagePath");
- this->syncHdValueToUi(this->m_strInstanceName, "ImageName");
- this->syncHdValueToUi(this->m_strInstanceName, "AcqIndex");
-
- QCoreApplication::sendEvent(m_pEventTarget, &m_eventOut);
- double nFps = getFps();
- QString str;
- str = QString(" %1 fps").arg(nFps, 0, 'G', 3);
- ui.labelFrameRate->setText(str);
-
- }
|