2020-05-05 07:44:20 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vpuzzlegraphicspiece.cpp
|
|
|
|
** @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/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vpuzzlegraphicspiece.h"
|
|
|
|
|
|
|
|
#include <QPen>
|
|
|
|
#include <QBrush>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QCursor>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2020-05-05 17:40:36 +02:00
|
|
|
#include <QStyleOptionGraphicsItem>
|
2020-05-09 09:54:56 +02:00
|
|
|
#include <QGraphicsSceneContextMenuEvent>
|
|
|
|
#include <QMenu>
|
2020-05-05 07:44:20 +02:00
|
|
|
|
2020-05-05 17:40:36 +02:00
|
|
|
#include "vpuzzlepiece.h"
|
2020-05-09 09:54:56 +02:00
|
|
|
#include "vpuzzlelayer.h"
|
|
|
|
#include "vpuzzlelayout.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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VPuzzleGraphicsPiece::VPuzzleGraphicsPiece(VPuzzlePiece *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()),
|
2020-05-06 15:05:01 +02:00
|
|
|
m_seamLine(QPainterPath()),
|
|
|
|
m_grainline(QPainterPath())
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VPuzzleGraphicsPiece::~VPuzzleGraphicsPiece()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleGraphicsPiece::Init()
|
|
|
|
{
|
|
|
|
// set some infos
|
2020-05-05 17:40:36 +02:00
|
|
|
setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
|
2020-05-05 07:44:20 +02:00
|
|
|
setCursor(QCursor(Qt::OpenHandCursor));
|
|
|
|
|
|
|
|
// initialises the seam line
|
|
|
|
QVector<QPointF> seamLinePoints = m_piece->GetSeamLine();
|
|
|
|
m_seamLine.moveTo(seamLinePoints.first());
|
|
|
|
for (int i = 1; i < seamLinePoints.size(); ++i)
|
|
|
|
m_seamLine.lineTo(seamLinePoints.at(i));
|
|
|
|
|
|
|
|
// initiliases the cutting line
|
|
|
|
QVector<QPointF> cuttingLinepoints = m_piece->GetCuttingLine();
|
|
|
|
m_cuttingLine.moveTo(cuttingLinepoints.first());
|
|
|
|
for (int i = 1; i < cuttingLinepoints.size(); ++i)
|
|
|
|
m_cuttingLine.lineTo(cuttingLinepoints.at(i));
|
|
|
|
|
2020-05-06 15:05:01 +02:00
|
|
|
// initialises the grainline
|
|
|
|
QVector<QPointF> grainLinepoints = m_piece->GetGrainline();
|
|
|
|
m_grainline.moveTo(grainLinepoints.first());
|
|
|
|
for (int i = 1; i < grainLinepoints.size(); ++i)
|
|
|
|
m_grainline.lineTo(grainLinepoints.at(i));
|
2020-05-05 07:44:20 +02:00
|
|
|
|
2020-05-06 15:05:01 +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
|
|
|
|
connect(m_piece, &VPuzzlePiece::SelectionChanged, this, &VPuzzleGraphicsPiece::on_PieceSelectionChanged);
|
|
|
|
connect(m_piece, &VPuzzlePiece::PositionChanged, this, &VPuzzleGraphicsPiece::on_PiecePositionChanged);
|
2020-05-05 07:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 23:49:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VPuzzlePiece* VPuzzleGraphicsPiece::GetPiece()
|
|
|
|
{
|
|
|
|
return m_piece;
|
|
|
|
}
|
2020-05-05 07:44:20 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QRectF VPuzzleGraphicsPiece::boundingRect() const
|
|
|
|
{
|
|
|
|
if(!m_cuttingLine.isEmpty())
|
|
|
|
{
|
|
|
|
return m_cuttingLine.boundingRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_seamLine.boundingRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPainterPath VPuzzleGraphicsPiece::shape() const
|
|
|
|
{
|
|
|
|
if(!m_cuttingLine.isEmpty())
|
|
|
|
{
|
|
|
|
return m_cuttingLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_seamLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleGraphicsPiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
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);
|
2020-05-08 23:49:41 +02:00
|
|
|
QBrush selectionBrush(QColor(255,160,160,60));
|
2020-05-05 07:44:20 +02:00
|
|
|
|
|
|
|
painter->setPen(pen);
|
2020-05-08 23:49:41 +02:00
|
|
|
|
|
|
|
if(isSelected())
|
|
|
|
{
|
|
|
|
painter->setBrush(selectionBrush);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
painter->setBrush(noBrush);
|
|
|
|
}
|
2020-05-05 07:44:20 +02:00
|
|
|
|
2020-05-06 15:05:01 +02:00
|
|
|
// paint the cutting line
|
2020-05-05 07:44:20 +02:00
|
|
|
if(!m_cuttingLine.isEmpty())
|
|
|
|
{
|
|
|
|
painter->drawPath(m_cuttingLine);
|
2020-05-08 23:49:41 +02:00
|
|
|
painter->setBrush(noBrush);
|
2020-05-05 07:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 15:05:01 +02:00
|
|
|
// paint the seam line
|
2020-05-05 07:44:20 +02:00
|
|
|
if(!m_seamLine.isEmpty())
|
|
|
|
{
|
|
|
|
painter->drawPath(m_seamLine);
|
|
|
|
}
|
2020-05-06 15:05:01 +02:00
|
|
|
|
2020-05-08 23:49:41 +02:00
|
|
|
painter->setBrush(noBrush);
|
|
|
|
|
2020-05-06 15:05:01 +02:00
|
|
|
// paint the grainline
|
|
|
|
if(!m_grainline.isEmpty())
|
|
|
|
{
|
|
|
|
painter->drawPath(m_grainline);
|
|
|
|
}
|
2020-05-05 07:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2020-05-06 15:05:01 +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
|
2020-05-06 15:05:01 +02:00
|
|
|
if (event->button() == Qt::LeftButton)
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
2020-05-06 15:05:01 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleGraphicsPiece::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2020-05-06 15:05:01 +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
|
2020-05-06 15:05:01 +02:00
|
|
|
if (event->button() == Qt::LeftButton)
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
2020-05-06 15:05:01 +02:00
|
|
|
setCursor(Qt::OpenHandCursor);
|
|
|
|
|
|
|
|
setSelected(selectionState);
|
|
|
|
}
|
2020-05-05 07:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 09:54:56 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
QMenu contextMenu;
|
|
|
|
|
|
|
|
// move to layer actions -- TODO : To be tested properly when we have several layers
|
|
|
|
QList<VPuzzleLayer*> layers = m_piece->GetLayer()->GetLayout()->GetLayers();
|
|
|
|
layers.removeAll(m_piece->GetLayer());
|
|
|
|
|
|
|
|
if(layers.count() > 0)
|
|
|
|
{
|
|
|
|
QMenu *moveMenu = contextMenu.addMenu(tr("Move to"));
|
|
|
|
|
|
|
|
// TODO order in alphabetical order
|
|
|
|
|
|
|
|
for (auto layer : layers)
|
|
|
|
{
|
|
|
|
QAction* moveToLayer = moveMenu->addAction(layer->GetName());
|
|
|
|
QVariant data = QVariant::fromValue(layer);
|
|
|
|
moveToLayer->setData(data);
|
|
|
|
|
|
|
|
connect(moveToLayer, &QAction::triggered, this, &VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove from layout action
|
|
|
|
QAction *removeAction = contextMenu.addAction(tr("Remove from Layout"));
|
|
|
|
QVariant data = QVariant::fromValue(m_piece->GetLayer()->GetLayout()->GetUnplacedPiecesLayer());
|
|
|
|
removeAction->setData(data);
|
|
|
|
connect(removeAction, &QAction::triggered, this, &VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer);
|
|
|
|
|
|
|
|
contextMenu.exec(event->screenPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer()
|
|
|
|
{
|
|
|
|
QAction *act = qobject_cast<QAction *>(sender());
|
|
|
|
QVariant v = act->data();
|
|
|
|
VPuzzleLayer *layer = (VPuzzleLayer *) v.value<VPuzzleLayer *>();
|
|
|
|
if(layer != nullptr)
|
|
|
|
{
|
|
|
|
layer->GetLayout()->MovePieceToLayer(m_piece, layer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 07:44:20 +02:00
|
|
|
|
2020-05-05 17:40:36 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleGraphicsPiece::on_PieceSelectionChanged()
|
|
|
|
{
|
|
|
|
setSelected(m_piece->GetIsSelected());
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleGraphicsPiece::on_PiecePositionChanged()
|
|
|
|
{
|
|
|
|
setPos(m_piece->GetPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVariant VPuzzleGraphicsPiece::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
|
|
|
if (scene()) {
|
|
|
|
if(change == ItemPositionHasChanged)
|
|
|
|
{
|
|
|
|
blockSignals(true);
|
|
|
|
m_piece->SetPosition(pos());
|
|
|
|
blockSignals(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|