2020-04-13 18:58:16 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
2020-05-23 15:42:51 +02:00
|
|
|
** @file vppiece.cpp
|
2020-04-13 18:58:16 +02:00
|
|
|
** @author Ronan Le Tiec
|
|
|
|
** @date 13 4, 2020
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentina project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2020 Valentina project
|
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
2020-05-23 15:42:51 +02:00
|
|
|
#include "vppiece.h"
|
2020-04-13 18:58:16 +02:00
|
|
|
|
2020-05-09 11:13:29 +02:00
|
|
|
#include <QtMath>
|
|
|
|
|
2020-05-23 15:29:57 +02:00
|
|
|
#include "vppiecelist.h"
|
2020-05-10 10:51:54 +02:00
|
|
|
#include "../vmisc/def.h"
|
2020-05-08 23:49:41 +02:00
|
|
|
|
2020-05-11 16:48:08 +02:00
|
|
|
#include <QIcon>
|
2020-05-08 23:49:41 +02:00
|
|
|
#include <QLoggingCategory>
|
2020-05-11 16:48:08 +02:00
|
|
|
#include <QPainter>
|
2020-05-08 23:49:41 +02:00
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(pPiece, "p.piece")
|
|
|
|
|
2020-04-18 11:54:34 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
VPPiece::VPPiece()
|
2020-04-13 18:58:16 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2020-04-18 11:54:34 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-06-25 14:17:31 +02:00
|
|
|
VPPiece::VPPiece(VLayoutPiece layoutPiece): VLayoutPiece(layoutPiece)
|
2020-04-26 12:09:28 +02:00
|
|
|
{
|
2020-06-25 14:17:31 +02:00
|
|
|
// Resets the translation of the matrix
|
|
|
|
QTransform matrix = GetMatrix();
|
|
|
|
matrix.translate(-matrix.dx() ,-matrix.dy());
|
|
|
|
SetMatrix(matrix);
|
2020-04-26 12:09:28 +02:00
|
|
|
|
2020-06-25 14:17:31 +02:00
|
|
|
// then translate the piece so that the top left corner of the bouding rect of the piece is at the position
|
|
|
|
// (0,0) in the sheet coordinate system
|
|
|
|
QRectF boundingRect = DetailBoundingRect();
|
2020-06-25 16:59:48 +02:00
|
|
|
QPointF offset = boundingRect.topLeft();
|
2020-06-25 14:17:31 +02:00
|
|
|
matrix = GetMatrix();
|
2020-06-25 16:59:48 +02:00
|
|
|
matrix.translate(-offset.x() ,-offset.y());
|
2020-06-25 14:17:31 +02:00
|
|
|
SetMatrix(matrix);
|
2020-04-26 12:09:28 +02:00
|
|
|
}
|
2020-04-26 15:36:20 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-06-25 14:17:31 +02:00
|
|
|
VPPiece::~VPPiece()
|
2020-04-26 15:36:20 +02:00
|
|
|
{
|
2020-05-01 19:08:48 +02:00
|
|
|
|
2020-05-02 12:17:06 +02:00
|
|
|
}
|
2020-05-01 19:08:48 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
bool VPPiece::GetShowSeamLine() const
|
2020-05-01 19:08:48 +02:00
|
|
|
{
|
|
|
|
return m_showSeamline;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
void VPPiece::SetShowSeamLine(bool value)
|
2020-05-01 19:08:48 +02:00
|
|
|
{
|
|
|
|
m_showSeamline = value;
|
2020-05-05 17:40:36 +02:00
|
|
|
|
|
|
|
emit PropertiesChanged();
|
2020-05-01 19:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
bool VPPiece::GetPieceMirrored() const
|
2020-05-01 19:08:48 +02:00
|
|
|
{
|
|
|
|
return m_mirrorPiece;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
void VPPiece::SetPieceMirrored(bool value)
|
2020-05-01 19:08:48 +02:00
|
|
|
{
|
|
|
|
m_mirrorPiece = value;
|
2020-05-05 17:40:36 +02:00
|
|
|
|
|
|
|
emit PropertiesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
void VPPiece::SetPosition(QPointF point)
|
2020-05-05 17:40:36 +02:00
|
|
|
{
|
2020-06-25 16:59:48 +02:00
|
|
|
m_transform.translate(point.x() - m_transform.dx(), point.y() - m_transform.dy());
|
2020-05-05 17:40:36 +02:00
|
|
|
|
|
|
|
emit PositionChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
QPointF VPPiece::GetPosition()
|
2020-05-05 17:40:36 +02:00
|
|
|
{
|
2020-06-25 16:59:48 +02:00
|
|
|
return QPointF(m_transform.dx(),m_transform.dy());
|
2020-05-05 17:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
void VPPiece::SetRotation(qreal angle)
|
2020-05-05 17:40:36 +02:00
|
|
|
{
|
2020-05-09 14:45:36 +02:00
|
|
|
// qreal currentAngle = GetRotation();
|
|
|
|
// qreal newAngle = angle - currentAngle;
|
2020-05-09 11:13:29 +02:00
|
|
|
|
2020-05-09 14:45:36 +02:00
|
|
|
// m_transform.rotate(newAngle);
|
2020-05-09 11:13:29 +02:00
|
|
|
|
2020-05-10 10:51:54 +02:00
|
|
|
if(not VFuzzyComparePossibleNulls(m_pieceAngle, angle))
|
2020-05-09 11:13:29 +02:00
|
|
|
{
|
2020-05-09 14:45:36 +02:00
|
|
|
m_pieceAngle = angle;
|
2020-05-09 11:13:29 +02:00
|
|
|
|
2020-05-09 14:45:36 +02:00
|
|
|
// make sure the angle is [0 <= angle < 360]
|
|
|
|
while(m_pieceAngle >= 360)
|
|
|
|
{
|
|
|
|
m_pieceAngle -= 360;
|
|
|
|
}
|
2020-05-09 11:13:29 +02:00
|
|
|
|
2020-05-09 14:45:36 +02:00
|
|
|
while(m_pieceAngle < 0)
|
|
|
|
{
|
|
|
|
m_pieceAngle += 360;
|
|
|
|
}
|
2020-05-09 11:13:29 +02:00
|
|
|
|
2020-05-09 14:45:36 +02:00
|
|
|
emit RotationChanged();
|
|
|
|
}
|
2020-05-01 19:08:48 +02:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:27:45 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPPiece::RotateBy(qreal angle)
|
|
|
|
{
|
|
|
|
SetRotation(m_pieceAngle + angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPPiece::RotateToGrainline(qreal angleOfGrainline, bool add180IfAlreadyInPosition)
|
|
|
|
{
|
|
|
|
qreal newAngle = -GrainlineAngle() + angleOfGrainline;
|
|
|
|
if(newAngle < 0)
|
|
|
|
{
|
|
|
|
newAngle += 360;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not VFuzzyComparePossibleNulls(m_pieceAngle, newAngle))
|
|
|
|
{
|
|
|
|
SetRotation(newAngle);
|
|
|
|
}
|
|
|
|
else if(add180IfAlreadyInPosition)
|
|
|
|
{
|
|
|
|
newAngle += 180;
|
|
|
|
SetRotation(newAngle);
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 14:17:31 +02:00
|
|
|
|
2020-05-05 17:40:36 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
qreal VPPiece::GetRotation()
|
2020-05-05 17:40:36 +02:00
|
|
|
{
|
2020-05-09 11:13:29 +02:00
|
|
|
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;
|
2020-05-05 17:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
void VPPiece::SetIsSelected(bool value)
|
2020-05-05 17:40:36 +02:00
|
|
|
{
|
2020-05-06 15:05:01 +02:00
|
|
|
if(m_isSelected != value)
|
|
|
|
{
|
|
|
|
m_isSelected = value;
|
|
|
|
emit SelectionChanged();
|
|
|
|
}
|
2020-05-05 17:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
bool VPPiece::GetIsSelected()
|
2020-05-05 17:40:36 +02:00
|
|
|
{
|
|
|
|
return m_isSelected;
|
|
|
|
}
|
|
|
|
|
2020-05-08 23:49:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
VPPieceList* VPPiece::GetPieceList()
|
2020-05-08 23:49:41 +02:00
|
|
|
{
|
2020-05-23 15:29:57 +02:00
|
|
|
return m_pieceList;
|
2020-05-08 23:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:42:51 +02:00
|
|
|
void VPPiece::SetPieceList(VPPieceList* pieceList)
|
2020-05-08 23:49:41 +02:00
|
|
|
{
|
2020-05-23 15:29:57 +02:00
|
|
|
if(pieceList != m_pieceList)
|
2020-05-08 23:49:41 +02:00
|
|
|
{
|
2020-05-23 15:29:57 +02:00
|
|
|
m_pieceList = pieceList;
|
2020-05-08 23:49:41 +02:00
|
|
|
}
|
|
|
|
}
|