valentina/src/app/puzzle/layout/vplayout.cpp

292 lines
9.8 KiB
C++
Raw Normal View History

2020-04-13 18:58:16 +02:00
/************************************************************************
**
2020-05-23 15:34:11 +02:00
** @file vplayout.cpp
2020-04-13 18:58:16 +02:00
** @author Ronan Le Tiec
** @date 13 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/>.
**
*************************************************************************/
2021-07-29 16:11:18 +02:00
2020-05-23 15:34:11 +02:00
#include "vplayout.h"
2020-05-23 15:42:51 +02:00
#include "vppiece.h"
#include "vpsheet.h"
2021-08-21 15:13:56 +02:00
#include "../vpapplication.h"
2020-04-13 18:58:16 +02:00
2020-05-24 19:53:51 +02:00
#include <QLoggingCategory>
2021-08-18 19:33:47 +02:00
#include <QUndoStack>
2020-05-24 19:53:51 +02:00
Q_LOGGING_CATEGORY(pLayout, "p.layout")
2020-04-13 18:58:16 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
VPLayout::VPLayout(QUndoStack *undoStack) :
m_undoStack(undoStack)
{
SCASSERT(m_undoStack != nullptr)
}
2020-04-13 18:58:16 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-19 15:09:38 +02:00
auto VPLayout::CreateLayout(QUndoStack *undoStack) -> VPLayoutPtr
2020-04-13 18:58:16 +02:00
{
2021-08-18 19:33:47 +02:00
SCASSERT(undoStack != nullptr)
undoStack->clear();
VPLayoutPtr layout(new VPLayout(undoStack));
layout->AddTrashSheet(VPSheetPtr(new VPSheet(layout)));
2021-08-19 15:09:38 +02:00
// create a standard sheet
VPSheetPtr sheet(new VPSheet(layout));
sheet->SetName(tr("Sheet %1").arg(layout->GetSheets().size()+1));
layout->AddSheet(sheet);
layout->SetFocusedSheet(sheet);
2021-08-21 15:13:56 +02:00
VPSettings *settings = VPApplication::VApp()->PuzzleSettings();
layout->LayoutSettings().SetUnit(settings->LayoutUnit());
layout->LayoutSettings().SetShowTiles(settings->GetLayoutTileShowTiles());
layout->LayoutSettings().SetTilesSize(QSizeF(settings->GetLayoutTilePaperWidth(),
settings->GetLayoutTilePaperHeight()));
layout->LayoutSettings().SetIgnoreTilesMargins(settings->GetLayoutTileIgnoreMargins());
layout->LayoutSettings().SetTilesMargins(settings->GetLayoutTileMargins());
layout->LayoutSettings().SetIgnoreMargins(settings->GetLayoutSheetIgnoreMargins());
layout->LayoutSettings().SetSheetMargins(settings->GetLayoutSheetMargins());
layout->LayoutSettings().SetSheetSize(QSizeF(settings->GetLayoutSheetPaperWidth(),
settings->GetLayoutSheetPaperHeight()));
layout->LayoutSettings().SetWarningSuperpositionOfPieces(settings->GetLayoutWarningPiecesSuperposition());
layout->LayoutSettings().SetWarningPiecesOutOfBound(settings->GetLayoutWarningPiecesOutOfBound());
layout->LayoutSettings().SetFollowGrainline(settings->GetLayoutFollowGrainline());
layout->LayoutSettings().SetStickyEdges(settings->GetLayoutStickyEdges());
layout->LayoutSettings().SetPiecesGap(settings->GetLayoutPieceGap());
// ----- for test purposes, to be removed------------------
layout->LayoutSettings().SetTitle(QString("My Test Layout"));
layout->LayoutSettings().SetDescription(QString("Description of my Layout"));
// --------------------------------------------------------
2021-08-18 19:33:47 +02:00
return layout;
2020-04-13 18:58:16 +02:00
}
2020-05-05 17:40:36 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayout::AddPiece(const VPLayoutPtr &layout, const VPPiecePtr &piece)
2020-05-05 17:40:36 +02:00
{
2021-08-18 19:33:47 +02:00
piece->SetLayout(layout);
layout->AddPiece(piece);
2020-04-13 18:58:16 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayout::AddPiece(const VPPiecePtr &piece)
2020-04-13 18:58:16 +02:00
{
2021-08-18 19:33:47 +02:00
if ((piece != nullptr) && not m_pieces.contains(piece->GetUniqueID()))
{
m_pieces.insert(piece->GetUniqueID(), piece);
}
2020-04-13 18:58:16 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::GetPieces() const -> QList<VPPiecePtr>
{
2021-08-18 19:33:47 +02:00
return m_pieces.values();
}
2021-08-26 18:04:24 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPLayout::GetPlacedPieces() const -> QList<VPPiecePtr>
{
QList<VPPiecePtr> pieces;
pieces.reserve(m_pieces.size());
for (const auto& piece : m_pieces)
{
if (not piece->isNull() && piece->Sheet() != VPSheetPtr() && piece->Sheet() != m_trashSheet)
{
pieces.append(piece);
}
}
return pieces;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::GetUnplacedPieces() const -> QList<VPPiecePtr>
{
2021-08-18 19:33:47 +02:00
return PiecesForSheet(VPSheetPtr());
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::GetTrashedPieces() const -> QList<VPPiecePtr>
{
2021-08-18 19:33:47 +02:00
if (m_trashSheet.isNull())
{
return {};
}
return PiecesForSheet(m_trashSheet->Uuid());
2020-04-13 18:58:16 +02:00
}
2020-05-24 19:53:51 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::AddSheet(const VPSheetPtr &sheet) -> VPSheetPtr
2020-05-24 19:53:51 +02:00
{
2021-08-18 19:33:47 +02:00
if (not sheet.isNull() && GetSheet(sheet->Uuid()).isNull())
2020-05-24 19:53:51 +02:00
{
2021-07-29 16:11:18 +02:00
m_sheets.append(sheet);
connect(this, &VPLayout::PieceTransformationChanged, sheet.get(), &VPSheet::CheckPiecePositionValidity);
2020-05-24 19:53:51 +02:00
}
2021-07-29 16:11:18 +02:00
return sheet;
2020-05-24 19:53:51 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::GetSheets() -> QList<VPSheetPtr>
{
2021-07-29 16:11:18 +02:00
return m_sheets;
}
2021-07-31 11:21:07 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::GetSheet(const QUuid &uuid) -> VPSheetPtr
2021-07-31 11:21:07 +02:00
{
2021-08-18 19:33:47 +02:00
auto sheet = std::find_if(m_sheets.begin(), m_sheets.end(),
[uuid](const VPSheetPtr &sheet) { return sheet->Uuid() == uuid; });
if (sheet != m_sheets.end())
2021-07-31 11:21:07 +02:00
{
2021-08-18 19:33:47 +02:00
return *sheet;
2021-07-31 11:21:07 +02:00
}
2021-08-18 19:33:47 +02:00
return {};
2021-07-31 11:21:07 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
void VPLayout::SetFocusedSheet(const VPSheetPtr &focusedSheet)
{
2021-07-29 16:11:18 +02:00
if (m_sheets.isEmpty())
{
2021-08-18 19:33:47 +02:00
m_focusedSheet = {};
}
else
{
2021-08-18 19:33:47 +02:00
m_focusedSheet = focusedSheet.isNull() ? m_sheets.first() : focusedSheet;
}
2021-08-18 19:33:47 +02:00
emit ActiveSheetChanged(m_focusedSheet);
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::GetFocusedSheet() -> VPSheetPtr
{
return m_focusedSheet;
}
2020-11-14 15:58:42 +01:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::GetTrashSheet() -> VPSheetPtr
2020-11-14 15:58:42 +01:00
{
2021-07-29 16:11:18 +02:00
return m_trashSheet;
2020-11-14 15:58:42 +01:00
}
2020-11-15 12:30:29 +01:00
//---------------------------------------------------------------------------------------------------------------------
2021-07-29 16:11:18 +02:00
auto VPLayout::LayoutSettings() -> VPLayoutSettings &
2020-11-15 12:30:29 +01:00
{
2021-07-29 16:11:18 +02:00
return m_layoutSettings;
2020-11-15 12:30:29 +01:00
}
2020-11-14 15:58:42 +01:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPLayout::PiecesForSheet(const VPSheetPtr &sheet) const -> QList<VPPiecePtr>
2020-11-14 15:58:42 +01:00
{
2021-08-18 19:33:47 +02:00
QList<VPPiecePtr> list;
2021-07-29 16:11:18 +02:00
list.reserve(m_pieces.size());
2020-11-14 15:58:42 +01:00
2021-08-19 14:13:54 +02:00
for (const auto& piece : m_pieces)
2020-11-14 15:58:42 +01:00
{
2021-08-18 19:33:47 +02:00
if (not piece.isNull() && piece->Sheet() == sheet)
2021-07-29 16:11:18 +02:00
{
list.append(piece);
}
2020-11-14 15:58:42 +01:00
}
2021-07-29 16:11:18 +02:00
return list;
2020-11-14 15:58:42 +01:00
}
2021-08-18 19:33:47 +02:00
//---------------------------------------------------------------------------------------------------------------------
2021-08-19 14:13:54 +02:00
auto VPLayout::PiecesForSheet(const QUuid &uuid) const -> QList<VPPiecePtr>
2021-08-18 19:33:47 +02:00
{
QList<VPPiecePtr> list;
list.reserve(m_pieces.size());
2021-08-19 14:13:54 +02:00
for (const auto& piece : m_pieces)
2021-08-18 19:33:47 +02:00
{
if (not piece.isNull())
{
VPSheetPtr sheet = piece->Sheet();
if (not sheet.isNull() && sheet->Uuid() == uuid)
{
list.append(piece);
}
}
}
return list;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-19 14:13:54 +02:00
auto VPLayout::UndoStack() const -> QUndoStack *
2021-08-18 19:33:47 +02:00
{
return m_undoStack;
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayout::SetUndoStack(QUndoStack *newUndoStack)
{
m_undoStack = newUndoStack;
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayout::Clear()
{
if (m_undoStack != nullptr)
{
m_undoStack->clear();
}
m_pieces.clear();
m_trashSheet->Clear();
m_sheets.clear();
m_focusedSheet.clear();
m_layoutSettings = VPLayoutSettings();
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayout::CheckPiecesPositionValidity() const
{
for (const auto &sheet : m_sheets)
{
if (not sheet.isNull())
{
sheet->ValidateSuperpositionOfPieces();
sheet->ValidatePiecesOutOfBound();
}
}
}
2021-08-18 19:33:47 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPLayout::AddTrashSheet(const VPSheetPtr &sheet)
{
m_trashSheet = sheet;
2021-08-19 14:13:54 +02:00
m_trashSheet->SetTrashSheet(true);
2021-08-18 19:33:47 +02:00
}