diff --git a/src/app/puzzle/puzzlemainwindow.cpp b/src/app/puzzle/puzzlemainwindow.cpp index ef3e8d775..83375cba9 100644 --- a/src/app/puzzle/puzzlemainwindow.cpp +++ b/src/app/puzzle/puzzlemainwindow.cpp @@ -372,7 +372,8 @@ void PuzzleMainWindow::SetPropertyTabCurrentPieceData() SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionX, UnitConvertor(pos.x(), Unit::Px, m_layout->GetUnit())); SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionY, UnitConvertor(pos.y(), Unit::Px, m_layout->GetUnit())); - // TODO: rotation + qreal angle = selectedPiece->GetRotation(); + SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceAngle, angle); } else { @@ -831,24 +832,17 @@ void PuzzleMainWindow::on_checkBoxCurrentPieceMirrorPiece_toggled(bool checked) //--------------------------------------------------------------------------------------------------------------------- void PuzzleMainWindow::on_doubleSpinBoxCurrentPieceAngle_valueChanged(double value) { - // just for test purpuses, to be removed: - QMessageBox msgBox; - msgBox.setText("TODO PuzzleMainWindow::CurrentPieceAngleChanged"); - int ret = msgBox.exec(); - - Q_UNUSED(value); - Q_UNUSED(ret); - - // TODO + if(m_selectedPieces.count() == 1) + { + VPuzzlePiece *piece = m_selectedPieces.first(); + piece->SetRotation(value); + } } //--------------------------------------------------------------------------------------------------------------------- void PuzzleMainWindow::on_CurrentPiecePositionEdited() { -// ui->doubleSpinBoxCurrentPieceBoxPositionX->blockSignals(true); -// ui->doubleSpinBoxCurrentPieceBoxPositionY->blockSignals(true); - if(m_selectedPieces.count() == 1) { VPuzzlePiece *piece = m_selectedPieces.first(); @@ -856,10 +850,6 @@ void PuzzleMainWindow::on_CurrentPiecePositionEdited() UnitConvertor(ui->doubleSpinBoxCurrentPieceBoxPositionY->value(), m_layout->GetUnit(), Unit::Px)); piece->SetPosition(pos); } - -// ui->doubleSpinBoxCurrentPieceBoxPositionX->blockSignals(false); -// ui->doubleSpinBoxCurrentPieceBoxPositionY->blockSignals(false); - } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/puzzlemainwindow.ui b/src/app/puzzle/puzzlemainwindow.ui index 7d6205696..79ed17157 100644 --- a/src/app/puzzle/puzzlemainwindow.ui +++ b/src/app/puzzle/puzzlemainwindow.ui @@ -228,9 +228,9 @@ 0 - -542 + 0 342 - 1302 + 1318 @@ -333,7 +333,7 @@ 360.000000000000000 - 0.100000000000000 + 1.000000000000000 diff --git a/src/app/puzzle/vpuzzlegraphicspiece.cpp b/src/app/puzzle/vpuzzlegraphicspiece.cpp index 6b221427f..fb18783f6 100644 --- a/src/app/puzzle/vpuzzlegraphicspiece.cpp +++ b/src/app/puzzle/vpuzzlegraphicspiece.cpp @@ -92,6 +92,7 @@ void VPuzzleGraphicsPiece::Init() // Initialises the connectors connect(m_piece, &VPuzzlePiece::SelectionChanged, this, &VPuzzleGraphicsPiece::on_PieceSelectionChanged); connect(m_piece, &VPuzzlePiece::PositionChanged, this, &VPuzzleGraphicsPiece::on_PiecePositionChanged); + connect(m_piece, &VPuzzlePiece::RotationChanged, this, &VPuzzleGraphicsPiece::on_PieceRotationChanged); } //--------------------------------------------------------------------------------------------------------------------- @@ -265,6 +266,13 @@ void VPuzzleGraphicsPiece::on_PiecePositionChanged() setPos(m_piece->GetPosition()); } +//--------------------------------------------------------------------------------------------------------------------- +void VPuzzleGraphicsPiece::on_PieceRotationChanged() +{ + setTransformOriginPoint(boundingRect().center()); + setRotation(-m_piece->GetRotation()); +} + //--------------------------------------------------------------------------------------------------------------------- QVariant VPuzzleGraphicsPiece::itemChange(GraphicsItemChange change, const QVariant &value) { diff --git a/src/app/puzzle/vpuzzlegraphicspiece.h b/src/app/puzzle/vpuzzlegraphicspiece.h index c11680008..496f55e76 100644 --- a/src/app/puzzle/vpuzzlegraphicspiece.h +++ b/src/app/puzzle/vpuzzlegraphicspiece.h @@ -49,15 +49,20 @@ public: public slots: /** - * @brief on_PieceSelectionChanged When the piece selection was changed + * @brief on_PieceSelectionChanged Slot called when the piece selection was changed */ void on_PieceSelectionChanged(); /** - * @brief on_PiecePositionChanged When the piece position was changed + * @brief on_PiecePositionChanged Slot called when the piece position was changed */ void on_PiecePositionChanged(); + /** + * @brief on_PieceRotationChanged Slot called when the piece rotation was changed + */ + void on_PieceRotationChanged(); + protected: QRectF boundingRect() const override; QPainterPath shape() const override; diff --git a/src/app/puzzle/vpuzzlepiece.cpp b/src/app/puzzle/vpuzzlepiece.cpp index 9b2a6908a..92a42191f 100644 --- a/src/app/puzzle/vpuzzlepiece.cpp +++ b/src/app/puzzle/vpuzzlepiece.cpp @@ -27,6 +27,8 @@ *************************************************************************/ #include "vpuzzlepiece.h" +#include + #include "vpuzzlelayer.h" #include @@ -142,8 +144,25 @@ QPointF VPuzzlePiece::GetPosition() //--------------------------------------------------------------------------------------------------------------------- void VPuzzlePiece::SetRotation(qreal angle) { - Q_UNUSED(angle); - //TODO + m_pieceAngle = angle; + + // make sure the angle is [0 <= angle < 360] + while(m_pieceAngle >= 360) + { + m_pieceAngle -= 360; + } + + while(m_pieceAngle < 0) + { + m_pieceAngle += 360; + } + + +// qreal currentAngle = GetRotation(); +// qreal newAngle = angle - currentAngle; + +// m_transform.rotate(newAngle); + emit RotationChanged(); } @@ -151,8 +170,20 @@ void VPuzzlePiece::SetRotation(qreal angle) //--------------------------------------------------------------------------------------------------------------------- qreal VPuzzlePiece::GetRotation() { - // TODO - return 0; + return m_pieceAngle; + + // We don't use the QTransform vor now because the math behind it to retrieve the angle is not trivial. + // TODO / FIXME: we can use QTransform later for optimization + + +// QTransform tmpTransform = m_transform; +// tmpTransform.translate(-tmpTransform.dx(), -tmpTransform.dy()); // make sure there is only the rotation in the matrix + +// qreal angle = qRadiansToDegrees(qAcos(tmpTransform.m11())); + +// qCDebug(pPiece, "new angle : %f", angle); + +// return angle; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/vpuzzlepiece.h b/src/app/puzzle/vpuzzlepiece.h index f5dd3ef5f..229036139 100644 --- a/src/app/puzzle/vpuzzlepiece.h +++ b/src/app/puzzle/vpuzzlepiece.h @@ -127,7 +127,7 @@ public: /** * @brief SetRotation Sets the rotation of the piece to the given angle. - * @param angle the angle of the rotation + * @param angle the angle of the rotation in degree */ void SetRotation(qreal angle); @@ -235,6 +235,8 @@ private: qreal m_grainlineAngle{0}; QTransform m_transform{QTransform()}; + // use a separate value for now because it's not easy to get the angle from the transform matrix + qreal m_pieceAngle{0}; bool m_showSeamline{true}; bool m_mirrorPiece{false};