diff --git a/sources/editor/UndoCommand/deletepartscommand.cpp b/sources/editor/UndoCommand/deletepartscommand.cpp new file mode 100644 index 000000000..53f768956 --- /dev/null +++ b/sources/editor/UndoCommand/deletepartscommand.cpp @@ -0,0 +1,66 @@ +/* + Copyright 2006-2022 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech 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 2 of the License, or + (at your option) any later version. + + QElectroTech 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 QElectroTech. If not, see . +*/ +#include "deletepartscommand.h" +#include "../elementscene.h" + +/** + * @brief DeletePartsCommand::DeletePartsCommand + * @param scene + * @param parts + * @param parent + */ +DeletePartsCommand::DeletePartsCommand(QPointer scene, + const QVector &parts, + QUndoCommand *parent) : + QUndoCommand{parent}, + m_scene{scene}, + m_parts{parts} +{ + setText(QObject::tr("suppression", "undo caption")); + m_scene->qgiManager().manage(parts); +} + +/** + * @brief DeletePartsCommand::~DeletePartsCommand + */ +DeletePartsCommand::~DeletePartsCommand() +{ + if (m_scene) { + m_scene->qgiManager().release(m_parts); + } +} + +/** + * @brief DeletePartsCommand::undo + */ +void DeletePartsCommand::undo() +{ + if (m_scene) { + m_scene->addItems(m_parts); + } +} + +/** + * @brief DeletePartsCommand::redo + */ +void DeletePartsCommand::redo() +{ + if (m_scene) { + m_scene->removeItems(m_parts); + } +} diff --git a/sources/editor/UndoCommand/deletepartscommand.h b/sources/editor/UndoCommand/deletepartscommand.h new file mode 100644 index 000000000..dde5c45c5 --- /dev/null +++ b/sources/editor/UndoCommand/deletepartscommand.h @@ -0,0 +1,48 @@ +/* + Copyright 2006-2022 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech 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 2 of the License, or + (at your option) any later version. + + QElectroTech 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 QElectroTech. If not, see . +*/ +#ifndef DELETEPARTSCOMMAND_H +#define DELETEPARTSCOMMAND_H + +#include +#include + +class ElementScene; +class QGraphicsItem; + +/** + * @brief The DeletePartsCommand class + * Class used to remove part from an element scene. + */ +class DeletePartsCommand : public QUndoCommand +{ + public: + DeletePartsCommand(QPointer scene, const QVector &parts, QUndoCommand *parent = nullptr); + ~DeletePartsCommand() override; + private: + DeletePartsCommand(const DeletePartsCommand &); + + public: + void undo() override; + void redo() override; + + private: + QPointer m_scene; + QVector m_parts; +}; + +#endif // DELETEPARTSCOMMAND_H diff --git a/sources/editor/editorcommands.cpp b/sources/editor/editorcommands.cpp index ba7ec27c5..02f57795e 100644 --- a/sources/editor/editorcommands.cpp +++ b/sources/editor/editorcommands.cpp @@ -90,54 +90,6 @@ void ElementEditionCommand::setElementView(ElementView *view) { m_view = view; } -/*** DeletePartsCommand ***/ -/** - Constructeur - @param scene ElementScene concernee - @param parts Liste des parties supprimees - @param parent QUndoCommand parent -*/ -DeletePartsCommand::DeletePartsCommand( - ElementScene *scene, - const QList& parts, - QUndoCommand *parent -) : - ElementEditionCommand(QObject::tr("suppression", "undo caption"), scene, nullptr, parent), - deleted_parts(parts) -{ - foreach(QGraphicsItem *qgi, deleted_parts) { - m_scene -> qgiManager().manage(qgi); - } -} - -/// Destructeur : detruit egalement les parties supprimees -DeletePartsCommand::~DeletePartsCommand() -{ - foreach(QGraphicsItem *qgi, deleted_parts) { - m_scene -> qgiManager().release(qgi); - } -} - -/// Restaure les parties supprimees -void DeletePartsCommand::undo() -{ - m_scene -> blockSignals(true); - foreach(QGraphicsItem *qgi, deleted_parts) { - m_scene -> addItem(qgi); - } - m_scene -> blockSignals(false); -} - -/// Supprime les parties -void DeletePartsCommand::redo() -{ - m_scene -> blockSignals(true); - foreach(QGraphicsItem *qgi, deleted_parts) { - m_scene -> removeItem(qgi); - } - m_scene -> blockSignals(false); -} - /*** CutPartsCommand ***/ /** Constructeur @@ -150,7 +102,7 @@ CutPartsCommand::CutPartsCommand( const QList& parts, QUndoCommand *parent ) : - DeletePartsCommand(scene, parts, parent) + DeletePartsCommand(scene, parts.toVector(), parent) { setText(QString(QObject::tr("couper des parties", "undo caption"))); } diff --git a/sources/editor/editorcommands.h b/sources/editor/editorcommands.h index cad08767c..202e4150b 100644 --- a/sources/editor/editorcommands.h +++ b/sources/editor/editorcommands.h @@ -34,6 +34,7 @@ #include "graphicspart/partterminal.h" #include "graphicspart/parttext.h" #include "../QPropertyUndoCommand/qpropertyundocommand.h" +#include "UndoCommand/deletepartscommand.h" /** @@ -71,29 +72,6 @@ class ElementEditionCommand : public QUndoCommand ElementView *m_view; }; -/** - This command deletes one or several primitives/parts when editing an - electrical element. -*/ -class DeletePartsCommand : public ElementEditionCommand { - // constructors, destructor - public: - DeletePartsCommand(ElementScene *, const QList&, QUndoCommand * = nullptr); - ~DeletePartsCommand() override; - private: - DeletePartsCommand(const DeletePartsCommand &); - - // methods - public: - void undo() override; - void redo() override; - - // attributes - private: - /// Deleted primitives - QList deleted_parts; -}; - /** This command cut primitives when editing an electrical element. */ diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index f04292a9f..7bfcc258e 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -823,16 +823,14 @@ void ElementScene::slot_invertSelection() */ void ElementScene::slot_delete() { - // check that there is something selected - // verifie qu'il y a qqc de selectionne - QList selected_items = selectedItems(); - if (selected_items.isEmpty()) return; + const auto selected_items{selectedItems().toVector()}; + if (selected_items.isEmpty()) { + return; + } - // erase everything that is selected - // efface tout ce qui est selectionne m_undo_stack.push(new DeletePartsCommand(this, selected_items)); - // removing items does not trigger QGraphicsScene::selectionChanged() + // removing items does not trigger QGraphicsScene::selectionChanged() emit selectionChanged(); } diff --git a/sources/qgimanager.cpp b/sources/qgimanager.cpp index b72d2eb98..34c7ddecc 100644 --- a/sources/qgimanager.cpp +++ b/sources/qgimanager.cpp @@ -86,6 +86,18 @@ void QGIManager::release(const QList &qgis) { foreach(QGraphicsItem *qgi, qgis) release(qgi); } +void QGIManager::manage(const QVector &items) { + for (const auto &qgi : items) { + manage(qgi); + } +} + +void QGIManager::release(const QVector &items) { + for (const auto &qgi : items) { + release(qgi); + } +} + /** Indique au QGIManager de detruire les QGraphicsItem restants lors de sa destruction si ceux-ci n'appartiennent pas a la scene diff --git a/sources/qgimanager.h b/sources/qgimanager.h index 40c786093..15f24d362 100644 --- a/sources/qgimanager.h +++ b/sources/qgimanager.h @@ -42,8 +42,12 @@ class QGIManager { public: void manage(QGraphicsItem *); void release(QGraphicsItem *); - void manage(const QList &); - void release(const QList &); + QT_DEPRECATED_X("Use QGIManager::manage(const QVector &) instead") + void manage(const QList &); + QT_DEPRECATED_X("Use QGIManager::release(const QVector &) instead") + void release(const QList &); + void manage(const QVector &items); + void release(const QVector &items); void setDestroyQGIOnDelete(bool); bool manages(QGraphicsItem *) const; };