valentina/src/app/puzzle/vpgraphicspiece.cpp

487 lines
15 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 <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-11-22 12:45:38 +01:00
#include <QApplication>
2020-05-05 07:44:20 +02:00
2020-05-23 15:42:51 +02:00
#include "vppiece.h"
2020-05-23 15:34:11 +02:00
#include "vplayout.h"
#include "vpsheet.h"
2020-05-05 17:40:36 +02:00
2020-11-19 16:32:47 +01:00
#include "vlayoutpiecepath.h"
2020-11-20 17:05:56 +01:00
#include "vplacelabelitem.h"
2020-11-19 16:32:47 +01:00
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()),
2020-11-20 17:05:56 +01:00
m_passmarks(QPainterPath()),
2020-11-19 16:32:47 +01:00
m_internalPaths(QVector<QPainterPath>()),
m_internalPathsPenStyle(QVector<Qt::PenStyle>()),
2020-11-20 17:05:56 +01:00
m_placeLabels(QVector<QPainterPath>()),
2020-11-22 12:45:38 +01:00
m_rotationStartPoint(QPointF()),
m_rotateCursor(QCursor())
2020-05-05 07:44:20 +02:00
{
2020-11-22 12:45:38 +01:00
QPixmap cursor_pixmap = QIcon("://puzzleicon/svg/cursor_rotate.svg").pixmap(QSize(32,32));
m_rotateCursor= QCursor(cursor_pixmap, 16, 16);
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());
2020-11-19 16:32:47 +01:00
for (int i = 0; i < seamLinePoints.size(); i++)
2020-05-23 13:10:25 +02:00
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());
2020-11-19 16:32:47 +01:00
for (int i = 0; i < cuttingLinepoints.size(); i++)
2020-05-23 13:10:25 +02:00
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
{
QVector<QPointF> grainLinepoints = m_piece->GetMappedGrainline();
2020-06-25 14:17:31 +02:00
if(!grainLinepoints.isEmpty())
{
m_grainline.moveTo(grainLinepoints.first());
2020-11-19 16:32:47 +01:00
for (int i = 0; i < grainLinepoints.size(); i++)
2020-06-25 14:17:31 +02:00
m_grainline.lineTo(grainLinepoints.at(i));
}
2020-05-23 13:10:25 +02:00
}
2020-11-19 16:32:47 +01:00
// initialises the internal paths
QVector<VLayoutPiecePath> internalPaths = m_piece->GetInternalPaths();
for (int i = 0; i < internalPaths.size(); i++)
{
VLayoutPiecePath piecePath = internalPaths.at(i);
QPainterPath path = m_piece->GetMatrix().map(piecePath.GetPainterPath());
m_internalPaths.append(path);
m_internalPathsPenStyle.append(piecePath.PenStyle());
}
2020-11-20 17:05:56 +01:00
// initialises the passmarks
QVector<VLayoutPassmark> passmarks = m_piece->GetMappedPassmarks();
2020-11-20 17:05:56 +01:00
for(auto &passmark : passmarks)
{
for (auto &line : passmark.lines)
{
m_passmarks.moveTo(line.p1());
m_passmarks.lineTo(line.p2());
}
}
// initialises the place labels (buttons etc)
QVector<VLayoutPlaceLabel> placeLabels = m_piece->GetPlaceLabels();
for(auto &placeLabel : placeLabels)
{
QPainterPath path = VPlaceLabelItem::LabelShapePath(placeLabel.shape);
m_placeLabels.append(path);
}
// TODO : initialises the text labels
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-11-10 21:29:23 +01:00
connect(m_piece, &VPPiece::PropertiesChanged, this, &VPGraphicsPiece::on_PiecePropertiesChanged);
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-11-19 16:32:47 +01:00
QBrush blackBrush(Qt::black);
2020-05-05 07:44:20 +02:00
painter->setPen(pen);
2020-11-19 16:32:47 +01:00
// selection
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-11-10 21:29:23 +01:00
if(!m_seamLine.isEmpty() && m_piece->GetShowSeamLine())
2020-05-05 07:44:20 +02:00
{
painter->drawPath(m_seamLine);
}
painter->setBrush(noBrush);
// paint the grainline
if(!m_grainline.isEmpty())
{
2020-11-19 17:24:48 +01:00
// here to fill the grainlines arrow. Not wanted for mvp
// later maybe if it's configurable
// painter->setBrush(blackBrush);
painter->drawPath(m_grainline);
}
2020-11-19 16:32:47 +01:00
// paint the internal paths
painter->setBrush(noBrush);
if(!m_internalPaths.isEmpty())
{
Qt::PenStyle penStyleTmp = pen.style();
for (int i = 0; i < m_internalPaths.size(); i++)
{
painter->setPen(m_internalPathsPenStyle.at(i));
painter->drawPath(m_internalPaths.at(i));
}
2020-11-20 17:05:56 +01:00
painter->setPen(penStyleTmp);
2020-11-19 16:32:47 +01:00
}
2020-11-20 17:05:56 +01:00
// paint the passmarks
if(!m_passmarks.isEmpty())
{
painter->drawPath(m_passmarks);
}
// paint the place labels (buttons etc)
if(!m_placeLabels.isEmpty())
{
for(auto &placeLabel : m_placeLabels)
{
painter->drawPath(placeLabel);
}
}
// TODO Detail & Piece Label
2020-11-21 13:45:26 +01:00
// QPointF position = m_piece->GetPatternTextPosition();
// QStringList texts = m_piece->GetPatternText();
// painter->drawText();
2020-11-19 16:32:47 +01:00
// when using m_piece->GetItem(), the results were quite bad
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);
2020-11-22 12:45:38 +01:00
// change the cursor when clicking the left button
if((event->button() == Qt::LeftButton))
{
2021-05-27 10:35:39 +02:00
if((event->modifiers() & Qt::AltModifier) != 0U)
2020-11-22 12:45:38 +01:00
{
setCursor(m_rotateCursor);
}
else
{
setCursor(Qt::ClosedHandCursor);
}
}
// change the selected state when clicking left button
if (event->button() == Qt::LeftButton)
2020-05-05 07:44:20 +02:00
{
setSelected(true);
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();
}
}
//---------------------------------------------------------------------------------------------------------------------
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))
{
2020-11-22 12:45:38 +01:00
//FIXME: it flickers between the arrow cursor and the rotate cursor
setCursor(m_rotateCursor);
2020-05-09 14:45:36 +02:00
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)
{
2020-11-22 12:45:38 +01:00
//FIXME: it flickers between the arrow cursor and the rotate cursor
setCursor(m_rotateCursor);
2020-05-09 14:45:36 +02:00
}
else
{
2020-11-22 12:45:38 +01:00
setCursor(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
2021-07-29 16:11:18 +02:00
// QMenu menu;
2021-07-29 16:11:18 +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());
// }
2021-07-29 16:11:18 +02:00
// pieceLists.removeAll(m_piece->GetPieceList());
2021-07-29 16:11:18 +02:00
// if(pieceLists.count() > 0)
// {
// QMenu *moveMenu = menu.addMenu(tr("Move to"));
2021-07-29 16:11:18 +02:00
// // TODO order in alphabetical order
2021-07-29 16:11:18 +02:00
// for (auto pieceList : pieceLists)
// {
// QAction* moveToPieceList = moveMenu->addAction(pieceList->GetName());
// QVariant data = QVariant::fromValue(pieceList);
// moveToPieceList->setData(data);
2021-07-29 16:11:18 +02:00
// connect(moveToPieceList, &QAction::triggered, this, &VPGraphicsPiece::on_ActionPieceMovedToPieceList);
// }
// }
2021-07-29 16:11:18 +02:00
// // remove from layout action
// QAction *removeAction = menu.addAction(tr("Remove from Sheet"));
// QVariant data = QVariant::fromValue(m_piece->GetPieceList()->GetLayout()->GetUnplacedPieceList());
// removeAction->setData(data);
// connect(removeAction, &QAction::triggered, this, &VPGraphicsPiece::on_ActionPieceMovedToPieceList);
2021-07-29 16:11:18 +02:00
// menu.exec(event->screenPos());
}
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-11-10 21:29:23 +01:00
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::on_PiecePropertiesChanged()
{
if(scene())
{
scene()->update();
}
}
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