Move piece with keyboard.

This commit is contained in:
Roman Telezhynskyi 2021-08-09 15:24:36 +03:00
parent 3fbe96c2a7
commit d6fe9508a6
4 changed files with 74 additions and 15 deletions

View File

@ -76,11 +76,18 @@ auto VPGraphicsPiece::GetPiece() -> VPPiece*
return m_piece; return m_piece;
} }
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::TranslatePiece(qreal dx, qreal dy)
{
TranslatePiece(QPointF(dx, dy));
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::TranslatePiece(const QPointF &p) void VPGraphicsPiece::TranslatePiece(const QPointF &p)
{ {
m_piece->Translate(p);
prepareGeometryChange(); prepareGeometryChange();
m_piece->Translate(p);
PaintPiece(); // refresh shapes
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -49,6 +49,7 @@ public:
*/ */
auto GetPiece() -> VPPiece*; auto GetPiece() -> VPPiece*;
void TranslatePiece(qreal dx, qreal dy);
void TranslatePiece(const QPointF &p); void TranslatePiece(const QPointF &p);
virtual int type() const override {return Type;} virtual int type() const override {return Type;}

View File

@ -262,9 +262,7 @@ void VPMainGraphicsView::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)
{ {
QList<VPGraphicsPiece*> tmpGraphicsPieces = m_graphicsPieces; for(auto *graphicsPiece : m_graphicsPieces)
for(auto *graphicsPiece : tmpGraphicsPieces)
{ {
VPPiece *piece = graphicsPiece->GetPiece(); VPPiece *piece = graphicsPiece->GetPiece();
@ -272,9 +270,55 @@ void VPMainGraphicsView::keyPressEvent(QKeyEvent *event)
{ {
piece->SetSelected(false); piece->SetSelected(false);
piece->SetSheet(nullptr); piece->SetSheet(nullptr);
m_graphicsPieces.removeAll(graphicsPiece);
delete graphicsPiece;
} }
} }
} }
else if (event->key() == Qt::Key_Left)
{
if((event->modifiers() & Qt::ShiftModifier) != 0U)
{
TranslatePiecesOn(-10, 0);
}
else
{
TranslatePiecesOn(-1, 0);
}
}
else if (event->key() == Qt::Key_Right)
{
if((event->modifiers() & Qt::ShiftModifier) != 0U)
{
TranslatePiecesOn(10, 0);
}
else
{
TranslatePiecesOn(1, 0);
}
}
else if (event->key() == Qt::Key_Up)
{
if((event->modifiers() & Qt::ShiftModifier) != 0U)
{
TranslatePiecesOn(0, -10);
}
else
{
TranslatePiecesOn(0, 1);
}
}
else if (event->key() == Qt::Key_Down)
{
if((event->modifiers() & Qt::ShiftModifier) != 0U)
{
TranslatePiecesOn(0, 10);
}
else
{
TranslatePiecesOn(0, 1);
}
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -390,12 +434,6 @@ void VPMainGraphicsView::ConnectPiece(VPGraphicsPiece *piece)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RotatePiecesByAngle(qreal angle) const void VPMainGraphicsView::RotatePiecesByAngle(qreal angle) const
{ {
QGraphicsScene *scene = this->scene();
if (scene == nullptr)
{
return;
}
VPSheet *sheet = m_layout->GetFocusedSheet(); VPSheet *sheet = m_layout->GetFocusedSheet();
if (sheet == nullptr) if (sheet == nullptr)
{ {
@ -404,13 +442,25 @@ void VPMainGraphicsView::RotatePiecesByAngle(qreal angle) const
VPTransformationOrigon origin = sheet->TransformationOrigin(); VPTransformationOrigon origin = sheet->TransformationOrigin();
QList<QGraphicsItem *> list = scene->selectedItems(); for(auto *graphicsPiece : m_graphicsPieces)
for (auto *item : list)
{ {
if (item->type() == VPGraphicsPiece::Type) if (graphicsPiece->isSelected())
{ {
auto *pieceItem = dynamic_cast<VPGraphicsPiece*>(item); graphicsPiece->on_Rotate(origin.origin, angle);
pieceItem->on_Rotate(origin.origin, angle); m_rotationControls->on_UpdateControls();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::TranslatePiecesOn(qreal dx, qreal dy) const
{
for(auto *graphicsPiece : m_graphicsPieces)
{
if (graphicsPiece->isSelected())
{
graphicsPiece->TranslatePiece(dx, dy);
m_rotationControls->on_UpdateControls();
} }
} }
} }

View File

@ -132,6 +132,7 @@ private:
void ConnectPiece(VPGraphicsPiece *piece); void ConnectPiece(VPGraphicsPiece *piece);
void RotatePiecesByAngle(qreal angle) const; void RotatePiecesByAngle(qreal angle) const;
void TranslatePiecesOn(qreal dx, qreal dy) const;
}; };