enable rotation

This commit is contained in:
Ronan Le Tiec 2020-05-09 11:13:29 +02:00
parent e72a664c8a
commit dc44bef761
6 changed files with 63 additions and 27 deletions

View File

@ -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);
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -228,9 +228,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-542</y>
<y>0</y>
<width>342</width>
<height>1302</height>
<height>1318</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
@ -333,7 +333,7 @@
<double>360.000000000000000</double>
</property>
<property name="singleStep">
<double>0.100000000000000</double>
<double>1.000000000000000</double>
</property>
</widget>
</item>

View File

@ -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)
{

View File

@ -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;

View File

@ -27,6 +27,8 @@
*************************************************************************/
#include "vpuzzlepiece.h"
#include <QtMath>
#include "vpuzzlelayer.h"
#include <QLoggingCategory>
@ -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;
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -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};