mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 20:50:34 +01:00
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:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright 2006-2020 The QElectroTech Team
|
||||
Copyright 2006-2021 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
@@ -29,20 +29,6 @@
|
||||
|
||||
#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
|
||||
Constructor
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright 2006-2020 The QElectroTech Team
|
||||
Copyright 2006-2021 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
@@ -33,54 +33,6 @@ class IndependentTextItem;
|
||||
class DiagramImageItem;
|
||||
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
|
||||
This command pastes some content onto a particular diagram.
|
||||
|
||||
@@ -19,9 +19,10 @@
|
||||
|
||||
#include "../conductorautonumerotation.h"
|
||||
#include "../diagram.h"
|
||||
#include "../diagramcommands.h"
|
||||
#include "../undocommand/addgraphicsobjectcommand.h"
|
||||
#include "../factory/elementfactory.h"
|
||||
#include "../qetgraphicsitem/element.h"
|
||||
#include "../qetgraphicsitem/conductor.h"
|
||||
|
||||
/**
|
||||
@brief DiagramEventAddElement::DiagramEventAddElement
|
||||
@@ -228,7 +229,7 @@ void DiagramEventAddElement::addElement()
|
||||
element->m_converted_text_from_xml_description.clear();
|
||||
|
||||
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
|
||||
//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();
|
||||
|
||||
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
|
||||
ConductorAutoNumerotation can (conductor, m_diagram, undo_object);
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#include "diagrameventaddimage.h"
|
||||
|
||||
#include "../diagram.h"
|
||||
#include "../diagramcommands.h"
|
||||
#include "../undocommand/addgraphicsobjectcommand.h"
|
||||
#include "../qetgraphicsitem/diagramimageitem.h"
|
||||
|
||||
/**
|
||||
@@ -62,7 +62,7 @@ void DiagramEventAddImage::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
QPointF pos = event->scenePos();
|
||||
pos.rx() -= m_image->boundingRect().width()/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()) {
|
||||
view->setContextMenuPolicy((Qt::DefaultContextMenu));
|
||||
@@ -88,7 +88,7 @@ void DiagramEventAddImage::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (!m_image || event->buttons() != Qt::NoButton) {
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
QPointF pos = event->scenePos();
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#include "diagrameventaddshape.h"
|
||||
|
||||
#include "../diagram.h"
|
||||
#include "../diagramcommands.h"
|
||||
#include "../undocommand/addgraphicsobjectcommand.h"
|
||||
|
||||
/**
|
||||
@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) {
|
||||
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
|
||||
}
|
||||
//Else add a new point to polyline
|
||||
@@ -192,7 +192,7 @@ void DiagramEventAddShape::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event
|
||||
m_shape_item->removePoints();
|
||||
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
|
||||
event->setAccepted(true);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#include "diagrameventaddtext.h"
|
||||
|
||||
#include "../diagram.h"
|
||||
#include "../diagramcommands.h"
|
||||
#include "../undocommand/addgraphicsobjectcommand.h"
|
||||
#include "../qetgraphicsitem/independenttextitem.h"
|
||||
|
||||
/**
|
||||
@@ -46,11 +46,10 @@ void DiagramEventAddText::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
if (event->button() == Qt::LeftButton)
|
||||
{
|
||||
IndependentTextItem *text = new IndependentTextItem();
|
||||
m_diagram -> undoStack().push(
|
||||
new AddItemCommand<IndependentTextItem *>(
|
||||
text,
|
||||
m_diagram,
|
||||
event->scenePos()));
|
||||
m_diagram->undoStack().push(new AddGraphicsObjectCommand(
|
||||
text,
|
||||
m_diagram,
|
||||
event->scenePos()));
|
||||
text->setTextInteractionFlags(Qt::TextEditorInteraction);
|
||||
text->setFocus(Qt::MouseFocusReason);
|
||||
emit finish();
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "ui/multipastedialog.h"
|
||||
#include "undocommand/changetitleblockcommand.h"
|
||||
#include "utils/conductorcreator.h"
|
||||
#include "undocommand/addgraphicsobjectcommand.h"
|
||||
|
||||
#include <QDropEvent>
|
||||
|
||||
@@ -283,16 +284,14 @@ void DiagramView::handleTextDrop(QDropEvent *e) {
|
||||
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove
|
||||
|
||||
m_diagram -> undoStack().push(
|
||||
new AddItemCommand<IndependentTextItem *>(
|
||||
iti, m_diagram, mapToScene(e->pos())));
|
||||
m_diagram->undoStack().push(new AddGraphicsObjectCommand(
|
||||
iti, m_diagram, mapToScene(e->pos())));
|
||||
#else
|
||||
#if TODO_LIST
|
||||
#pragma message("@TODO remove code for QT 6 or later")
|
||||
#endif
|
||||
m_diagram -> undoStack().push(
|
||||
new AddItemCommand<IndependentTextItem *>(
|
||||
iti, m_diagram, e->position()));
|
||||
m_diagram->undoStack().push(new AddGraphicsObjectCommand(
|
||||
iti, m_diagram, e->position()));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -19,14 +19,15 @@
|
||||
|
||||
#include "conductorautonumerotation.h"
|
||||
#include "diagram.h"
|
||||
#include "diagramcommands.h"
|
||||
#include "qetgraphicsitem/conductor.h"
|
||||
#include "diagramcommands.h"
|
||||
#include "qetgraphicsitem/conductortextitem.h"
|
||||
#include "qetgraphicsitem/diagramimageitem.h"
|
||||
#include "qetgraphicsitem/dynamicelementtextitem.h"
|
||||
#include "qetgraphicsitem/element.h"
|
||||
#include "qetgraphicsitem/elementtextitemgroup.h"
|
||||
#include "qetgraphicsitem/independenttextitem.h"
|
||||
#include "undocommand/addgraphicsobjectcommand.h"
|
||||
|
||||
/**
|
||||
@brief ElementsMover::ElementsMover Constructor
|
||||
@@ -185,7 +186,7 @@ void ElementsMover::endMovement()
|
||||
Conductor *conductor = new Conductor(pair.first, pair.second);
|
||||
|
||||
//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())
|
||||
undo_object->setText(QObject::tr("Ajouter %n conducteur(s)", "add a numbers of conductor one or more", acc));
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
#include "../conductorautonumerotation.h"
|
||||
#include "../diagram.h"
|
||||
#include "../diagramcommands.h"
|
||||
#include "../undocommand/addgraphicsobjectcommand.h"
|
||||
#include "../properties/terminaldata.h"
|
||||
#include "../qetgraphicsitem/conductor.h"
|
||||
#include "../qetgraphicsitem/element.h"
|
||||
@@ -676,7 +676,7 @@ void Terminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
|
||||
|
||||
|
||||
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());
|
||||
|
||||
if (use_properties)
|
||||
|
||||
@@ -21,7 +21,9 @@
|
||||
#include "../conductorautonumerotation.h"
|
||||
#include "../diagram.h"
|
||||
#include "../diagramcommands.h"
|
||||
#include "../undocommand/addgraphicsobjectcommand.h"
|
||||
#include "../qetgraphicsitem/element.h"
|
||||
#include "../qetgraphicsitem/conductor.h"
|
||||
#include "../ui_multipastedialog.h"
|
||||
|
||||
#include <QHash>
|
||||
@@ -131,7 +133,7 @@ void MultiPasteDialog::on_m_button_box_accepted()
|
||||
QPair <Terminal *, Terminal *> pair = elmt->AlignedFreeTerminals().takeFirst();
|
||||
|
||||
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
|
||||
ConductorAutoNumerotation can (conductor, m_diagram);
|
||||
|
||||
94
sources/undocommand/addgraphicsobjectcommand.cpp
Normal file
94
sources/undocommand/addgraphicsobjectcommand.cpp
Normal 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");
|
||||
}
|
||||
}
|
||||
52
sources/undocommand/addgraphicsobjectcommand.h
Normal file
52
sources/undocommand/addgraphicsobjectcommand.h
Normal 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
|
||||
@@ -18,7 +18,7 @@
|
||||
#include "deleteqgraphicsitemcommand.h"
|
||||
|
||||
#include "../diagram.h"
|
||||
#include "../diagramcommands.h"
|
||||
#include "addgraphicsobjectcommand.h"
|
||||
#include "../qetdiagrameditor.h"
|
||||
#include "../qetgraphicsitem/ViewItem/qetgraphicstableitem.h"
|
||||
#include "../qetgraphicsitem/conductor.h"
|
||||
@@ -202,7 +202,7 @@ void DeleteQGraphicsItemCommand::setPotentialsOfRemovedElements()
|
||||
#endif
|
||||
Conductor *new_cond = new Conductor(hub_terminal, t);
|
||||
new_cond->setProperties(properties);
|
||||
new AddItemCommand<Conductor*>(new_cond, t->diagram(), QPointF(), this);
|
||||
new AddGraphicsObjectCommand(new_cond, t->diagram(), QPointF(), this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
#include "../conductorautonumerotation.h"
|
||||
#include "../diagram.h"
|
||||
#include "../diagramcommands.h"
|
||||
#include "../undocommand/addgraphicsobjectcommand.h"
|
||||
#include "../qetgraphicsitem/conductor.h"
|
||||
#include "../qetgraphicsitem/element.h"
|
||||
#include "../qetgraphicsitem/terminal.h"
|
||||
@@ -58,7 +58,7 @@ ConductorCreator::ConductorCreator(Diagram *d, QList<Terminal *> terminals_list)
|
||||
Conductor *cond = new Conductor(hub_terminal, t);
|
||||
cond->setProperties(m_properties);
|
||||
cond->setSequenceNum(m_sequential_number);
|
||||
d->undoStack().push(new AddItemCommand<Conductor *>(cond, d));
|
||||
d->undoStack().push(new AddGraphicsObjectCommand(cond, d));
|
||||
|
||||
c_list.append(cond);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user