#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 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 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 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 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 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 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 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); } } }