From 087d0855dd9c5d8b234c18e4d5aabc3452530060 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 19 Aug 2021 16:24:43 +0300 Subject: [PATCH] Undo/Redo remove sheet. --- src/app/puzzle/puzzle.pri | 2 + src/app/puzzle/scene/vpmaingraphicsview.cpp | 18 +-- src/app/puzzle/scene/vpmaingraphicsview.h | 3 - .../puzzle/undocommands/vpundoremovesheet.cpp | 120 ++++++++++++++++++ .../puzzle/undocommands/vpundoremovesheet.h | 52 ++++++++ src/app/puzzle/vpmainwindow.cpp | 1 - 6 files changed, 176 insertions(+), 20 deletions(-) create mode 100644 src/app/puzzle/undocommands/vpundoremovesheet.cpp create mode 100644 src/app/puzzle/undocommands/vpundoremovesheet.h diff --git a/src/app/puzzle/puzzle.pri b/src/app/puzzle/puzzle.pri index 5d262fc5e..2f605734f 100644 --- a/src/app/puzzle/puzzle.pri +++ b/src/app/puzzle/puzzle.pri @@ -13,6 +13,7 @@ SOURCES += \ $$PWD/undocommands/vpundooriginmove.cpp \ $$PWD/undocommands/vpundopiecemove.cpp \ $$PWD/undocommands/vpundopiecerotate.cpp \ + $$PWD/undocommands/vpundoremovesheet.cpp \ $$PWD/vpapplication.cpp \ $$PWD/carousel/vpcarrousel.cpp \ $$PWD/carousel/vpcarrouselpiece.cpp \ @@ -53,6 +54,7 @@ HEADERS += \ $$PWD/undocommands/vpundooriginmove.h \ $$PWD/undocommands/vpundopiecemove.h \ $$PWD/undocommands/vpundopiecerotate.h \ + $$PWD/undocommands/vpundoremovesheet.h \ $$PWD/vpapplication.h \ $$PWD/carousel/vpcarrousel.h \ $$PWD/carousel/vpcarrouselpiece.h \ diff --git a/src/app/puzzle/scene/vpmaingraphicsview.cpp b/src/app/puzzle/scene/vpmaingraphicsview.cpp index 75e9a7f41..19267a8df 100644 --- a/src/app/puzzle/scene/vpmaingraphicsview.cpp +++ b/src/app/puzzle/scene/vpmaingraphicsview.cpp @@ -49,6 +49,7 @@ #include "../undocommands/vpundopiecerotate.h" #include "../undocommands/vpundooriginmove.h" #include "../undocommands/vpundomovepieceonsheet.h" +#include "../undocommands/vpundoremovesheet.h" #include @@ -401,22 +402,7 @@ void VPMainGraphicsView::contextMenuEvent(QContextMenuEvent *event) QAction *selectedAction = menu.exec(event->globalPos()); if (selectedAction == removeSheetAction) { - if (sheet != nullptr) - { - sheet->SetVisible(false); - - QList pieces = sheet->GetPieces(); - for (const auto &piece : pieces) - { - if (not piece.isNull()) - { - piece->SetSheet(nullptr); - } - } - } - - layout->SetFocusedSheet(VPSheetPtr()); - emit on_SheetRemoved(); + layout->UndoStack()->push(new VPUndoRemoveSheet(sheet)); } else if (selectedAction == restoreOriginAction) { diff --git a/src/app/puzzle/scene/vpmaingraphicsview.h b/src/app/puzzle/scene/vpmaingraphicsview.h index f3b883104..8033fdc2f 100644 --- a/src/app/puzzle/scene/vpmaingraphicsview.h +++ b/src/app/puzzle/scene/vpmaingraphicsview.h @@ -70,9 +70,6 @@ public: */ void CleanAfterExport(); -signals: - void on_SheetRemoved(); - public slots: /** * @brief on_PieceSheetChanged The slot is called when the given piece was moved from the given piece list to diff --git a/src/app/puzzle/undocommands/vpundoremovesheet.cpp b/src/app/puzzle/undocommands/vpundoremovesheet.cpp new file mode 100644 index 000000000..a06d3e56e --- /dev/null +++ b/src/app/puzzle/undocommands/vpundoremovesheet.cpp @@ -0,0 +1,120 @@ +/************************************************************************ + ** + ** @file vpundoremovesheet.cpp + ** @author Roman Telezhynskyi + ** @date 19 8, 2021 + ** + ** @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) 2021 Valentina project + ** 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 . + ** + *************************************************************************/ +#include "vpundoremovesheet.h" +#include "../vmisc/def.h" +#include "../layout/vpsheet.h" +#include "../layout/vppiece.h" +#include "../layout/vplayout.h" + +//--------------------------------------------------------------------------------------------------------------------- +VPUndoRemoveSheet::VPUndoRemoveSheet(const VPSheetPtr &sheet, QUndoCommand *parent) + : VPUndoCommand(false, parent), + m_sheet(sheet) +{ + SCASSERT(not sheet.isNull()) + + QList pieces = sheet->GetPieces(); + for (auto piece : pieces) + { + m_pieces.append(piece); + } + + setText(tr("add sheet")); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPUndoRemoveSheet::undo() +{ + VPSheetPtr sheet = m_sheet.toStrongRef(); + if (sheet.isNull()) + { + return; + } + + if (not sheet.isNull()) + { + sheet->SetVisible(true); + + for (const auto &piece : m_pieces) + { + VPPiecePtr p = piece.toStrongRef(); + if (not p.isNull()) + { + p->SetSheet(sheet); + } + } + + VPLayoutPtr layout = sheet->GetLayout(); + if (layout.isNull()) + { + return; + } + + emit layout->SheetListChanged(); + layout->SetFocusedSheet(sheet); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPUndoRemoveSheet::redo() +{ + VPSheetPtr sheet = m_sheet.toStrongRef(); + if (sheet.isNull()) + { + return; + } + + if (not sheet.isNull()) + { + sheet->SetVisible(false); + + for (const auto &piece : m_pieces) + { + VPPiecePtr p = piece.toStrongRef(); + if (not p.isNull()) + { + p->SetSheet(VPSheetPtr()); + } + } + } + + VPLayoutPtr layout = sheet->GetLayout(); + if (layout.isNull()) + { + return; + } + + emit layout->SheetListChanged(); + layout->SetFocusedSheet(VPSheetPtr()); +} + +//--------------------------------------------------------------------------------------------------------------------- +int VPUndoRemoveSheet::id() const +{ + return static_cast(ML::UndoCommand::RemoveSheet); +} diff --git a/src/app/puzzle/undocommands/vpundoremovesheet.h b/src/app/puzzle/undocommands/vpundoremovesheet.h new file mode 100644 index 000000000..e3fea07b0 --- /dev/null +++ b/src/app/puzzle/undocommands/vpundoremovesheet.h @@ -0,0 +1,52 @@ +/************************************************************************ + ** + ** @file vpundoremovesheet.h + ** @author Roman Telezhynskyi + ** @date 19 8, 2021 + ** + ** @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) 2021 Valentina project + ** 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 . + ** + *************************************************************************/ +#ifndef VPUNDOREMOVESHEET_H +#define VPUNDOREMOVESHEET_H + +#include "vpundocommand.h" + +#include "../layout/layoutdef.h" + +class VPUndoRemoveSheet : public VPUndoCommand +{ +public: + explicit VPUndoRemoveSheet(const VPSheetPtr &sheet, QUndoCommand *parent = nullptr); + virtual ~VPUndoRemoveSheet()=default; + + virtual void undo() override; + virtual void redo() override; + virtual auto id() const -> int override; + +private: + Q_DISABLE_COPY(VPUndoRemoveSheet) + + QList m_pieces{}; + VPSheetWeakPtr m_sheet; +}; + +#endif // VPUNDOREMOVESHEET_H diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 963a710ca..8b437f0dc 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -784,7 +784,6 @@ void VPMainWindow::InitMainGraphics() connect(m_graphicsView, &VPMainGraphicsView::ScaleChanged, this, &VPMainWindow::on_ScaleChanged); connect(m_graphicsView->GetScene(), &VMainGraphicsScene::mouseMove, this, &VPMainWindow::on_MouseMoved); - connect(m_graphicsView, &VPMainGraphicsView::on_SheetRemoved, m_carrousel, &VPCarrousel::Refresh); connect(m_layout.get(), &VPLayout::PieceSheetChanged, m_carrousel, &VPCarrousel::Refresh); }