diff --git a/src/app/puzzle/vpcarrouselpiecelist.cpp b/src/app/puzzle/vpcarrouselpiecelist.cpp index e89104191..852dfc90e 100644 --- a/src/app/puzzle/vpcarrouselpiecelist.cpp +++ b/src/app/puzzle/vpcarrouselpiecelist.cpp @@ -198,20 +198,17 @@ void VPCarrouselPieceList::contextMenuEvent(QContextMenuEvent *event) VPSheet *sheet = layout->GetFocusedSheet(); piece->SetSheet(sheet); emit layout->PieceSheetChanged(piece); - m_carrousel->Refresh(); } else if (selectedAction == deleteAction) { VPSheet *sheet = layout->GetTrashSheet(); piece->SetSheet(sheet); emit layout->PieceSheetChanged(piece); - m_carrousel->Refresh(); } else if (selectedAction == removeAction) { piece->SetSheet(nullptr); emit layout->PieceSheetChanged(piece); - m_carrousel->Refresh(); } } } diff --git a/src/app/puzzle/vpgraphicspiece.cpp b/src/app/puzzle/vpgraphicspiece.cpp index fedeadfba..8f74e7a14 100644 --- a/src/app/puzzle/vpgraphicspiece.cpp +++ b/src/app/puzzle/vpgraphicspiece.cpp @@ -271,7 +271,7 @@ void VPGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event) { bool selectionState = isSelected(); //perform the default behaviour - QGraphicsItem::mousePressEvent(event); + QGraphicsObject::mousePressEvent(event); // change the cursor when clicking the left button if((event->button() == Qt::LeftButton)) @@ -286,7 +286,6 @@ void VPGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event) } } - // change the selected state when clicking left button if (event->button() == Qt::LeftButton) { @@ -385,43 +384,40 @@ void VPGraphicsPiece::hoverMoveEvent(QGraphicsSceneHoverEvent *event) //--------------------------------------------------------------------------------------------------------------------- void VPGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { + QMenu menu; - // TODO/FIXME context menu needs to be refactored + QList sheets = m_piece->Layout()->GetSheets(); + sheets.removeAll(m_piece->Sheet()); -// QMenu menu; + QVector moveToActions; -// // move to piece list actions -- TODO : To be tested properly when we have several piece lists -// QList pieceLists = QList(); -// for(auto sheet : m_piece->GetPieceList()->GetLayout()->GetSheets()) -// { -// pieceLists.append(sheet->GetPieceList()); -// } + if (not sheets.isEmpty()) + { + QMenu *moveMenu = menu.addMenu(tr("Move to")); -// pieceLists.removeAll(m_piece->GetPieceList()); + for (auto *sheet : sheets) + { + QAction* moveToSheet = moveMenu->addAction(sheet->GetName()); + moveToSheet->setData(QVariant::fromValue(sheet)); + moveToActions.append(moveToSheet); + } + } -// if(pieceLists.count() > 0) -// { -// QMenu *moveMenu = menu.addMenu(tr("Move to")); + // remove from layout action + QAction *removeAction = menu.addAction(tr("Remove from Sheet")); -// // TODO order in alphabetical order + QAction *selectedAction = menu.exec(event->screenPos()); -// for (auto pieceList : pieceLists) -// { -// QAction* moveToPieceList = moveMenu->addAction(pieceList->GetName()); -// QVariant data = QVariant::fromValue(pieceList); -// moveToPieceList->setData(data); - -// connect(moveToPieceList, &QAction::triggered, this, &VPGraphicsPiece::on_ActionPieceMovedToPieceList); -// } -// } - -// // remove from layout action -// QAction *removeAction = menu.addAction(tr("Remove from Sheet")); -// QVariant data = QVariant::fromValue(m_piece->GetPieceList()->GetLayout()->GetUnplacedPieceList()); -// removeAction->setData(data); -// connect(removeAction, &QAction::triggered, this, &VPGraphicsPiece::on_ActionPieceMovedToPieceList); - -// menu.exec(event->screenPos()); + if (moveToActions.contains(selectedAction)) + { + m_piece->SetSheet(qvariant_cast(selectedAction->data())); + emit m_piece->Layout()->PieceSheetChanged(m_piece); + } + else if (selectedAction == removeAction) + { + m_piece->SetSheet(nullptr); + emit m_piece->Layout()->PieceSheetChanged(m_piece); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/vpgraphicspiece.h b/src/app/puzzle/vpgraphicspiece.h index 58e680b21..2e5db17d1 100644 --- a/src/app/puzzle/vpgraphicspiece.h +++ b/src/app/puzzle/vpgraphicspiece.h @@ -49,6 +49,9 @@ public: */ auto GetPiece() -> VPPiece*; + virtual int type() const override {return Type;} + enum { Type = UserType + 1}; + public slots: /** * @brief on_PieceSelectionChanged Slot called when the piece selection was changed diff --git a/src/app/puzzle/vpmaingraphicsview.cpp b/src/app/puzzle/vpmaingraphicsview.cpp index 5952e65fd..bfa413103 100644 --- a/src/app/puzzle/vpmaingraphicsview.cpp +++ b/src/app/puzzle/vpmaingraphicsview.cpp @@ -220,6 +220,13 @@ void VPMainGraphicsView::keyPressEvent(QKeyEvent *event) //--------------------------------------------------------------------------------------------------------------------- void VPMainGraphicsView::contextMenuEvent(QContextMenuEvent *event) { + QGraphicsItem *item = itemAt(event->pos()); + if (item != nullptr && item->type() == VPGraphicsPiece::Type) + { + VMainGraphicsView::contextMenuEvent(event); + return; + } + QMenu menu; VPSheet *sheet = m_layout->GetFocusedSheet(); @@ -258,12 +265,14 @@ void VPMainGraphicsView::on_PieceSheetChanged(VPPiece *piece) } } - if (piece->Sheet() == nullptr || piece->Sheet() == m_layout->GetTrashSheet()) // remove + if (piece->Sheet() == nullptr || piece->Sheet() == m_layout->GetTrashSheet() || + piece->Sheet() != m_layout->GetFocusedSheet()) // remove { if (_graphicsPiece != nullptr) { scene()->removeItem(_graphicsPiece); m_graphicsPieces.removeAll(_graphicsPiece); + delete _graphicsPiece; } } else // add diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index b87b03930..cbdcb24f2 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -736,6 +736,7 @@ void VPMainWindow::InitMainGraphics() connect(m_graphicsView->GetScene(), &VMainGraphicsScene::mouseMove, this, &VPMainWindow::on_MouseMoved); connect(m_carrousel, &VPCarrousel::on_ActiveSheetChanged, m_graphicsView, &VPMainGraphicsView::RefreshPieces); connect(m_graphicsView, &VPMainGraphicsView::on_SheetRemoved, m_carrousel, &VPCarrousel::Refresh); + connect(m_layout, &VPLayout::PieceSheetChanged, m_carrousel, &VPCarrousel::Refresh); } //---------------------------------------------------------------------------------------------------------------------