From 3f73bd9d7562105aff2b89a7e439cbeb6d514bf1 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 19 Aug 2021 16:09:38 +0300 Subject: [PATCH] Undo/Redo add sheet. --- src/app/puzzle/carousel/vpcarrousel.cpp | 1 + src/app/puzzle/layout/vplayout.cpp | 9 +- src/app/puzzle/layout/vplayout.h | 1 + src/app/puzzle/puzzle.pri | 2 + src/app/puzzle/scene/vpmaingraphicsview.cpp | 1 + .../puzzle/undocommands/vpundoaddsheet.cpp | 89 +++++++++++++++++++ src/app/puzzle/undocommands/vpundoaddsheet.h | 51 +++++++++++ src/app/puzzle/undocommands/vpundocommand.h | 2 + src/app/puzzle/vpmainwindow.cpp | 31 +++---- src/app/puzzle/vpmainwindow.h | 2 - src/app/puzzle/xml/vplayoutfilewriter.cpp | 5 +- 11 files changed, 169 insertions(+), 25 deletions(-) create mode 100644 src/app/puzzle/undocommands/vpundoaddsheet.cpp create mode 100644 src/app/puzzle/undocommands/vpundoaddsheet.h diff --git a/src/app/puzzle/carousel/vpcarrousel.cpp b/src/app/puzzle/carousel/vpcarrousel.cpp index 96774bfe4..0957edaa0 100644 --- a/src/app/puzzle/carousel/vpcarrousel.cpp +++ b/src/app/puzzle/carousel/vpcarrousel.cpp @@ -57,6 +57,7 @@ VPCarrousel::VPCarrousel(const VPLayoutPtr &layout, QWidget *parent) : &VPCarrousel::on_ActivePieceListChanged); connect(layout.get(), &VPLayout::ActiveSheetChanged, this, &VPCarrousel::on_ActiveSheetChanged); + connect(layout.get(), &VPLayout::SheetListChanged, this, &VPCarrousel::Refresh); // ------ then we fill the carrousel with the layout content Refresh(); diff --git a/src/app/puzzle/layout/vplayout.cpp b/src/app/puzzle/layout/vplayout.cpp index e36af4d96..165f07fe7 100644 --- a/src/app/puzzle/layout/vplayout.cpp +++ b/src/app/puzzle/layout/vplayout.cpp @@ -43,12 +43,19 @@ VPLayout::VPLayout(QUndoStack *undoStack) : } //--------------------------------------------------------------------------------------------------------------------- -VPLayoutPtr VPLayout::CreateLayout(QUndoStack *undoStack) +auto VPLayout::CreateLayout(QUndoStack *undoStack) -> VPLayoutPtr { SCASSERT(undoStack != nullptr) undoStack->clear(); VPLayoutPtr layout(new VPLayout(undoStack)); layout->AddTrashSheet(VPSheetPtr(new VPSheet(layout))); + + // 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); + return layout; } diff --git a/src/app/puzzle/layout/vplayout.h b/src/app/puzzle/layout/vplayout.h index 03e75bd91..d565e54a5 100644 --- a/src/app/puzzle/layout/vplayout.h +++ b/src/app/puzzle/layout/vplayout.h @@ -89,6 +89,7 @@ signals: void ActiveSheetChanged(const VPSheetPtr &focusedSheet); void PieceTransformationChanged(const VPPiecePtr &piece); void TransformationOriginChanged(); + void SheetListChanged(); protected: explicit VPLayout(QUndoStack *undoStack); diff --git a/src/app/puzzle/puzzle.pri b/src/app/puzzle/puzzle.pri index 6f6d6de2e..5d262fc5e 100644 --- a/src/app/puzzle/puzzle.pri +++ b/src/app/puzzle/puzzle.pri @@ -7,6 +7,7 @@ SOURCES += \ $$PWD/dialogs/dialogpuzzlepreferences.cpp \ $$PWD/dialogs/vpdialogabout.cpp \ $$PWD/main.cpp \ + $$PWD/undocommands/vpundoaddsheet.cpp \ $$PWD/undocommands/vpundocommand.cpp \ $$PWD/undocommands/vpundomovepieceonsheet.cpp \ $$PWD/undocommands/vpundooriginmove.cpp \ @@ -46,6 +47,7 @@ HEADERS += \ $$PWD/layout/layoutdef.h \ $$PWD/scene/scenedef.h \ $$PWD/stable.h \ + $$PWD/undocommands/vpundoaddsheet.h \ $$PWD/undocommands/vpundocommand.h \ $$PWD/undocommands/vpundomovepieceonsheet.h \ $$PWD/undocommands/vpundooriginmove.h \ diff --git a/src/app/puzzle/scene/vpmaingraphicsview.cpp b/src/app/puzzle/scene/vpmaingraphicsview.cpp index 8ba4a9865..75e9a7f41 100644 --- a/src/app/puzzle/scene/vpmaingraphicsview.cpp +++ b/src/app/puzzle/scene/vpmaingraphicsview.cpp @@ -93,6 +93,7 @@ VPMainGraphicsView::VPMainGraphicsView(const VPLayoutPtr &layout, VPTileFactory // add the connections connect(layout.get(), &VPLayout::PieceSheetChanged, this, &VPMainGraphicsView::on_PieceSheetChanged); + connect(layout.get(), &VPLayout::ActiveSheetChanged, this, &VPMainGraphicsView::RefreshPieces); auto *restoreOrigin = new QAction(this); restoreOrigin->setShortcut(restoreOriginShortcut); diff --git a/src/app/puzzle/undocommands/vpundoaddsheet.cpp b/src/app/puzzle/undocommands/vpundoaddsheet.cpp new file mode 100644 index 000000000..520cc4445 --- /dev/null +++ b/src/app/puzzle/undocommands/vpundoaddsheet.cpp @@ -0,0 +1,89 @@ +/************************************************************************ + ** + ** @file vpundoaddsheet.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 "vpundoaddsheet.h" +#include "../vmisc/def.h" +#include "../layout/vpsheet.h" +#include "../layout/vplayout.h" + +//--------------------------------------------------------------------------------------------------------------------- +VPUndoAddSheet::VPUndoAddSheet(const VPSheetPtr &sheet, QUndoCommand *parent) + : VPUndoCommand(false, parent), + m_sheet(sheet) +{ + SCASSERT(not sheet.isNull()) + + setText(tr("add sheet")); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPUndoAddSheet::undo() +{ + VPSheetPtr sheet = m_sheet.toStrongRef(); + if (sheet.isNull()) + { + return; + } + + sheet->SetVisible(false); + + VPLayoutPtr layout = sheet->GetLayout(); + if (layout.isNull()) + { + return; + } + + emit layout->SheetListChanged(); + layout->SetFocusedSheet(VPSheetPtr()); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPUndoAddSheet::redo() +{ + VPSheetPtr sheet = m_sheet.toStrongRef(); + if (sheet.isNull()) + { + return; + } + + VPLayoutPtr layout = sheet->GetLayout(); + if (layout.isNull()) + { + return; + } + + sheet->SetVisible(true); + layout->AddSheet(sheet); + emit layout->SheetListChanged(); + layout->SetFocusedSheet(sheet); +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VPUndoAddSheet::id() const -> int +{ + return static_cast(ML::UndoCommand::AddSheet); +} diff --git a/src/app/puzzle/undocommands/vpundoaddsheet.h b/src/app/puzzle/undocommands/vpundoaddsheet.h new file mode 100644 index 000000000..628aeef8e --- /dev/null +++ b/src/app/puzzle/undocommands/vpundoaddsheet.h @@ -0,0 +1,51 @@ +/************************************************************************ + ** + ** @file vpundoaddsheet.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 VPUNDOADDSHEET_H +#define VPUNDOADDSHEET_H + +#include "vpundocommand.h" + +#include "../layout/layoutdef.h" + +class VPUndoAddSheet : public VPUndoCommand +{ +public: + explicit VPUndoAddSheet(const VPSheetPtr &sheet, QUndoCommand *parent = nullptr); + virtual ~VPUndoAddSheet()=default; + + virtual void undo() override; + virtual void redo() override; + virtual auto id() const -> int override; + +private: + Q_DISABLE_COPY(VPUndoAddSheet) + + VPSheetWeakPtr m_sheet; +}; + +#endif // VPUNDOADDSHEET_H diff --git a/src/app/puzzle/undocommands/vpundocommand.h b/src/app/puzzle/undocommands/vpundocommand.h index 3e8b02e1e..967e0b9e2 100644 --- a/src/app/puzzle/undocommands/vpundocommand.h +++ b/src/app/puzzle/undocommands/vpundocommand.h @@ -42,6 +42,8 @@ enum class UndoCommand: qint8 RotatePieces = 3, MoveOrigin = 4, MoveOnSheet = 5, + AddSheet = 6, + RemoveSheet = 7, }; } diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 1212b936e..963a710ca 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -48,6 +48,7 @@ #include "../vwidgets/vmaingraphicsscene.h" #include "layout/vpsheet.h" #include "dialogs/dialogpuzzlepreferences.h" +#include "undocommands/vpundoaddsheet.h" #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0) #include "../vmisc/backport/qscopeguard.h" @@ -77,20 +78,21 @@ VPMainWindow::VPMainWindow(const VPCommandLinePtr &cmd, QWidget *parent) : { ui->setupUi(this); - // create a standard sheet - AddSheet(); - // ----- for test purposes, to be removed------------------ m_layout->LayoutSettings().SetUnit(Unit::Cm); m_layout->LayoutSettings().SetWarningSuperpositionOfPieces(true); m_layout->LayoutSettings().SetTitle(QString("My Test Layout")); m_layout->LayoutSettings().SetDescription(QString("Description of my Layout")); - m_layout->LayoutSettings().SetTilesSizeConverted(21,29.7); + m_layout->LayoutSettings().SetTilesSizeConverted(21, 29.7); m_layout->LayoutSettings().SetTilesOrientation(PageOrientation::Portrait); - m_layout->LayoutSettings().SetTilesMarginsConverted(1,1,1,1); + m_layout->LayoutSettings().SetTilesMarginsConverted(1, 1, 1, 1); m_layout->LayoutSettings().SetShowTiles(true); + m_layout->LayoutSettings().SetSheetMarginsConverted(1, 1, 1, 1); + m_layout->LayoutSettings().SetSheetSizeConverted(84.1, 118.9); + m_layout->LayoutSettings().SetPiecesGapConverted(1); + // -------------------------------------------------------- // init the tile factory @@ -1119,20 +1121,6 @@ void VPMainWindow::CreateWindowMenu(QMenu *menu) } } -//--------------------------------------------------------------------------------------------------------------------- -void VPMainWindow::AddSheet() -{ - VPSheetPtr sheet(new VPSheet(m_layout)); - sheet->SetName(QObject::tr("Sheet %1").arg(m_layout->GetSheets().size()+1)); - m_layout->AddSheet(sheet); - m_layout->SetFocusedSheet(sheet); - -// // ----- for test purposes, to be removed------------------ - m_layout->LayoutSettings().SetSheetMarginsConverted(1, 1, 1, 1); - m_layout->LayoutSettings().SetSheetSizeConverted(84.1, 118.9); - m_layout->LayoutSettings().SetPiecesGapConverted(1); -} - //--------------------------------------------------------------------------------------------------------------------- auto VPMainWindow::IsLayoutReadOnly() const -> bool { @@ -1948,8 +1936,9 @@ void VPMainWindow::ToolBarStyles() //--------------------------------------------------------------------------------------------------------------------- void VPMainWindow::on_actionAddSheet_triggered() { - AddSheet(); - m_carrousel->Refresh(); + VPSheetPtr sheet(new VPSheet(m_layout)); + sheet->SetName(QObject::tr("Sheet %1").arg(m_layout->GetSheets().size()+1)); + m_layout->UndoStack()->push(new VPUndoAddSheet(sheet)); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/vpmainwindow.h b/src/app/puzzle/vpmainwindow.h index b6c89f76b..81d45aa17 100644 --- a/src/app/puzzle/vpmainwindow.h +++ b/src/app/puzzle/vpmainwindow.h @@ -562,8 +562,6 @@ private: void CreateWindowMenu(QMenu *menu); - void AddSheet(); - auto IsLayoutReadOnly() const -> bool; }; diff --git a/src/app/puzzle/xml/vplayoutfilewriter.cpp b/src/app/puzzle/xml/vplayoutfilewriter.cpp index e0f0d4c0d..aee2b5e34 100644 --- a/src/app/puzzle/xml/vplayoutfilewriter.cpp +++ b/src/app/puzzle/xml/vplayoutfilewriter.cpp @@ -194,7 +194,10 @@ void VPLayoutFileWriter::WriteSheets(const VPLayoutPtr &layout) QList sheets = layout->GetSheets(); for (const auto &sheet : sheets) { - WriteSheet(sheet); + if (not sheet.isNull() && sheet->IsVisible()) + { + WriteSheet(sheet); + } } writeEndElement(); // sheets