Revamp code

-Move MoveElementsCommand class from diagramcommands file to
movegraphicsitemcommand file.
-Rename the to class MoveGraphicsItemCommand.
-Minor code change to make it more modern.
This commit is contained in:
joshua
2022-11-08 22:06:50 +01:00
parent 7fede2277f
commit 9e1ef8c42f
5 changed files with 242 additions and 190 deletions

View File

@@ -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<ElementTextItemGroup *>(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 @brief MoveConductorsTextsCommand::MoveConductorsTextsCommand
Constructeur Constructeur

View File

@@ -69,45 +69,6 @@ class CutDiagramCommand : public DeleteQGraphicsItemCommand {
CutDiagramCommand(const CutDiagramCommand &); 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 @brief The MoveConductorsTextsCommand class
This command moves text items related to conductors This command moves text items related to conductors

View File

@@ -20,7 +20,6 @@
#include "conductorautonumerotation.h" #include "conductorautonumerotation.h"
#include "diagram.h" #include "diagram.h"
#include "qetgraphicsitem/conductor.h" #include "qetgraphicsitem/conductor.h"
#include "diagramcommands.h"
#include "qetgraphicsitem/conductortextitem.h" #include "qetgraphicsitem/conductortextitem.h"
#include "qetgraphicsitem/diagramimageitem.h" #include "qetgraphicsitem/diagramimageitem.h"
#include "qetgraphicsitem/dynamicelementtextitem.h" #include "qetgraphicsitem/dynamicelementtextitem.h"
@@ -28,6 +27,7 @@
#include "qetgraphicsitem/elementtextitemgroup.h" #include "qetgraphicsitem/elementtextitemgroup.h"
#include "qetgraphicsitem/independenttextitem.h" #include "qetgraphicsitem/independenttextitem.h"
#include "undocommand/addgraphicsobjectcommand.h" #include "undocommand/addgraphicsobjectcommand.h"
#include "undocommand/movegraphicsitemcommand.h"
/** /**
@brief ElementsMover::ElementsMover Constructor @brief ElementsMover::ElementsMover Constructor
@@ -164,7 +164,7 @@ void ElementsMover::endMovement()
//Create undo move if there is a movement //Create undo move if there is a movement
if (!current_movement_.isNull()) { 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()); undo_object->setText(quc->text());
} }

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <QGraphicsItemGroup>
#include <QPropertyAnimation>
#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<ElementTextItemGroup *>(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);
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef MOVEGRAPHICSITEMCOMMAND_H
#define MOVEGRAPHICSITEMCOMMAND_H
#include <QUndoCommand>
#include <QParallelAnimationGroup>
#include <QPointer>
#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<Diagram> m_diagram;
DiagramContent m_content;
const QPointF m_movement;
QParallelAnimationGroup m_anim_group;
bool m_first_redo{true};
};
#endif // MOVEGRAPHICSITEMCOMMAND_H