valentina/src/app/puzzle/scene/vpgraphicspiece.cpp

380 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 <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
2021-08-09 14:09:10 +02:00
#include "../layout/vppiece.h"
#include "../layout/vplayout.h"
#include "../layout/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
2021-08-09 14:09:10 +02:00
namespace
{
constexpr qreal penWidth = 1;
}
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),
2021-07-31 11:32:23 +02:00
m_piece(piece)
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
// set some infos
2021-08-09 14:09:10 +02:00
setFlags(ItemIsSelectable | ItemSendsGeometryChanges);
2020-05-09 14:45:36 +02:00
setAcceptHoverEvents(true);
2021-08-09 14:09:10 +02:00
setCursor(Qt::OpenHandCursor);
2020-11-20 17:05:56 +01:00
2021-08-09 14:09:10 +02:00
PaintPiece();
2020-05-05 07:44:20 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-07-31 11:32:23 +02:00
auto VPGraphicsPiece::GetPiece() -> VPPiece*
{
return m_piece;
}
2020-05-05 07:44:20 +02:00
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::TranslatePiece(const QPointF &p)
{
m_piece->Translate(p);
prepareGeometryChange();
}
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-07-31 11:32:23 +02:00
auto VPGraphicsPiece::boundingRect() const -> QRectF
2020-05-05 07:44:20 +02:00
{
2021-08-09 14:09:10 +02:00
constexpr qreal halfPenWidth = penWidth/2.;
2020-05-05 07:44:20 +02:00
if(!m_cuttingLine.isEmpty())
{
2021-08-09 14:09:10 +02:00
return m_cuttingLine.boundingRect().adjusted(-halfPenWidth, -halfPenWidth, halfPenWidth, halfPenWidth);
2020-05-05 07:44:20 +02:00
}
2021-08-09 14:09:10 +02:00
return m_seamLine.boundingRect().adjusted(-halfPenWidth, -halfPenWidth, halfPenWidth, halfPenWidth);
2020-05-05 07:44:20 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-07-31 11:32:23 +02:00
auto VPGraphicsPiece::shape() const -> QPainterPath
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
2021-08-09 14:09:10 +02:00
QPen pen(Qt::black, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
2020-05-05 07:44:20 +02:00
painter->setPen(pen);
2021-08-09 14:09:10 +02:00
PaintPiece(painter);
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
{
//perform the default behaviour
2021-07-31 15:00:32 +02:00
QGraphicsObject::mousePressEvent(event);
2020-05-05 07:44:20 +02:00
2020-11-22 12:45:38 +01:00
// change the cursor when clicking the left button
if((event->button() == Qt::LeftButton))
{
2021-08-09 14:09:10 +02:00
setCursor(Qt::ClosedHandCursor);
2020-05-09 14:45:36 +02:00
2021-08-09 14:09:10 +02:00
m_moveStartPoint = event->pos();
emit HideTransformationHandles(true);
2020-05-09 14:45:36 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-09 14:09:10 +02:00
void VPGraphicsPiece::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
2020-05-09 14:45:36 +02:00
{
2021-08-09 14:09:10 +02:00
QGraphicsObject::mouseMoveEvent(event);
2020-05-09 14:45:36 +02:00
2021-08-09 14:09:10 +02:00
GroupMove(event->pos());
2020-05-09 14:45:36 +02:00
2021-08-09 14:09:10 +02:00
m_moveStartPoint = event->pos();
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
{
//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);
2021-08-09 14:09:10 +02:00
GroupMove(event->pos());
emit HideTransformationHandles(false);
}
2020-05-05 07:44:20 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:50:22 +02:00
void VPGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
2021-07-31 15:00:32 +02:00
QMenu menu;
2021-07-31 15:00:32 +02:00
QList<VPSheet *> sheets = m_piece->Layout()->GetSheets();
sheets.removeAll(m_piece->Sheet());
2021-07-31 15:00:32 +02:00
QVector<QAction*> moveToActions;
2021-07-31 15:00:32 +02:00
if (not sheets.isEmpty())
{
QMenu *moveMenu = menu.addMenu(tr("Move to"));
2021-07-31 15:00:32 +02:00
for (auto *sheet : sheets)
{
QAction* moveToSheet = moveMenu->addAction(sheet->GetName());
moveToSheet->setData(QVariant::fromValue(sheet));
moveToActions.append(moveToSheet);
}
}
2021-07-31 15:00:32 +02:00
// remove from layout action
QAction *removeAction = menu.addAction(tr("Remove from Sheet"));
2021-07-31 15:00:32 +02:00
QAction *selectedAction = menu.exec(event->screenPos());
2021-07-31 15:00:32 +02:00
if (moveToActions.contains(selectedAction))
{
m_piece->SetSheet(qvariant_cast<VPSheet *>(selectedAction->data()));
emit m_piece->Layout()->PieceSheetChanged(m_piece);
}
else if (selectedAction == removeAction)
{
m_piece->SetSheet(nullptr);
emit m_piece->Layout()->PieceSheetChanged(m_piece);
}
}
2020-05-05 17:40:36 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-09 14:09:10 +02:00
void VPGraphicsPiece::PaintPiece(QPainter *painter)
2020-05-05 17:40:36 +02:00
{
2021-08-09 14:09:10 +02:00
QBrush noBrush(Qt::NoBrush);
QBrush selectionBrush(QColor(255,160,160,60));
2020-05-05 17:40:36 +02:00
2021-08-09 14:09:10 +02:00
QRectF rect = m_piece->MappedDetailBoundingRect();
QPointF p = rect.topLeft();
// initialises the seam line
QVector<QPointF> seamLinePoints = m_piece->GetMappedContourPoints();
if(!seamLinePoints.isEmpty())
{
m_seamLine = QPainterPath();
m_seamLine.moveTo(seamLinePoints.first());
for (int i = 1; i < seamLinePoints.size(); i++)
{
m_seamLine.lineTo(seamLinePoints.at(i));
}
if (painter != nullptr)
{
painter->save();
painter->setBrush(isSelected() ? selectionBrush : noBrush);
painter->drawPath(m_seamLine);
painter->restore();
}
}
// initiliases the cutting line
QVector<QPointF> cuttingLinepoints = m_piece->GetMappedSeamAllowancePoints();
if(!cuttingLinepoints.isEmpty())
{
m_cuttingLine = QPainterPath();
m_cuttingLine.moveTo(cuttingLinepoints.first());
for (int i = 1; i < cuttingLinepoints.size(); i++)
{
m_cuttingLine.lineTo(cuttingLinepoints.at(i));
}
if (painter != nullptr)
{
painter->save();
painter->setBrush(isSelected() ? selectionBrush : noBrush);
painter->drawPath(m_cuttingLine);
painter->restore();
}
}
// initialises the grainline
if(m_piece->IsGrainlineEnabled())
{
QVector<QPointF> grainLinepoints = m_piece->GetMappedGrainline();
if(!grainLinepoints.isEmpty())
{
QPainterPath grainline;
grainline.moveTo(grainLinepoints.first());
for (int i = 1; i < grainLinepoints.size(); i++)
{
grainline.lineTo(grainLinepoints.at(i));
}
if (painter != nullptr)
{
painter->save();
// here to fill the grainlines arrow. Not wanted for mvp
// later maybe if it's configurable
// painter->setBrush(blackBrush);
painter->setBrush(noBrush);
painter->drawPath(grainline);
painter->restore();
}
}
}
// initialises the internal paths
QVector<VLayoutPiecePath> internalPaths = m_piece->GetInternalPaths();
for (const auto& piecePath : internalPaths)
{
QPainterPath path = m_piece->GetMatrix().map(piecePath.GetPainterPath());
if (painter != nullptr)
{
painter->save();
painter->setPen(piecePath.PenStyle());
painter->drawPath(path);
painter->restore();
}
}
// initialises the passmarks
QVector<VLayoutPassmark> passmarks = m_piece->GetMappedPassmarks();
for(auto &passmark : passmarks)
{
QPainterPath passmarkPath;
for (auto &line : passmark.lines)
{
passmarkPath.moveTo(line.p1());
passmarkPath.lineTo(line.p2());
}
if (painter != nullptr)
{
painter->save();
painter->setBrush(noBrush);
painter->drawPath(passmarkPath);
painter->restore();
}
}
// initialises the place labels (buttons etc)
QVector<VLayoutPlaceLabel> placeLabels = m_piece->GetPlaceLabels();
for(auto &placeLabel : placeLabels)
{
QPainterPath path = VPlaceLabelItem::LabelShapePath(placeLabel.shape);
if (painter != nullptr)
{
painter->save();
painter->setBrush(noBrush);
painter->drawPath(path);
painter->restore();
}
}
// TODO : initialises the text labels
// QPointF position = m_piece->GetPatternTextPosition();
// QStringList texts = m_piece->GetPatternText();
// painter->drawText();
2020-05-05 17:40:36 +02:00
}
2020-05-09 11:13:29 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-09 14:09:10 +02:00
void VPGraphicsPiece::GroupMove(const QPointF &pos)
2020-05-09 11:13:29 +02:00
{
2021-08-09 14:09:10 +02:00
if (scene() != nullptr)
{
QList<QGraphicsItem *> list = scene()->selectedItems();
for (auto *item : list)
{
if (item->type() == UserType + static_cast<int>(PGraphicsItem::Piece))
{
auto *pieceItem = dynamic_cast<VPGraphicsPiece*>(item);
pieceItem->TranslatePiece(pos-m_moveStartPoint);
}
}
emit PiecePositionChanged();
}
2020-05-09 11:13:29 +02:00
}
2020-11-10 21:29:23 +01:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-09 14:09:10 +02:00
void VPGraphicsPiece::on_Rotate(const QPointF &center, qreal angle)
2020-11-10 21:29:23 +01:00
{
2021-08-09 14:09:10 +02:00
if (isSelected())
2020-11-10 21:29:23 +01:00
{
2021-08-09 14:09:10 +02:00
prepareGeometryChange();
m_piece->Rotate(center, angle);
PaintPiece(); // Update shapes
update();
2020-11-10 21:29:23 +01:00
}
}
2020-05-05 17:40:36 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-07-31 11:32:23 +02:00
auto VPGraphicsPiece::itemChange(GraphicsItemChange change, const QVariant &value) -> QVariant
2020-05-05 17:40:36 +02:00
{
2021-07-31 11:32:23 +02:00
if (scene() != nullptr)
{
2020-05-05 17:40:36 +02:00
if(change == ItemSelectedHasChanged)
{
2021-08-09 14:09:10 +02:00
emit PieceSelectionChanged();
m_piece->SetSelected(value.toBool());
2020-05-05 17:40:36 +02:00
}
}
return QGraphicsObject::itemChange(change, value);
}
2020-05-05 07:44:20 +02:00