valentina/src/app/puzzle/vpgraphicspiece.cpp

398 lines
12 KiB
C++
Raw Normal View History

2020-05-05 07:44:20 +02:00
/************************************************************************
**
2020-05-23 14:50:22 +02:00
** @file vpgraphicspiece.cpp
2020-05-05 07:44:20 +02:00
** @author Ronan Le Tiec
** @date 4 5, 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 14:50:22 +02:00
#include "vpgraphicspiece.h"
2020-05-05 07:44:20 +02:00
#include <QPen>
#include <QBrush>
#include <QPainter>
#include <QCursor>
#include <QGraphicsSceneMouseEvent>
2020-05-05 17:40:36 +02:00
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>
2020-05-09 14:45:36 +02:00
#include <QtMath>
#include <QGraphicsScene>
2020-05-05 07:44:20 +02:00
2020-05-23 15:42:51 +02:00
#include "vppiece.h"
2020-05-23 15:29:57 +02:00
#include "vppiecelist.h"
2020-05-23 15:34:11 +02:00
#include "vplayout.h"
#include "vpsheet.h"
2020-05-05 17:40:36 +02:00
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(pGraphicsPiece, "p.graphicsPiece")
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:42:51 +02:00
VPGraphicsPiece::VPGraphicsPiece(VPPiece *piece, QGraphicsItem *parent) :
2020-05-05 17:40:36 +02:00
QGraphicsObject(parent),
2020-05-05 07:44:20 +02:00
m_piece(piece),
m_cuttingLine(QPainterPath()),
m_seamLine(QPainterPath()),
2020-05-09 14:45:36 +02:00
m_grainline(QPainterPath()),
m_rotationStartPoint(QPointF())
2020-05-05 07:44:20 +02:00
{
Init();
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
VPGraphicsPiece::~VPGraphicsPiece()
2020-05-05 07:44:20 +02:00
{
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::Init()
2020-05-05 07:44:20 +02:00
{
// set some infos
2020-05-05 17:40:36 +02:00
setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
2020-05-09 14:45:36 +02:00
setAcceptHoverEvents(true);
2020-05-05 07:44:20 +02:00
setCursor(QCursor(Qt::OpenHandCursor));
// initialises the seam line
2020-06-25 14:17:31 +02:00
QVector<QPointF> seamLinePoints = m_piece->GetMappedContourPoints();
2020-05-23 13:10:25 +02:00
if(!seamLinePoints.isEmpty())
{
m_seamLine.moveTo(seamLinePoints.first());
for (int i = 1; i < seamLinePoints.size(); ++i)
m_seamLine.lineTo(seamLinePoints.at(i));
}
2020-05-05 07:44:20 +02:00
// initiliases the cutting line
2020-06-25 14:17:31 +02:00
QVector<QPointF> cuttingLinepoints = m_piece->GetMappedSeamAllowancePoints();
2020-05-23 13:10:25 +02:00
if(!cuttingLinepoints.isEmpty())
{
m_cuttingLine.moveTo(cuttingLinepoints.first());
for (int i = 1; i < cuttingLinepoints.size(); ++i)
m_cuttingLine.lineTo(cuttingLinepoints.at(i));
}
2020-05-05 07:44:20 +02:00
// initialises the grainline
2020-06-25 14:17:31 +02:00
if(m_piece->IsGrainlineEnabled())
2020-05-23 13:10:25 +02:00
{
2020-06-25 14:17:31 +02:00
QVector<QPointF> grainLinepoints = m_piece->GetGrainline();
if(!grainLinepoints.isEmpty())
{
m_grainline.moveTo(grainLinepoints.first());
for (int i = 1; i < grainLinepoints.size(); ++i)
m_grainline.lineTo(grainLinepoints.at(i));
}
2020-05-23 13:10:25 +02:00
}
// TODO : initialises the other elements labels, passmarks etc.
2020-05-05 07:44:20 +02:00
2020-05-05 17:40:36 +02:00
// Initialises the connectors
2020-05-23 15:42:51 +02:00
connect(m_piece, &VPPiece::SelectionChanged, this, &VPGraphicsPiece::on_PieceSelectionChanged);
connect(m_piece, &VPPiece::PositionChanged, this, &VPGraphicsPiece::on_PiecePositionChanged);
connect(m_piece, &VPPiece::RotationChanged, this, &VPGraphicsPiece::on_PieceRotationChanged);
2020-05-05 07:44:20 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:42:51 +02:00
VPPiece* VPGraphicsPiece::GetPiece()
{
return m_piece;
}
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
QRectF VPGraphicsPiece::boundingRect() const
2020-05-05 07:44:20 +02:00
{
if(!m_cuttingLine.isEmpty())
{
return m_cuttingLine.boundingRect();
}
return m_seamLine.boundingRect();
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
QPainterPath VPGraphicsPiece::shape() const
2020-05-05 07:44:20 +02:00
{
if(!m_cuttingLine.isEmpty())
{
return m_cuttingLine;
}
return m_seamLine;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
2020-05-05 07:44:20 +02:00
{
Q_UNUSED(widget);
2020-05-05 17:40:36 +02:00
Q_UNUSED(option);
2020-05-05 07:44:20 +02:00
QPen pen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
QBrush noBrush(Qt::NoBrush);
QBrush selectionBrush(QColor(255,160,160,60));
2020-05-05 07:44:20 +02:00
painter->setPen(pen);
if(isSelected())
{
painter->setBrush(selectionBrush);
}
else
{
painter->setBrush(noBrush);
}
2020-05-05 07:44:20 +02:00
// paint the cutting line
2020-05-05 07:44:20 +02:00
if(!m_cuttingLine.isEmpty())
{
painter->drawPath(m_cuttingLine);
painter->setBrush(noBrush);
2020-05-05 07:44:20 +02:00
}
// paint the seam line
2020-05-05 07:44:20 +02:00
if(!m_seamLine.isEmpty())
{
painter->drawPath(m_seamLine);
}
painter->setBrush(noBrush);
// paint the grainline
if(!m_grainline.isEmpty())
{
painter->drawPath(m_grainline);
}
2020-05-05 07:44:20 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event)
2020-05-05 07:44:20 +02:00
{
bool selectionState = isSelected();
2020-05-05 07:44:20 +02:00
//perform the default behaviour
QGraphicsItem::mousePressEvent(event);
// change the cursor when clicking left button
if (event->button() == Qt::LeftButton)
2020-05-05 07:44:20 +02:00
{
setSelected(true);
setCursor(Qt::ClosedHandCursor);
if (event->modifiers() & Qt::ControlModifier)
{
setSelected(!selectionState);
}
else
{
setSelected(true);
}
2020-05-05 07:44:20 +02:00
}
2020-05-09 14:45:36 +02:00
if((event->button() == Qt::LeftButton) && (event->modifiers() & Qt::AltModifier))
{
m_rotationStartPoint = event->scenePos();
QPixmap cursor_pixmap = QPixmap(":/cursor_rotate");
cursor_pixmap = cursor_pixmap.scaledToWidth(32);
QCursor cursor_rotate = QCursor(cursor_pixmap, 16, 16);
setCursor(cursor_rotate);
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
2020-05-09 14:45:36 +02:00
{
if((event->buttons() == Qt::LeftButton) && (event->modifiers() & Qt::AltModifier))
{
QPointF rotationNewPoint = event->scenePos();
QPointF rotationCenter = sceneBoundingRect().center();
// get the angle from the center to the initial click point
qreal init_x = m_rotationStartPoint.x() - rotationCenter.x();
qreal init_y = m_rotationStartPoint.y() - rotationCenter.y();
qreal initial_angle = qAtan2(init_y, init_x);
qreal x = rotationNewPoint.x() - rotationCenter.x();
qreal y = rotationNewPoint.y() - rotationCenter.y();
qreal mv_angle = qAtan2(y,x);
qreal angle = (initial_angle-mv_angle)*180/M_PI;
setTransformOriginPoint(boundingRect().center());
setRotation(-(angle+m_piece->GetRotation()));
event->accept();
}
else
{
QGraphicsItem::mouseMoveEvent(event);
}
2020-05-05 07:44:20 +02:00
}
2020-05-09 14:45:36 +02:00
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
2020-05-05 07:44:20 +02:00
{
bool selectionState = isSelected();
2020-05-05 07:44:20 +02:00
//perform the default behaviour
QGraphicsItem::mouseReleaseEvent(event);
// change the cursor when clicking left button
if (event->button() == Qt::LeftButton)
2020-05-05 07:44:20 +02:00
{
setCursor(Qt::OpenHandCursor);
setSelected(selectionState);
2020-05-09 14:45:36 +02:00
if(m_piece->GetPosition() != pos())
{
m_piece->SetPosition(pos());
}
}
if((event->button() == Qt::LeftButton) && (event->modifiers() & Qt::AltModifier))
{
m_piece->SetRotation(-rotation());
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
2020-05-09 14:45:36 +02:00
{
if(event->modifiers() & Qt::AltModifier)
{
// TODO FIXME: find a more efficient way
QPixmap cursor_pixmap = QPixmap(":/cursor_rotate");
cursor_pixmap = cursor_pixmap.scaledToWidth(32);
QCursor cursor_rotate = QCursor(cursor_pixmap, 16, 16);
setCursor(cursor_rotate);
}
else
{
setCursor(QCursor(Qt::OpenHandCursor));
}
2020-05-05 07:44:20 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
// TODO/FIXME context menu needs to be refactored
QMenu contextMenu;
2020-05-23 15:29:57 +02:00
// move to piece list actions -- TODO : To be tested properly when we have several piece lists
QList<VPPieceList*> pieceLists = QList<VPPieceList*>();
for(auto sheet : m_piece->GetPieceList()->GetLayout()->GetSheets())
{
pieceLists.append(sheet->GetPieceList());
}
2020-05-23 15:29:57 +02:00
pieceLists.removeAll(m_piece->GetPieceList());
2020-05-23 15:29:57 +02:00
if(pieceLists.count() > 0)
{
QMenu *moveMenu = contextMenu.addMenu(tr("Move to"));
// TODO order in alphabetical order
2020-05-23 15:29:57 +02:00
for (auto pieceList : pieceLists)
{
2020-05-23 15:29:57 +02:00
QAction* moveToPieceList = moveMenu->addAction(pieceList->GetName());
QVariant data = QVariant::fromValue(pieceList);
moveToPieceList->setData(data);
2020-05-23 15:29:57 +02:00
connect(moveToPieceList, &QAction::triggered, this, &VPGraphicsPiece::on_ActionPieceMovedToPieceList);
}
}
// remove from layout action
2020-05-24 14:55:03 +02:00
QAction *removeAction = contextMenu.addAction(tr("Remove from Sheet"));
2020-05-23 15:29:57 +02:00
QVariant data = QVariant::fromValue(m_piece->GetPieceList()->GetLayout()->GetUnplacedPieceList());
removeAction->setData(data);
2020-05-23 15:29:57 +02:00
connect(removeAction, &QAction::triggered, this, &VPGraphicsPiece::on_ActionPieceMovedToPieceList);
contextMenu.exec(event->screenPos());
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:29:57 +02:00
void VPGraphicsPiece::on_ActionPieceMovedToPieceList()
{
QAction *act = qobject_cast<QAction *>(sender());
QVariant v = act->data();
2020-05-23 15:29:57 +02:00
VPPieceList *pieceList = v.value<VPPieceList *>();
if(pieceList != nullptr)
{
2020-05-23 15:29:57 +02:00
pieceList->GetLayout()->MovePieceToPieceList(m_piece, pieceList);
}
}
2020-05-05 07:44:20 +02:00
2020-05-05 17:40:36 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::on_PieceSelectionChanged()
2020-05-05 17:40:36 +02:00
{
setSelected(m_piece->GetIsSelected());
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::on_PiecePositionChanged()
2020-05-05 17:40:36 +02:00
{
setPos(m_piece->GetPosition());
}
2020-05-09 11:13:29 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::on_PieceRotationChanged()
2020-05-09 11:13:29 +02:00
{
setTransformOriginPoint(boundingRect().center());
setRotation(-m_piece->GetRotation());
}
2020-05-05 17:40:36 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
QVariant VPGraphicsPiece::itemChange(GraphicsItemChange change, const QVariant &value)
2020-05-05 17:40:36 +02:00
{
if (scene()) {
2020-05-09 14:45:36 +02:00
// we do this in the mouseRelease button to avoid updated this property all the time.
// if(change == ItemPositionHasChanged)
// {
// blockSignals(true);
// m_piece->SetPosition(pos());
// blockSignals(false);
// }
2020-05-05 17:40:36 +02:00
if(change == ItemSelectedHasChanged)
{
if(m_piece->GetIsSelected() != isSelected())
{
m_piece->SetIsSelected(isSelected());
}
}
}
return QGraphicsObject::itemChange(change, value);
}
2020-05-05 07:44:20 +02:00