#include "ToolDialog.h" #include #include #include #include #include #include "toolinterface.h" #include "Tools/ThresholdTool.h" #include "Tools/DynthresholdTool.h" #include "Tools/MeanfilterTool.h" #include "Tools/MedianfilterTool.h" #include "Tools/GaussfilterTool.h" #include "Tools/GraydilationTool.h" #include "Tools/GrayerosionTool.h" #include "Tools/EmphasizeTool.h" #include "Tools/IlluminatetoolTool.h" #include "Tools/LightTool.h" #include "Tools/GrayopenTool.h" #include "Tools/GraycloseTool.h" #include "Tools/InvertcolorTool.h" #include "Tools/ImageconvertTool.h" #include "Tools/MirrorTool.h" #include "Tools/RotateTool.h" #include "Tools/ChangesizeTool.h" ToolDialogImpl::ToolDialogImpl(QWidget *parent, DllTool* pDllTool) : DllToolDialog(parent) { ui.setupUi(this); this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/); connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI())); connect(ui.tableWidget, SIGNAL(sigClick(bool) ), this, SLOT(clickSlot(bool) )); // 显示控件初始化 hwndUnit = new HWndUnit(this); Util::ShowUnitInWidget(ui.Image_widget, hwndUnit); // 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); QString strPath = QCoreApplication::applicationDirPath() + "/ocv1.bmp"; try { m_Image.ReadImage(strPath.toStdString().c_str()); hwndUnit->ShowImage(m_Image); hwndUnit->Refresh(false); } catch (...) { } ////////////////////////////////////////////////////////////////////////// ui.checkBox->setChecked(true); //ui.tableWidget->setMouseTracking(true); QMenu* menu = new QMenu(); QMenu* binarization = menu->addMenu("二值化"); binarization->addAction("二值化", this, SLOT(ActionSlot())); binarization->addAction("均值二值化", this, SLOT(ActionSlot())); QMenu* filter = menu->addMenu("滤波"); filter->addAction("均值滤波", this, SLOT(ActionSlot())); filter->addAction("中值滤波", this, SLOT(ActionSlot())); filter->addAction("高斯滤波", this, SLOT(ActionSlot())); QMenu* orphological = menu->addMenu("形态学运算"); orphological->addAction("灰度膨胀", this, SLOT(ActionSlot())); orphological->addAction("灰度腐蚀", this, SLOT(ActionSlot())); QMenu* imageenhance = menu->addMenu("图像增强"); imageenhance->addAction("锐化", this, SLOT(ActionSlot())); imageenhance->addAction("对比度", this, SLOT(ActionSlot())); imageenhance->addAction("亮度调节", this, SLOT(ActionSlot())); imageenhance->addAction("灰度开运算", this, SLOT(ActionSlot())); imageenhance->addAction("灰度闭运算", this, SLOT(ActionSlot())); QMenu* imageadjust = menu->addMenu("图像调整"); imageadjust->addAction("反色", this, SLOT(ActionSlot())); imageadjust->addAction("彩色转灰", this, SLOT(ActionSlot())); imageadjust->addAction("图像镜像", this, SLOT(ActionSlot())); imageadjust->addAction("图像旋转", this, SLOT(ActionSlot())); imageadjust->addAction("深度转彩色", this, SLOT(ActionSlot())); imageadjust->addAction("修改图像尺寸", this, SLOT(ActionSlot())); imageadjust->addAction("深度转灰度", this, SLOT(ActionSlot())); ui.btnAdd->setMenu(menu); } ToolDialogImpl::~ToolDialogImpl() { } VPEnum::RETURN_VALUE ToolDialogImpl::Execute() { HObject ImageTemp; ImageTemp = m_Image; hwndUnit->ShowImage(m_Image); for (int i = 0; i < m_LinkProcess.size(); i++) { if (m_LinkProcess[i].p_dlg != nullptr && m_LinkProcess[i].isrun == true) { m_LinkProcess[i].p_dlg->setValue(ImageTemp); m_LinkProcess[i].p_dlg->Execute(); ImageTemp = m_LinkProcess[i].p_dlg->GetResult(); } } hwndUnit->ShowObj(ImageTemp, false); hwndUnit->Refresh(); m_ImageOut = ImageTemp; emit sigUpdateUI(); return VPEnum::RETURN_VALUE::Success; } /// /// /// /// void ToolDialogImpl::Running(bool bRun) { } /// /// /// /// /// /// bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut) { int paranum;//参数数量 if (bIsOut)//保存参数流程 { paranum = 1; ar << paranum;//先保存参数数量 ar << (int)1 << m_Image; paranum = 2;//以下参数的数量 ar << paranum;//先保存参数数量 ar << (UINT)m_LinkProcess.size(); for (UINT i = 0; i < m_LinkProcess.size(); i++) { LINKCTRL& link = m_LinkProcess[i]; ar << (int)1 << link.ToolName; ar << (int)2 << link.ToolID; } // 保存工具内的参数 for (UINT i = 0; i < m_LinkProcess.size(); i++) { m_LinkProcess[i].p_dlg->Serialized(ar, true); } } else//加载参数流程,参数加载顺序一定要跟保存顺序一致 { m_bLoading = true; int nSize1 = 0; int para; ar >> paranum;//读取参数数量 for (int i = 0; i < paranum; i++) { ar >> para; switch (para) { case 1: ar >> m_Image; break; default: { qWarning() << "Serialized(In) Error" ; return false; } break; } } { m_LinkProcess.clear(); ui.tableWidget->clear(); for (int i = ui.stackedWidget->count(); i >= 0; i--) { QWidget* widget = ui.stackedWidget->widget(i); ui.stackedWidget->removeWidget(widget); } } UINT LinkSize = 0; ar >> paranum;//读取参数数量 ar >> LinkSize; for (UINT i = 0; i < LinkSize; i++) { int nToolID = 0; QString strToolName; int nPpara; for (int j = 0; j < paranum; j++) { ar >> nPpara; switch (nPpara) { case 1: ar >> strToolName; break; case 2: ar >> nToolID; break; default: return false; } } AddTool(nToolID); } // 加载工具内的参数 for (UINT i = 0; i < m_LinkProcess.size(); i++) { m_LinkProcess[i].p_dlg->Serialized(ar, false); } m_bLoading = false; } return true; } void ToolDialogImpl::on_UpdateUI() { } /// /// 确定 /// 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; } void ToolDialogImpl::ActionSlot() { if (m_bLoading) { return; } QAction* action = (QAction*)sender(); if (action->text() == "二值化") { AddTool(0); } if (action->text() == "均值二值化") { AddTool(1); } if (action->text() == "均值滤波") { AddTool(2); } if (action->text() == "中值滤波") { AddTool(3); } if (action->text() == "高斯滤波") { AddTool(4); } if (action->text() == "灰度膨胀") { AddTool(5); } if (action->text() == "灰度腐蚀") { AddTool(6); } if (action->text() == "锐化") { AddTool(7); } if (action->text() == "对比度") { AddTool(8); } if (action->text() == "亮度调节") { AddTool(9); } if (action->text() == "灰度开运算") { AddTool(10); } if (action->text() == "灰度闭运算") { AddTool(11); } if (action->text() == "反色") { AddTool(12); } if (action->text() == "彩色转灰") { AddTool(13); } if (action->text() == "图像镜像") { AddTool(14); } if (action->text() == "图像旋转") { AddTool(15); } if (action->text() == "修改图像尺寸") { AddTool(17); } } void ToolDialogImpl::clickSlot(bool ok) { if (ui.tableWidget->currentRow() != -1) { int nRow = ui.tableWidget->currentRow(); LINKCTRL aLinkCtrl = m_LinkProcess[nRow]; aLinkCtrl.isrun = ok; } } void ToolDialogImpl::AddTool(int nToolID) { if (nToolID == -1) { return; } ToolInterface* pDlg; LINKCTRL aLinkCtrl; QString strToolName; if (nToolID == 0) { strToolName = "二值化"; pDlg = new ThresholdTool(); } else if (nToolID == 1) { strToolName = "均值二值化"; pDlg = new dynthreshold(); } else if (nToolID == 2) { strToolName = "均值滤波"; pDlg = new meanfilter(); } else if (nToolID == 3) { strToolName = "中值滤波"; pDlg = new medianfilter(); } else if (nToolID == 4) { strToolName = "高斯滤波"; pDlg = new gaussfilter(); } else if (nToolID == 5) { strToolName = "灰度膨胀"; pDlg = new graydilation(); } else if (nToolID == 6) { strToolName = "灰度腐蚀"; pDlg = new grayerosion(); } else if (nToolID == 7) { strToolName = "锐化"; pDlg = new emphasizetool(); } else if (nToolID == 8) { strToolName = "对比度"; pDlg = new illuminatetool(); } else if (nToolID == 9) { strToolName = "亮度调节"; pDlg = new lighttool(); } else if (nToolID == 10) { strToolName = "灰度开运算"; pDlg = new grayopen(); } else if (nToolID == 11) { strToolName = "灰度闭运算"; pDlg = new grayclose(); } else if (nToolID == 12) { strToolName = "反色"; pDlg = new invertcolor(); } else if (nToolID == 13) { strToolName = "彩色转灰"; pDlg = new imageconverttool(); } else if (nToolID == 14) { strToolName = "图像镜像"; pDlg = new mirrortool(); } else if (nToolID == 15) { strToolName = "图像旋转"; pDlg = new rotatetool(); } else if (nToolID == 17) { strToolName = "修改图像尺寸"; pDlg = new changesize(); } connect(pDlg, SIGNAL(updateParameter(QString)), this, SLOT(UpdateUI(QString))); ui.tableWidget->addTool(m_LinkProcess.size(), strToolName, "null"/*pDlg->ShowParameter()*/); ui.tableWidget->selectRow(m_LinkProcess.size()); ui.stackedWidget->insertWidget(m_LinkProcess.size(), pDlg); ui.stackedWidget->setCurrentIndex(m_LinkProcess.size() ); aLinkCtrl.ToolID = nToolID; aLinkCtrl.p_dlg = pDlg; aLinkCtrl.ToolName = strToolName; aLinkCtrl.isrun = true; aLinkCtrl.nUi = m_LinkProcess.size(); m_LinkProcess.push_back(aLinkCtrl); if (ui.checkBox->isChecked()) { Execute(); } } void ToolDialogImpl::UpdateUI(QString name) { if (ui.checkBox->isChecked()) { ui.tableWidget->setItem(ui.tableWidget->currentRow(), 2, new QTableWidgetItem(name)); ui.tableWidget->item(ui.tableWidget->currentRow(), 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); try { Execute(); } catch (...) { qWarning() << "Error"; } } } void ToolDialogImpl::on_tableWidget_clicked(const QModelIndex& index) { int nRow = index.row(); LINKCTRL& aLink1 = m_LinkProcess.at(nRow); ui.stackedWidget->setCurrentIndex(aLink1.nUi); } void ToolDialogImpl::on_btnDel_clicked() { } void ToolDialogImpl::SwapTwoRow(int selectRow, int targetRow) { QString text1 = ui.tableWidget->item(selectRow, 1)->text(); QString text2 = ui.tableWidget->item(selectRow, 2)->text(); QString text3 = ui.tableWidget->item(targetRow, 1)->text(); QString text4 = ui.tableWidget->item(targetRow, 2)->text(); ui.tableWidget->setItem(selectRow, 1, new QTableWidgetItem(text3)); ui.tableWidget->setItem(selectRow, 2, new QTableWidgetItem(text4)); ui.tableWidget->setItem(targetRow, 1, new QTableWidgetItem(text1)); ui.tableWidget->setItem(targetRow, 2, new QTableWidgetItem(text2)); ui.tableWidget->item(selectRow, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.tableWidget->item(selectRow, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.tableWidget->item(targetRow, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.tableWidget->item(targetRow, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.tableWidget->selectRow(targetRow); } void ToolDialogImpl::on_btnMoveUp_clicked() { if (ui.tableWidget->currentRow() != -1 && ui.tableWidget->currentRow() > 0) { int nRow = ui.tableWidget->currentRow(); swap(m_LinkProcess.at(nRow), m_LinkProcess.at(nRow - 1));//向上交换处理模块位置 LINKCTRL aLink1 = m_LinkProcess.at(nRow -1); ui.stackedWidget->setCurrentIndex(aLink1.nUi); this->SwapTwoRow(nRow, nRow - 1); } } void ToolDialogImpl::on_btnMoveDown_clicked() { int a = ui.tableWidget->currentRow(); int b = ui.tableWidget->rowCount(); if (ui.tableWidget->currentRow() != -1 && ui.tableWidget->currentRow() < ui.tableWidget->rowCount()-1) { int nRow = ui.tableWidget->currentRow(); swap(m_LinkProcess.at(nRow), m_LinkProcess.at(nRow + 1));//向下交换处理模块位置 LINKCTRL aLink1 = m_LinkProcess.at(nRow + 1); ui.stackedWidget->setCurrentIndex(aLink1.nUi); this->SwapTwoRow(nRow, nRow + 1); } }