CameraModifyScene.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "CameraModifyScene.h"
  2. CameraModifyScene::CameraModifyScene(QObject* parent)
  3. {
  4. //set the parent
  5. this->setParent(parent);
  6. //set the scene rect
  7. this->setSceneRect(0, 0, CAMERA_SCENE_WIDTH, CAMERA_SCENE_HEIGHT);
  8. //set the background color
  9. this->setBackgroundBrush(QColor(64, 64, 64, 255));
  10. InitConnect();
  11. }
  12. void CameraModifyScene::InitConnect()
  13. {
  14. //connect the cameraitem modify to the CameraItemModified slots
  15. connect(this, &CameraModifyScene::CameraItemModified, this, &CameraModifyScene::OnCameraItemModified);
  16. //connect the cameraitem delete to the cameraitemModified slots
  17. connect(this,&CameraModifyScene::CameraItemDelete, this, &CameraModifyScene::OnCameraItemModified);
  18. }
  19. void CameraModifyScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
  20. {
  21. //ÐÞ¸ÄΪÓÉͼԪ·¢ËÍÏûÏ¢
  22. //
  23. //
  24. //
  25. // QPointF CurMousePt=event->pos();
  26. //QList<QGraphicsItem*> ItemsInScene = this->items();
  27. //for each (auto iter in ItemsInScene)
  28. //{
  29. // //judge the mouse point is in the CameraItem or not
  30. // if (//iter->type() == CameraItem::Type &&
  31. // IsPointInRect(CurMousePt, iter->boundingRect()))
  32. // {
  33. // //set the CameraItem is selected
  34. // ((CameraItem*)iter)->bSelected = true;
  35. // //emit the CameraItemSelected signal
  36. // emit CameraItemSelected((CameraItem*)iter);
  37. // //break the loop
  38. // break;
  39. // }
  40. //}
  41. //first this function is not block
  42. //QGraphicsScene::mousePressEvent(event);
  43. //int Type = event->type();
  44. ////QPointF CurMousePt = event->pos();
  45. //if ( Type == QGraphicsSceneMouseEvent::GraphicsSceneMouseDoubleClick)
  46. //{
  47. // //check the select model
  48. // if (bSelectModel)
  49. // {
  50. // //under this line is the single select model
  51. // //get the current selected CameraItem
  52. // CameraItem* pCurSelectedCameraItem = GetCurrentSelectedCameraItem();
  53. // if (!pCurSelectedCameraItem->bActive)
  54. // return;
  55. // pCameraGripper->EmptyMagazine();
  56. // pCameraGripper->fillingGripper(pCurSelectedCameraItem);
  57. // }
  58. // else
  59. // {
  60. // //under this line is the multi select model
  61. // //get the current selected CameraItems
  62. // CameraItem* pCurSelectedCameraItem = GetCurrentSelectedCameraItem();
  63. // //get the cameraitem row and col index
  64. // int nRow = pCurSelectedCameraItem->nItemRowIndex;
  65. // int nCol = pCurSelectedCameraItem->nItemColIndex;
  66. // //bring the item which is in the same row with the current selected CameraItem
  67. // //clear the gripper
  68. // pCameraGripper->EmptyMagazine();
  69. // //first get all the items in the scene
  70. // QList<QGraphicsItem*> ItemsInScene = this->items();
  71. // for (auto& item : ItemsInScene)
  72. // {
  73. // CameraItem* Item = (CameraItem*)item;
  74. // if (Item->nItemRowIndex == nRow)
  75. // {
  76. // pCameraGripper->fillingGripper(Item);
  77. // }
  78. // }
  79. // }
  80. //}
  81. }
  82. void CameraModifyScene::AddCameraItem(CameraItem* pItem)
  83. {
  84. //add the CameraItem to the scene
  85. this->addItem(pItem);
  86. //connect the CameraItem mouse press event to the CameraItemMousePressEvent slots
  87. connect(pItem, &CameraItem::CameraItemPressed, this, &CameraModifyScene::OnCameraItemMousePressEvent);
  88. //emit the Cameraitem modify
  89. emit CameraItemModified();
  90. }
  91. void CameraModifyScene::RemoveCameraItem(CameraItem* pItem)
  92. {
  93. int nDeleteIndex = pItem->nItemColIndex;
  94. //remove the CameraItem from the scene
  95. this->removeItem(pItem);
  96. //deconstration the cameraitem
  97. delete pItem;
  98. QList<QGraphicsItem*> List = this->items();
  99. for (auto& item : List)
  100. {
  101. //trun the pointer into CameraItem
  102. CameraItem* pIter = (CameraItem * )item;
  103. if (pIter->nItemColIndex > nDeleteIndex)
  104. {
  105. pIter->nItemColIndex--;
  106. }
  107. }
  108. //emit the CameraItemDelete signal
  109. emit CameraItemDelete();
  110. }
  111. CameraItem* CameraModifyScene::GetCurrentSelectedCameraItem()
  112. {
  113. //get the CameraItem in the scene
  114. QList<QGraphicsItem*> ItemsInScene = this->items();
  115. for each (auto iter in ItemsInScene)
  116. {
  117. if (//iter->type() == CameraItem::Type &&
  118. ((CameraItem*)iter)->bSelected)
  119. {
  120. return (CameraItem*)iter;
  121. }
  122. }
  123. return nullptr;
  124. }
  125. void CameraModifyScene::SetSelectModel(bool bModel)
  126. {
  127. //set the select model
  128. bSelectModel = bModel;
  129. }
  130. void CameraModifyScene::SetGripper(CameraGripper* pGripper)
  131. {
  132. pCameraGripper = pGripper;
  133. }
  134. void CameraModifyScene::OnCameraItemDelete(CameraItem* pItem)
  135. {
  136. emit CameraItemModified();
  137. }
  138. void CameraModifyScene::OnCameraItemSelected(CameraItem* pItem)
  139. {
  140. //set the CameraItem is selected
  141. pItem->bSelected = true;
  142. //and other CameraItem is unselected
  143. QList<QGraphicsItem*> ItemsInScene = this->items();
  144. for each (auto iter in ItemsInScene)
  145. {
  146. //if (iter->type() == CameraItem::Type && iter != pItem)
  147. {
  148. ((CameraItem*)iter)->bSelected = false;
  149. }
  150. }
  151. //emit the CameraItemSelected signal
  152. emit CameraItemSelected(pItem);
  153. }
  154. void CameraModifyScene::OnCameraItemUnSelected(CameraItem* pItem)
  155. {
  156. }
  157. void CameraModifyScene::OnCameraItemMousePressEvent(CameraItem* pItem)
  158. {
  159. //set the CameraItem is selected
  160. //pItem->bSelected = true;
  161. ////and other CameraItem is unselected
  162. //QList<QGraphicsItem*> ItemsInScene = this->items();
  163. //for each (auto iter in ItemsInScene)
  164. //{
  165. // if (//iter->type() == CameraItem::Type &&
  166. // iter != pItem)
  167. // {
  168. // ((CameraItem*)iter)->bSelected = false;
  169. // }
  170. //}
  171. //emit the CameraItemSelected signal
  172. emit CameraItemModified();
  173. }
  174. void CameraModifyScene::OnCameraItemModified()
  175. {
  176. //rearrange all item in the scene and paint the scene
  177. RearrangeAllItem();
  178. this->update();
  179. }
  180. bool CameraModifyScene::IsPointInRect(QPointF Point, QRectF Rect)
  181. {
  182. if (Point.x() >= Rect.left()
  183. && Point.x() <= Rect.right()
  184. && Point.y() >= Rect.top()
  185. && Point.y() <= Rect.bottom())
  186. {
  187. return true;
  188. }
  189. return false;
  190. }
  191. void CameraModifyScene::RearrangeAllItem()
  192. {
  193. //get the CameraItem in the scene
  194. QList<QGraphicsItem*> ItemsInScene = this->items();
  195. //get the CameraItem count
  196. int CameraItemCount = 0;
  197. for each (auto iter in ItemsInScene)
  198. {
  199. //if (iter->type() == CameraItem::Type)
  200. {
  201. CameraItemCount++;
  202. }
  203. }
  204. //all cameraitem width is 100
  205. //the orgin point is the center of scene
  206. //make all cameraitem in the scene arrange in a line
  207. //calculate the start point
  208. QPointF StartPoint = QPointF(CAMERAITEM_WIDTH / 2 , CAMERAITEM_HEIGHT /2);//+ ((CameraItemCount) * 0.5) * 100
  209. //on this account the cameraitem width is 100
  210. //calculate the interval
  211. int Interval = CAMERAITEM_WIDTH;
  212. //move all cameraitem to the right position
  213. for each (auto iter in ItemsInScene)
  214. {
  215. //if (iter->type() == CameraItem::Type)
  216. {
  217. //iter->setPos(StartPoint.x() - ((CameraItem*)iter)->m_CameraInfo.nHorizonOffset,StartPoint.y() + ((CameraItem*)iter)->m_CameraInfo.nVerticalOffset);
  218. ((CameraItem*)iter)->CurrentPos =
  219. QPoint(StartPoint.x() + ((CameraItem*)iter)->m_CameraInfo.nHorizonOffset + ((CameraItem*)iter)->nItemColIndex * Interval,
  220. StartPoint.y() + ((CameraItem*)iter)->m_CameraInfo.nVerticalOffset + ((CameraItem*)iter)->nItemRowIndex * Interval);
  221. //((CameraItem*)iter)->CurrentPos.rx() -= CAMERAITEM_WIDTH / 2;
  222. //((CameraItem*)iter)->CurrentPos.ry() -= CAMERAITEM_HEIGHT / 2;
  223. //StartPoint.setX(StartPoint.x() - Interval);
  224. }
  225. }
  226. }