From 0944ea968aed3f14a93a62073818f5ce9d2d98f5 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 30 Aug 2021 20:00:10 +0300 Subject: [PATCH] Validate a piece when import raw layout or read the layout format. --- src/app/puzzle/layout/vppiece.cpp | 26 +++++++++++++++ src/app/puzzle/layout/vppiece.h | 2 ++ src/app/puzzle/scene/vpgraphicspiece.cpp | 2 +- src/app/puzzle/vpmainwindow.cpp | 39 ++++++++--------------- src/app/puzzle/vpmainwindow.h | 6 ---- src/app/puzzle/xml/vplayoutfilereader.cpp | 6 ++++ 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/app/puzzle/layout/vppiece.cpp b/src/app/puzzle/layout/vppiece.cpp index ba0d21475..0e90e77e7 100644 --- a/src/app/puzzle/layout/vppiece.cpp +++ b/src/app/puzzle/layout/vppiece.cpp @@ -428,3 +428,29 @@ auto VPPiece::PathsSuperposition(const QVector &path1, const QVector bool +{ + if (not IsHideMainPath() && GetContourPoints().isEmpty()) + { + return false; + } + + if (IsSeamAllowance() && IsSeamAllowanceBuiltIn() && GetContourPoints().isEmpty()) + { + return false; + } + + if (IsSeamAllowance() && not IsSeamAllowanceBuiltIn() && GetSeamAllowancePoints().isEmpty()) + { + return false; + } + + if (IsGrainlineEnabled() && GetGrainline().isEmpty()) + { + return false; + } + + return true; +} diff --git a/src/app/puzzle/layout/vppiece.h b/src/app/puzzle/layout/vppiece.h index 4541ed210..35a41d12b 100644 --- a/src/app/puzzle/layout/vppiece.h +++ b/src/app/puzzle/layout/vppiece.h @@ -116,6 +116,8 @@ public: static auto PathsSuperposition(const QVector &path1, const QVector &path2) -> bool; + auto IsValid() const -> bool; + private: Q_DISABLE_COPY(VPPiece) diff --git a/src/app/puzzle/scene/vpgraphicspiece.cpp b/src/app/puzzle/scene/vpgraphicspiece.cpp index 8767cf4e1..2571b0522 100644 --- a/src/app/puzzle/scene/vpgraphicspiece.cpp +++ b/src/app/puzzle/scene/vpgraphicspiece.cpp @@ -582,7 +582,7 @@ void VPGraphicsPiece::GroupMove(const QPointF &pos) } //--------------------------------------------------------------------------------------------------------------------- -QColor VPGraphicsPiece::PieceColor() const +auto VPGraphicsPiece::PieceColor() const -> QColor { VPPiecePtr piece = m_piece.toStrongRef(); if (piece.isNull()) diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 4f521e894..48302b807 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -371,15 +371,20 @@ void VPMainWindow::ImportRawLayouts(const QStringList &rawLayouts) { for (const auto& rawPiece : data.pieces) { - // TODO / FIXME: make a few tests, on the data to check for validity. If not - // - // If seam allowance enabled, but the path is empty — invalid. - // If seam line path not hidden, but the path is empty — invalid. - // If seam allowance is built-in, but the seam line path is empty — invalid. - - // TODO for feature "Update piece" : CreateOrUpdate() function indstead of CreatePiece() - VPPiecePtr piece(CreatePiece(rawPiece)); + VPPiecePtr piece(new VPPiece(rawPiece)); + + if (not piece->IsValid()) + { + qCCritical(pWindow) << qPrintable(tr("Piece %1 invalid.").arg(piece->GetName())); + + if (m_cmd != nullptr && not m_cmd->IsGuiEnabled()) + { + QGuiApplication::exit(V_EX_DATAERR); + return; + } + } + piece->SetSheet(nullptr); // just in case VPLayout::AddPiece(m_layout, piece); } @@ -408,24 +413,6 @@ void VPMainWindow::InitZoom() } } -//--------------------------------------------------------------------------------------------------------------------- -VPPiece* VPMainWindow::CreatePiece(const VLayoutPiece &rawPiece) -{ - auto *piece = new VPPiece(rawPiece); - - - // cutting line : GetMappedSeamAllowancePoints(); - // seamline : GetMappedContourPoints(); - - // rawPiece.IsGrainlineEnabled() , GrainlineAngle , GetGrainline - - - // TODO : set all the information we need for the piece! - - - return piece; -} - //--------------------------------------------------------------------------------------------------------------------- void VPMainWindow::SetupMenu() { diff --git a/src/app/puzzle/vpmainwindow.h b/src/app/puzzle/vpmainwindow.h index 730bd3634..d1f29de1e 100644 --- a/src/app/puzzle/vpmainwindow.h +++ b/src/app/puzzle/vpmainwindow.h @@ -324,12 +324,6 @@ private: Unit m_oldPieceTranslationUnit{Unit::Mm}; Unit m_oldLayoutUnit{Unit::Mm}; - /** - * @brief CreatePiece creates a piece from the given VLayoutPiece data - * @param rawPiece the raw piece data - */ - Q_REQUIRED_RESULT VPPiece* CreatePiece(const VLayoutPiece &rawPiece); - /** * @brief InitMenuBar Inits the menu bar (File, Edit, Help ...) */ diff --git a/src/app/puzzle/xml/vplayoutfilereader.cpp b/src/app/puzzle/xml/vplayoutfilereader.cpp index 6406e840e..a6507066a 100644 --- a/src/app/puzzle/xml/vplayoutfilereader.cpp +++ b/src/app/puzzle/xml/vplayoutfilereader.cpp @@ -410,6 +410,12 @@ void VPLayoutFileReader::ReadPieces(const VPLayoutPtr &layout, const VPSheetPtr { VPPiecePtr piece(new VPPiece()); ReadPiece(piece); + + if (not piece->IsValid()) + { + throw VException(tr("Piece %1 invalid.").arg(piece->GetName())); + } + piece->SetSheet(sheet); VPLayout::AddPiece(layout, piece); }