diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index 13cb89c24..0125d9c9a 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -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 -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 diff --git a/sources/diagramcommands.h b/sources/diagramcommands.h index 822359be0..08bd9e9d3 100644 --- a/sources/diagramcommands.h +++ b/sources/diagramcommands.h @@ -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 -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. diff --git a/sources/diagramevent/diagrameventaddelement.cpp b/sources/diagramevent/diagrameventaddelement.cpp index c0f8a2ee2..379bdaf13 100644 --- a/sources/diagramevent/diagrameventaddelement.cpp +++ b/sources/diagramevent/diagrameventaddelement.cpp @@ -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, 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 pair = element -> AlignedFreeTerminals().takeFirst(); Conductor *conductor = new Conductor(pair.first, pair.second); - new AddItemCommand(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); diff --git a/sources/diagramevent/diagrameventaddimage.cpp b/sources/diagramevent/diagrameventaddimage.cpp index 3184fe63f..0b16d9e7c 100644 --- a/sources/diagramevent/diagrameventaddimage.cpp +++ b/sources/diagramevent/diagrameventaddimage.cpp @@ -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(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(); diff --git a/sources/diagramevent/diagrameventaddshape.cpp b/sources/diagramevent/diagrameventaddshape.cpp index 146aaa954..3222a08b5 100644 --- a/sources/diagramevent/diagrameventaddshape.cpp +++ b/sources/diagramevent/diagrameventaddshape.cpp @@ -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 (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 (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); } diff --git a/sources/diagramevent/diagrameventaddtext.cpp b/sources/diagramevent/diagrameventaddtext.cpp index 9613707ba..af1071750 100644 --- a/sources/diagramevent/diagrameventaddtext.cpp +++ b/sources/diagramevent/diagrameventaddtext.cpp @@ -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( - 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(); diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index 7bb72f4a6..45ea8ad96 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -32,6 +32,7 @@ #include "ui/multipastedialog.h" #include "undocommand/changetitleblockcommand.h" #include "utils/conductorcreator.h" +#include "undocommand/addgraphicsobjectcommand.h" #include @@ -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( - 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( - iti, m_diagram, e->position())); + m_diagram->undoStack().push(new AddGraphicsObjectCommand( + iti, m_diagram, e->position())); #endif } diff --git a/sources/elementsmover.cpp b/sources/elementsmover.cpp index 0f14f838d..7c5386d0f 100644 --- a/sources/elementsmover.cpp +++ b/sources/elementsmover.cpp @@ -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, 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)); diff --git a/sources/qetgraphicsitem/terminal.cpp b/sources/qetgraphicsitem/terminal.cpp index ff382f265..2f9e41609 100644 --- a/sources/qetgraphicsitem/terminal.cpp +++ b/sources/qetgraphicsitem/terminal.cpp @@ -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(new_conductor, diagram(), QPointF(), undo); + QUndoCommand *aic = new AddGraphicsObjectCommand(new_conductor, diagram(), QPointF(), undo); undo->setText(aic->text()); if (use_properties) diff --git a/sources/ui/multipastedialog.cpp b/sources/ui/multipastedialog.cpp index 1da333af2..122b21ae1 100644 --- a/sources/ui/multipastedialog.cpp +++ b/sources/ui/multipastedialog.cpp @@ -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 @@ -131,7 +133,7 @@ void MultiPasteDialog::on_m_button_box_accepted() QPair pair = elmt->AlignedFreeTerminals().takeFirst(); Conductor *conductor = new Conductor(pair.first, pair.second); - m_diagram->undoStack().push(new AddItemCommand(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); diff --git a/sources/undocommand/addgraphicsobjectcommand.cpp b/sources/undocommand/addgraphicsobjectcommand.cpp new file mode 100644 index 000000000..f17a1b2fe --- /dev/null +++ b/sources/undocommand/addgraphicsobjectcommand.cpp @@ -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 . +*/ +#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(m_item.data())) { + return qgi->name(); + } else if (dynamic_cast(m_item.data())) { + return QObject::tr("un champ texte"); + } else if (dynamic_cast(m_item.data())) { + return QObject::tr("un conducteur"); + } else { + return QObject::tr("un element graphique"); + } +} diff --git a/sources/undocommand/addgraphicsobjectcommand.h b/sources/undocommand/addgraphicsobjectcommand.h new file mode 100644 index 000000000..5796b14b7 --- /dev/null +++ b/sources/undocommand/addgraphicsobjectcommand.h @@ -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 . +*/ +#ifndef ADDGRAPHICSOBJECTCOMMAND_H +#define ADDGRAPHICSOBJECTCOMMAND_H + +#include +#include +#include + +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 m_item; + Diagram *m_diagram = nullptr; + QPointF m_pos; +}; + +#endif // ADDGRAPHICSOBJECTCOMMAND_H diff --git a/sources/undocommand/deleteqgraphicsitemcommand.cpp b/sources/undocommand/deleteqgraphicsitemcommand.cpp index 6b4855942..de5d5ccda 100644 --- a/sources/undocommand/deleteqgraphicsitemcommand.cpp +++ b/sources/undocommand/deleteqgraphicsitemcommand.cpp @@ -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(new_cond, t->diagram(), QPointF(), this); + new AddGraphicsObjectCommand(new_cond, t->diagram(), QPointF(), this); } } } diff --git a/sources/utils/conductorcreator.cpp b/sources/utils/conductorcreator.cpp index 144c1484d..d384540f5 100644 --- a/sources/utils/conductorcreator.cpp +++ b/sources/utils/conductorcreator.cpp @@ -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 terminals_list) Conductor *cond = new Conductor(hub_terminal, t); cond->setProperties(m_properties); cond->setSequenceNum(m_sequential_number); - d->undoStack().push(new AddItemCommand(cond, d)); + d->undoStack().push(new AddGraphicsObjectCommand(cond, d)); c_list.append(cond); }