valentina/src/app/puzzle/vpiececarrouselpiece.cpp

286 lines
9.2 KiB
C++
Raw Normal View History

2020-04-26 12:09:28 +02:00
/************************************************************************
**
** @file vpiececarrouselpiece.cpp
** @author Ronan Le Tiec
** @date 25 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
** <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 "vpiececarrouselpiece.h"
2020-05-05 07:44:20 +02:00
2020-04-26 12:09:28 +02:00
#include <QLabel>
#include <QVBoxLayout>
2020-04-26 15:36:20 +02:00
#include <QGraphicsScene>
#include <QPainter>
2020-05-05 07:44:20 +02:00
#include <QDrag>
#include <QPainter>
#include <QApplication>
#include <QMenu>
2020-05-05 07:44:20 +02:00
#include "vpuzzlemimedatapiece.h"
#include "vpiececarrousellayer.h"
#include "vpiececarrousel.h"
2020-04-26 12:09:28 +02:00
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(pCarrouselPiece, "p.carrouselPiece")
2020-05-05 07:44:20 +02:00
2020-04-26 12:09:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
VPieceCarrouselPiece::VPieceCarrouselPiece(VPuzzlePiece *piece, VPieceCarrouselLayer *carrouselLayer) :
m_piece(piece),
m_carrouselLayer(carrouselLayer),
m_dragStart(QPoint())
2020-04-26 12:09:28 +02:00
{
Init();
}
//---------------------------------------------------------------------------------------------------------------------
VPieceCarrouselPiece::~VPieceCarrouselPiece()
{
2020-05-05 07:44:20 +02:00
delete m_piecePreview;
2020-04-26 12:09:28 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::Init()
{
2020-04-26 15:36:20 +02:00
// Define the structure
setFixedSize(124,128);
2020-04-26 12:09:28 +02:00
QVBoxLayout *pieceLayout = new QVBoxLayout();
pieceLayout->setMargin(0);
pieceLayout->setSpacing(0);
2020-04-26 12:09:28 +02:00
setLayout(pieceLayout);
setStyleSheet("background-color:white; border: 2px solid transparent;");
2020-04-26 15:36:20 +02:00
// define the preview of the piece
2020-05-05 07:44:20 +02:00
m_piecePreview = new VPieceCarrouselPiecePreview(this);
// m_graphicsView = new VMainGraphicsView(this);
// --> undefined reference to 'VMainGraphicsView::VMainGraphicView(QWidget*)'
2020-04-26 15:36:20 +02:00
QGraphicsScene *graphicsScene = new QGraphicsScene(this);
2020-05-05 07:44:20 +02:00
m_piecePreview->setScene(graphicsScene);
m_piecePreview->setFixedSize(120,100);
m_piecePreview->setStyleSheet("border: 4px solid transparent;");
m_piecePreview->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
2020-04-26 15:36:20 +02:00
// define the label
2020-04-26 12:09:28 +02:00
m_label = new QLabel();
m_label->sizePolicy();
2020-04-26 15:36:20 +02:00
m_label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
m_label->setFixedSize(120,24);
m_label->setStyleSheet("border: 0px;");
2020-05-05 07:44:20 +02:00
m_label->setMouseTracking(false);
2020-04-26 12:09:28 +02:00
2020-05-05 07:44:20 +02:00
pieceLayout->addWidget(m_piecePreview);
2020-04-26 12:09:28 +02:00
pieceLayout->addWidget(m_label);
2020-05-05 17:40:36 +02:00
// connect the signals
connect(m_piece, &VPuzzlePiece::SelectionChanged, this, &VPieceCarrouselPiece::on_PieceSelectionChanged);
2020-04-26 12:09:28 +02:00
// then refresh the data
Refresh();
}
//---------------------------------------------------------------------------------------------------------------------
2020-04-26 22:32:08 +02:00
void VPieceCarrouselPiece::CleanPreview()
{
2020-05-05 07:44:20 +02:00
m_piecePreview->fitInView(m_piecePreview->scene()->sceneRect(), Qt::KeepAspectRatio);
2020-04-26 22:32:08 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-04-26 12:09:28 +02:00
void VPieceCarrouselPiece::Refresh()
{
2020-04-26 15:36:20 +02:00
// update the graphic view / the scene
2020-05-02 12:17:06 +02:00
QVector<QPointF> points = m_piece->GetSeamLine();
if(points.isEmpty())
{
points = m_piece->GetCuttingLine();
}
2020-04-26 12:09:28 +02:00
2020-04-26 15:36:20 +02:00
QPen pen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
pen.setCosmetic(true);
QBrush noBrush(Qt::NoBrush);
QPainterPath path;
path.moveTo(points.first());
for (int i = 1; i < points.size(); ++i)
path.lineTo(points.at(i));
2020-05-05 07:44:20 +02:00
m_piecePreview->scene()->addPath(path, pen, noBrush);
2020-04-26 15:36:20 +02:00
2020-05-05 07:44:20 +02:00
m_piecePreview->fitInView(m_piecePreview->scene()->sceneRect(), Qt::KeepAspectRatio);
2020-04-26 15:36:20 +02:00
// update the label of the piece
2020-04-26 12:09:28 +02:00
QFontMetrics metrix(m_label->font());
int width = m_label->width() - 8;
QString clippedText = metrix.elidedText(m_piece->GetName(), Qt::ElideRight, width);
m_label->setText(clippedText);
2020-05-02 09:44:45 +02:00
// set the tooltip
setToolTip(m_piece->GetName());
// set the selection state correctly.
on_PieceSelectionChanged();
}
//---------------------------------------------------------------------------------------------------------------------
VPuzzlePiece * VPieceCarrouselPiece::GetPiece()
{
return m_piece;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-05 17:40:36 +02:00
void VPieceCarrouselPiece::on_PieceSelectionChanged()
{
2020-05-05 17:40:36 +02:00
if(m_piece->GetIsSelected())
{
setStyleSheet("background-color:white; border: 2px solid red;");
}
else
{
setStyleSheet("background-color:white; border: 2px solid transparent;");
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::mousePressEvent(QMouseEvent *event)
{
2020-05-05 07:44:20 +02:00
qCDebug(pCarrouselPiece, "mouse pressed");
if (event->button() == Qt::LeftButton)
{
if(!(event->modifiers() & Qt::ControlModifier))
{
m_carrouselLayer->GetCarrousel()->ClearSelection();
2020-05-05 17:40:36 +02:00
m_piece->SetIsSelected(true);
}
else
{
m_piece->SetIsSelected(!m_piece->GetIsSelected());
}
2020-05-05 07:44:20 +02:00
m_dragStart = event->pos();
}
2020-04-26 12:09:28 +02:00
}
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::mouseMoveEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
{
return;
}
if(m_piece->GetLayer() != m_piece->GetLayer()->GetLayout()->GetUnplacedPiecesLayer())
{
return;
}
2020-05-05 07:44:20 +02:00
if((event->pos() - m_dragStart).manhattanLength() < QApplication::startDragDistance())
{
return;
}
// make sure the multiple selection is removed
m_carrouselLayer->GetCarrousel()->ClearSelection();
m_piece->SetIsSelected(true);
// starts the dragging
2020-05-05 07:44:20 +02:00
QDrag *drag = new QDrag(this);
VPuzzleMimeDataPiece *mimeData = new VPuzzleMimeDataPiece();
mimeData->SetPiecePtr(m_piece);
mimeData->setObjectName("piecePointer");
// in case we would want to have the pieces original size:
//drag->setHotSpot(QPoint(0,0));
//QPixmap pixmap(m_piecePreview->sceneRect().size().toSize());
QPixmap pixmap(112,92);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
m_piecePreview->scene()->render(&painter);
drag->setPixmap(pixmap);
drag->setMimeData(mimeData);
drag->exec();
}
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::contextMenuEvent(QContextMenuEvent *event)
{
QMenu contextMenu;
VPuzzleLayer* unplacedLayer = m_piece->GetLayer()->GetLayout()->GetUnplacedPiecesLayer();
QList<VPuzzleLayer*> layers = m_piece->GetLayer()->GetLayout()->GetLayers();
// move to layer actions -- TODO : To be tested properly when we have several layers
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, &VPieceCarrouselPiece::on_ActionPieceMovedToLayer);
}
}
// remove from layout action
if(m_piece->GetLayer() != unplacedLayer)
{
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, &VPieceCarrouselPiece::on_ActionPieceMovedToLayer);
}
contextMenu.exec(event->globalPos());
}
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::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);
}
}