/************************************************************************ ** ** @file vppiece.cpp ** @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 ** 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 . ** *************************************************************************/ #include "vppiece.h" #include #include "../vmisc/def.h" #include "vpsheet.h" #include #include #include Q_LOGGING_CATEGORY(pPiece, "p.piece") //--------------------------------------------------------------------------------------------------------------------- VPPiece::VPPiece(const VLayoutPiece &layoutPiece) : VLayoutPiece(layoutPiece) { ClearTransformations(); } //--------------------------------------------------------------------------------------------------------------------- void VPPiece::ClearTransformations() { // Reset the piece position to the default state QTransform matrix; SetMatrix(matrix); // 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 const QPointF offset = MappedDetailBoundingRect().topLeft(); matrix.translate(-offset.x() ,-offset.y()); SetMatrix(matrix); } //--------------------------------------------------------------------------------------------------------------------- void VPPiece::SetPosition(QPointF point) { QTransform matrix = GetMatrix(); const QPointF offset = MappedDetailBoundingRect().topLeft(); matrix.translate(point.x() - offset.x(), point.y() - offset.y()); SetMatrix(matrix); } //--------------------------------------------------------------------------------------------------------------------- auto VPPiece::GetPosition() -> QPointF { QTransform matrix = GetMatrix(); return QPointF(matrix.dx(), matrix.dy()); } //--------------------------------------------------------------------------------------------------------------------- void VPPiece::RotateToGrainline() { if (not IsGrainlineEnabled() || m_sheet == nullptr) { return; } const QVector grainlinePoints = GetMappedGrainline(); if (grainlinePoints.count() < 2) { return; } QLineF grainline(grainlinePoints.first(), grainlinePoints.last()); QLineF canonical(grainlinePoints.first().x(), grainlinePoints.first().y(), grainlinePoints.first().x()+100, grainlinePoints.first().y()); GrainlineType grainlineType = m_sheet->GrainlineType(); auto DegreesAtFront = [grainline, canonical, grainlineType]() { QLineF atFront = canonical; if (grainlineType == GrainlineType::Vertical) { atFront.setAngle(90); } return grainline.angleTo(atFront); }; auto DegreesAtRear = [grainline, canonical, grainlineType]() { QLineF atRear = canonical; atRear.setAngle(grainlineType == GrainlineType::Vertical ? 270 : 180); return grainline.angleTo(atRear); }; GrainlineArrowDirection type = GrainlineArrowType(); qreal degrees = 0; if (type == GrainlineArrowDirection::atFront) { degrees = DegreesAtFront(); } else if (type == GrainlineArrowDirection::atRear) { degrees = DegreesAtRear(); } else { degrees = qMin(DegreesAtFront(), DegreesAtRear()); } Rotate(MappedDetailBoundingRect().center(), degrees); } //--------------------------------------------------------------------------------------------------------------------- void VPPiece::SetSelected(bool value) { m_isSelected = value; } //--------------------------------------------------------------------------------------------------------------------- auto VPPiece::IsSelected() const -> bool { return m_isSelected; } //--------------------------------------------------------------------------------------------------------------------- auto VPPiece::Sheet() const -> VPSheet * { return m_sheet; } //--------------------------------------------------------------------------------------------------------------------- void VPPiece::SetSheet(VPSheet *newSheet) { m_sheet = newSheet; } //--------------------------------------------------------------------------------------------------------------------- auto VPPiece::Layout() const -> VPLayout * { return m_layout; } //--------------------------------------------------------------------------------------------------------------------- void VPPiece::SetLayout(VPLayout *layout) { SCASSERT(layout != nullptr) m_layout = layout; }