123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- #include "CameraModifyScene.h"
- CameraModifyScene::CameraModifyScene(QObject* parent)
- {
- //set the parent
- this->setParent(parent);
- //set the scene rect
- this->setSceneRect(0, 0, CAMERA_SCENE_WIDTH, CAMERA_SCENE_HEIGHT);
- //set the background color
- this->setBackgroundBrush(QColor(64, 64, 64, 255));
- InitConnect();
- }
- void CameraModifyScene::InitConnect()
- {
- //connect the cameraitem modify to the CameraItemModified slots
- connect(this, &CameraModifyScene::CameraItemModified, this, &CameraModifyScene::OnCameraItemModified);
- //connect the cameraitem delete to the cameraitemModified slots
- connect(this,&CameraModifyScene::CameraItemDelete, this, &CameraModifyScene::OnCameraItemModified);
-
- }
- void CameraModifyScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
- {
- //ÐÞ¸ÄΪÓÉͼԪ·¢ËÍÏûÏ¢
- //
- //
- //
- // QPointF CurMousePt=event->pos();
- //QList<QGraphicsItem*> ItemsInScene = this->items();
- //for each (auto iter in ItemsInScene)
- //{
- // //judge the mouse point is in the CameraItem or not
- // if (//iter->type() == CameraItem::Type &&
- // IsPointInRect(CurMousePt, iter->boundingRect()))
- // {
- // //set the CameraItem is selected
- // ((CameraItem*)iter)->bSelected = true;
- // //emit the CameraItemSelected signal
- // emit CameraItemSelected((CameraItem*)iter);
- // //break the loop
- // break;
- // }
- //}
- //first this function is not block
- //QGraphicsScene::mousePressEvent(event);
- //int Type = event->type();
- ////QPointF CurMousePt = event->pos();
- //if ( Type == QGraphicsSceneMouseEvent::GraphicsSceneMouseDoubleClick)
- //{
- // //check the select model
- // if (bSelectModel)
- // {
- // //under this line is the single select model
- // //get the current selected CameraItem
- // CameraItem* pCurSelectedCameraItem = GetCurrentSelectedCameraItem();
- // if (!pCurSelectedCameraItem->bActive)
- // return;
- // pCameraGripper->EmptyMagazine();
- // pCameraGripper->fillingGripper(pCurSelectedCameraItem);
- // }
- // else
- // {
- // //under this line is the multi select model
- // //get the current selected CameraItems
- // CameraItem* pCurSelectedCameraItem = GetCurrentSelectedCameraItem();
- // //get the cameraitem row and col index
- // int nRow = pCurSelectedCameraItem->nItemRowIndex;
- // int nCol = pCurSelectedCameraItem->nItemColIndex;
- // //bring the item which is in the same row with the current selected CameraItem
- // //clear the gripper
- // pCameraGripper->EmptyMagazine();
- // //first get all the items in the scene
- // QList<QGraphicsItem*> ItemsInScene = this->items();
- // for (auto& item : ItemsInScene)
- // {
- // CameraItem* Item = (CameraItem*)item;
- // if (Item->nItemRowIndex == nRow)
- // {
- // pCameraGripper->fillingGripper(Item);
- // }
- // }
- // }
- //}
- }
- void CameraModifyScene::AddCameraItem(CameraItem* pItem)
- {
- //add the CameraItem to the scene
- this->addItem(pItem);
-
- //connect the CameraItem mouse press event to the CameraItemMousePressEvent slots
- connect(pItem, &CameraItem::CameraItemPressed, this, &CameraModifyScene::OnCameraItemMousePressEvent);
- //emit the Cameraitem modify
- emit CameraItemModified();
- }
- void CameraModifyScene::RemoveCameraItem(CameraItem* pItem)
- {
- int nDeleteIndex = pItem->nItemColIndex;
- //remove the CameraItem from the scene
- this->removeItem(pItem);
- //deconstration the cameraitem
- delete pItem;
- QList<QGraphicsItem*> List = this->items();
- for (auto& item : List)
- {
- //trun the pointer into CameraItem
- CameraItem* pIter = (CameraItem * )item;
- if (pIter->nItemColIndex > nDeleteIndex)
- {
- pIter->nItemColIndex--;
- }
- }
- //emit the CameraItemDelete signal
- emit CameraItemDelete();
- }
- CameraItem* CameraModifyScene::GetCurrentSelectedCameraItem()
- {
- //get the CameraItem in the scene
- QList<QGraphicsItem*> ItemsInScene = this->items();
- for each (auto iter in ItemsInScene)
- {
- if (//iter->type() == CameraItem::Type &&
- ((CameraItem*)iter)->bSelected)
- {
- return (CameraItem*)iter;
- }
- }
- return nullptr;
- }
- void CameraModifyScene::SetSelectModel(bool bModel)
- {
- //set the select model
- bSelectModel = bModel;
- }
- void CameraModifyScene::SetGripper(CameraGripper* pGripper)
- {
- pCameraGripper = pGripper;
- }
- void CameraModifyScene::OnCameraItemDelete(CameraItem* pItem)
- {
- emit CameraItemModified();
- }
- void CameraModifyScene::OnCameraItemSelected(CameraItem* pItem)
- {
- //set the CameraItem is selected
- pItem->bSelected = true;
- //and other CameraItem is unselected
- QList<QGraphicsItem*> ItemsInScene = this->items();
- for each (auto iter in ItemsInScene)
- {
- //if (iter->type() == CameraItem::Type && iter != pItem)
- {
- ((CameraItem*)iter)->bSelected = false;
- }
- }
- //emit the CameraItemSelected signal
- emit CameraItemSelected(pItem);
- }
- void CameraModifyScene::OnCameraItemUnSelected(CameraItem* pItem)
- {
- }
- void CameraModifyScene::OnCameraItemMousePressEvent(CameraItem* pItem)
- {
- //set the CameraItem is selected
- //pItem->bSelected = true;
- ////and other CameraItem is unselected
- //QList<QGraphicsItem*> ItemsInScene = this->items();
- //for each (auto iter in ItemsInScene)
- //{
- // if (//iter->type() == CameraItem::Type &&
- // iter != pItem)
- // {
- // ((CameraItem*)iter)->bSelected = false;
- // }
- //}
- //emit the CameraItemSelected signal
- emit CameraItemModified();
- }
- void CameraModifyScene::OnCameraItemModified()
- {
- //rearrange all item in the scene and paint the scene
- RearrangeAllItem();
- this->update();
- }
- bool CameraModifyScene::IsPointInRect(QPointF Point, QRectF Rect)
- {
- if (Point.x() >= Rect.left()
- && Point.x() <= Rect.right()
- && Point.y() >= Rect.top()
- && Point.y() <= Rect.bottom())
- {
- return true;
- }
- return false;
- }
- void CameraModifyScene::RearrangeAllItem()
- {
- //get the CameraItem in the scene
- QList<QGraphicsItem*> ItemsInScene = this->items();
- //get the CameraItem count
- int CameraItemCount = 0;
- for each (auto iter in ItemsInScene)
- {
- //if (iter->type() == CameraItem::Type)
- {
- CameraItemCount++;
- }
- }
- //all cameraitem width is 100
- //the orgin point is the center of scene
- //make all cameraitem in the scene arrange in a line
- //calculate the start point
- QPointF StartPoint = QPointF(CAMERAITEM_WIDTH / 2 , CAMERAITEM_HEIGHT /2);//+ ((CameraItemCount) * 0.5) * 100
- //on this account the cameraitem width is 100
- //calculate the interval
- int Interval = CAMERAITEM_WIDTH;
- //move all cameraitem to the right position
- for each (auto iter in ItemsInScene)
- {
- //if (iter->type() == CameraItem::Type)
- {
- //iter->setPos(StartPoint.x() - ((CameraItem*)iter)->m_CameraInfo.nHorizonOffset,StartPoint.y() + ((CameraItem*)iter)->m_CameraInfo.nVerticalOffset);
- ((CameraItem*)iter)->CurrentPos =
- QPoint(StartPoint.x() + ((CameraItem*)iter)->m_CameraInfo.nHorizonOffset + ((CameraItem*)iter)->nItemColIndex * Interval,
- StartPoint.y() + ((CameraItem*)iter)->m_CameraInfo.nVerticalOffset + ((CameraItem*)iter)->nItemRowIndex * Interval);
-
- //((CameraItem*)iter)->CurrentPos.rx() -= CAMERAITEM_WIDTH / 2;
- //((CameraItem*)iter)->CurrentPos.ry() -= CAMERAITEM_HEIGHT / 2;
- //StartPoint.setX(StartPoint.x() - Interval);
- }
- }
- }
|