123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969 |
- #include "QHalconWindow.h"
- #include "ROIController.h"
- #include <QDebug>
- #include<QWheelEvent>
- #ifdef _WIN32
- #include <Exception>
- #endif
- #include<HalconCpp.h>
- #include "HWndMessage.h"
- #include <QtWidgets/QLabel>
- #include<QFileDialog>
- #include<QMessageBox>
- #include "HImage.h"
- #include"Util.h"
- #include "ROI.h"
- #include"ROICircle.h"
- #include"ROIRect1.h"
- #include"ROICircularArc.h"
- #include"ROIRect2.h"
- #include"ROILine.h"
- #include<QThread>
- #include <QCoreApplication>
- #include <QDateTime>
- using namespace std;
- using namespace HalconCpp;
- bool inMeasureLine = false;
- //构造函数
- QHalconWindow::QHalconWindow(QWidget* parent)
- : QWidget(parent)
- {
- resize(400, 300);
- setGeometry(QRect(30, 30, 400, 300));
- setMouseTracking(true);
- resultShow = ResultShow::AllObject;
- //show();
- OpenWindow();
- SetSystem("int_zooming", "true");
- SetSystem("border_shape_models", "false");
- SetSystem("tsp_width", 6000);
- SetSystem("tsp_height", 6000);
- backguoundColor = "#696969";
- windowWidth = size().width();
- windowHeight = size().height();
- showMode = ShowMode::IncludeROI;
- imageHeight = 400;
- imageWidth = 300;
- zoomWndFactor = (double)imageWidth / this->width();
- InitUserUI();
- roiManager = nullptr;
- firstEnter = true;
- }
- /**
- * @brief QHalconWindow::~QHalconWindow 析构函数
- */
- QHalconWindow::~QHalconWindow(void)
- {
- using namespace HalconCpp;
- CloseWindow(winID);
- // delete roiManager;
- qDebug() << " CloseWindow(winID);";
- }
- void QHalconWindow::refresh()
- {
- try
- {
- using namespace HalconCpp;
-
- SetSystem("flush_graphic", "false");
- SetDraw(winID, "margin");
- ClearWindow(winID);
- mGC.stateOfSettings.clear();
- //Util::notify(GBK("对象个数%1").arg(count));
-
- //m_Mutex.lock();
- int count = HObjList.count();
- for (int i = 0; i < count; i++)
- {
- if (i > 0 && resultShow == ResultShow::Image)
- {
- break;
- }
- HObject objShow = HObjList[i].getHobj();
- HWndMessage messageShow = HObjList[i].getMessage();
- if (objShow.IsInitialized())
- {
- this->mGC.applyContext(winID, HObjList[i].getGC());
- DispObj(objShow, winID);
- }
- else if (messageShow.IsInitialized())
- {
- messageShow.dispMessage(winID, "image", ((double)imageWidth / this->width()) / zoomWndFactor);
- }
- }
- //m_Mutex.unlock();
- if (roiManager != nullptr && (showMode == ShowMode::IncludeROI))
- {
- int dat = imageWidth / 300;
- if (dat < 5)
- {
- dat = 5;
- }
- roiManager->paintData(getWindowID(), dat, zoomWndFactor);
- }
- SetSystem("flush_graphic", "true");
- //获取当前显示信息
- HTuple hv_Red, hv_Green, hv_Blue;
- HTuple hv_lineWidth;
- GetRgb(winID, &hv_Red, &hv_Green, &hv_Blue);
- GetLineWidth(winID, &hv_lineWidth);
- HTuple hv_Draw;
- GetDraw(winID, &hv_Draw);
- if (showCross == false)
- {
- SetColor(winID, backguoundColor.toStdString().c_str());
- DispLine(winID, -100.0, -100.0, -101.0, -101.0);
- }
- else
- {
- SetDraw(winID, "margin");
- SetLineWidth(winID, 1);//设置线宽
- SetLineStyle(winID, HTuple());
- SetColor(winID, "green");//十字架显示颜色
- double CrossCol = (double)imageWidth / 2,
- CrossRow = (double)imageHeight / 2;
- //竖线
- HTuple row, col;
- row[0] = 0;
- row[1] = CrossRow - 50;
- col[0] = CrossCol;
- col[1] = CrossCol;
- DispPolygon(winID, row, col);
- row[0] = CrossRow + 50;
- row[1] = imageHeight;
- col[0] = CrossCol;
- col[1] = CrossCol;
- DispPolygon(winID, row, col);
- //中心点
- row[0] = CrossRow - 2;
- row[1] = CrossRow + 2;
- col[0] = CrossCol;
- col[1] = CrossCol;
- DispPolygon(winID, row, col);
- row[0] = CrossRow;
- row[1] = CrossRow;
- col[0] = CrossCol - 2;
- col[1] = CrossCol + 2;
- DispPolygon(winID, row, col);
- //横线
- row[0] = CrossRow;
- row[1] = CrossRow;
- col[0] = 0;
- col[1] = CrossCol - 50;
- DispPolygon(winID, row, col);
- row[0] = CrossRow;
- row[1] = CrossRow;
- col[0] = CrossCol + 50;
- col[1] = imageWidth;
- DispPolygon(winID, row, col);
- }
- //恢复窗口显示信息
- SetRgb(winID, hv_Red, hv_Green, hv_Blue);
- SetLineWidth(winID, hv_lineWidth);
- SetDraw(winID, hv_Draw);
- // SetColor(WindowID(),"black");
- // DispLine(WindowID(),-100.0, -100.0, -101.0, -101.0);
- }
- catch (HalconCpp::HException& exception)
- {
- char m_szMsg[2048] = "";
- snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s\n", exception.ErrorCode(),
- exception.ProcName().TextA(),
- exception.ErrorMessage().TextA());
- qWarning() << m_szMsg;
- }
- }
- void QHalconWindow::addIconicVar(HImage img)
- {
- using namespace HalconCpp;
- if (img.IsInitialized() == false)
- {
- return;
- }
- double r = 0, c = 0;
- Hlong h = 0, w = 0;
- Hlong area;
- //当前使用重载来判断是否图像,如果是hobject的图像,无法进入此处重载
- try
- {
- area = img.GetDomain().AreaCenter(&r, &c);
- img.GetImageSize(&w, &h);
- //面积=长*宽 表示确实是图片
- if (area == (w * h))
- {
- //添加图像就显示所有obj
- resultShow = ResultShow::AllObject;
- //HObjList.clear();
- if ((h != imageHeight) || (w != imageWidth))
- {
- imageHeight = h;
- imageWidth = w;
- resetWindow();
- }
- }//if
- originalImage = img;
- if (img.IsInitialized())
- {
- HObject ho_object(img);
- addIconicVar(ho_object);
- }
-
- // HObject ho_object = static_cast<HObject>(img);
- // addIconicVar(ho_object);
- }
- catch (HalconCpp::HException& exception)
- {
- char m_szMsg[2048] = "";
- snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s\n", exception.ErrorCode(),
- exception.ProcName().TextA(),
- exception.ErrorMessage().TextA());
- qWarning() << m_szMsg;
- }
- }
- void QHalconWindow::addIconicVar(HObject obj)
- {
- using namespace HalconCpp;
- if (obj.IsInitialized() == false)
- {
- return;
- }
- //m_Mutex.lock();
- //属性复制
- HObjectEntry entry(obj, mGC.graphicalSettings);
- HObjList.append(entry);
- if (HObjList.count() > MAX_NUM_OBJ_LIST)
- {
- HObjList.removeAt(1);
- }
- //m_Mutex.unlock();
- }
- void QHalconWindow::addMessage(HWndMessage message)
- {
- HObjectEntry entry(message);
- //m_Mutex.lock();
- HObjList.append(entry);
- if (HObjList.count() > MAX_NUM_OBJ_LIST)
- {
- HObjList.removeAt(1);
- }
- //m_Mutex.unlock();
- }
- void QHalconWindow::useROIController(ROIController* rC)
- {
- roiManager = rC;
- rC->setViewController(this);
- rC->setROISign(ROIOperation::Positive);
- }
- void QHalconWindow::resetWindow()
- {
- // setImagePart(0, 0, ImgRow2, ImgCol2);
- QRect rect = this->geometry();
- //判断行缩放还是列缩放
- double scaleC = (double)imageWidth / rect.width();
- double scaleR = (double)imageHeight / rect.height();
- double w, h;
- if (scaleC < scaleR)
- {
- h = rect.height() * scaleR;
- w = rect.width() * scaleR;
- ImgRow1 = 0;
- ImgCol1 = (imageWidth - w) / 2.0;
- }
- else {
- h = rect.height() * scaleC;
- w = rect.width() * scaleC;
- ImgRow1 = (imageHeight - h) / 2.0;
- ImgCol1 = 0;
- }
- ImgRow2 = ImgRow1 + h - 1;
- ImgCol2 = ImgCol1 + w - 1;
- zoomWndFactor = w / rect.width();
- SetPart(winID, ImgRow1, ImgCol1, ImgRow2, ImgCol2);
- // SetPart(WindowID(), 0,0,imageHeight-1,imageWidth-1);
- }
- void QHalconWindow::clearWindow()
- {
- HObjList.clear();
- refresh();
- }
- void QHalconWindow::changeGraphicSettings(const QString& mode, const HTuple& val)
- {
- this->mGC.setAttribute(mode, val);
- }
- void QHalconWindow::setResultShow(const ResultShow& resultShow)
- {
- this->resultShow = resultShow;
- refresh();
- }
- void QHalconWindow::setShowMode(const ShowMode& showMode)
- {
- this->showMode = showMode;
- refresh();
- }
- void QHalconWindow::on_actDispFit()
- {
- resetWindow();
- refresh();
- }
- void QHalconWindow::on_actSaveImage()
- {
- if (HObjList.count() < 1)
- {
- //QMessageBox::information(this, GBK("提示"), GBK("无图像数据,无法保存"));
- return;
- }
- QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
- QString strFileName = "Image_" + QDateTime::currentDateTime().toString("[MM-dd_hh_mm_ss_zzz]");
- QString strPath = runPath + "//..//PrjImage//" ;
- QDir dir(strPath);
- if (!dir.exists())
- {
- dir.mkdir(strPath);
- }
-
- QString fileName = QFileDialog::getSaveFileName(this,
- GBK("另存为"),
- strPath + strFileName,
- GBK("图像文件 (*.bmp)"));
- if (!fileName.isNull())
- {
- try
- {
- HTuple path = HTuple(fileName.toLocal8Bit().toStdString().c_str());
- if (originalImage.IsInitialized())
- {
- WriteImage(originalImage, "bmp", 0, path);
- //QMessageBox::information(this, GBK("提示"), GBK("图像保存完成"));
- }
- else
- {
- QMessageBox::information(this, GBK("提示"), GBK("图像未初始化"));
- }
- }
- catch (HException&)
- {
- QMessageBox::information(this, GBK("提示"), GBK("操作异常,请检查图像数据及文件路径"));
- return;
- }
- //fileName是文件名
- }
- }
- void QHalconWindow::on_actDumpWindow()
- {
- QString fileName = QFileDialog::getSaveFileName(this,
- GBK("截图另存为"),
- "",
- GBK("图像 (*.bmp)"));
- if (!fileName.isNull())
- {
- try
- {
- HTuple path = HTuple(fileName.toLocal8Bit().toStdString().c_str());
- DumpWindow(winID, "bmp", path);
- QMessageBox::information(this, GBK("提示"), GBK("截图保存完成"));
- }
- catch (HException&)
- {
- QMessageBox::information(this, GBK("提示"), GBK("文件路径异常,保存失败"));
- return;
- }
- //fileName是文
- }
- }
- void QHalconWindow::on_actShowCross()
- {
- showCross = !showCross;
- refresh();
- }
- void QHalconWindow::on_actDelActiveRoi()
- {
- if (roiManager == Q_NULLPTR) return;
- roiManager->removeActive();
- }
- void QHalconWindow::on_actDelAllRoi()
- {
- if (roiManager == Q_NULLPTR) return;
- roiManager->reset();
- }
- void QHalconWindow::on_actSetRoiAdd()
- {
- if (roiManager == Q_NULLPTR) return;
- roiManager->setROISign(ROIOperation::Positive);
- }
- void QHalconWindow::on_actSetRoiSub()
- {
- if (roiManager == Q_NULLPTR) return;
- roiManager->setROISign(ROIOperation::Negative);
- }
- void QHalconWindow::on_actGetDistance()
- {
- HWndMessage message = HWndMessage(HTuple(QString("鼠标点击两个位置,单击鼠标右键完成。").toStdString().c_str()), 20, 20, 20, "green");
- message.dispMessage(winID, "window", 1);
- inMeasureLine = true;
- // viewPort.ContextMenuStrip = null;
- HTuple r1, c1, r2, c2;
- HTuple dd;
- //获取当前显示信息
- HTuple hv_Red, hv_Green, hv_Blue;
- GetRgb(winID, &hv_Red, &hv_Green, &hv_Blue);
- HTuple hv_lineWidth;
- GetLineWidth(winID, &hv_lineWidth);
- HTuple hv_Draw;
- GetDraw(winID, &hv_Draw);
- SetLineWidth(winID, 1);//设置线宽
- SetLineStyle(winID, HTuple());
- SetColor(winID, "green");//画点的颜色
- DrawLine(winID, &r1, &c1, &r2, &c2);
- DispLine(winID, r1, c1, r2, c2);
- //恢复窗口显示信息
- SetRgb(winID, hv_Red, hv_Green, hv_Blue);
- SetLineWidth(winID, hv_lineWidth);
- SetDraw(winID, hv_Draw);
- DistancePp(r1, c1, r2, c2, &dd);
- double dr = abs(r2.D() - r1.D());
- double dc = abs(c2.D() - c1.D());
- QMessageBox::information(this, GBK("提示"), QString(GBK("直线长度:%1 px\nx轴距离:%2 px\ny轴距离:%3 px")).arg(dd.D(), 0, 'f', 2)
- .arg(dc, 0, 'f', 2)
- .arg(dr, 0, 'f', 2));
- // MessageBox.Show(string.Format("直线距离%1 px\rx轴距离%2 px\ry轴距离%3 px", dd.D, dc, dr), "结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
- //hv_MenuStrip.Visible = true;
- //viewPort.ContextMenuStrip = hv_MenuStrip;
- inMeasureLine = false;
- refresh();
- }
- void QHalconWindow::on_actShowImageOnly()
- {
- if (resultShow == ResultShow::AllObject)
- {
- resultShow = ResultShow::Image;
- }
- else
- {
- resultShow = ResultShow::AllObject;
- }
- refresh();
- }
- void QHalconWindow::on_actCreateCircle()
- {
- if (roiManager == NULL)
- {
- return;
- }
- ROICircle* roi = new ROICircle();
- roiManager->setROISign(ROIOperation::Negative);
- roiManager->setROIShape(roi);
- }
- void QHalconWindow::on_actCreateCircleArc()
- {
- if (roiManager == NULL)
- {
- return;
- }
- ROICircularArc* roi = new ROICircularArc();
- roiManager->setROISign(ROIOperation::Negative);
- roiManager->setROIShape(roi);
- }
- void QHalconWindow::on_actCreateLine()
- {
- if (roiManager == NULL)
- {
- return;
- }
- ROILine* roi = new ROILine();
- roiManager->setROISign(ROIOperation::Negative);
- roiManager->setROIShape(roi);
- }
- void QHalconWindow::on_actCreateRect1()
- {
- if (roiManager == NULL)
- {
- return;
- }
- ROIRect1* roi = new ROIRect1();
- roiManager->setROISign(ROIOperation::Negative);
- roiManager->setROIShape(roi);
- }
- void QHalconWindow::on_actCreateRect2()
- {
- if (roiManager == NULL)
- {
- return;
- }
- ROIRect2* roi = new ROIRect2();
- roiManager->setROISign(ROIOperation::Negative);
- roiManager->setROIShape(roi);
- }
- // Open a HALCON window inside the QHalconWindow widget
- void QHalconWindow::OpenWindow(void)
- {
- using namespace HalconCpp;
- #ifdef Q_WS_X11
- // In Qt versions older version 3.3, it is necessary to temporarily
- // change the SubstructureRedirectMask attribute of a Qt widget
- // in order to be able to open a HALCON window inside the widget
- XWindowAttributes attr;
- #if QT_VERSION >= 0x040000
- XGetWindowAttributes(x11Info().display(), winId(), &attr);
- if (attr.your_event_mask & SubstructureRedirectMask)
- XSelectInput(x11Info().display(), winId(),
- attr.your_event_mask & ~SubstructureRedirectMask);
- XFlush(x11Info().display());
- XSync(x11Info().display(), False);
- #else
- XGetWindowAttributes(x11Display(), winId(), &attr);
- if (attr.your_event_mask & SubstructureRedirectMask)
- XSelectInput(x11Display(), winId(),
- attr.your_event_mask & ~SubstructureRedirectMask);
- XFlush(x11Display());
- XSync(x11Display(), False);
- #endif
- #endif
- SetWindowAttr("border_width", 0);
- SetCheck("~father");
- // Open a HALCON window with dummy width and height
- // it will be resized soon
- HalconCpp::OpenWindow(0, 0, 100, 100, (Hlong)winId(), "visible", "", &winID);
- // HalconCpp::OpenWindow(0,0,100,100, (Hlong)this,"invisible","",&winID);
- SetCheck("father");
- #ifdef Q_WS_X11
- // Reset widget attributes to previous settings (see above comment)
- #if QT_VERSION >= 0x040000
- if (attr.your_event_mask & SubstructureRedirectMask)
- XSelectInput(x11Info().display(), winId(), attr.your_event_mask);
- #else
- if (attr.your_event_mask & SubstructureRedirectMask)
- XSelectInput(x11Display(), winId(), attr.your_event_mask);
- #endif
- #endif
- }
- void QHalconWindow::DispImageZoom(HTuple mode, HTuple Mouse_row, HTuple Mouse_col)
- {
- GetPart(winID, ¤t_beginRow, ¤t_beginCol, ¤t_endRow, ¤t_endCol);
- if (mode > 0) // 放大图像
- {
- zoom_beginRow = (int)(current_beginRow.D() + (Mouse_row.D() - current_beginRow.D()) * 0.3);
- zoom_beginCol = (int)(current_beginCol.D() + (Mouse_col.D() - current_beginCol.D()) * 0.3);
- zoom_endRow = (int)(current_endRow.D() - (current_endRow.D() - Mouse_row.D()) * 0.3);
- zoom_endCol = (int)(current_endCol.D() - (current_endCol.D() - Mouse_col.D()) * 0.3);
- }
- else // 缩小图像
- {
- zoom_beginRow = (int)(Mouse_row.D() - (Mouse_row.D() - current_beginRow.D()) / 0.7);
- zoom_beginCol = (int)(Mouse_col.D() - (Mouse_col.D() - current_beginCol.D()) / 0.7);
- zoom_endRow = (int)(Mouse_row.D() + (current_endRow.D() - Mouse_row.D()) / 0.7);
- zoom_endCol = (int)(Mouse_col.D() + (current_endCol.D() - Mouse_col.D()) / 0.7);
- }
- QRect rect = this->geometry();
- int hw_width = rect.width();
- int hw_height = rect.height();
- bool _isOutOfArea = true;
- bool _isOutOfSize = true;
- bool _isOutOfPixel = true; //避免像素过大
- if (zoom_endRow == zoom_beginRow || zoom_endCol == zoom_beginCol)
- {
- return;
- }
- _isOutOfArea = zoom_beginRow >= imageHeight || zoom_endRow <= 0 || zoom_beginCol >= imageWidth || zoom_endCol < 0;
- _isOutOfSize = (zoom_endRow - zoom_beginRow) > imageHeight * 20 || (zoom_endCol - zoom_beginCol) > imageWidth * 20;
- _isOutOfPixel = hw_height / (zoom_endRow - zoom_beginRow) > 500 || hw_width / (zoom_endCol - zoom_beginCol) > 500;
- if (_isOutOfArea || _isOutOfSize || _isOutOfPixel)
- {
- return;
- }
- SetPaint(winID, "default");
- SetPart(winID, zoom_beginRow, zoom_beginCol,
- zoom_endRow, zoom_beginCol +
- (zoom_endRow - zoom_beginRow) * hw_width / hw_height);
- int w = (zoom_endRow - zoom_beginRow) * hw_width / hw_height;
- int w0 = current_endCol.D() - current_beginCol.D();
- double scale = (double)w / w0;
- zoomWndFactor *= scale;
- refresh();
- }
- void QHalconWindow::InitUserUI()
- {
- actDispImageOnly = new QAction(this);
- actDispFit = new QAction(this);
- actShowCross = new QAction(this);
- actSaveImage = new QAction(this);
- actDumpWindow = new QAction(this);
- actDispImageOnly->setText((GBK("显示原图/所有")));
- actDispFit->setText(GBK("适应图像显示"));
- actShowCross->setText(GBK("显示/隐藏十字架"));
- actSaveImage->setText(GBK("保存图像"));
- actDumpWindow->setText(GBK("截图另存"));
- connect(actDispFit, SIGNAL(triggered(bool)), this, SLOT(on_actDispFit()));
- connect(actSaveImage, SIGNAL(triggered(bool)), this, SLOT(on_actSaveImage()));
- connect(actDumpWindow, SIGNAL(triggered(bool)), this, SLOT(on_actDumpWindow()));
- connect(actShowCross, SIGNAL(triggered(bool)), this, SLOT(on_actShowCross()));
- }
- void QHalconWindow::moveImage(double motionX, double motionY)
- {
- GetPart(winID, ¤t_beginRow, ¤t_beginCol, ¤t_endRow, ¤t_endCol);
- ImgRow1 = current_beginRow - motionY;
- ImgRow2 = current_endRow - motionY;
- ImgCol1 = current_beginCol - motionX;
- ImgCol2 = current_endCol - motionX;
- SetPart(winID, ImgRow1, ImgCol1,
- ImgRow2, ImgCol2);
- refresh();
- }
- // Resize the HALCON window whenever the QHalconWindow widget is resized
- void QHalconWindow::resizeEvent(QResizeEvent*)
- {
- using namespace HalconCpp;
- #ifdef Q_WS_X11
- // See comment in ::OpenWindow()
- XWindowAttributes attr;
- #if QT_VERSION >= 0x040000
- XGetWindowAttributes(x11Info().display(), winId(), &attr);
- if (attr.your_event_mask & SubstructureRedirectMask)
- XSelectInput(x11Info().display(), winId(),
- attr.your_event_mask & ~SubstructureRedirectMask);
- XFlush(x11Info().display());
- XSync(x11Info().display(), False);
- #else
- XGetWindowAttributes(x11Display(), winId(), &attr);
- if (attr.your_event_mask & SubstructureRedirectMask)
- XSelectInput(x11Display(), winId(),
- attr.your_event_mask & ~SubstructureRedirectMask);
- XFlush(x11Display());
- XSync(x11Display(), False);
- #endif
- #endif
- // Set the HALCON window to its new size.
- SetWindowExtents(winID, 0, 0, rect().width(), rect().height() );
- #ifdef Q_WS_X11
- // See comment in ::OpenWindow()
- #if QT_VERSION >= 0x040000
- if (attr.your_event_mask & SubstructureRedirectMask)
- XSelectInput(x11Info().display(), winId(), attr.your_event_mask);
- #else
- if (attr.your_event_mask & SubstructureRedirectMask)
- XSelectInput(x11Display(), winId(), attr.your_event_mask);
- #endif
- #endif
- }
- void QHalconWindow::mousePressEvent(QMouseEvent*)
- {
- if (inMeasureLine)return;
- mousePressed = true;
- HTuple state;
- HTuple x, y;
- try
- {
- GetMpositionSubPix(winID, &y, &x, &state);
- }
- catch (HException&)
- {
- return;
- }
- int activeROIidx = -1;
- if (roiManager != Q_NULLPTR && (showMode == ShowMode::IncludeROI))
- {
- activeROIidx = roiManager->mouseDownAction(x.D(), y.D());
- }
- if (activeROIidx == -1)
- {
- startX = x;
- startY = y;
- }
- }
- void QHalconWindow::wheelEvent(QWheelEvent* event)
- {
- if (inMeasureLine)return;
- using namespace HalconCpp;
- HTuple Row, Column, Button;
- try
- {
- GetMpositionSubPix(winID, &Row, &Column, &Button);
- }
- catch (HException&)
- {
- return;
- }
- HTuple mode = 1;
- if (event->delta() > 0)
- {
- mode = 1;
- }
- else
- {
- mode = -1;
- }
- DispImageZoom(mode, Row, Column);
- }
- void QHalconWindow::mouseReleaseEvent(QMouseEvent*)
- {
- if (inMeasureLine)return;
- mousePressed = false;
- if (roiManager != NULL
- && (roiManager->getActiveROIidx() != -1)
- && (showMode == ShowMode::IncludeROI))
- {
- roiManager->tiggerROINotifySignals(ViewMessage::UpdateROI);
- }
- }
- void QHalconWindow::mouseMoveEvent(QMouseEvent*)
- {
- if (inMeasureLine)return;
- // qDebug()<<"mouse move";
- double motionX, motionY;
- if (HObjList.count() < 1)
- {
- return;
- }
- HTuple currX, currY, state;
- try
- {
- GetMpositionSubPix(winID, &currY, &currX, &state);
- if (originalImage.IsInitialized())
- {
- QString str_value = "";
- QString str_position = "";
- bool _isXOut = true, _isYOut = true;
- int channel_count;
- QString str_imgSize = QString("%1*%2").arg(imageWidth).arg(imageHeight);
- channel_count = originalImage.CountChannels();
- str_position = QString("|%1*%2").arg((int)currX.D()).arg((int)currY.D());;
- _isXOut = (currX.D() < 0 || currX.D() >= imageWidth);
- _isYOut = (currY.D() < 0 || currY.D() >= imageHeight);
- if (!_isXOut && !_isYOut)
- {
- if ((int)channel_count == 1)
- {
- double grayVal;
- grayVal = originalImage.GetGrayval((int)currY.D(), (int)currX.D());
- str_value = QString("|%1").arg((int)grayVal);
- }
- else if ((int)channel_count == 3)
- {
- double grayValRed, grayValGreen, grayValBlue;
- HImage _RedChannel, _GreenChannel, _BlueChannel;
- _RedChannel = originalImage.AccessChannel(1);
- _GreenChannel = originalImage.AccessChannel(2);
- _BlueChannel = originalImage.AccessChannel(3);
- grayValRed = _RedChannel.GetGrayval((int)currY.D(), (int)currX.D());
- grayValGreen = _GreenChannel.GetGrayval((int)currY.D(), (int)currX.D());
- grayValBlue = _BlueChannel.GetGrayval((int)currY.D(), (int)currX.D());
- str_value = QString("|%1,%2,%3").arg((int)grayValRed).arg((int)grayValGreen).arg((int)grayValBlue);
- }
- else
- {
- str_value = "";
- }
- }
- QString MousePosMessage = str_imgSize + str_position + str_value;
- emit mouseMoveStatus(MousePosMessage);
- }
- if (!mousePressed)
- {
- return;
- }
- if (roiManager != Q_NULLPTR &&
- (roiManager->getActiveROIidx() != -1) &&
- (showMode == ShowMode::IncludeROI))
- {
- roiManager->mouseMoveAction(currX.D(), currY.D());
- }
- else
- {
- //qDebug()<<"xx.D():"<<xx.;
- motionX = ((currX.D() - startX));
- motionY = ((currY.D() - startY));
- if (((int)motionX != 0) || ((int)motionY != 0))
- {
- moveImage(motionX, motionY);
- startX = currX.D() - motionX;
- startY = currY.D() - motionY;
- }
- }
- }
- catch (HOperatorException&)
- {
- return;
- }
- catch (HException&)
- {
- return;
- }
- // HImage *img=&(HObjList[0].HObj);
- }
- void QHalconWindow::contextMenuEvent(QContextMenuEvent* event)
- {
- popMenu = new QMenu(this);
- //清除原有菜单
- popMenu->clear();
- popMenu->addAction(actDispFit);
- popMenu->addSeparator();
- popMenu->addAction(actDispImageOnly);
- popMenu->addAction(actShowCross);
- popMenu->addSeparator();
- popMenu->addAction(actSaveImage);
- popMenu->addAction(actDumpWindow);
- //菜单出现的位置为当前鼠标的位置
- popMenu->exec(event->globalPos());
- delete popMenu;
- popMenu = NULL;
- }
- void QHalconWindow::enterEvent(QEvent*)
- {
- // if(firstEnter)
- // {
- // firstEnter=false;
- // refresh();
- // }
- }
- void QHalconWindow::showEvent(QShowEvent*)
- {
- refresh();
- }
- void QHalconWindow::setBackgroundColor(QString color)
- {
- backguoundColor = color;
- SetWindowParam(winID, "background_color", color.toStdString().c_str());
- }
- void QHalconWindow::on_actDispImageOnly()
- {
- if (resultShow == ResultShow::Image)
- {
- setResultShow(ResultShow::AllObject);
- }
- else
- {
- setResultShow(ResultShow::Image);
- }
- }
|