diff --git a/sources/editor/UndoCommand/addpartcommand.cpp b/sources/editor/UndoCommand/addpartcommand.cpp new file mode 100644 index 000000000..c5944ab0b --- /dev/null +++ b/sources/editor/UndoCommand/addpartcommand.cpp @@ -0,0 +1,84 @@ +/* + 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 "addpartcommand.h" +#include "../elementscene.h" + +/** + * @brief AddPartCommand::AddPartCommand + * @param text : text of the undo command + * @param scene : scene where @a part must be added + * @param part : part to add + * @param parent : parent undo command + */ +AddPartCommand::AddPartCommand(const QString &text, + QPointer scene, + QGraphicsItem *part, + QUndoCommand *parent) : + QUndoCommand{parent}, + m_scene{scene}, + m_part{part} +{ + setText(text); + m_scene->qgiManager().manage(part); +} + +/** + * @brief AddPartCommand::~AddPartCommand + */ +AddPartCommand::~AddPartCommand() +{ + if (m_scene) { + m_scene->qgiManager().release(m_part.first()); + } +} + +/** + * @brief AddPartCommand::undo + */ +void AddPartCommand::undo() +{ + if (m_scene) { + m_scene->removeItems(m_part); + } +} + +/** + * @brief AddPartCommand::redo + */ +void AddPartCommand::redo() +{ + if (!m_scene) { + return; + } + + if (m_first_redo) + { + auto part_{m_part.first()}; + if (!part_->zValue()) + { + // the added part has no specific zValue already defined, we put it + // above existing items (but still under terminals) + const auto existing_items = m_scene->zItems(ElementScene::SortByZValue | ElementScene::SelectedOrNot); + const auto z_{existing_items.count() ? existing_items.last()->zValue() + 1 : 1}; + part_->setZValue(z_); + } + m_scene->clearSelection(); + m_first_redo = false; + } + m_scene->addItems(m_part); +} diff --git a/sources/editor/UndoCommand/addpartcommand.h b/sources/editor/UndoCommand/addpartcommand.h new file mode 100644 index 000000000..11914c365 --- /dev/null +++ b/sources/editor/UndoCommand/addpartcommand.h @@ -0,0 +1,50 @@ +/* + 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 ADDPARTCOMMAND_H +#define ADDPARTCOMMAND_H + +#include +#include + +class ElementScene; +class QGraphicsItem; + +/** + * @brief The AddPartCommand class + * Undo command use to add a graphics part into an element scene. + */ +class AddPartCommand : public QUndoCommand +{ + public: + AddPartCommand(const QString &text, QPointer scene, QGraphicsItem *part, QUndoCommand *parent = nullptr); + ~AddPartCommand() override; + + private: + AddPartCommand(const AddPartCommand &); + + public: + void undo() override; + void redo() override; + + private: + QPointer m_scene; + QVector m_part; + bool m_first_redo{true}; +}; + +#endif // ADDPARTCOMMAND_H diff --git a/sources/editor/UndoCommand/openelmtcommand.cpp b/sources/editor/UndoCommand/openelmtcommand.cpp index 8fa9f9898..79786dfc6 100644 --- a/sources/editor/UndoCommand/openelmtcommand.cpp +++ b/sources/editor/UndoCommand/openelmtcommand.cpp @@ -31,11 +31,16 @@ OpenElmtCommand::OpenElmtCommand(const QDomDocument &document, setText(QObject::tr("Ouvrir un element")); } +OpenElmtCommand::~OpenElmtCommand() +{ + if (m_scene) { + m_scene->qgiManager().release(m_graphics_item); + } +} + void OpenElmtCommand::undo() { - for (const auto &item : qAsConst(m_graphics_item)) { - m_scene->removeItem(item); - } + m_scene->removeItems(m_graphics_item.toVector()); } void OpenElmtCommand::redo() @@ -47,6 +52,7 @@ void OpenElmtCommand::redo() if (m_first_redo) { m_scene->fromXml(m_document, QPointF(), true, &m_graphics_item); + m_scene->qgiManager().manage(m_graphics_item); m_first_redo = false; //m_document is now useless, @@ -54,9 +60,8 @@ void OpenElmtCommand::redo() m_document.clear(); } else { - for (const auto &item : qAsConst(m_graphics_item)) { - m_scene->addItem(item); - } + m_scene->addItems(m_graphics_item.toVector()); } + m_scene->slot_select(m_graphics_item); } diff --git a/sources/editor/UndoCommand/openelmtcommand.h b/sources/editor/UndoCommand/openelmtcommand.h index e318b4ee3..be85bc744 100644 --- a/sources/editor/UndoCommand/openelmtcommand.h +++ b/sources/editor/UndoCommand/openelmtcommand.h @@ -29,6 +29,7 @@ class OpenElmtCommand : public QUndoCommand { public: OpenElmtCommand(const QDomDocument &document, QPointer scene, QUndoCommand *parent = nullptr); + ~OpenElmtCommand() override; virtual void undo() override; virtual void redo() override; diff --git a/sources/editor/editorcommands.cpp b/sources/editor/editorcommands.cpp index 9be73c34a..ba7ec27c5 100644 --- a/sources/editor/editorcommands.cpp +++ b/sources/editor/editorcommands.cpp @@ -203,58 +203,6 @@ void MovePartsCommand::redo() foreach(QGraphicsItem *qgi, moved_parts) qgi -> moveBy(movement.x(), movement.y()); } -/*** AddPartCommand ***/ -/** - Constructeur - @param name Nom de la partie ajoutee - @param scene ElementScene concernee - @param p partie ajoutee - @param parent QUndoCommand parent -*/ -AddPartCommand::AddPartCommand( - const QString &name, - ElementScene *scene, - QGraphicsItem *p, - QUndoCommand *parent -) : - ElementEditionCommand(QString(QObject::tr("ajout %1", "undo caption")).arg(name), scene, nullptr, parent), - part(p), - first_redo(true) -{ - m_scene -> qgiManager().manage(part); -} - -/// Destructeur -AddPartCommand::~AddPartCommand() -{ - m_scene -> qgiManager().release(part); -} - -/// Annule l'ajout -void AddPartCommand::undo() -{ - m_scene -> removeItem(part); -} - -/// Refait l'ajout -void AddPartCommand::redo() -{ - // le premier appel a redo, lors de la construction de l'objet, ne doit pas se faire - if (first_redo) { - if (!part -> zValue()) { - // the added part has no specific zValue already defined, we put it - // above existing items (but still under terminals) - QList existing_items = m_scene -> zItems(ElementScene::SortByZValue | ElementScene::SelectedOrNot); - qreal z = existing_items.count() ? existing_items.last() -> zValue() + 1 : 1; - part -> setZValue(z); - } - m_scene -> clearSelection(); - first_redo = false; - return; - } - m_scene -> addItem(part); -} - /** Constructeur @param element_scene Element edite diff --git a/sources/editor/editorcommands.h b/sources/editor/editorcommands.h index 6e803b3c9..cad08767c 100644 --- a/sources/editor/editorcommands.h +++ b/sources/editor/editorcommands.h @@ -132,30 +132,6 @@ class MovePartsCommand : public ElementEditionCommand { bool first_redo; }; -/** - This command adds a primitive when editing an electrical element. -*/ -class AddPartCommand : public ElementEditionCommand { - // constructors, destructor - public: - AddPartCommand(const QString &, ElementScene *, QGraphicsItem *, QUndoCommand * = nullptr); - ~AddPartCommand() override; - private: - AddPartCommand(const AddPartCommand &); - - // methods - public: - void undo() override; - void redo() override; - - // attributes - private: - /// Added primitive - QGraphicsItem *part; - /// Prevent the first call to redo() - bool first_redo; -}; - /** This command changes the translated names of an electrical element. */ @@ -311,5 +287,4 @@ private: }; - #endif diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index 2d8d7a302..f04292a9f 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -119,7 +119,6 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) if (m_event_interface->isFinish()) { delete m_event_interface; m_event_interface = nullptr; - emit(partsAdded()); } return; } @@ -151,7 +150,6 @@ void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e) if (m_event_interface->isFinish()) { delete m_event_interface; m_event_interface = nullptr; - emit(partsAdded()); } return; } @@ -171,7 +169,6 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) if (m_event_interface->isFinish()) { delete m_event_interface; m_event_interface = nullptr; - emit(partsAdded()); } return; } @@ -199,7 +196,6 @@ void ElementScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) if (m_event_interface->isFinish()) { delete m_event_interface; m_event_interface = nullptr; - emit(partsAdded()); } return; } @@ -223,7 +219,6 @@ void ElementScene::keyPressEvent(QKeyEvent *event) { delete m_event_interface; m_event_interface = nullptr; - emit(partsAdded()); } return; } @@ -725,6 +720,38 @@ QETElementEditor* ElementScene::editor() const return m_element_editor; } +/** + * @brief ElementScene::addItems + * Add items to the scene and emit partsAdded. + * Prefer always use this method instead of QGraphicsScene::addItem + * even if you want to add one item, for gain the signal emission + * @param items + */ +void ElementScene::addItems(QVector items) +{ + for (const auto &item : items) { + addItem(item); + } + + emit partsAdded(); +} + +/** + * @brief ElementScene::removeItems + * Remove items from the scene and emit partsRemoved. + * Prefer always use this method instead of QGraphicsScene::removeItem + * even if you want to remove one item, for gain the signal emission + * @param items + */ +void ElementScene::removeItems(QVector items) +{ + for (const auto &item : items) { + removeItem(item); + } + + emit partsRemoved(); +} + /** @brief ElementScene::slot_select Select the item in content, @@ -806,8 +833,7 @@ void ElementScene::slot_delete() m_undo_stack.push(new DeletePartsCommand(this, selected_items)); // removing items does not trigger QGraphicsScene::selectionChanged() - emit(partsRemoved()); - emit(selectionChanged()); + emit selectionChanged(); } /** diff --git a/sources/editor/elementscene.h b/sources/editor/elementscene.h index 3df7819f9..5f275c344 100644 --- a/sources/editor/elementscene.h +++ b/sources/editor/elementscene.h @@ -37,6 +37,9 @@ class QKeyEvent; This class is the canvas allowing the visual edition of an electrial element. It displays the various primitives composing the drawing of the element, the border due to its fixed size and its hotspot. + + For add and remove item prefer use custom method addItems and removeItems instead of + addItem and removeItem, because these methods emit signal partAdded and partRemoved. */ class ElementScene : public QGraphicsScene { @@ -124,6 +127,8 @@ class ElementScene : public QGraphicsScene void cut(); void copy(); QETElementEditor* editor() const; + void addItems(QVector items); + void removeItems(QVector items); protected: void mouseMoveEvent (QGraphicsSceneMouseEvent *) override; diff --git a/sources/editor/esevent/eseventaddarc.cpp b/sources/editor/esevent/eseventaddarc.cpp index 7ba700b08..6fb7dadaa 100644 --- a/sources/editor/esevent/eseventaddarc.cpp +++ b/sources/editor/esevent/eseventaddarc.cpp @@ -17,7 +17,7 @@ */ #include "eseventaddarc.h" -#include "../editorcommands.h" +#include "../UndoCommand/addpartcommand.h" #include "../elementscene.h" #include "../graphicspart/partarc.h" diff --git a/sources/editor/esevent/eseventadddynamictextfield.cpp b/sources/editor/esevent/eseventadddynamictextfield.cpp index defa52544..71b06d077 100644 --- a/sources/editor/esevent/eseventadddynamictextfield.cpp +++ b/sources/editor/esevent/eseventadddynamictextfield.cpp @@ -17,7 +17,7 @@ */ #include "eseventadddynamictextfield.h" -#include "../editorcommands.h" +#include "../UndoCommand/addpartcommand.h" #include "../elementscene.h" #include "../graphicspart/partdynamictextfield.h" diff --git a/sources/editor/esevent/eseventaddellipse.cpp b/sources/editor/esevent/eseventaddellipse.cpp index c7dba6b7f..d704f47cb 100644 --- a/sources/editor/esevent/eseventaddellipse.cpp +++ b/sources/editor/esevent/eseventaddellipse.cpp @@ -17,7 +17,7 @@ */ #include "eseventaddellipse.h" -#include "../editorcommands.h" +#include "../UndoCommand/addpartcommand.h" #include "../elementscene.h" #include "../graphicspart/partellipse.h" diff --git a/sources/editor/esevent/eseventaddline.cpp b/sources/editor/esevent/eseventaddline.cpp index 93f83a320..41fb137d0 100644 --- a/sources/editor/esevent/eseventaddline.cpp +++ b/sources/editor/esevent/eseventaddline.cpp @@ -17,7 +17,7 @@ */ #include "eseventaddline.h" -#include "../editorcommands.h" +#include "../UndoCommand/addpartcommand.h" #include "../elementscene.h" #include "../graphicspart/partline.h" #include "../ui/qetelementeditor.h" diff --git a/sources/editor/esevent/eseventaddpolygon.cpp b/sources/editor/esevent/eseventaddpolygon.cpp index 357c5c849..1c7e4af04 100644 --- a/sources/editor/esevent/eseventaddpolygon.cpp +++ b/sources/editor/esevent/eseventaddpolygon.cpp @@ -17,7 +17,7 @@ */ #include "eseventaddpolygon.h" -#include "../editorcommands.h" +#include "../UndoCommand/addpartcommand.h" #include "../elementscene.h" #include "../graphicspart/partpolygon.h" diff --git a/sources/editor/esevent/eseventaddrect.cpp b/sources/editor/esevent/eseventaddrect.cpp index 108b99c65..ed3b038a3 100644 --- a/sources/editor/esevent/eseventaddrect.cpp +++ b/sources/editor/esevent/eseventaddrect.cpp @@ -17,7 +17,7 @@ */ #include "eseventaddrect.h" -#include "../editorcommands.h" +#include "../UndoCommand/addpartcommand.h" #include "../elementscene.h" #include "../graphicspart/partrectangle.h" diff --git a/sources/editor/esevent/eseventaddterminal.cpp b/sources/editor/esevent/eseventaddterminal.cpp index a48516e99..827136cbb 100644 --- a/sources/editor/esevent/eseventaddterminal.cpp +++ b/sources/editor/esevent/eseventaddterminal.cpp @@ -17,7 +17,7 @@ */ #include "eseventaddterminal.h" -#include "../editorcommands.h" +#include "../UndoCommand/addpartcommand.h" #include "../elementscene.h" #include "../graphicspart/partterminal.h" diff --git a/sources/editor/esevent/eseventaddtext.cpp b/sources/editor/esevent/eseventaddtext.cpp index 8f1521f82..816faf23d 100644 --- a/sources/editor/esevent/eseventaddtext.cpp +++ b/sources/editor/esevent/eseventaddtext.cpp @@ -17,7 +17,7 @@ */ #include "eseventaddtext.h" -#include "../editorcommands.h" +#include "../UndoCommand/addpartcommand.h" #include "../elementscene.h" #include "../graphicspart/parttext.h" diff --git a/sources/editor/ui/qetelementeditor.cpp b/sources/editor/ui/qetelementeditor.cpp index 09dcb03de..f5c4f02be 100644 --- a/sources/editor/ui/qetelementeditor.cpp +++ b/sources/editor/ui/qetelementeditor.cpp @@ -1130,7 +1130,6 @@ void QETElementEditor::setupConnection() }); connect(&(m_elmt_scene -> undoStack()), &QUndoStack::indexChanged, [this]() { - this->updatePartsList(); this->updateInformations(); }); }