Replace AddItemCommand class by AddGraphicsObjectCommand

The class is the same except the name and the use of QPointer to avoid
segfault with deleted QGraphicsObject.
This commit is contained in:
joshua
2021-02-07 16:20:55 +01:00
parent ac23010559
commit 0c4f87bd76
14 changed files with 180 additions and 94 deletions

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2006-2020 The QElectroTech Team Copyright 2006-2021 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
@@ -29,20 +29,6 @@
#include <QPropertyAnimation> #include <QPropertyAnimation>
QString itemText(const QetGraphicsItem *item) {
return item->name();
}
QString itemText(const IndependentTextItem *item) {
Q_UNUSED(item)
return QObject::tr("un champ texte");
}
QString itemText(const Conductor *item) {
Q_UNUSED(item)
return QObject::tr("un conducteur");
}
/** /**
@brief PasteDiagramCommand::PasteDiagramCommand @brief PasteDiagramCommand::PasteDiagramCommand
Constructor Constructor

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2006-2020 The QElectroTech Team Copyright 2006-2021 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
@@ -33,54 +33,6 @@ class IndependentTextItem;
class DiagramImageItem; class DiagramImageItem;
class QetGraphicsItem; class QetGraphicsItem;
/**
@brief The AddItemCommand class
This command add an item in a diagram
The item to add is template, but must be QGraphicsItem or derived.
*/
template <typename QGI>
class AddItemCommand : public QUndoCommand {
public:
AddItemCommand(QGI item, Diagram *diagram,
const QPointF &pos = QPointF(),
QUndoCommand *parent = nullptr) :
QUndoCommand (parent),
m_item (item),
m_diagram (diagram),
m_pos(pos)
{
setText(QObject::tr("Ajouter ") + itemText(item));
m_diagram -> qgiManager().manage(m_item);
}
~AddItemCommand() override {
m_diagram -> qgiManager().release(m_item);
}
void undo() override {
m_diagram -> showMe();
m_diagram -> removeItem(m_item);
QUndoCommand::undo();
}
void redo() override {
m_diagram -> showMe();
m_diagram -> addItem(m_item);
m_item -> setPos(m_pos);
QUndoCommand::redo();
}
private:
QGI m_item;
Diagram *m_diagram;
QPointF m_pos;
};
//Return a string to describe a QGraphicsItem
QString itemText(const QetGraphicsItem *item);
QString itemText(const IndependentTextItem *item);
QString itemText(const Conductor *item);
/** /**
@brief The PasteDiagramCommand class @brief The PasteDiagramCommand class
This command pastes some content onto a particular diagram. This command pastes some content onto a particular diagram.

View File

@@ -19,9 +19,10 @@
#include "../conductorautonumerotation.h" #include "../conductorautonumerotation.h"
#include "../diagram.h" #include "../diagram.h"
#include "../diagramcommands.h" #include "../undocommand/addgraphicsobjectcommand.h"
#include "../factory/elementfactory.h" #include "../factory/elementfactory.h"
#include "../qetgraphicsitem/element.h" #include "../qetgraphicsitem/element.h"
#include "../qetgraphicsitem/conductor.h"
/** /**
@brief DiagramEventAddElement::DiagramEventAddElement @brief DiagramEventAddElement::DiagramEventAddElement
@@ -228,7 +229,7 @@ void DiagramEventAddElement::addElement()
element->m_converted_text_from_xml_description.clear(); element->m_converted_text_from_xml_description.clear();
QUndoCommand *undo_object = new QUndoCommand(tr("Ajouter %1").arg(element->name())); QUndoCommand *undo_object = new QUndoCommand(tr("Ajouter %1").arg(element->name()));
new AddItemCommand<Element *>(element, m_diagram, m_element -> pos(), undo_object); new AddGraphicsObjectCommand(element, m_diagram, m_element -> pos(), undo_object);
//When we search for free aligned terminal we //When we search for free aligned terminal we
//temporally remove m_element to avoid any interaction with the function Element::AlignedFreeTerminals //temporally remove m_element to avoid any interaction with the function Element::AlignedFreeTerminals
@@ -242,7 +243,7 @@ void DiagramEventAddElement::addElement()
QPair <Terminal *, Terminal *> pair = element -> AlignedFreeTerminals().takeFirst(); QPair <Terminal *, Terminal *> pair = element -> AlignedFreeTerminals().takeFirst();
Conductor *conductor = new Conductor(pair.first, pair.second); Conductor *conductor = new Conductor(pair.first, pair.second);
new AddItemCommand<Conductor *>(conductor, m_diagram, QPointF(), undo_object); new AddGraphicsObjectCommand(conductor, m_diagram, QPointF(), undo_object);
//Autonum the new conductor, the undo command associated for this, have for parent undo_object //Autonum the new conductor, the undo command associated for this, have for parent undo_object
ConductorAutoNumerotation can (conductor, m_diagram, undo_object); ConductorAutoNumerotation can (conductor, m_diagram, undo_object);

View File

@@ -19,7 +19,7 @@
#include "diagrameventaddimage.h" #include "diagrameventaddimage.h"
#include "../diagram.h" #include "../diagram.h"
#include "../diagramcommands.h" #include "../undocommand/addgraphicsobjectcommand.h"
#include "../qetgraphicsitem/diagramimageitem.h" #include "../qetgraphicsitem/diagramimageitem.h"
/** /**
@@ -62,7 +62,7 @@ void DiagramEventAddImage::mousePressEvent(QGraphicsSceneMouseEvent *event)
QPointF pos = event->scenePos(); QPointF pos = event->scenePos();
pos.rx() -= m_image->boundingRect().width()/2; pos.rx() -= m_image->boundingRect().width()/2;
pos.ry() -= m_image->boundingRect().height()/2; pos.ry() -= m_image->boundingRect().height()/2;
m_diagram -> undoStack().push (new AddItemCommand<DiagramImageItem *>(m_image, m_diagram, pos)); m_diagram -> undoStack().push (new AddGraphicsObjectCommand(m_image, m_diagram, pos));
for (QGraphicsView *view : m_diagram->views()) { for (QGraphicsView *view : m_diagram->views()) {
view->setContextMenuPolicy((Qt::DefaultContextMenu)); view->setContextMenuPolicy((Qt::DefaultContextMenu));
@@ -88,7 +88,7 @@ void DiagramEventAddImage::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
if (!m_image || event->buttons() != Qt::NoButton) { if (!m_image || event->buttons() != Qt::NoButton) {
return; return;
}; }
QPointF pos = event->scenePos(); QPointF pos = event->scenePos();

View File

@@ -18,7 +18,7 @@
#include "diagrameventaddshape.h" #include "diagrameventaddshape.h"
#include "../diagram.h" #include "../diagram.h"
#include "../diagramcommands.h" #include "../undocommand/addgraphicsobjectcommand.h"
/** /**
@brief DiagramEventAddShape::DiagramEventAddShape @brief DiagramEventAddShape::DiagramEventAddShape
@@ -89,7 +89,7 @@ void DiagramEventAddShape::mousePressEvent(QGraphicsSceneMouseEvent *event)
if (m_shape_item->shapeType() == QetShapeItem::Rectangle || m_shape_item->shapeType() == QetShapeItem::Ellipse) { if (m_shape_item->shapeType() == QetShapeItem::Rectangle || m_shape_item->shapeType() == QetShapeItem::Ellipse) {
m_shape_item->setRect(m_shape_item->rect().normalized()); m_shape_item->setRect(m_shape_item->rect().normalized());
} }
m_diagram->undoStack().push (new AddItemCommand<QetShapeItem *> (m_shape_item, m_diagram)); m_diagram->undoStack().push (new AddGraphicsObjectCommand(m_shape_item, m_diagram));
m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic
} }
//Else add a new point to polyline //Else add a new point to polyline
@@ -192,7 +192,7 @@ void DiagramEventAddShape::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event
m_shape_item->removePoints(); m_shape_item->removePoints();
m_shape_item->setClosed(true); m_shape_item->setClosed(true);
} }
m_diagram->undoStack().push (new AddItemCommand<QetShapeItem *> (m_shape_item, m_diagram)); m_diagram->undoStack().push (new AddGraphicsObjectCommand(m_shape_item, m_diagram));
m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic m_shape_item = nullptr; //< set to nullptr for create new shape at next left clic
event->setAccepted(true); event->setAccepted(true);
} }

View File

@@ -19,7 +19,7 @@
#include "diagrameventaddtext.h" #include "diagrameventaddtext.h"
#include "../diagram.h" #include "../diagram.h"
#include "../diagramcommands.h" #include "../undocommand/addgraphicsobjectcommand.h"
#include "../qetgraphicsitem/independenttextitem.h" #include "../qetgraphicsitem/independenttextitem.h"
/** /**
@@ -46,11 +46,10 @@ void DiagramEventAddText::mousePressEvent(QGraphicsSceneMouseEvent *event)
if (event->button() == Qt::LeftButton) if (event->button() == Qt::LeftButton)
{ {
IndependentTextItem *text = new IndependentTextItem(); IndependentTextItem *text = new IndependentTextItem();
m_diagram -> undoStack().push( m_diagram->undoStack().push(new AddGraphicsObjectCommand(
new AddItemCommand<IndependentTextItem *>( text,
text, m_diagram,
m_diagram, event->scenePos()));
event->scenePos()));
text->setTextInteractionFlags(Qt::TextEditorInteraction); text->setTextInteractionFlags(Qt::TextEditorInteraction);
text->setFocus(Qt::MouseFocusReason); text->setFocus(Qt::MouseFocusReason);
emit finish(); emit finish();

View File

@@ -32,6 +32,7 @@
#include "ui/multipastedialog.h" #include "ui/multipastedialog.h"
#include "undocommand/changetitleblockcommand.h" #include "undocommand/changetitleblockcommand.h"
#include "utils/conductorcreator.h" #include "utils/conductorcreator.h"
#include "undocommand/addgraphicsobjectcommand.h"
#include <QDropEvent> #include <QDropEvent>
@@ -283,16 +284,14 @@ void DiagramView::handleTextDrop(QDropEvent *e) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove
m_diagram -> undoStack().push( m_diagram->undoStack().push(new AddGraphicsObjectCommand(
new AddItemCommand<IndependentTextItem *>( iti, m_diagram, mapToScene(e->pos())));
iti, m_diagram, mapToScene(e->pos())));
#else #else
#if TODO_LIST #if TODO_LIST
#pragma message("@TODO remove code for QT 6 or later") #pragma message("@TODO remove code for QT 6 or later")
#endif #endif
m_diagram -> undoStack().push( m_diagram->undoStack().push(new AddGraphicsObjectCommand(
new AddItemCommand<IndependentTextItem *>( iti, m_diagram, e->position()));
iti, m_diagram, e->position()));
#endif #endif
} }

View File

@@ -19,14 +19,15 @@
#include "conductorautonumerotation.h" #include "conductorautonumerotation.h"
#include "diagram.h" #include "diagram.h"
#include "diagramcommands.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"
#include "qetgraphicsitem/element.h" #include "qetgraphicsitem/element.h"
#include "qetgraphicsitem/elementtextitemgroup.h" #include "qetgraphicsitem/elementtextitemgroup.h"
#include "qetgraphicsitem/independenttextitem.h" #include "qetgraphicsitem/independenttextitem.h"
#include "undocommand/addgraphicsobjectcommand.h"
/** /**
@brief ElementsMover::ElementsMover Constructor @brief ElementsMover::ElementsMover Constructor
@@ -185,7 +186,7 @@ void ElementsMover::endMovement()
Conductor *conductor = new Conductor(pair.first, pair.second); Conductor *conductor = new Conductor(pair.first, pair.second);
//Create an undo object for each new auto conductor, with undo_object for parent //Create an undo object for each new auto conductor, with undo_object for parent
new AddItemCommand<Conductor *>(conductor, diagram_, QPointF(), undo_object); new AddGraphicsObjectCommand(conductor, diagram_, QPointF(), undo_object);
if (undo_object->text().isEmpty()) if (undo_object->text().isEmpty())
undo_object->setText(QObject::tr("Ajouter %n conducteur(s)", "add a numbers of conductor one or more", acc)); undo_object->setText(QObject::tr("Ajouter %n conducteur(s)", "add a numbers of conductor one or more", acc));

View File

@@ -19,7 +19,7 @@
#include "../conductorautonumerotation.h" #include "../conductorautonumerotation.h"
#include "../diagram.h" #include "../diagram.h"
#include "../diagramcommands.h" #include "../undocommand/addgraphicsobjectcommand.h"
#include "../properties/terminaldata.h" #include "../properties/terminaldata.h"
#include "../qetgraphicsitem/conductor.h" #include "../qetgraphicsitem/conductor.h"
#include "../qetgraphicsitem/element.h" #include "../qetgraphicsitem/element.h"
@@ -676,7 +676,7 @@ void Terminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
QUndoCommand *undo = new QUndoCommand(); QUndoCommand *undo = new QUndoCommand();
QUndoCommand *aic = new AddItemCommand<Conductor *>(new_conductor, diagram(), QPointF(), undo); QUndoCommand *aic = new AddGraphicsObjectCommand(new_conductor, diagram(), QPointF(), undo);
undo->setText(aic->text()); undo->setText(aic->text());
if (use_properties) if (use_properties)

View File

@@ -21,7 +21,9 @@
#include "../conductorautonumerotation.h" #include "../conductorautonumerotation.h"
#include "../diagram.h" #include "../diagram.h"
#include "../diagramcommands.h" #include "../diagramcommands.h"
#include "../undocommand/addgraphicsobjectcommand.h"
#include "../qetgraphicsitem/element.h" #include "../qetgraphicsitem/element.h"
#include "../qetgraphicsitem/conductor.h"
#include "../ui_multipastedialog.h" #include "../ui_multipastedialog.h"
#include <QHash> #include <QHash>
@@ -131,7 +133,7 @@ void MultiPasteDialog::on_m_button_box_accepted()
QPair <Terminal *, Terminal *> pair = elmt->AlignedFreeTerminals().takeFirst(); QPair <Terminal *, Terminal *> pair = elmt->AlignedFreeTerminals().takeFirst();
Conductor *conductor = new Conductor(pair.first, pair.second); Conductor *conductor = new Conductor(pair.first, pair.second);
m_diagram->undoStack().push(new AddItemCommand<Conductor *>(conductor, m_diagram, QPointF())); m_diagram->undoStack().push(new AddGraphicsObjectCommand(conductor, m_diagram, QPointF()));
//Autonum the new conductor, the undo command associated for this, have for parent undo_object //Autonum the new conductor, the undo command associated for this, have for parent undo_object
ConductorAutoNumerotation can (conductor, m_diagram); ConductorAutoNumerotation can (conductor, m_diagram);

View File

@@ -0,0 +1,94 @@
/*
Copyright 2006-2021 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 "addgraphicsobjectcommand.h"
#include "../qetgraphicsitem/qetgraphicsitem.h"
#include "../qetgraphicsitem/independenttextitem.h"
#include "../qetgraphicsitem/conductor.h"
#include "../diagram.h"
/**
* @brief AddGraphicsObjectCommand::AddGraphicsObjectCommand
* Default constructor
* @param qgo : QGraphicsObject to manage by this undo command
* @param diagram : Diagram where the graphics object must be added
* @param pos : position of the qgraphics object in the diagram in scene coordinate
* @param parent : parent undo command of this class.
*/
AddGraphicsObjectCommand::AddGraphicsObjectCommand(QGraphicsObject *qgo, Diagram *diagram,
const QPointF &pos, QUndoCommand *parent) :
QUndoCommand(parent),
m_item(qgo),
m_diagram(diagram),
m_pos(pos)
{
setText(QObject::tr("Ajouter ") + itemText());
m_diagram->qgiManager().manage(m_item);
}
/**
* @brief AddGraphicsObjectCommand::~AddGraphicsObjectCommand
*/
AddGraphicsObjectCommand::~AddGraphicsObjectCommand() {
m_diagram->qgiManager().release(m_item);
}
/**
* @brief AddGraphicsObjectCommand::undo
* Reimplemented from QUndoCommand
*/
void AddGraphicsObjectCommand::undo()
{
if (m_item)
{
m_diagram->showMe();
m_diagram->removeItem(m_item);
}
QUndoCommand::undo();
}
/**
* @brief AddGraphicsObjectCommand::redo
* Reimplemented from QUndoCommand
*/
void AddGraphicsObjectCommand::redo()
{
if (m_item)
{
m_diagram->showMe();
m_diagram->addItem(m_item);
m_item->setPos(m_pos);
}
QUndoCommand::redo();
}
/**
* @brief AddGraphicsObjectCommand::itemText
* @return
*/
QString AddGraphicsObjectCommand::itemText() const
{
if (auto qgi = dynamic_cast<QetGraphicsItem *>(m_item.data())) {
return qgi->name();
} else if (dynamic_cast<IndependentTextItem *>(m_item.data())) {
return QObject::tr("un champ texte");
} else if (dynamic_cast<Conductor *>(m_item.data())) {
return QObject::tr("un conducteur");
} else {
return QObject::tr("un element graphique");
}
}

