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

411 lines
13 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"
2021-09-06 14:31:19 +02:00
#include "../vptilefactory.h"
2021-09-11 18:39:38 +02:00
#include "../ifc/xml/vwatermarkconverter.h"
#include "../vformat/vwatermark.h"
#include "../ifc/exception/vexception.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>
2021-09-11 18:39:38 +02:00
#include <QPixmapCache>
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));
2021-09-13 16:27:46 +02:00
sheet->SetName(tr("Sheet %1").arg(layout->GetAllSheets().size()+1));
2021-08-19 15:09:38 +02:00
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());
2021-09-11 18:39:38 +02:00
layout->LayoutSettings().SetShowWatermark(settings->GetLayoutTileShowWatermark());
2021-08-21 15:13:56 +02:00
layout->LayoutSettings().SetTilesSize(QSizeF(settings->GetLayoutTilePaperWidth(),
settings->GetLayoutTilePaperHeight()));
layout->LayoutSettings().SetIgnoreTilesMargins(settings->GetLayoutTileIgnoreMargins());
layout->LayoutSettings().SetTilesMargins(settings->GetLayoutTileMargins());
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-09-06 14:31:19 +02:00
// init the tile factory
auto *tileFactory = new VPTileFactory(layout, settings);
tileFactory->refreshTileInfos();
layout->SetTileFactory(tileFactory);
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-31 12:08:59 +02:00
if (not piece.isNull())
2021-08-18 19:33:47 +02:00
{
2021-08-31 12:08:59 +02:00
if (not m_pieces.contains(piece->GetUniqueID()))
{
m_pieces.insert(piece->GetUniqueID(), piece);
}
else
{
VPPiecePtr oldPiece = m_pieces.value(piece->GetUniqueID());
if (not oldPiece.isNull())
{
oldPiece->Update(piece);
}
}
2021-08-18 19:33:47 +02:00
}
2020-04-13 18:58:16 +02:00
}
2021-09-28 16:12:55 +02:00
//---------------------------------------------------------------------------------------------------------------------
const QUuid &VPLayout::Uuid() const
{
return m_uuid;
}
2021-09-06 14:31:19 +02:00
//---------------------------------------------------------------------------------------------------------------------
VPTileFactory *VPLayout::TileFactory() const
{
return m_tileFactory;
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayout::SetTileFactory(VPTileFactory *newTileFactory)
{
m_tileFactory = newTileFactory;
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayout::RefreshScenePieces() const
{
2022-02-09 16:49:14 +01:00
const QList<VPSheetPtr> sheets = GetSheets();
for (const auto& sheet : sheets)
2021-09-06 14:31:19 +02:00
{
if (not sheet.isNull())
{
sheet->SceneData()->RefreshPieces();
2021-09-06 14:31:19 +02:00
}
}
}
2021-09-11 18:39:38 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPLayout::WatermarkData() const -> VWatermarkData
{
VWatermarkData data;
if (not m_layoutSettings.WatermarkPath().isEmpty())
{
try
{
VWatermarkConverter converter(m_layoutSettings.WatermarkPath());
VWatermark watermark;
watermark.setXMLContent(converter.Convert());
data = watermark.GetWatermark();
}
2021-09-13 16:27:46 +02:00
catch (VException &)
2021-09-11 18:39:38 +02:00
{
data.invalidFile = true;
data.opacity = 20;
data.showImage = true;
data.path = "fake.png";
data.showText = false;
return data;
}
}
return data;
}
2021-09-13 16:27:46 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPLayout::IsSheetsUniform() const -> bool
{
QList<VPSheetPtr> sheets = GetSheets();
if (sheets.size() < 2)
{
return true;
}
2022-01-29 09:59:02 +01:00
const VPSheetPtr &sheet = ConstFirst(sheets);
2021-09-13 16:27:46 +02:00
if (sheet.isNull())
{
return false;
}
QSizeF sheetSize = sheet->GetSheetSize().toSize();
return std::all_of(sheets.begin(), sheets.end(), [sheetSize](const VPSheetPtr &sheet)
{
if (sheet.isNull())
{
return false;
}
QSize size = sheet->GetSheetSize().toSize();
return size == sheetSize || size.transposed() == sheetSize;
});
}
//---------------------------------------------------------------------------------------------------------------------
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);
2021-09-13 17:19:39 +02:00
connect(this, &VPLayout::PieceTransformationChanged, sheet.data(), &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-09-13 16:27:46 +02:00
QList<VPSheetPtr> VPLayout::GetAllSheets() const
{
2021-07-29 16:11:18 +02:00
return m_sheets;
}
2021-09-13 16:27:46 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPLayout::GetSheets() const -> QList<VPSheetPtr>
{
QList<VPSheetPtr> sheets;
sheets.reserve(m_sheets.size());
for (const auto &sheet : m_sheets)
{
if (not sheet.isNull() && sheet->IsVisible())
{
sheets.append(sheet);
}
}
return 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
{
2022-01-29 09:59:02 +01:00
m_focusedSheet = focusedSheet.isNull() ? ConstFirst(m_sheets) : 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
}