diff --git a/sources/editor/UndoCommand/pastepartscommand.cpp b/sources/editor/UndoCommand/pastepartscommand.cpp new file mode 100644 index 000000000..a72cfe8c7 --- /dev/null +++ b/sources/editor/UndoCommand/pastepartscommand.cpp @@ -0,0 +1,114 @@ +/* + Copyright 2006-2020 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 "pastepartscommand.h" +#include "elementview.h" +#include "partterminal.h" + +/** + * @brief PastePartsCommand::PastePartsCommand + * @param view : view where this command work + * @param content_to_paste : content to paste + * @param parent : parent undo command + * + * Note : all terminal stored in @content_to_paste get a new uuid in the constructor of this class to avoid have + * several terminal of an element with the same uuid. + */ +PastePartsCommand::PastePartsCommand(ElementView *view, const ElementContent &content_to_paste, QUndoCommand *parent) : + ElementEditionCommand(view ? view -> scene() : nullptr, view, parent) +{ + for (auto qgi : content_to_paste) + { + if (qgi->type() == PartTerminal::Type) { + auto part_terminal = static_cast(qgi); + part_terminal->setNewUuid(); + } + m_pasted_content.append(qgi); + } + setText(QObject::tr("Coller")); + m_scene->qgiManager().manage(m_pasted_content); +} + +/** + * @brief PastePartsCommand::~PastePartsCommand + */ +PastePartsCommand::~PastePartsCommand() +{ + m_scene->qgiManager().release(m_pasted_content); +} + +/** + * @brief PastePartsCommand::undo + */ +void PastePartsCommand::undo() +{ + m_scene->blockSignals(true); + for (auto qgi : m_pasted_content) { + m_scene->removeItem(qgi); + } + m_scene->blockSignals(false); + + if (m_uses_offset) + { + m_view->offset_paste_count_ = m_old_offset_paste_count; + m_view->start_top_left_corner_ = m_old_start_top_left_corner; + } + + m_view->adjustSceneRect(); +} + +/** + * @brief PastePartsCommand::redo + */ +void PastePartsCommand::redo() +{ + if (m_first_redo) { + m_first_redo = false; + } else { + m_scene->blockSignals(true); + for (auto qgi : m_pasted_content) { + m_scene->addItem(qgi); + } + m_scene->blockSignals(false); + + if (m_uses_offset) + { + m_view->offset_paste_count_ = m_new_offset_paste_count; + m_view->start_top_left_corner_ = m_new_start_top_left_corner; + } + } + + m_scene->slot_select(m_pasted_content); + m_view->adjustSceneRect(); +} + +/** + * @brief PastePartsCommand::setOffset + * Describe the offset to use with this undo command + * @param old_offset_paste_count + * @param old_start_top_left_corner + * @param new_offset_paste_count + * @param new_start_top_left_corner + */ +void PastePartsCommand::setOffset(int old_offset_paste_count, const QPointF &old_start_top_left_corner, int new_offset_paste_count, const QPointF &new_start_top_left_corner) +{ + m_old_offset_paste_count = old_offset_paste_count; + m_old_start_top_left_corner = old_start_top_left_corner; + m_new_offset_paste_count = new_offset_paste_count; + m_new_start_top_left_corner = new_start_top_left_corner; + m_uses_offset = true; +} diff --git a/sources/editor/UndoCommand/pastepartscommand.h b/sources/editor/UndoCommand/pastepartscommand.h new file mode 100644 index 000000000..eac997eee --- /dev/null +++ b/sources/editor/UndoCommand/pastepartscommand.h @@ -0,0 +1,56 @@ +/* + Copyright 2006-2020 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 PASTEPARTSCOMMAND_H +#define PASTEPARTSCOMMAND_H + +#include "editorcommands.h" +#include "elementcontent.h" + +class ElementView; + +/** + * @brief The PastePartsCommand class + * Undo command for paste element primitive in an element editor + */ +class PastePartsCommand : public ElementEditionCommand +{ + public: + PastePartsCommand(ElementView *view, const ElementContent &content_to_paste, QUndoCommand *parent = nullptr); + ~PastePartsCommand() override; + private: + PastePartsCommand(const PastePartsCommand &); + + public: + void undo() override; + void redo() override; + virtual void setOffset(int old_offset_paste_count, const QPointF &old_start_top_left_corner, int new_offset_paste_count, const QPointF &new_start_top_left_corner); + + private: + ElementContent m_pasted_content; + + int m_old_offset_paste_count, + m_new_offset_paste_count; + + QPointF m_old_start_top_left_corner, + m_new_start_top_left_corner; + + bool m_uses_offset = false, + m_first_redo = true; +}; + +#endif // PASTEPARTSCOMMAND_H diff --git a/sources/editor/editorcommands.cpp b/sources/editor/editorcommands.cpp index 1fb7966b9..7c4526b79 100644 --- a/sources/editor/editorcommands.cpp +++ b/sources/editor/editorcommands.cpp @@ -119,79 +119,6 @@ void DeletePartsCommand::redo() { m_scene -> blockSignals(false); } -/*** CutPartsCommand ***/ -/** - Constructeur - @param view ElementView concernee - @param c Liste des parties collees - @param parent QUndoCommand parent -*/ -PastePartsCommand::PastePartsCommand( - ElementView *view, - const ElementContent &c, - QUndoCommand *parent -) : - ElementEditionCommand(view ? view -> scene() : nullptr, view, parent), - content_(c), - uses_offset(false), - first_redo(true) -{ - setText(QObject::tr("coller")); - m_scene -> qgiManager().manage(content_); -} - -/// Destructeur -PastePartsCommand::~PastePartsCommand() { - m_scene -> qgiManager().release(content_); -} - -/// annule le coller -void PastePartsCommand::undo() { - // enleve les parties - m_scene -> blockSignals(true); - foreach(QGraphicsItem *part, content_) { - m_scene -> removeItem(part); - } - m_scene -> blockSignals(false); - if (uses_offset) { - m_view -> offset_paste_count_ = old_offset_paste_count_; - m_view -> start_top_left_corner_ = old_start_top_left_corner_; - } - m_view -> adjustSceneRect(); -} - -/// refait le coller -void PastePartsCommand::redo() { - if (first_redo) first_redo = false; - else { - // pose les parties - m_scene -> blockSignals(true); - foreach(QGraphicsItem *part, content_) { - m_scene -> addItem(part); - } - m_scene -> blockSignals(false); - if (uses_offset) { - m_view -> offset_paste_count_ = new_offset_paste_count_; - m_view -> start_top_left_corner_ = new_start_top_left_corner_; - } - } - m_scene -> slot_select(content_); - m_view -> adjustSceneRect(); -} - -/** - Indique a cet objet d'annulation que le c/c a annuler ou refaire etait un - c/c avec decalage ; il faut plus d'informations pour annuler ce type de - collage. -*/ -void PastePartsCommand::setOffset(int old_offset_pc, const QPointF &old_start_tlc, int new_offset_pc, const QPointF &new_start_tlc) { - old_offset_paste_count_ = old_offset_pc; - old_start_top_left_corner_ = old_start_tlc; - new_offset_paste_count_ = new_offset_pc; - new_start_top_left_corner_ = new_start_tlc; - uses_offset = true; -} - /*** CutPartsCommand ***/ /** Constructeur diff --git a/sources/editor/editorcommands.h b/sources/editor/editorcommands.h index bfd509c2f..d6174ffbf 100644 --- a/sources/editor/editorcommands.h +++ b/sources/editor/editorcommands.h @@ -76,37 +76,6 @@ class DeletePartsCommand : public ElementEditionCommand { QList deleted_parts; }; -/** - This command pastes primitives when editing an electrical element. -*/ -class PastePartsCommand : public ElementEditionCommand { - // constructors, destructor - public: - PastePartsCommand(ElementView *, const ElementContent &, QUndoCommand * = nullptr); - ~PastePartsCommand() override; - private: - PastePartsCommand(const PastePartsCommand &); - - // methods - public: - void undo() override; - void redo() override; - virtual void setOffset(int, const QPointF &, int, const QPointF &); - - // attributes - private: - /// Pasted content - ElementContent content_; - /// Data required to undo a copy/paste with offset - int old_offset_paste_count_; - QPointF old_start_top_left_corner_; - int new_offset_paste_count_; - QPointF new_start_top_left_corner_; - bool uses_offset; - /// Prevent the first call to redo() - bool first_redo; -}; - /** This command cut primitives when editing an electrical element. */ diff --git a/sources/editor/elementview.cpp b/sources/editor/elementview.cpp index 5cb99066b..a8da50452 100644 --- a/sources/editor/elementview.cpp +++ b/sources/editor/elementview.cpp @@ -17,8 +17,8 @@ */ #include "elementview.h" #include "qetelementeditor.h" -#include "editorcommands.h" #include "qetapp.h" +#include "pastepartscommand.h" /** Constructeur @param scene ElementScene visualisee par cette ElementView diff --git a/sources/editor/graphicspart/partterminal.cpp b/sources/editor/graphicspart/partterminal.cpp index f8256e02b..94d95fcb4 100644 --- a/sources/editor/graphicspart/partterminal.cpp +++ b/sources/editor/graphicspart/partterminal.cpp @@ -144,6 +144,11 @@ void PartTerminal::setName(QString& name) emit nameChanged(); } +void PartTerminal::setNewUuid() +{ + d->m_uuid = QUuid::createUuid(); +} + /** Met a jour la position du second point en fonction de la position et de l'orientation de la borne. diff --git a/sources/editor/graphicspart/partterminal.h b/sources/editor/graphicspart/partterminal.h index a66aeb2fc..43107b0b0 100644 --- a/sources/editor/graphicspart/partterminal.h +++ b/sources/editor/graphicspart/partterminal.h @@ -72,7 +72,8 @@ class PartTerminal : public CustomElementGraphicPart Qet::Orientation orientation() const {return d->m_orientation;} void setOrientation(Qet::Orientation ori); - void setName(QString& name); + void setName(QString& name); + void setNewUuid(); private: void updateSecondPoint();