From 14e542b4124316ce245ba9cae67329ebc577d124 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 11 Apr 2024 18:38:43 +0300 Subject: [PATCH] Optimize calling position validations. --- src/app/puzzle/layout/vplayout.cpp | 49 +++++++++++++------ src/app/puzzle/layout/vplayout.h | 2 + src/app/puzzle/layout/vpsheet.cpp | 75 ++++++++++++++++-------------- src/app/puzzle/layout/vpsheet.h | 1 + src/app/puzzle/vpmainwindow.cpp | 37 ++++----------- 5 files changed, 86 insertions(+), 78 deletions(-) diff --git a/src/app/puzzle/layout/vplayout.cpp b/src/app/puzzle/layout/vplayout.cpp index 0dee14286..31e8d1c92 100644 --- a/src/app/puzzle/layout/vplayout.cpp +++ b/src/app/puzzle/layout/vplayout.cpp @@ -123,13 +123,14 @@ void VPLayout::AddPiece(const VPPiecePtr &piece) VPPiece::CleanPosition(piece); - if (not m_pieces.contains(piece->GetUniqueID())) + const QString uniqueId = piece->GetUniqueID(); + if (not m_pieces.contains(uniqueId)) { - m_pieces.insert(piece->GetUniqueID(), piece); + m_pieces.insert(uniqueId, piece); } else { - VPPiecePtr const oldPiece = m_pieces.value(piece->GetUniqueID()); + VPPiecePtr const oldPiece = m_pieces.value(uniqueId); if (not oldPiece.isNull()) { oldPiece->Update(piece); @@ -137,7 +138,7 @@ void VPLayout::AddPiece(const VPPiecePtr &piece) } else { - m_pieces.insert(piece->GetUniqueID(), piece); + m_pieces.insert(uniqueId, piece); } } } @@ -327,15 +328,7 @@ void VPLayout::SetFocusedSheet(const VPSheetPtr &focusedSheet) m_focusedSheet = focusedSheet.isNull() ? m_sheets.constFirst() : focusedSheet; } - if (LayoutSettings().GetWarningSuperpositionOfPieces()) - { - m_focusedSheet->ValidateSuperpositionOfPieces(); - } - - if (LayoutSettings().GetWarningPieceGapePosition()) - { - m_focusedSheet->ValidatePieceGapePosition(); - } + CheckPiecesPositionValidity(m_focusedSheet); emit ActiveSheetChanged(m_focusedSheet); } @@ -352,6 +345,12 @@ auto VPLayout::GetTrashSheet() -> VPSheetPtr return m_trashSheet; } +//--------------------------------------------------------------------------------------------------------------------- +auto VPLayout::LayoutSettings() const -> const VPLayoutSettings & +{ + return m_layoutSettings; +} + //--------------------------------------------------------------------------------------------------------------------- auto VPLayout::LayoutSettings() -> VPLayoutSettings & { @@ -428,10 +427,30 @@ void VPLayout::CheckPiecesPositionValidity() const { for (const auto &sheet : m_sheets) { - if (not sheet.isNull()) + CheckPiecesPositionValidity(sheet); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPLayout::CheckPiecesPositionValidity(const VPSheetPtr &sheet) const +{ + if (not sheet.isNull()) + { + const VPLayoutSettings &settings = LayoutSettings(); + + if (settings.GetWarningPiecesOutOfBound()) + { + sheet->ValidatePiecesOutOfBound(); + } + + if (settings.GetWarningSuperpositionOfPieces()) { sheet->ValidateSuperpositionOfPieces(); - sheet->ValidatePiecesOutOfBound(); + } + + if (settings.GetWarningPieceGapePosition()) + { + sheet->ValidatePieceGapePosition(); } } } diff --git a/src/app/puzzle/layout/vplayout.h b/src/app/puzzle/layout/vplayout.h index a4758a0f0..2478a121f 100644 --- a/src/app/puzzle/layout/vplayout.h +++ b/src/app/puzzle/layout/vplayout.h @@ -78,6 +78,7 @@ public: void AddTrashSheet(const VPSheetPtr &sheet); auto GetTrashSheet() -> VPSheetPtr; + auto LayoutSettings() const -> const VPLayoutSettings &; auto LayoutSettings() -> VPLayoutSettings &; auto PiecesForSheet(const VPSheetPtr &sheet) const -> QList; @@ -90,6 +91,7 @@ public: void Clear(); void CheckPiecesPositionValidity() const; + void CheckPiecesPositionValidity(const VPSheetPtr &sheet) const; auto TileFactory() const -> QSharedPointer; void SetTileFactory(const QSharedPointer &newTileFactory); diff --git a/src/app/puzzle/layout/vpsheet.cpp b/src/app/puzzle/layout/vpsheet.cpp index 3f68d74f5..5e0728ca3 100644 --- a/src/app/puzzle/layout/vpsheet.cpp +++ b/src/app/puzzle/layout/vpsheet.cpp @@ -515,7 +515,7 @@ void VPSheet::ValidateSuperpositionOfPieces() const for (const auto &piece : pieces) { - if (piece.isNull()) + if (piece.isNull() || piece->OutOfBound()) { continue; } @@ -557,7 +557,7 @@ void VPSheet::ValidateSuperpositionOfPieces() const } //--------------------------------------------------------------------------------------------------------------------- -void VPSheet::ValidatePieceGapePosition() const +void VPSheet::ValidatePieceGapePosition(const VPPiecePtr &piece) const { VPLayoutPtr const layout = GetLayout(); if (layout.isNull()) @@ -571,55 +571,62 @@ void VPSheet::ValidatePieceGapePosition() const return; } + if (piece.isNull() || piece->HasSuperpositionWithPieces() || piece->OutOfBound()) + { + return; + } + + const bool oldInvalidPieceGapPosition = piece->HasInvalidPieceGapPosition(); + + QVector path1; + CastTo(piece->GetMappedExternalContourPoints(), path1); + path1 = VPPiece::PrepareStickyPath(path1); + bool hasInvalidPieceGapPosition = false; + QList const pieces = GetPieces(); - for (const auto &piece : pieces) + for (const auto &p : pieces) { - if (piece.isNull()) + if (p.isNull() || piece == p) { continue; } - const bool oldInvalidPieceGapPosition = piece->HasInvalidPieceGapPosition(); + QVector path2; + CastTo(p->GetMappedExternalContourPoints(), path2); + path2 = VPPiece::PrepareStickyPath(path2); - QVector path1; - CastTo(piece->GetMappedExternalContourPoints(), path1); - path1 = VPPiece::PrepareStickyPath(path1); - bool hasInvalidPieceGapPosition = false; + QLineF const distance = VPPiece::ClosestDistance(path1, path2); - for (const auto &p : pieces) + if (distance.length() < pieceGap - accuracyPointOnLine) { - if (p.isNull() || piece == p) - { - continue; - } - - QVector path2; - CastTo(p->GetMappedExternalContourPoints(), path2); - path2 = VPPiece::PrepareStickyPath(path2); - - QLineF const distance = VPPiece::ClosestDistance(path1, path2); - - if (distance.length() < pieceGap - accuracyPointOnLine) - { - hasInvalidPieceGapPosition = true; - break; - } + hasInvalidPieceGapPosition = true; + break; } + } - piece->SetHasInvalidPieceGapPosition(hasInvalidPieceGapPosition); + piece->SetHasInvalidPieceGapPosition(hasInvalidPieceGapPosition); - if (oldInvalidPieceGapPosition != piece->HasInvalidPieceGapPosition()) + if (oldInvalidPieceGapPosition != piece->HasInvalidPieceGapPosition()) + { + VPLayoutPtr const layout = GetLayout(); + if (not layout.isNull()) { - VPLayoutPtr const layout = GetLayout(); - if (not layout.isNull()) - { - emit layout->PiecePositionValidityChanged(piece); - } + emit layout->PiecePositionValidityChanged(piece); } } } +//--------------------------------------------------------------------------------------------------------------------- +void VPSheet::ValidatePieceGapePosition() const +{ + QList const pieces = GetPieces(); + for (const auto &piece : pieces) + { + ValidatePieceGapePosition(piece); + } +} + //--------------------------------------------------------------------------------------------------------------------- void VPSheet::ValidatePieceOutOfBound(const VPPiecePtr &piece) const { @@ -770,7 +777,7 @@ void VPSheet::CheckPiecePositionValidity(const VPPiecePtr &piece) const if (layout->LayoutSettings().GetWarningPieceGapePosition()) { - ValidatePieceGapePosition(); + ValidatePieceGapePosition(piece); } } diff --git a/src/app/puzzle/layout/vpsheet.h b/src/app/puzzle/layout/vpsheet.h index 2c91c81c7..401a79af6 100644 --- a/src/app/puzzle/layout/vpsheet.h +++ b/src/app/puzzle/layout/vpsheet.h @@ -184,6 +184,7 @@ public: void SetTrashSheet(bool newTrashSheet); void ValidateSuperpositionOfPieces() const; + void ValidatePieceGapePosition(const VPPiecePtr &piece) const; void ValidatePieceGapePosition() const; void ValidatePieceOutOfBound(const VPPiecePtr &piece) const; void ValidatePiecesOutOfBound() const; diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 4c121de20..e2098b4b1 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -1112,8 +1112,7 @@ void VPMainWindow::InitMarginsData(const QString &suffix) LayoutWasSaved(false); m_layout->TileFactory()->RefreshTileInfos(); m_graphicsView->RefreshLayout(); - - sheet->ValidatePiecesOutOfBound(); + m_layout->CheckPiecesPositionValidity(sheet); } } }); @@ -2131,11 +2130,9 @@ void VPMainWindow::SheetPaperSizeChanged() { RotatePiecesToGrainline(); } - - VPSheetPtr const sheet = m_layout->GetFocusedSheet(); - if (not sheet.isNull()) + else { - sheet->ValidatePiecesOutOfBound(); + m_layout->CheckPiecesPositionValidity(m_layout->GetFocusedSheet()); } } @@ -3878,7 +3875,7 @@ void VPMainWindow::on_SheetMarginChanged() LayoutWasSaved(false); - sheet->ValidatePiecesOutOfBound(); + m_layout->CheckPiecesPositionValidity(sheet); } m_graphicsView->RefreshLayout(); @@ -4880,11 +4877,7 @@ void VPMainWindow::LayoutWarningPieceGapePosition_toggled(bool checked) LayoutWasSaved(false); if (checked) { - VPSheetPtr const sheet = m_layout->GetFocusedSheet(); - if (not sheet.isNull()) - { - sheet->ValidatePieceGapePosition(); - } + m_layout->CheckPiecesPositionValidity(m_layout->GetFocusedSheet()); } m_graphicsView->RefreshPieces(); } @@ -4899,11 +4892,7 @@ void VPMainWindow::LayoutWarningPiecesSuperposition_toggled(bool checked) LayoutWasSaved(false); if (checked) { - VPSheetPtr const sheet = m_layout->GetFocusedSheet(); - if (not sheet.isNull()) - { - sheet->ValidateSuperpositionOfPieces(); - } + m_layout->CheckPiecesPositionValidity(m_layout->GetFocusedSheet()); } m_graphicsView->RefreshPieces(); } @@ -4919,11 +4908,7 @@ void VPMainWindow::LayoutWarningPiecesOutOfBound_toggled(bool checked) if (checked) { - VPSheetPtr const sheet = m_layout->GetFocusedSheet(); - if (not sheet.isNull()) - { - sheet->ValidatePiecesOutOfBound(); - } + m_layout->CheckPiecesPositionValidity(m_layout->GetFocusedSheet()); } m_graphicsView->RefreshPieces(); } @@ -4936,13 +4921,7 @@ void VPMainWindow::LayoutCutOnFold_toggled(bool checked) { m_layout->LayoutSettings().SetCutOnFold(checked); LayoutWasSaved(false); - - VPSheetPtr const sheet = m_layout->GetFocusedSheet(); - if (not sheet.isNull()) - { - sheet->ValidatePiecesOutOfBound(); - } - + m_layout->CheckPiecesPositionValidity(m_layout->GetFocusedSheet()); m_graphicsView->RefreshLayout(); } }