123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- #include "ToolDialog.h"
- #include <QImage>
- #include <QFileInfo>
- #include <QFileDialog>
- #include <QElapsedTimer>
- #include <QGridLayout>
- #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;
- }
- /// <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 = 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()
- {
- }
- /// <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;
- }
- 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);
- }
- }
|