#include "ToolDialog.h" #include #include #include #include #include QEvent::Type SyncValueEvent::m_EventType = QEvent::None; QEvent::Type SyncInterfaceEvent::m_EventType = QEvent::None; ToolDialogImpl::ToolDialogImpl(QWidget *parent, DllTool* pDllTool) : DllToolDialog(parent) , m_pDllTool(pDllTool) { ui.setupUi(this); this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/); m_Value = 0; m_Value2 = 0; m_Value3 = 0; connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(onButtonTest())); connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI())); // 连接DB读和写 //connect(ui.buttonDbRead, SIGNAL(clicked()), this, SLOT(on_pushButton_DbRead())); //connect(ui.buttonDbWrite, SIGNAL(clicked()), this, SLOT(on_pushButton_DbWrite())); // 连接动态端口和增加和删除 //connect(ui.buttonInfAdd, SIGNAL(clicked()), this, SLOT(on_pushButton_InfAdd())); //connect(ui.buttonInfDel, SIGNAL(clicked()), this, SLOT(on_pushButton_InfDel())); m_nTimerID = 0; m_nTimerID = this->startTimer(10); // 显示控件初始化 hwndUnit = new HWndUnit(this); Util::ShowUnitInWidget(ui.Image_widget, hwndUnit); hwndUnit->setWndTitle(("相机1")); hwndUnit->getHWndCtrl()->useROIController(&roiController); connect(&roiController, SIGNAL(ROIChange(const ViewMessage)), this, SLOT(on_ROIChange(const ViewMessage))); ROIRect1* roi = new ROIRect1(); roi->setTitle("搜索区域"); roiController.setROISign(ROIOperation::Negative); roiController.setROIShape(roi); roiController.mouseDownAction(500, 500); //m_eventOut.setExType(TOOL_EVENT_EXECUTION::EXECUTION_1); // int* pValue1 = new int; // *pValue1 = 0; // m_nValues.push_back(*pValue1); // // // int* pValue2 = new int; // *pValue2 = 0; // m_nValues.push_back(*pValue2); // // int* pValue3 = new int; // *pValue3 = 0; // m_nValues.push_back(*pValue3); int a = 0; int b = 0; int c = 0; m_nValues.push_back(a); m_nValues.push_back(b); m_nValues.push_back(c); } ToolDialogImpl::~ToolDialogImpl() { } VPEnum::RETURN_VALUE ToolDialogImpl::Execute() { // qDebug() << "[Dll] CsGetInt tool Execute start."; m_Value2++; m_Value3 = m_Value + m_Value2; // if (m_Value3 >= 2) // { // m_Value3 = 0; // } m_nValues[1] = m_Value2 ; m_nValues[2] = m_Value2 + 100; //秒级时间戳(十位) //QString timestamp = QString::number(QDateTime::currentMSecsSinceEpoch() / 1000); //毫秒级时间戳(十三位) QString timestamp = QString::number(QDateTime::currentMSecsSinceEpoch()); //m_Value2 = m_Value3 * 2; //qDebug() << "Data Time is " << timestamp; //m_Value2++; // 测试延时 //Utility::qSleep(30); try { hwndUnit->ShowImage(m_Image); hwndUnit->ShowObj(m_objROI, qRgb( 255, 0, 0 ), 2); hwndUnit->ShowMsg((HTuple)"msg", qRgb(255, 0, 0),10, 10, 200); hwndUnit->Refresh(); } catch (...) { qWarning() << "Execute() Error"; } emit sigUpdateUI(); // Example:在数值变动时向UI同步(可以在任何位置发送) // 本变量对应 "Int1.out" //syncValueToUi(&m_Value3); //// 本变量对应 "Int2.in" //syncValueToUi(&m_Value2); //m_TestClass.SetValue(m_Value3); return VPEnum::RETURN_VALUE::Success; } void ToolDialogImpl::on_ROIChange(const ViewMessage& sign) { switch (sign) { case ViewMessage::MovingROI: { ROI* roi = roiController.getActiveROI(); hv_roiDate = roi->getROIData(); try { GenRectangle1(&m_objROI, hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], hv_roiDate[3]); } catch (...) { } } case ViewMessage::UpdateROI: case ViewMessage::CreatedROI: { roiController.defineModelROI(); ROI* roi = roiController.getActiveROI(); hv_roiDate = roi->getROIData(); try { GenRectangle1(&m_objROI, hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], hv_roiDate[3]); } catch (...) { } } break; default: break; } } /// /// DB读(For Test) /// void ToolDialogImpl::on_buttonDbRead_clicked() { if (m_pDB == nullptr) { //MessageBox(nullptr, L"DB is nullptr", L"Get_Int", MB_ICONWARNING); return; } // 显示Gvl中的值 for (VARIABLE* var : m_pDB->Variables) { //qDebug() << "Variable:" << var->strName << "[" << var->getValueString() << "]"; } } /// /// DB写 /// void ToolDialogImpl::on_buttonDbWrite_clicked() { if (m_pDB == nullptr) { //MessageBox(nullptr, L"DB is nullptr", L"Get_Int", MB_ICONWARNING); return; } // 遍历所有的值,为其中的int自增 QList infs; for (VARIABLE* var : m_pDB->Variables) { if (var->value.type == VALUE_TYPE::Type_Int) { (*(int*)(var->value.Ptr))++; } if (var->value.type == VALUE_TYPE::Type_Bool) { (*(bool*)(var->value.Ptr)) = !(*(bool*)(var->value.Ptr)); } // 储存要刷新的变量列表 infs.push_back(var); } // 发送变更通知 this->syncValuesToTableUI(infs); } /// /// 动态添加接口 /// void ToolDialogImpl::on_buttonInfAdd_clicked() { // qWarning() << "on_buttonInfAdd_clicked()"; // 通知exe端添加对应的接口数据结构 QList dynInfs; // 目前接口的生成都没有绑定Value,SetValue单独拆分出来 // 生成一个测试用的int类型输入接口 DLL_INF inf; inf.strName = ("Int1.Dyn.in"); inf.Direction = INF_DIRECTION::INF_DIR_IN; inf.Type = INF_TYPE::INF_TYPE_STANDARD; inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE; inf.value.type = VALUE_TYPE::Type_Int; //inf.value.Ptr = (void**)valueIn; inf.nIndex = m_pDllTool->GetInterfaceSize(); // 设置为动态接口 inf.bDynamic = true; // 2022-3-14,通过函数手工绑定Interface的Value m_pDllTool->SetValue(inf.value, m_nValues[0]); // For Test //int nValue = *(int*)inf.value.Ptr; // 动态添加 m_pDllTool->AddInterface(inf); // 添加到通知集合中 dynInfs.push_back(inf); inf.reset(); // 生成一个测试用的int类型输出接口 inf.strName = ("Int1.Dyn.out"); inf.Direction = INF_DIRECTION::INF_DIR_OUT; inf.Type = INF_TYPE::INF_TYPE_STANDARD; inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE; inf.value.type = VALUE_TYPE::Type_Int; // inf.value.Ptr = (void**)valueOut; inf.nIndex = m_pDllTool->GetInterfaceSize(); // 设置为动态接口 inf.bDynamic = true; // 动态添加 m_pDllTool->AddInterface(inf); // SetValue() 另外一种调用方式示例 // // 2022-3-14,接口添加完毕后,通过函数手工绑定Interface的Value m_pDllTool->SetValue(inf.nIndex, m_nValues[1] ); // For Test //nValue = *(int*)m_pDllTool->Interface(inf.nIndex).value.Ptr; // 添加到通知集合中 dynInfs.push_back(inf); // 生成一个测试用的int类型输出接口 inf.strName = ("Int2.Dyn.out"); inf.Direction = INF_DIRECTION::INF_DIR_OUT; inf.Type = INF_TYPE::INF_TYPE_STANDARD; inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE; inf.value.type = VALUE_TYPE::Type_Int; inf.value.Ptr = (void**)&m_nValues[2]; inf.nIndex = m_pDllTool->GetInterfaceSize(); // 设置为动态接口 inf.bDynamic = true; // 动态添加 m_pDllTool->AddInterface(inf); // 添加到通知集合中 dynInfs.push_back(inf); // 发送同步动态端口的通知 this->syncInterfacesToPou(dynInfs, true); } /// /// 动态删除接口 /// void ToolDialogImpl::on_buttonInfDel_clicked() { // 通知exe端添加对应的接口数据结构 QList dynInfs; // 要删除的接口名字 DLL_INF inf; inf.strName = ("Int1.Dyn.out"); // 动态删除 // TODO:此处需要考虑是否还需要手工删除Interface中绑定的Value bool bRet = m_pDllTool->DelInterface(inf.strName); // 如果删除成功,则添加到通知集合中 if (bRet) { // 添加到通知集合中 dynInfs.push_back(inf); } inf.reset(); // 发送同步动态端口的通知 if (dynInfs.size() > 0) { this->syncInterfacesToPou(dynInfs, false); } } /// /// 2022-3-1 向exe中的变量表界面同步数值 /// /// void ToolDialogImpl::syncValuesToTableUI(QList pInfs) { SyncValueEvent* valueEvent = new SyncValueEvent(); // 携带需要同步的多个接口 valueEvent->setSyncValues(pInfs); // post数值同步消息 QCoreApplication::postEvent(this->m_pGvlTarget, valueEvent); } /// /// 2022-3-8 向exe中的同步最新的接口变动(一个或多个) /// /// void ToolDialogImpl::syncInterfacesToPou(QList pInfs, bool bAdd) { SyncInterfaceEvent infEvent;// = new SyncInterfaceEvent(); // 携带需要同步的多个接口 infEvent.setSyncValues(pInfs); // 设置要本工具的实例名称 infEvent.m_strInstanceName = this->m_strInstanceName; // 增加端口 infEvent.m_bAdd = bAdd; // post数值同步消息 QCoreApplication::sendEvent(this->m_pPouTarget, &infEvent); // 获取事件执行状态 bool bRet = infEvent.m_bSuccess; if (!bRet) { qWarning() << m_strInstanceName << " add interface error, reason: " << infEvent.m_strReason; } } /// /// /// /// void ToolDialogImpl::Running(bool bRun) { } /// /// /// /// void ToolDialogImpl::timerEvent(QTimerEvent* event) { if (event->timerId() == m_nTimerID) { //QCoreApplication::sendEvent(m_pEventTarget, &m_eventOut); //QCoreApplication::sendEvent(m_pEventTarget, &m_eventOut); //qDebug() << m_nTimerID; } } /// /// /// /// /// bool ToolDialogImpl::bindValuePtrByName(const QString strName, const int nIndex) { // int* valueOut = new int; // *valueOut = 888; // m_pDllTool->Interface(nIndex).value.Ptr = (void**)m_pValue; m_pDllTool->SetValue(nIndex, m_nValues[nIndex -7 ] ); return true; } /// /// /// /// /// /// bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut) { int paranum;//参数数量 if (bIsOut)//保存参数流程 { paranum = 5; ar << paranum;//先保存参数数量 ar << (int)1 << hv_roiDate; ar << (int)2 << m_Value2; ar << (int)3 << m_Image; ar << (int)4 << roiController; ar << (int)5 << m_objROI; } else//加载参数流程,参数加载顺序一定要跟保存顺序一致 { int nSize1 = 0; int para; ar >> paranum;//读取参数数量 for (int i = 0; i < paranum; i++) { ar >> para; switch (para) { case 1: ar >> hv_roiDate; break; case 2: ar >> m_Value2; break; case 3: ar >> m_Image; break; case 4: ar >> roiController; break; case 5: ar >> m_objROI; break; default: { qWarning() << "Serialized(In) Error" ; return false; } break; } } //hwndUnit->getHWndCtrl()->useROIController(&roiController); } return true; } ///// ///// 设置全局变量操作指针(exe中GvlManager),并且获取其中的DB数据信息 ///// ///// //void ToolDialogImpl::setGvlManager(QObject* pManager) //{ // this->m_pGvlManager = pManager; // // // 2022-3-1,这段废弃,已经不从dll中枚举所有的DB了 // //// 界面同步DB数据库组列表 // //GvlManager* gvlManager = (GvlManager*)this->m_pGvlManager; // // //QList gvls = gvlManager->getAllDB(); // // //for (GVL* gvl : gvls) // //{ // // ui.comboDB->addItem(gvl->strName); // //} //} ///// ///// 变动的数值向UI同步 ///// ///// //void ToolDialogImpl::syncValueToUi(void* pValue) //{ // if (this->m_pSyncTarget==nullptr) // { // qDebug() << "ToolDialogImpl::syncValueToUi failed - m_pSyncTarget is nullptr."; // // return; // } // // // 生成数值同步消息,向UI同步 // SyncHdValueEvent* valueEvent = new SyncHdValueEvent(); // // // 保存变动数值信息 // valueEvent->m_pSrcValue = pValue; // // // 发送 value 同步 event,event指针会自动释放 // QCoreApplication::postEvent(m_pSyncTarget, valueEvent); //} ///// ///// 变动的控件向UI同步 ///// ///// //void ToolDialogImpl::syncControlToUi(QWidget* pWidget, UI_SYNC_MSG syncMsg) //{ // if (this->m_pSyncTarget == nullptr) // { // qDebug() << "ToolDialogImpl::syncControlToUi failed - m_pSyncTarget is nullptr."; // // return; // } // // // 生成控件的同步消息,向UI同步 // SyncControlEvent* controlEvent = new SyncControlEvent(); // // // 保存变动数值信息 // controlEvent->m_pSrcControl = pWidget; // controlEvent->m_SyncMsg = syncMsg; // // // 发送 value 同步 event,event指针会自动释放 // QCoreApplication::postEvent(m_pSyncTarget, controlEvent); //} void ToolDialogImpl::on_UpdateUI() { ui.textEdit_1->setText(QString::number(m_Value)); ui.textEdit_2->setText(QString::number(m_Value2)); ui.textEdit_3->setText(QString::number(m_Value3)); } void ToolDialogImpl::onButtonTest() { QString OpenFile, OpenFilePath; OpenFile = QFileDialog::getOpenFileName(this, "please choose an image file", "C:\\VisionPlus_V3.0\\DemoImage\\", "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)"); if (! OpenFile.isEmpty()) { try { ReadImage(&m_Image, HString::FromUtf8(OpenFile.toStdString().c_str())); m_QImage.load(OpenFile); hwndUnit->ShowImage(m_Image); hwndUnit->Refresh(); } catch (...) { qWarning() << "Error"; } } } void ToolDialogImpl::on_pushButton_2_clicked() { QCoreApplication::sendEvent(m_pEventTarget, &m_eventOut); } void ToolDialogImpl::on_pushButton_3_clicked() { // QCoreApplication::sendEvent(m_pEventTarget, &m_eventOut2); } void ToolDialogImpl::on_radioButton_clicked() { ui.stackedWidget->setCurrentIndex(0); } void ToolDialogImpl::on_radioButton_2_clicked() { ui.stackedWidget->setCurrentIndex(1); } /// /// 确定 /// void ToolDialogImpl::on_btnOK_clicked() { this->hide(); } /// /// 取消按钮 /// void ToolDialogImpl::on_btnCancel_clicked() { // 将本工具的恢复到打开工具之前的状态 RecoverData(); } /// /// 测试按钮 /// 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().key((short)ret)); ui.label_state->setText(str); delete pToolEvent; }