CameraItem.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "CameraItem.h"
  2. #include "CameraModifyScene.h"
  3. #include "IncCamera.h"
  4. bool* CameraItem::pCameraItemModified = nullptr;
  5. CameraItem::CameraItem(QGraphicsItem* parent)
  6. {
  7. nItemColIndex = 0;
  8. pIntersectInstance = NULL;
  9. bActive = false;
  10. }
  11. CameraItem::CameraItem()
  12. {
  13. bSelected = false;
  14. nItemColIndex = 0;
  15. pCurrentFrame = nullptr;
  16. bActive = false;
  17. }
  18. CameraItem::~CameraItem()
  19. {
  20. m_VideoStreamCatcher.Stop();
  21. }
  22. CameraItem::CameraItem(const CameraItem& other)
  23. {
  24. //set the CurrentPos
  25. CurrentPos = other.CurrentPos;
  26. //set the CameraItem is unselected
  27. bSelected = other.bSelected;
  28. //set the CameraItem index
  29. nItemColIndex = other.nItemColIndex;
  30. //init the intersect rect
  31. IntersectRect = QRect(0, 0, 0, 0);
  32. //init the overlap bounding
  33. m_vvOverlapBounding.clear();
  34. bActive = other.bActive;
  35. }
  36. bool CameraItem::Init(int nIndex,QPointF Pos)
  37. {
  38. //set the CurrentPos
  39. //CurrentPos = Pos;
  40. //set the CameraItem is unselected
  41. bSelected = false;
  42. //set the CameraItem index
  43. nItemColIndex = nIndex;
  44. bool bRet = m_VideoStreamCatcher.Init(m_CameraInfo.GetCompleteIpAddress());
  45. if (bRet)
  46. {
  47. m_vvOverlapBounding.resize(m_VideoStreamCatcher.m_nVideoHeight);
  48. for (int i = 0; i < m_VideoStreamCatcher.m_nVideoHeight; i++)
  49. {
  50. m_vvOverlapBounding[i].resize(2);
  51. m_vvOverlapBounding[i][0] = 0;
  52. m_vvOverlapBounding[i][1] = m_VideoStreamCatcher.m_nVideoWidth;
  53. }
  54. }
  55. //set the Stream Catcher
  56. bActive = bRet;
  57. return bRet;
  58. }
  59. QRectF CameraItem::boundingRect() const
  60. {
  61. //set a CameraItem rect width is 100, height is 100
  62. return QRectF(CurrentPos,QSize(CAMERAITEM_WIDTH,CAMERAITEM_HEIGHT));
  63. }
  64. void CameraItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
  65. {
  66. QPen pen;
  67. pen.setColor(QColor(255, 0, 0, 255));
  68. if (bSelected)
  69. {
  70. pen.setWidth(15);
  71. }
  72. else
  73. {
  74. pen.setWidth(8);
  75. }
  76. //set a painter to draw the CameraItem
  77. painter->setPen(pen);
  78. painter->drawRect(CurrentPos.x(), CurrentPos.y(), CAMERAITEM_WIDTH, CAMERAITEM_HEIGHT);
  79. }
  80. void CameraItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
  81. {
  82. QGraphicsItem::mousePressEvent(event);
  83. QPointF Point = event->pos();
  84. if (!boundingRect().contains(Point))
  85. return;
  86. if (bActive)
  87. {
  88. //get the scene pointer
  89. QGraphicsScene* pScene = this->scene();
  90. //set the all CameraItem is unselected
  91. QList<QGraphicsItem*> ItemsInScene = pScene->items();
  92. QPoint CurPoint = event->pos().toPoint();
  93. for each (auto iter in ItemsInScene)
  94. {
  95. QRect testRc = iter->boundingRect().toRect();
  96. if (iter->boundingRect().contains(CurPoint))
  97. ((CameraItem*)iter)->bSelected = true;
  98. else
  99. ((CameraItem*)iter)->bSelected = false;
  100. }
  101. //set the Current CameraItem is selected
  102. //bSelected = true;
  103. scene()->update();
  104. //emit the CameraItemSelected signal
  105. emit CameraItemPressed(this);
  106. }
  107. else
  108. {
  109. //create a IncCamera
  110. IncCamera* pIncCamera = new IncCamera();
  111. //set the IncCamera block the main thread
  112. pIncCamera->setWindowModality(Qt::ApplicationModal);
  113. //Show IncCamera
  114. pIncCamera->show();
  115. //block the main thread until the IncCamera close
  116. while (pIncCamera->isVisible())
  117. {
  118. QCoreApplication::processEvents();
  119. }
  120. //if the IncCamera is closed by push the cancel button break the function
  121. if (pIncCamera->GetIsOKBtnClicked() != true)
  122. {
  123. return;
  124. }
  125. //wait the IncCamera close,get the information from the IncCamera,and push the information to the CameraInfoList
  126. //m_CameraItemList.push_back(pIncCamera->GetCameraInfo());
  127. m_CameraInfo = pIncCamera->GetCameraInfo();
  128. //init the CameraItem
  129. if (!Init(nItemColIndex, QPointF(0, 0)))
  130. {
  131. //if the CameraItem init failed,delete the CameraItem and return
  132. QString Title = "错误";
  133. QString Text = "当前输入的链接无法初始化解码器";
  134. QMessageBox box;
  135. box.setWindowTitle(Title);
  136. box.setText(Text);
  137. box.addButton(QMessageBox::Cancel);
  138. box.show();
  139. return;
  140. }
  141. bSelected = true;
  142. //reset the pano buffer
  143. //ResetPanoImageBuffer();
  144. *pCameraItemModified = true;
  145. //emit the signal to the CameraItem bas been selected
  146. emit ((CameraModifyScene*)scene())->CameraItemSelected(this);
  147. //open the thread catch stream
  148. m_VideoStreamCatcher.Start();
  149. delete pIncCamera;
  150. // bActive = true;
  151. }
  152. }