View File

@@ -0,0 +1,52 @@
/*
Copyright 2006-2021 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 ADDGRAPHICSOBJECTCOMMAND_H
#define ADDGRAPHICSOBJECTCOMMAND_H
#include <QUndoCommand>
#include <QPointF>
#include <QPointer>
class QGraphicsObject;
class Diagram;
/**
* @brief The AddGraphicsObjectCommand class
* Undo command to used to add item to a diagram.
*/
class AddGraphicsObjectCommand : public QUndoCommand
{
public:
AddGraphicsObjectCommand(QGraphicsObject *qgo, Diagram *diagram,
const QPointF &pos = QPointF(),
QUndoCommand *parent = nullptr);
~AddGraphicsObjectCommand() override;
void undo() override;
void redo() override;
private:
QString itemText() const;
private:
QPointer<QGraphicsObject> m_item;
Diagram *m_diagram = nullptr;
QPointF m_pos;
};
#endif // ADDGRAPHICSOBJECTCOMMAND_H

View File

@@ -18,7 +18,7 @@
#include "deleteqgraphicsitemcommand.h" #include "deleteqgraphicsitemcommand.h"
#include "../diagram.h" #include "../diagram.h"
#include "../diagramcommands.h" #include "addgraphicsobjectcommand.h"
#include "../qetdiagrameditor.h" #include "../qetdiagrameditor.h"
#include "../qetgraphicsitem/ViewItem/qetgraphicstableitem.h" #include "../qetgraphicsitem/ViewItem/qetgraphicstableitem.h"
#include "../qetgraphicsitem/conductor.h" #include "../qetgraphicsitem/conductor.h"
@@ -202,7 +202,7 @@ void DeleteQGraphicsItemCommand::setPotentialsOfRemovedElements()
#endif #endif
Conductor *new_cond = new Conductor(hub_terminal, t); Conductor *new_cond = new Conductor(hub_terminal, t);
new_cond->setProperties(properties); new_cond->setProperties(properties);
new AddItemCommand<Conductor*>(new_cond, t->diagram(), QPointF(), this); new AddGraphicsObjectCommand(new_cond, t->diagram(), QPointF(), this);
} }
} }
} }

View File

@@ -19,7 +19,7 @@
#include "../conductorautonumerotation.h" #include "../conductorautonumerotation.h"
#include "../diagram.h" #include "../diagram.h"
#include "../diagramcommands.h" #include "../undocommand/addgraphicsobjectcommand.h"
#include "../qetgraphicsitem/conductor.h" #include "../qetgraphicsitem/conductor.h"
#include "../qetgraphicsitem/element.h" #include "../qetgraphicsitem/element.h"
#include "../qetgraphicsitem/terminal.h" #include "../qetgraphicsitem/terminal.h"
@@ -58,7 +58,7 @@ ConductorCreator::ConductorCreator(Diagram *d, QList<Terminal *> terminals_list)
Conductor *cond = new Conductor(hub_terminal, t); Conductor *cond = new Conductor(hub_terminal, t);
cond->setProperties(m_properties); cond->setProperties(m_properties);
cond->setSequenceNum(m_sequential_number); cond->setSequenceNum(m_sequential_number);
d->undoStack().push(new AddItemCommand<Conductor *>(cond, d)); d->undoStack().push(new AddGraphicsObjectCommand(cond, d));
c_list.append(cond); c_list.append(cond);
} }