#include "HWndUnit.h" #include //#include #include #include #include #include #include "QtConcurrent" //#pragma execution_character_set("utf-8") #include"Util.h" #define UPDATE_TIME 200 HWndUnit::HWndUnit(QWidget *parent) : QWidget(parent) { m_paren = parent; this->setParent(parent); //resize(745, 424); m_plabelStatus = new QLabel(this); m_plabelTitle = new QLabel(this); m_plabelStatus->setEnabled(true); m_plabelTitle->setEnabled(true); m_pImageWindow = nullptr; m_pImageWindow = new QHalconWindow(this); if (m_pImageWindow) { m_pImageWindow->setBackgroundColor("#191970"); connect(m_pImageWindow, &QHalconWindow::mouseMoveStatus, this, &HWndUnit::on_MousePosMessage); } m_pTimer = new QTimer(); connect(m_pTimer, SIGNAL(timeout()), this, SLOT(onTimer())); m_pTimer->setInterval(UPDATE_TIME); m_pTimer->start(); } void HWndUnit::setWndTitle(const QString &title) { m_plabelTitle->setText(title); } void HWndUnit::on_MousePosMessage(const QString &message) { m_plabelStatus->setText(message); } void HWndUnit::resizeEvent(QResizeEvent *) { double nDpr = QApplication::screens()[0]->devicePixelRatio(); int h = this->height() * nDpr; int w = this->width() * nDpr; int labelHeight = 12; int labelWidth = w / 2 - 10; //m_plabelStatus->setGeometry(QRect(10, h-labelHeight-2, 54, labelHeight)); m_plabelStatus->setGeometry(QRect(10, h - labelHeight - 4, labelWidth, labelHeight)); m_plabelTitle->setGeometry(QRect(w / 2, h - labelHeight - 4, labelWidth, labelHeight)); if (m_pImageWindow) { m_pImageWindow->setGeometry(QRect(0, 0, w, h - labelHeight - 4)); m_pImageWindow->resetWindow(); m_pImageWindow->refresh(); } } void HWndUnit::ShowImage(HImage image) { try { if (m_pImageWindow) { m_pImageWindow->addIconicVar(image); } } catch (...) { } } // void HWndUnit::ShowObj(HObject obj, QString strColor, int nLinsWhite, bool bFillRawing) // { // try // { // if (strColor.isEmpty()) // { // strColor = "blue"; // } // if (m_pImageWindow) // { // m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_COLOR, (HTuple)strColor.toStdString().c_str()); // m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_LINEWIDTH, (HTuple)nLinsWhite); // m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_DRAWMODE, bFillRawing ? (HTuple)"fill" : (HTuple)"margin");//fill margin // m_pImageWindow->addIconicVar(obj); // } // } // catch (...) // { // // } // } // void HWndUnit::ShowObj(HObject obj, QColor color, int nLinsWhite) { try { if (nLinsWhite < 1 || nLinsWhite > 10) { nLinsWhite = 1; } if (m_pImageWindow) { QColor colorTimp = color; bool bFillRawing = false; QString strColor = color.toRgb().name(QColor::HexRgb); double fTransparency = colorTimp.alphaF(); if (fTransparency > 0.00001 && fTransparency < 0.99) { bFillRawing = true; QString str = QString("%1").arg(int(255 * fTransparency), 2, 16, QLatin1Char('0')); strColor.append(str); } m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_COLOR, (HTuple)strColor.toStdString().c_str()); m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_LINEWIDTH, (HTuple)nLinsWhite); m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_DRAWMODE, bFillRawing ? (HTuple)"fill" : (HTuple)"margin");//fill margin m_pImageWindow->addIconicVar(obj); } } catch (...) { } } void HWndUnit::ShowObj(HObject obj, bool bFillRawing, int nLinsWhite, int nModes) { try { if (nLinsWhite < 1 || nLinsWhite > 10) { nLinsWhite = 1; } if (m_pImageWindow) { m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_COLORED, HTuple(12)); switch (nModes) { case 0: m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_SHAPE, "original"); break; case 1: m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_SHAPE, "rectangle1"); break; case 2: m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_SHAPE, "rectangle2"); break; default: break; } m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_LINEWIDTH, (HTuple)nLinsWhite); m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_DRAWMODE, bFillRawing ? (HTuple)"fill" : (HTuple)"margin");//fill margin m_pImageWindow->addIconicVar(obj); } } catch (...) { } } void HWndUnit::ShowMsg(HTuple msg, QColor color, int nX, int nY, int nSize) { try { if (nX < 1 || nX > 10000) { nX = 1; } if (nY < 1 || nY > 10000) { nY = 1; } if (nSize < 1 || nSize > 100) { nSize = 1; } QString strColor = color.name(QColor::HexRgb); if (m_pImageWindow) { HWndMessage message(msg, nX, nY, nSize, strColor.toStdString().c_str()); m_pImageWindow->addMessage(message); } } catch (...) { } } /// /// /// /// void HWndUnit::Refresh(bool isRealTime) { // 如果窗口打开 if (m_paren && !m_paren->isHidden()) { //实时刷新 if (isRealTime) { RefreshWindow(); } //限制刷新率 else { if (m_bUpdate == true) { RefreshWindow(); m_bUpdate = false; } } } } void HWndUnit::RefreshWindow() { try { if (m_pImageWindow) { m_pImageWindow->refresh(); } } catch (...) { } } void HWndUnit::onTimer() { m_bUpdate = true; }