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

502 lines
17 KiB
C++
Raw Normal View History

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>
#include <QKeyEvent>
2021-07-31 11:21:07 +02:00
#include <QMenu>
2020-05-05 07:44:20 +02:00
2021-08-09 14:09:10 +02:00
#include "../scene/vpgraphicssheet.h"
#include "../scene/vpgraphicspiece.h"
#include "../vptilefactory.h"
#include "../scene/vpgraphicstilegrid.h"
#include "../carousel/vpmimedatapiece.h"
#include "../layout/vplayout.h"
#include "../layout/vpsheet.h"
#include "../layout/vppiece.h"
2020-05-11 16:39:54 +02:00
#include "../vwidgets/vmaingraphicsscene.h"
#include "vptilefactory.h"
2021-08-09 14:09:10 +02:00
#include "vpgraphicspiececontrols.h"
2020-05-05 07:44:20 +02:00
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(pMainGraphicsView, "p.mainGraphicsView")
2021-08-09 14:09:10 +02:00
namespace
{
const QKeySequence restoreOriginShortcut = QKeySequence(Qt::ControlModifier + Qt::Key_Asterisk);
}
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, VPTileFactory *tileFactory, QWidget *parent) :
2020-05-11 16:39:54 +02:00
VMainGraphicsView(parent),
2021-07-31 11:21:07 +02:00
m_scene(new VMainGraphicsScene(this)),
2020-05-11 16:39:54 +02:00
m_layout(layout)
2020-05-05 07:44:20 +02:00
{
2021-07-30 13:49:38 +02:00
SCASSERT(m_layout != nullptr)
2020-05-05 07:44:20 +02:00
setScene(m_scene);
2021-08-09 14:09:10 +02:00
m_graphicsSheet = new VPGraphicsSheet(m_layout->GetFocusedSheet());
2020-05-23 14:48:31 +02:00
m_graphicsSheet->setPos(0, 0);
m_scene->addItem(m_graphicsSheet);
2020-05-05 07:44:20 +02:00
setAcceptDrops(true);
2021-08-09 14:09:10 +02:00
m_graphicsTileGrid = new VPGraphicsTileGrid(m_layout, tileFactory);
m_scene->addItem(m_graphicsTileGrid);
2021-07-30 13:49:38 +02:00
2021-08-09 14:09:10 +02:00
m_rotationControls = new VPGraphicsPieceControls(m_layout);
m_rotationControls->setVisible(false);
m_scene->addItem(m_rotationControls);
m_rotationOrigin = new VPGraphicsTransformationOrigin(m_layout);
m_rotationOrigin->setVisible(false);
m_scene->addItem(m_rotationOrigin);
connect(m_rotationControls, &VPGraphicsPieceControls::ShowOrigin,
m_rotationOrigin, &VPGraphicsTransformationOrigin::on_ShowOrigin);
connect(m_rotationControls, &VPGraphicsPieceControls::TransformationOriginChanged,
m_rotationOrigin, &VPGraphicsTransformationOrigin::SetTransformationOrigin);
2021-07-30 13:49:38 +02:00
// add the connections
connect(m_layout, &VPLayout::PieceSheetChanged, this, &VPMainGraphicsView::on_PieceSheetChanged);
2021-08-09 14:09:10 +02:00
auto *restoreOrigin = new QAction(this);
restoreOrigin->setShortcut(restoreOriginShortcut);
connect(restoreOrigin, &QAction::triggered, this, &VPMainGraphicsView::RestoreOrigin);
this->addAction(restoreOrigin);
auto *rotateByPlus15 = new QAction(this);
rotateByPlus15->setShortcut(QKeySequence(Qt::Key_BracketLeft));
connect(rotateByPlus15, &QAction::triggered, this, &VPMainGraphicsView::RotatePiecesByPlus15);
this->addAction(rotateByPlus15);
auto *rotateByMinus15 = new QAction(this);
rotateByMinus15->setShortcut(QKeySequence(Qt::Key_BracketRight));
connect(rotateByMinus15, &QAction::triggered, this, &VPMainGraphicsView::RotatePiecesByMinus15);
this->addAction(rotateByMinus15);
auto *rotateByPlus90 = new QAction(this);
rotateByPlus90->setShortcut(QKeySequence(Qt::ControlModifier + Qt::Key_BracketLeft));
connect(rotateByPlus90, &QAction::triggered, this, &VPMainGraphicsView::RotatePiecesByPlus90);
this->addAction(rotateByPlus90);
auto *rotateByMinus90 = new QAction(this);
rotateByMinus90->setShortcut(QKeySequence(Qt::ControlModifier + Qt::Key_BracketRight));
connect(rotateByMinus90, &QAction::triggered, this, &VPMainGraphicsView::RotatePiecesByMinus90);
this->addAction(rotateByMinus90);
auto *rotateByPlus1 = new QAction(this);
rotateByPlus1->setShortcut(QKeySequence(Qt::AltModifier + Qt::Key_BracketLeft));
connect(rotateByPlus1, &QAction::triggered, this, &VPMainGraphicsView::RotatePiecesByPlus1);
this->addAction(rotateByPlus1);
auto *rotateByMinus1 = new QAction(this);
rotateByMinus1->setShortcut(QKeySequence(Qt::AltModifier + Qt::Key_BracketRight));
connect(rotateByMinus1, &QAction::triggered, this, &VPMainGraphicsView::RotatePiecesByMinus1);
this->addAction(rotateByMinus1);
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_graphicsTileGrid->update();
2020-05-05 07:44:20 +02:00
m_scene->update();
}
2021-07-31 11:21:07 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RefreshPieces()
{
qDeleteAll(m_graphicsPieces);
m_graphicsPieces.clear();
VPSheet *sheet = m_layout->GetFocusedSheet();
if (sheet != nullptr)
{
QList<VPPiece *> pieces = sheet->GetPieces();
m_graphicsPieces.reserve(pieces.size());
for (auto *piece : pieces)
{
auto *graphicsPiece = new VPGraphicsPiece(piece);
m_graphicsPieces.append(graphicsPiece);
scene()->addItem(graphicsPiece);
2021-08-09 14:09:10 +02:00
ConnectPiece(graphicsPiece);
2021-07-31 11:21:07 +02:00
}
}
}
2020-11-14 10:55:57 +01:00
//---------------------------------------------------------------------------------------------------------------------
2021-07-31 11:32:23 +02:00
auto VPMainGraphicsView::GetScene() -> VMainGraphicsScene*
2020-11-14 10:55:57 +01:00
{
return m_scene;
}
2020-11-14 12:37:43 +01:00
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::PrepareForExport()
{
m_graphicsSheet->SetShowBorder(false);
m_graphicsSheet->SetShowMargin(false);
2020-11-14 17:31:34 +01:00
2021-07-29 16:11:18 +02:00
m_showGridTmp = m_layout->GetFocusedSheet()->GetLayout()->LayoutSettings().GetShowGrid();
m_layout->GetFocusedSheet()->GetLayout()->LayoutSettings().SetShowGrid(false);
2021-07-29 16:11:18 +02:00
m_showTilesTmp = m_layout->LayoutSettings().GetShowTiles();
m_layout->LayoutSettings().SetShowTiles(false);
2020-11-14 17:31:34 +01:00
2020-11-14 12:37:43 +01:00
RefreshLayout();
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::CleanAfterExport()
{
m_graphicsSheet->SetShowBorder(true);
m_graphicsSheet->SetShowMargin(true);
2020-11-14 17:31:34 +01:00
2021-07-29 16:11:18 +02:00
m_layout->GetFocusedSheet()->GetLayout()->LayoutSettings().SetShowGrid(m_showGridTmp);
2021-07-29 16:11:18 +02:00
m_layout->LayoutSettings().SetShowTiles(m_showTilesTmp);
2020-11-14 17:31:34 +01:00
2020-11-14 12:37:43 +01:00
RefreshLayout();
}
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
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();
2021-07-30 13:49:38 +02:00
if(mime->hasFormat(VPMimeDataPiece::mineFormatPiecePtr))
2020-05-05 07:44:20 +02:00
{
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();
2021-07-30 13:49:38 +02:00
if(mime->hasFormat(VPMimeDataPiece::mineFormatPiecePtr))
2020-05-05 07:44:20 +02:00
{
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()));
2021-07-30 13:49:38 +02:00
if(mime->hasFormat(VPMimeDataPiece::mineFormatPiecePtr))
2020-05-05 07:44:20 +02:00
{
2021-07-30 13:49:38 +02:00
const auto *mimePiece = qobject_cast<const VPMimeDataPiece *> (mime);
2020-05-05 07:44:20 +02:00
2020-05-23 15:42:51 +02:00
VPPiece *piece = mimePiece->GetPiecePtr();
2020-05-05 07:44:20 +02:00
if(piece != nullptr)
{
qCDebug(pMainGraphicsView(), "element dropped, %s", qUtf8Printable(piece->GetName()));
event->acceptProposedAction();
2021-08-09 14:09:10 +02:00
piece->ClearTransformations();
piece->SetPosition(mapToScene(event->pos()));
2020-05-23 15:29:57 +02:00
// change the piecelist of the piece
2021-07-29 16:11:18 +02:00
piece->SetSheet(m_layout->GetFocusedSheet());
2021-07-30 13:49:38 +02:00
auto *graphicsPiece = new VPGraphicsPiece(piece);
m_graphicsPieces.append(graphicsPiece);
scene()->addItem(graphicsPiece);
2021-08-09 14:09:10 +02:00
ConnectPiece(graphicsPiece);
2021-07-30 13:49:38 +02:00
event->acceptProposedAction();
}
}
}
2020-05-05 07:44:20 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:36:46 +02:00
void VPMainGraphicsView::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete)
{
2021-08-09 14:24:36 +02:00
for(auto *graphicsPiece : m_graphicsPieces)
{
2020-05-23 15:42:51 +02:00
VPPiece *piece = graphicsPiece->GetPiece();
2020-05-05 17:40:36 +02:00
2021-08-09 14:09:10 +02:00
if(piece->IsSelected())
{
2021-08-09 14:09:10 +02:00
piece->SetSelected(false);
2021-07-29 16:11:18 +02:00
piece->SetSheet(nullptr);
2021-08-09 14:24:36 +02:00
m_graphicsPieces.removeAll(graphicsPiece);
delete graphicsPiece;
2020-11-19 15:08:45 +01:00
}
}
}
2021-08-09 14:24:36 +02:00
else if (event->key() == Qt::Key_Left)
{
if((event->modifiers() & Qt::ShiftModifier) != 0U)
{
TranslatePiecesOn(-10, 0);
}
else
{
TranslatePiecesOn(-1, 0);
}
}
else if (event->key() == Qt::Key_Right)
{
if((event->modifiers() & Qt::ShiftModifier) != 0U)
{
TranslatePiecesOn(10, 0);
}
else
{
TranslatePiecesOn(1, 0);
}
}
else if (event->key() == Qt::Key_Up)
{
if((event->modifiers() & Qt::ShiftModifier) != 0U)
{
TranslatePiecesOn(0, -10);
}
else
{
TranslatePiecesOn(0, 1);
}
}
else if (event->key() == Qt::Key_Down)
{
if((event->modifiers() & Qt::ShiftModifier) != 0U)
{
TranslatePiecesOn(0, 10);
}
else
{
TranslatePiecesOn(0, 1);
}
}
}
2021-07-30 13:49:38 +02:00
2021-07-31 11:21:07 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::contextMenuEvent(QContextMenuEvent *event)
{
2021-07-31 15:00:32 +02:00
QGraphicsItem *item = itemAt(event->pos());
if (item != nullptr && item->type() == VPGraphicsPiece::Type)
{
VMainGraphicsView::contextMenuEvent(event);
return;
}
2021-07-31 11:21:07 +02:00
QMenu menu;
VPSheet *sheet = m_layout->GetFocusedSheet();
2021-08-09 14:09:10 +02:00
QAction *restoreOriginAction = menu.addAction(tr("Restore transformation origin"));
restoreOriginAction->setShortcut(restoreOriginShortcut);
restoreOriginAction->setEnabled(sheet != nullptr && sheet->TransformationOrigin().custom);
2021-07-31 11:21:07 +02:00
QAction *removeSheetAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Remove sheet"));
2021-08-09 14:09:10 +02:00
removeSheetAction->setEnabled(sheet != nullptr && m_layout->GetSheets().size() > 1);
2021-07-31 11:21:07 +02:00
QAction *selectedAction = menu.exec(event->globalPos());
if (selectedAction == removeSheetAction)
{
if (sheet != nullptr)
{
sheet->SetVisible(false);
QList<VPPiece *> pieces = sheet->GetPieces();
for (auto *piece : pieces)
{
piece->SetSheet(nullptr);
}
}
m_layout->SetFocusedSheet(nullptr);
emit on_SheetRemoved();
RefreshPieces();
}
2021-08-09 14:09:10 +02:00
else if (selectedAction == restoreOriginAction)
{
RestoreOrigin();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RestoreOrigin() const
{
VPSheet *sheet = m_layout->GetFocusedSheet();
if (sheet != nullptr)
{
VPTransformationOrigon origin = sheet->TransformationOrigin();
origin.custom = false;
sheet->SetTransformationOrigin(origin);
m_rotationControls->on_UpdateControls();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RotatePiecesByPlus15() const
{
RotatePiecesByAngle(15);
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RotatePiecesByMinus15() const
{
RotatePiecesByAngle(-15);
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RotatePiecesByPlus90() const
{
RotatePiecesByAngle(90);
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RotatePiecesByMinus90() const
{
RotatePiecesByAngle(-90);
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RotatePiecesByPlus1() const
{
RotatePiecesByAngle(1);
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RotatePiecesByMinus1() const
{
RotatePiecesByAngle(-1);
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::ConnectPiece(VPGraphicsPiece *piece)
{
SCASSERT(piece != nullptr)
connect(piece, &VPGraphicsPiece::PieceSelectionChanged,
m_rotationControls, &VPGraphicsPieceControls::on_UpdateControls);
connect(piece, &VPGraphicsPiece::PiecePositionChanged,
m_rotationControls, &VPGraphicsPieceControls::on_UpdateControls);
connect(m_rotationControls, &VPGraphicsPieceControls::Rotate, piece, &VPGraphicsPiece::on_Rotate);
connect(piece, &VPGraphicsPiece::HideTransformationHandles,
m_rotationControls, &VPGraphicsPieceControls::on_HideHandles);
connect(piece, &VPGraphicsPiece::HideTransformationHandles,
m_rotationOrigin, &VPGraphicsTransformationOrigin::on_HideHandles);
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RotatePiecesByAngle(qreal angle) const
{
VPSheet *sheet = m_layout->GetFocusedSheet();
if (sheet == nullptr)
{
return;
}
VPTransformationOrigon origin = sheet->TransformationOrigin();
2021-08-09 14:24:36 +02:00
for(auto *graphicsPiece : m_graphicsPieces)
{
if (graphicsPiece->isSelected())
{
graphicsPiece->on_Rotate(origin.origin, angle);
m_rotationControls->on_UpdateControls();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::TranslatePiecesOn(qreal dx, qreal dy) const
{
for(auto *graphicsPiece : m_graphicsPieces)
2021-08-09 14:09:10 +02:00
{
2021-08-09 14:24:36 +02:00
if (graphicsPiece->isSelected())
2021-08-09 14:09:10 +02:00
{
2021-08-09 14:24:36 +02:00
graphicsPiece->TranslatePiece(dx, dy);
m_rotationControls->on_UpdateControls();
2021-08-09 14:09:10 +02:00
}
}
2021-07-31 11:21:07 +02:00
}
2021-07-30 13:49:38 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::on_PieceSheetChanged(VPPiece *piece)
{
VPGraphicsPiece *_graphicsPiece = nullptr;
for(auto *graphicPiece : m_graphicsPieces)
{
if(graphicPiece->GetPiece() == piece)
{
_graphicsPiece = graphicPiece;
}
}
2021-07-31 15:00:32 +02:00
if (piece->Sheet() == nullptr || piece->Sheet() == m_layout->GetTrashSheet() ||
piece->Sheet() != m_layout->GetFocusedSheet()) // remove
2021-07-30 13:49:38 +02:00
{
if (_graphicsPiece != nullptr)
{
scene()->removeItem(_graphicsPiece);
m_graphicsPieces.removeAll(_graphicsPiece);
2021-07-31 15:00:32 +02:00
delete _graphicsPiece;
2021-07-30 13:49:38 +02:00
}
}
else // add
{
if(_graphicsPiece == nullptr)
{
2021-08-09 14:09:10 +02:00
piece->ClearTransformations();
2021-07-30 13:49:38 +02:00
_graphicsPiece = new VPGraphicsPiece(piece);
m_graphicsPieces.append(_graphicsPiece);
2021-08-09 14:09:10 +02:00
ConnectPiece(_graphicsPiece);
2021-07-30 13:49:38 +02:00
}
scene()->addItem(_graphicsPiece);
}
}