improve unselect behaviour of unplaced pieces

This commit is contained in:
Ronan Le Tiec 2020-05-09 09:00:27 +02:00
parent 6c35dfc78a
commit f516d80513
5 changed files with 39 additions and 3 deletions

View file

@ -58,6 +58,16 @@ QList<VPuzzlePiece *> VPuzzleLayer::GetPieces()
return m_pieces; return m_pieces;
} }
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayer::ClearSelection()
{
for (auto piece: m_pieces)
{
piece->SetIsSelected(false);
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayer::AddPiece(VPuzzlePiece *piece) void VPuzzleLayer::AddPiece(VPuzzlePiece *piece)
{ {

View file

@ -59,6 +59,11 @@ public:
*/ */
VPuzzleLayout* GetLayout(); VPuzzleLayout* GetLayout();
/**
* @brief ClearSelection Clears the selection of the pieces in this layer
*/
void ClearSelection();
signals: signals:
/** /**
* @brief PieceAdded The signal is emited when a piece was added * @brief PieceAdded The signal is emited when a piece was added

View file

@ -275,9 +275,11 @@ bool VPuzzleLayout::GetStickyEdges() const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleLayout::ClearSelection() void VPuzzleLayout::ClearSelection()
{ {
for(auto piece : GetSelectedPieces()) m_unplacedPiecesLayer->ClearSelection();
for (auto layer : m_layers)
{ {
piece->SetIsSelected(false); layer->ClearSelection();
} }
} }

View file

@ -59,6 +59,7 @@ VPuzzleMainGraphicsView::VPuzzleMainGraphicsView(VPuzzleLayout *layout, QWidget
// add the connections // add the connections
connect(m_layout, &VPuzzleLayout::PieceMovedToLayer, this, &VPuzzleMainGraphicsView::on_PieceMovedToLayer); connect(m_layout, &VPuzzleLayout::PieceMovedToLayer, this, &VPuzzleMainGraphicsView::on_PieceMovedToLayer);
connect(m_scene, &VPuzzleMainGraphicsScene::selectionChanged, this, &VPuzzleMainGraphicsView::on_SceneSelectionChanged);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -143,12 +144,15 @@ void VPuzzleMainGraphicsView::keyPressEvent(QKeyEvent *event)
{ {
if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete)
{ {
for(auto graphicsPiece : m_graphicsPieces) QList<VPuzzleGraphicsPiece*> tmpGraphicsPieces = m_graphicsPieces;
for(auto graphicsPiece : tmpGraphicsPieces)
{ {
VPuzzlePiece *piece = graphicsPiece->GetPiece(); VPuzzlePiece *piece = graphicsPiece->GetPiece();
if(piece->GetIsSelected()) if(piece->GetIsSelected())
{ {
piece->SetIsSelected(false);
m_layout->MovePieceToLayer(piece, m_layout->GetUnplacedPiecesLayer()); m_layout->MovePieceToLayer(piece, m_layout->GetUnplacedPiecesLayer());
} }
} }
@ -188,3 +192,13 @@ void VPuzzleMainGraphicsView::on_PieceMovedToLayer(VPuzzlePiece *piece, VPuzzleL
_graphicsPiece->update(); _graphicsPiece->update();
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleMainGraphicsView::on_SceneSelectionChanged()
{
// most of the selection behaviour taks place automatically
// but we need to make sure that the unplaced pieces are unselected when the scene selection has changed
// because as they are not part of the scene, they are not updated
m_layout->GetUnplacedPiecesLayer()->ClearSelection();
}

View file

@ -67,6 +67,11 @@ private slots:
*/ */
void on_PieceMovedToLayer(VPuzzlePiece *piece, VPuzzleLayer *layerBefore, VPuzzleLayer *layerAfter); void on_PieceMovedToLayer(VPuzzlePiece *piece, VPuzzleLayer *layerBefore, VPuzzleLayer *layerAfter);
/**
* @brief on_SceneSelectionChanged Slot is called when the scene selection has changed
*/
void on_SceneSelectionChanged();
private: private:
Q_DISABLE_COPY(VPuzzleMainGraphicsView) Q_DISABLE_COPY(VPuzzleMainGraphicsView)