123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- #include "HWndUnit.h"
- #include <QAction>
- //#include <QAxFactory>
- #include <QMenuBar>
- #include <QDebug>
- #include <QApplication>
- #include <QScreen>
- #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 (...)
- {
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="isRealTime"></param>
- 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;
- }
|