HWndUnit.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "HWndUnit.h"
  2. #include <QAction>
  3. //#include <QAxFactory>
  4. #include <QMenuBar>
  5. #include <QDebug>
  6. #include <QApplication>
  7. #include <QScreen>
  8. #include "QtConcurrent"
  9. //#pragma execution_character_set("utf-8")
  10. #include"Util.h"
  11. #define UPDATE_TIME 200
  12. HWndUnit::HWndUnit(QWidget *parent) : QWidget(parent)
  13. {
  14. m_paren = parent;
  15. this->setParent(parent);
  16. //resize(745, 424);
  17. m_plabelStatus = new QLabel(this);
  18. m_plabelTitle = new QLabel(this);
  19. m_plabelStatus->setEnabled(true);
  20. m_plabelTitle->setEnabled(true);
  21. m_pImageWindow = nullptr;
  22. m_pImageWindow = new QHalconWindow(this);
  23. if (m_pImageWindow)
  24. {
  25. m_pImageWindow->setBackgroundColor("#191970");
  26. connect(m_pImageWindow, &QHalconWindow::mouseMoveStatus, this, &HWndUnit::on_MousePosMessage);
  27. }
  28. m_pTimer = new QTimer();
  29. connect(m_pTimer, SIGNAL(timeout()), this, SLOT(onTimer()));
  30. m_pTimer->setInterval(UPDATE_TIME);
  31. m_pTimer->start();
  32. }
  33. void HWndUnit::setWndTitle(const QString &title)
  34. {
  35. m_plabelTitle->setText(title);
  36. }
  37. void HWndUnit::on_MousePosMessage(const QString &message)
  38. {
  39. m_plabelStatus->setText(message);
  40. }
  41. void HWndUnit::resizeEvent(QResizeEvent *)
  42. {
  43. double nDpr = QApplication::screens()[0]->devicePixelRatio();
  44. int h = this->height() * nDpr;
  45. int w = this->width() * nDpr;
  46. int labelHeight = 12;
  47. int labelWidth = w / 2 - 10;
  48. //m_plabelStatus->setGeometry(QRect(10, h-labelHeight-2, 54, labelHeight));
  49. m_plabelStatus->setGeometry(QRect(10, h - labelHeight - 4, labelWidth, labelHeight));
  50. m_plabelTitle->setGeometry(QRect(w / 2, h - labelHeight - 4, labelWidth, labelHeight));
  51. if (m_pImageWindow)
  52. {
  53. m_pImageWindow->setGeometry(QRect(0, 0, w, h - labelHeight - 4));
  54. m_pImageWindow->resetWindow();
  55. m_pImageWindow->refresh();
  56. }
  57. }
  58. void HWndUnit::ShowImage(HImage image)
  59. {
  60. try
  61. {
  62. if (m_pImageWindow)
  63. {
  64. m_pImageWindow->addIconicVar(image);
  65. }
  66. }
  67. catch (...)
  68. {
  69. }
  70. }
  71. // void HWndUnit::ShowObj(HObject obj, QString strColor, int nLinsWhite, bool bFillRawing)
  72. // {
  73. // try
  74. // {
  75. // if (strColor.isEmpty())
  76. // {
  77. // strColor = "blue";
  78. // }
  79. // if (m_pImageWindow)
  80. // {
  81. // m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_COLOR, (HTuple)strColor.toStdString().c_str());
  82. // m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_LINEWIDTH, (HTuple)nLinsWhite);
  83. // m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_DRAWMODE, bFillRawing ? (HTuple)"fill" : (HTuple)"margin");//fill margin
  84. // m_pImageWindow->addIconicVar(obj);
  85. // }
  86. // }
  87. // catch (...)
  88. // {
  89. //
  90. // }
  91. // }
  92. //
  93. void HWndUnit::ShowObj(HObject obj, QColor color, int nLinsWhite)
  94. {
  95. try
  96. {
  97. if (nLinsWhite < 1 || nLinsWhite > 10)
  98. {
  99. nLinsWhite = 1;
  100. }
  101. if (m_pImageWindow)
  102. {
  103. QColor colorTimp = color;
  104. bool bFillRawing = false;
  105. QString strColor = color.toRgb().name(QColor::HexRgb);
  106. double fTransparency = colorTimp.alphaF();
  107. if (fTransparency > 0.00001 && fTransparency < 0.99)
  108. {
  109. bFillRawing = true;
  110. QString str = QString("%1").arg(int(255 * fTransparency), 2, 16, QLatin1Char('0'));
  111. strColor.append(str);
  112. }
  113. m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_COLOR, (HTuple)strColor.toStdString().c_str());
  114. m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_LINEWIDTH, (HTuple)nLinsWhite);
  115. m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_DRAWMODE, bFillRawing ? (HTuple)"fill" : (HTuple)"margin");//fill margin
  116. m_pImageWindow->addIconicVar(obj);
  117. }
  118. }
  119. catch (...)
  120. {
  121. }
  122. }
  123. void HWndUnit::ShowObj(HObject obj, bool bFillRawing, int nLinsWhite, int nModes)
  124. {
  125. try
  126. {
  127. if (nLinsWhite < 1 || nLinsWhite > 10)
  128. {
  129. nLinsWhite = 1;
  130. }
  131. if (m_pImageWindow)
  132. {
  133. m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_COLORED, HTuple(12));
  134. switch (nModes)
  135. {
  136. case 0: m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_SHAPE, "original"); break;
  137. case 1: m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_SHAPE, "rectangle1"); break;
  138. case 2: m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_SHAPE, "rectangle2"); break;
  139. default:
  140. break;
  141. }
  142. m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_LINEWIDTH, (HTuple)nLinsWhite);
  143. m_pImageWindow->changeGraphicSettings(GraphicsContext::GC_DRAWMODE, bFillRawing ? (HTuple)"fill" : (HTuple)"margin");//fill margin
  144. m_pImageWindow->addIconicVar(obj);
  145. }
  146. }
  147. catch (...)
  148. {
  149. }
  150. }
  151. void HWndUnit::ShowMsg(HTuple msg, QColor color, int nX, int nY, int nSize)
  152. {
  153. try
  154. {
  155. if (nX < 1 || nX > 10000)
  156. {
  157. nX = 1;
  158. }
  159. if (nY < 1 || nY > 10000)
  160. {
  161. nY = 1;
  162. }
  163. if (nSize < 1 || nSize > 100)
  164. {
  165. nSize = 1;
  166. }
  167. QString strColor = color.name(QColor::HexRgb);
  168. if (m_pImageWindow)
  169. {
  170. HWndMessage message(msg, nX, nY, nSize, strColor.toStdString().c_str());
  171. m_pImageWindow->addMessage(message);
  172. }
  173. }
  174. catch (...)
  175. {
  176. }
  177. }
  178. /// <summary>
  179. ///
  180. /// </summary>
  181. /// <param name="isRealTime"></param>
  182. void HWndUnit::Refresh(bool isRealTime)
  183. {
  184. // 如果窗口打开
  185. if (m_paren && !m_paren->isHidden())
  186. {
  187. //实时刷新
  188. if (isRealTime)
  189. {
  190. RefreshWindow();
  191. }
  192. //限制刷新率
  193. else
  194. {
  195. if (m_bUpdate == true)
  196. {
  197. RefreshWindow();
  198. m_bUpdate = false;
  199. }
  200. }
  201. }
  202. }
  203. void HWndUnit::RefreshWindow()
  204. {
  205. try
  206. {
  207. if (m_pImageWindow)
  208. {
  209. m_pImageWindow->refresh();
  210. }
  211. }
  212. catch (...)
  213. {
  214. }
  215. }
  216. void HWndUnit::onTimer()
  217. {
  218. m_bUpdate = true;
  219. }