From 9e1ef8c42f500679f6fa5e6dcf9193ba2ca3df36 Mon Sep 17 00:00:00 2001 From: joshua Date: Tue, 8 Nov 2022 22:06:50 +0100 Subject: [PATCH] Revamp code -Move MoveElementsCommand class from diagramcommands file to movegraphicsitemcommand file. -Rename the to class MoveGraphicsItemCommand. -Minor code change to make it more modern. --- sources/diagramcommands.cpp | 149 --------------- sources/diagramcommands.h | 39 ---- sources/elementsmover.cpp | 4 +- .../undocommand/movegraphicsitemcommand.cpp | 175 ++++++++++++++++++ sources/undocommand/movegraphicsitemcommand.h | 65 +++++++ 5 files changed, 242 insertions(+), 190 deletions(-) create mode 100644 sources/undocommand/movegraphicsitemcommand.cpp create mode 100644 sources/undocommand/movegraphicsitemcommand.h diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index 46e41b601..4c6b88c2f 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -148,155 +148,6 @@ CutDiagramCommand::~CutDiagramCommand() { } -/** - @brief MoveElementsCommand::MoveElementsCommand - Constructor - @param dia diagram - @param diagram_content diagram content (contain all items to be moved) - @param m movement to applied - @param parent parent undo command -*/ -MoveElementsCommand::MoveElementsCommand( - Diagram *dia, - const DiagramContent &diagram_content, - const QPointF &m, - QUndoCommand *parent -) : - QUndoCommand (parent), - diagram (dia), - content_to_move (diagram_content), - movement (m), - m_anim_group (nullptr), - first_redo (true) -{ - QString moved_content_sentence = content_to_move.sentence( - DiagramContent::Elements | - DiagramContent::TextFields | - DiagramContent::ConductorsToUpdate | - DiagramContent::ConductorsToMove | - DiagramContent::Images | - DiagramContent::Shapes | - DiagramContent::ElementTextFields | - DiagramContent::TerminalStrip - ); - - setText( - QString( - QObject::tr( - "déplacer %1", - "undo caption - %1 is a sentence listing the moved content" - ).arg(moved_content_sentence) - ) - ); -} - -/** - @brief MoveElementsCommand::~MoveElementsCommand - Destructor -*/ -MoveElementsCommand::~MoveElementsCommand() -{ - delete m_anim_group; -} - -/** - @brief MoveElementsCommand::undo -*/ -void MoveElementsCommand::undo() -{ - diagram -> showMe(); - m_anim_group->setDirection(QAnimationGroup::Forward); - m_anim_group->start(); - QUndoCommand::undo(); -} - -/** - @brief MoveElementsCommand::redo -*/ -void MoveElementsCommand::redo() -{ - diagram -> showMe(); - if (first_redo) { - first_redo = false; - move(-movement); - } - else { - m_anim_group->setDirection(QAnimationGroup::Backward); - m_anim_group->start(); - } - QUndoCommand::redo(); -} - -/** - @brief MoveElementsCommand::move - Move item and conductor to actual_movement - @param actual_movement movement to be applied -*/ -void MoveElementsCommand::move(const QPointF &actual_movement) -{ - typedef DiagramContent dc; - - //Move every movable items, except conductor - for (auto &&qgi : content_to_move.items(dc::Elements - | dc::TextFields - | dc::Images - | dc::Shapes - | dc::TextGroup - | dc::ElementTextFields - | dc::Tables - | dc::TerminalStrip)) - { - //If curent item have parent, and parent item is in content_to_move - //we don't apply movement to this item, because this item will be moved by is parent. - if (qgi->parentItem()) - if (content_to_move.items().contains(qgi->parentItem())) - continue; - - if(qgi->toGraphicsObject()) - setupAnimation(qgi->toGraphicsObject(), "pos", - qgi->pos(), qgi->pos() + actual_movement); - else if(qgi->type() == QGraphicsItemGroup::Type) - { - //ElementTextItemGroup is a QObject but not a QGraphicsObject - if(ElementTextItemGroup *etig = dynamic_cast(qgi)) - setupAnimation(etig, "pos", etig->pos(), - etig->pos() + actual_movement); - } - else qgi -> setPos(qgi->pos() + actual_movement); - } - - // Move some conductors - for (Conductor *conductor : content_to_move.m_conductors_to_move) - setupAnimation(conductor, "pos", conductor->pos(), - conductor->pos() + actual_movement); - - // Recalcul the path of other conductor - for (Conductor *conductor : content_to_move.m_conductors_to_update) - setupAnimation(conductor, "animPath", 1, 1); -} - -/** - @brief MoveElementsCommand::setupAnimation - Set up the animation for this undo command - @param target object to anim - @param propertyName property to animate - @param start value at start - @param end value at end -*/ -void MoveElementsCommand::setupAnimation(QObject *target, - const QByteArray &propertyName, - const QVariant& start, - const QVariant& end) { - //create animation group if not yet. - if (m_anim_group == nullptr) m_anim_group = new QParallelAnimationGroup(); - QPropertyAnimation *animation = new QPropertyAnimation(target, propertyName); - animation->setDuration(300); - animation->setStartValue(start); - animation->setEndValue(end); - animation->setEasingCurve(QEasingCurve::OutQuad); - m_anim_group->addAnimation(animation); -} - /** @brief MoveConductorsTextsCommand::MoveConductorsTextsCommand Constructeur diff --git a/sources/diagramcommands.h b/sources/diagramcommands.h index 94baea5ff..c869eb552 100644 --- a/sources/diagramcommands.h +++ b/sources/diagramcommands.h @@ -69,45 +69,6 @@ class CutDiagramCommand : public DeleteQGraphicsItemCommand { CutDiagramCommand(const CutDiagramCommand &); }; -/** - @brief The MoveElementsCommand class - This command moves some content on a particular diagram. -*/ -class MoveElementsCommand : public QUndoCommand { - // constructors, destructor - public: - MoveElementsCommand(Diagram *, const DiagramContent &, - const QPointF &m, QUndoCommand * = nullptr); - ~MoveElementsCommand() override; - private: - MoveElementsCommand(const MoveElementsCommand &); - - // methods - public: - void undo() override; - void redo() override; - virtual void move(const QPointF &); - - private: - void setupAnimation (QObject * target, - const QByteArray &propertyName, - const QVariant& start, - const QVariant& end); - - // attributes - private: - /// diagram the movement takes place on. - Diagram *diagram; - /// moved content - DiagramContent content_to_move; - /// applied movement - QPointF movement; - ///animation group - QParallelAnimationGroup *m_anim_group; - /// prevent the first call to redo() - bool first_redo; -}; - /** @brief The MoveConductorsTextsCommand class This command moves text items related to conductors diff --git a/sources/elementsmover.cpp b/sources/elementsmover.cpp index 11e1edaeb..bd41cc319 100644 --- a/sources/elementsmover.cpp +++ b/sources/elementsmover.cpp @@ -20,7 +20,6 @@ #include "conductorautonumerotation.h" #include "diagram.h" #include "qetgraphicsitem/conductor.h" -#include "diagramcommands.h" #include "qetgraphicsitem/conductortextitem.h" #include "qetgraphicsitem/diagramimageitem.h" #include "qetgraphicsitem/dynamicelementtextitem.h" @@ -28,6 +27,7 @@ #include "qetgraphicsitem/elementtextitemgroup.h" #include "qetgraphicsitem/independenttextitem.h" #include "undocommand/addgraphicsobjectcommand.h" +#include "undocommand/movegraphicsitemcommand.h" /** @brief ElementsMover::ElementsMover Constructor @@ -164,7 +164,7 @@ void ElementsMover::endMovement() //Create undo move if there is a movement if (!current_movement_.isNull()) { - QUndoCommand *quc = new MoveElementsCommand(diagram_, m_moved_content, current_movement_, undo_object); + QUndoCommand *quc = new MoveGraphicsItemCommand(diagram_, m_moved_content, current_movement_, undo_object); undo_object->setText(quc->text()); } diff --git a/sources/undocommand/movegraphicsitemcommand.cpp b/sources/undocommand/movegraphicsitemcommand.cpp new file mode 100644 index 000000000..33dd89b7e --- /dev/null +++ b/sources/undocommand/movegraphicsitemcommand.cpp @@ -0,0 +1,175 @@ +/* + 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 +#include + +#include "movegraphicsitemcommand.h" + +#include "../qetgraphicsitem/conductor.h" +#include "../qetgraphicsitem/elementtextitemgroup.h" + +#include "../diagram.h" + +/** + * @brief MoveGraphicsItemCommand::MoveGraphicsItemCommand + * @param diagram : Diagram where the movement occur + * @param content : content aka QGraphicsItem to move + * @param movement : the movement to apply + * @param parent : parent undo command + */ +MoveGraphicsItemCommand::MoveGraphicsItemCommand(Diagram *diagram, + const DiagramContent &content, + const QPointF &movement, + QUndoCommand *parent) : + QUndoCommand{parent}, + m_diagram{diagram}, + m_content{content}, + m_movement{movement} +{ + const auto moved_content_sentence = m_content.sentence(DiagramContent::Elements + | DiagramContent::TextFields + | DiagramContent::ConductorsToUpdate + | DiagramContent::ConductorsToMove + | DiagramContent::Images + | DiagramContent::Shapes + | DiagramContent::ElementTextFields + | DiagramContent::TerminalStrip); + + setText(QString(QObject::tr("déplacer %1", + "undo caption - %1 is a sentence listing the moved content").arg(moved_content_sentence))); +} + +/** + * @brief MoveGraphicsItemCommand::undo + * Reimplemented from QUndoCommand::undo() + */ +void MoveGraphicsItemCommand::undo() +{ + if (m_diagram) + { + m_diagram->showMe(); + m_anim_group.setDirection(QAnimationGroup::Forward); + m_anim_group.start(); + } + QUndoCommand::undo(); +} + +/** + * @brief MoveGraphicsItemCommand::redo + * Reimplemented from QUndoCommand::redo() + */ +void MoveGraphicsItemCommand::redo() +{ + if (m_diagram) + { + m_diagram->showMe(); + if (m_first_redo) + { + m_first_redo = false; + move(-m_movement); + } + else + { + m_anim_group.setDirection(QAnimationGroup::Backward); + m_anim_group.start(); + } + } + QUndoCommand::redo(); +} + +/** + * @brief MoveGraphicsItemCommand::move + * Apply @a movement to items of m_content + * @param movement + */ +void MoveGraphicsItemCommand::move(const QPointF &movement) +{ + for (auto &&qgi : m_content.items(DiagramContent::Elements + | DiagramContent::TextFields + | DiagramContent::Images + | DiagramContent::Shapes + | DiagramContent::TextGroup + | DiagramContent::ElementTextFields + | DiagramContent::Tables + | DiagramContent::TerminalStrip)) + { + //If item have a parent and the parent is in m_content, + //we don't apply movement because this item will be moved by his parent + if (const auto parent_ = qgi->parentItem()) { + if (m_content.items().contains(parent_)) { + continue; + } + } + + if (const auto graphics_object = qgi->toGraphicsObject()) { + setupAnimation(graphics_object, + "pos", + graphics_object->pos(), + graphics_object->pos() + movement); + } + else if (qgi->type() == QGraphicsItemGroup::Type) + { + //ElementTextItemGroup is a QObject not a QGraphicsObject + if (ElementTextItemGroup *etig = dynamic_cast(qgi)) { + setupAnimation(etig, + "pos", + etig->pos(), + etig->pos() + movement); + } + } + else + { + qgi->setPos(qgi->pos() + movement); + } + } + + //Move some conductors + for (const auto &conductor : qAsConst(m_content.m_conductors_to_move)) { + setupAnimation(conductor, + "pos", + conductor->pos(), + conductor->pos() + movement); + } + + //Recalcul the path of other conductors + for (const auto &conductor : qAsConst(m_content.m_conductors_to_update)) { + setupAnimation(conductor, "animPath", 1, 1); + } +} + +/** + * @brief MoveGraphicsItemCommand::setupAnimation + * Create the animation used for the movement. + * @see QPropertyAnimation. + * @param target + * @param property_name + * @param start + * @param end + */ +void MoveGraphicsItemCommand::setupAnimation(QObject *target, + const QByteArray &property_name, + const QVariant &start, + const QVariant &end) +{ + QPropertyAnimation *animation{new QPropertyAnimation(target, property_name)}; + animation->setDuration(300); + animation->setStartValue(start); + animation->setEndValue(end); + animation->setEasingCurve(QEasingCurve::OutQuad); + m_anim_group.addAnimation(animation); +} diff --git a/sources/undocommand/movegraphicsitemcommand.h b/sources/undocommand/movegraphicsitemcommand.h new file mode 100644 index 000000000..6b5722f59 --- /dev/null +++ b/sources/undocommand/movegraphicsitemcommand.h @@ -0,0 +1,65 @@ +/* + 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 MOVEGRAPHICSITEMCOMMAND_H +#define MOVEGRAPHICSITEMCOMMAND_H + +#include +#include +#include + +#include "../diagramcontent.h" + +class Diagram; + +/** + * @brief The MoveGraphicsItemCommand class + * An undo command used for move item(s) in a diagram + */ +class MoveGraphicsItemCommand : public QUndoCommand +{ + public: + MoveGraphicsItemCommand(Diagram *diagram, + const DiagramContent &content, + const QPointF &movement, + QUndoCommand *parent = nullptr); + + ~MoveGraphicsItemCommand() {} + + private: + MoveGraphicsItemCommand(const MoveGraphicsItemCommand &); + + public: + void undo() override; + void redo() override; + + private: + void move(const QPointF &movement); + void setupAnimation(QObject *target, + const QByteArray &property_name, + const QVariant &start, + const QVariant &end); + + private: + QPointer m_diagram; + DiagramContent m_content; + const QPointF m_movement; + QParallelAnimationGroup m_anim_group; + bool m_first_redo{true}; +}; + +#endif // MOVEGRAPHICSITEMCOMMAND_H