2020-05-05 07:44:20 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
2020-05-23 15:36:46 +02:00
|
|
|
** @file vpmaingraphicsview.cpp
|
2020-05-05 07:44:20 +02:00
|
|
|
** @author Ronan Le Tiec
|
|
|
|
** @date 3 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 15:36:46 +02:00
|
|
|
#include "vpmaingraphicsview.h"
|
2020-05-05 07:44:20 +02:00
|
|
|
|
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QMimeData>
|
2020-05-08 23:49:41 +02:00
|
|
|
#include <QKeyEvent>
|
2020-05-05 07:44:20 +02:00
|
|
|
|
|
|
|
#include "vpuzzlemimedatapiece.h"
|
2020-05-23 15:29:57 +02:00
|
|
|
#include "vppiecelist.h"
|
2020-05-11 16:39:54 +02:00
|
|
|
#include "../vwidgets/vmaingraphicsscene.h"
|
2020-05-05 07:44:20 +02:00
|
|
|
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(pMainGraphicsView, "p.mainGraphicsView")
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, QWidget *parent) :
|
2020-05-11 16:39:54 +02:00
|
|
|
VMainGraphicsView(parent),
|
|
|
|
m_layout(layout)
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
2020-05-11 16:39:54 +02:00
|
|
|
m_scene = new VMainGraphicsScene(this);
|
2020-05-05 07:44:20 +02:00
|
|
|
setScene(m_scene);
|
|
|
|
|
2020-05-23 14:48:31 +02:00
|
|
|
m_graphicsSheet = new VPGraphicsSheet(layout);
|
|
|
|
m_graphicsSheet->setPos(0, 0);
|
|
|
|
m_scene->addItem(m_graphicsSheet);
|
2020-05-05 07:44:20 +02:00
|
|
|
|
|
|
|
setAcceptDrops(true);
|
2020-05-08 23:49:41 +02:00
|
|
|
|
|
|
|
// add the connections
|
2020-05-23 15:36:46 +02:00
|
|
|
connect(m_layout, &VPLayout::PieceMovedToPieceList, this, &VPMainGraphicsView::on_PieceMovedToPieceList);
|
2020-05-11 16:39:54 +02:00
|
|
|
connect(m_scene, &VMainGraphicsScene::selectionChanged, this,
|
2020-05-23 15:36:46 +02:00
|
|
|
&VPMainGraphicsView::on_SceneSelectionChanged);
|
2020-05-05 07:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
void VPMainGraphicsView::RefreshLayout()
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
|
|
|
// FIXME: Is that the way to go?
|
|
|
|
|
2020-05-23 14:48:31 +02:00
|
|
|
m_graphicsSheet->update();
|
2020-05-05 07:44:20 +02:00
|
|
|
|
|
|
|
m_scene->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
void VPMainGraphicsView::dragEnterEvent(QDragEnterEvent *event)
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
|
|
|
const QMimeData *mime = event->mimeData();
|
|
|
|
|
|
|
|
if(mime->objectName() == "piecePointer")
|
|
|
|
{
|
|
|
|
qCDebug(pMainGraphicsView(), "drag enter");
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
void VPMainGraphicsView::dragMoveEvent(QDragMoveEvent *event)
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
|
|
|
const QMimeData *mime = event->mimeData();
|
|
|
|
|
|
|
|
if(mime->objectName() == "piecePointer")
|
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
void VPMainGraphicsView::dragLeaveEvent(QDragLeaveEvent *event)
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
void VPMainGraphicsView::dropEvent(QDropEvent *event)
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
|
|
|
const QMimeData *mime = event->mimeData();
|
|
|
|
|
|
|
|
qCDebug(pMainGraphicsView(), "drop enter , %s", qUtf8Printable(mime->objectName()));
|
|
|
|
|
|
|
|
if(mime->objectName() == "piecePointer")
|
|
|
|
{
|
|
|
|
const VPuzzleMimeDataPiece *mimePiece = qobject_cast<const VPuzzleMimeDataPiece *> (mime);
|
|
|
|
|
|
|
|
VPuzzlePiece *piece = mimePiece->GetPiecePtr();
|
|
|
|
if(piece != nullptr)
|
|
|
|
{
|
|
|
|
qCDebug(pMainGraphicsView(), "element dropped, %s", qUtf8Printable(piece->GetName()));
|
|
|
|
event->acceptProposedAction();
|
|
|
|
|
|
|
|
QPoint point = event->pos();
|
2020-05-08 23:49:41 +02:00
|
|
|
piece->SetPosition(mapToScene(point));
|
|
|
|
|
2020-05-23 15:29:57 +02:00
|
|
|
// change the piecelist of the piece
|
|
|
|
VPPieceList *focusedPieceList = m_layout->GetFocusedPieceList();
|
|
|
|
if(focusedPieceList != nullptr)
|
2020-05-08 23:49:41 +02:00
|
|
|
{
|
2020-05-23 15:29:57 +02:00
|
|
|
m_layout->MovePieceToPieceList(piece, focusedPieceList);
|
2020-05-08 23:49:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 07:44:20 +02:00
|
|
|
|
2020-05-08 23:49:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
void VPMainGraphicsView::keyPressEvent(QKeyEvent *event)
|
2020-05-08 23:49:41 +02:00
|
|
|
{
|
|
|
|
if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete)
|
|
|
|
{
|
2020-05-23 14:50:22 +02:00
|
|
|
QList<VPGraphicsPiece*> tmpGraphicsPieces = m_graphicsPieces;
|
2020-05-09 09:00:27 +02:00
|
|
|
|
|
|
|
for(auto graphicsPiece : tmpGraphicsPieces)
|
2020-05-08 23:49:41 +02:00
|
|
|
{
|
|
|
|
VPuzzlePiece *piece = graphicsPiece->GetPiece();
|
2020-05-05 17:40:36 +02:00
|
|
|
|
2020-05-08 23:49:41 +02:00
|
|
|
if(piece->GetIsSelected())
|
|
|
|
{
|
2020-05-09 09:00:27 +02:00
|
|
|
piece->SetIsSelected(false);
|
2020-05-23 15:29:57 +02:00
|
|
|
m_layout->MovePieceToPieceList(piece, m_layout->GetUnplacedPieceList());
|
2020-05-08 23:49:41 +02:00
|
|
|
}
|
2020-05-05 07:44:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 17:40:36 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
void VPMainGraphicsView::on_PieceMovedToPieceList(VPuzzlePiece *piece, VPPieceList *pieceListBefore, VPPieceList *pieceListAfter)
|
2020-05-05 17:40:36 +02:00
|
|
|
{
|
2020-05-23 15:29:57 +02:00
|
|
|
Q_UNUSED(pieceListBefore)
|
2020-05-06 15:05:01 +02:00
|
|
|
|
2020-05-23 14:50:22 +02:00
|
|
|
VPGraphicsPiece *_graphicsPiece = nullptr;
|
2020-05-08 23:49:41 +02:00
|
|
|
for(auto graphicPiece : m_graphicsPieces)
|
|
|
|
{
|
|
|
|
if(graphicPiece->GetPiece() == piece)
|
|
|
|
{
|
|
|
|
_graphicsPiece = graphicPiece;
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 15:05:01 +02:00
|
|
|
|
2020-05-23 15:29:57 +02:00
|
|
|
if(pieceListAfter == m_layout->GetUnplacedPieceList() && _graphicsPiece != nullptr)
|
2020-05-08 23:49:41 +02:00
|
|
|
{
|
|
|
|
scene()->removeItem(_graphicsPiece);
|
|
|
|
m_graphicsPieces.removeAll(_graphicsPiece);
|
|
|
|
}
|
2020-05-23 15:29:57 +02:00
|
|
|
else if(pieceListAfter != m_layout->GetUnplacedPieceList())
|
2020-05-08 23:49:41 +02:00
|
|
|
{
|
|
|
|
if(_graphicsPiece == nullptr)
|
|
|
|
{
|
2020-05-23 14:50:22 +02:00
|
|
|
_graphicsPiece = new VPGraphicsPiece(piece);
|
2020-05-08 23:49:41 +02:00
|
|
|
m_graphicsPieces.append(_graphicsPiece);
|
|
|
|
}
|
2020-05-06 15:05:01 +02:00
|
|
|
|
2020-05-08 23:49:41 +02:00
|
|
|
scene()->addItem(_graphicsPiece);
|
2020-05-09 15:13:00 +02:00
|
|
|
// not very clean to directly call slots
|
|
|
|
_graphicsPiece->on_PieceSelectionChanged();
|
|
|
|
_graphicsPiece->on_PiecePositionChanged();
|
|
|
|
_graphicsPiece->on_PieceRotationChanged();
|
2020-05-08 23:49:41 +02:00
|
|
|
}
|
2020-05-05 17:40:36 +02:00
|
|
|
}
|
2020-05-09 09:00:27 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 15:36:46 +02:00
|
|
|
void VPMainGraphicsView::on_SceneSelectionChanged()
|
2020-05-09 09:00:27 +02:00
|
|
|
{
|
|
|
|
// most of the selection behaviour taks place automatically
|
|
|
|
// but we need to make sure that the unplaced pieces are unselected when the scene selection has changed
|
|
|
|
// because as they are not part of the scene, they are not updated
|
|
|
|
|
2020-05-23 15:29:57 +02:00
|
|
|
m_layout->GetUnplacedPieceList()->ClearSelection();
|
2020-05-09 09:00:27 +02:00
|
|
|
}
|