diff --git a/sources/diagramcontent.cpp b/sources/diagramcontent.cpp index 607350a80..6cd211185 100644 --- a/sources/diagramcontent.cpp +++ b/sources/diagramcontent.cpp @@ -174,6 +174,31 @@ QList DiagramContent::conductors(int filter) const return(result.toList()); } +/** + * @brief DiagramContent::hasDeletableItems + * @return true if this diagram content have deletable item + * The deletable items correspond to the selected items of diagram + * at the moment of the creation of this DiagramContent, + * with the constructor : DiagramContent::DiagramContent(Diagram *diagram) + */ +bool DiagramContent::hasDeletableItems() const +{ + for(QGraphicsItem *qgi : m_selected_items) + { + if (qgi->type() == Element::Type || + qgi->type() == Conductor::Type || + qgi->type() == IndependentTextItem::Type || + qgi->type() == QetShapeItem::Type || + qgi->type() == DiagramImageItem::Type || + qgi->type() == DynamicElementTextItem::Type) + return true; + if(qgi->type() == QGraphicsItemGroup::Type) + if(dynamic_cast(qgi)) + return true; + } + return(false); +} + /** * @brief DiagramContent::clear * Remove all items from the diagram content diff --git a/sources/diagramcontent.h b/sources/diagramcontent.h index df3dc9cf6..ec0e1b63d 100644 --- a/sources/diagramcontent.h +++ b/sources/diagramcontent.h @@ -79,6 +79,7 @@ class DiagramContent QList selectedTexts() const; QList selectedTextsGroup() const; QList conductors(int = AnyConductor) const; + bool hasDeletableItems() const; QList items(int = All) const; QString sentence(int = All) const; int count(int = All) const; diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index f06b185a1..cc513d0df 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -126,20 +126,6 @@ void DiagramView::selectInvert() { m_diagram -> invertSelection(); } -/** - * @brief DiagramView::deleteSelection - * Delete the selected items - */ -void DiagramView::deleteSelection() -{ - if (m_diagram -> isReadOnly()) - return; - DiagramContent removed_content = DiagramContent(m_diagram); - m_diagram->clearSelection(); - m_diagram->undoStack().push(new DeleteQGraphicsItemCommand(m_diagram, removed_content)); - adjustSceneRect(); -} - /** Accepte ou refuse le drag'n drop en fonction du type de donnees entrant @param e le QDragEnterEvent correspondant au drag'n drop tente @@ -750,25 +736,6 @@ bool DiagramView::hasTextItems() { return(false); } -/** - * @brief DiagramView::hasDeletableItems - * @return True if a least on of selected item can be deleted - */ -bool DiagramView::hasDeletableItems() -{ - for(QGraphicsItem *qgi : m_diagram->selectedItems()) - { - if (qgi->type() == Element::Type || - qgi->type() == Conductor::Type || - qgi->type() == IndependentTextItem::Type || - qgi->type() == QetShapeItem::Type || - qgi->type() == DiagramImageItem::Type || - qgi->type() == DynamicElementTextItem::Type) - return true; - } - return(false); -} - /** Ajoute une colonne au schema. */ diff --git a/sources/diagramview.h b/sources/diagramview.h index e821b18ec..2f676ab6a 100644 --- a/sources/diagramview.h +++ b/sources/diagramview.h @@ -68,7 +68,6 @@ class DiagramView : public QGraphicsView QETDiagramEditor *diagramEditor() const; bool hasCopiableItems(); bool hasTextItems(); - bool hasDeletableItems(); void editSelection(); void setEventInterface (DVEventInterface *event_interface); @@ -119,7 +118,6 @@ class DiagramView : public QGraphicsView void selectNothing(); void selectAll(); void selectInvert(); - void deleteSelection(); void setVisualisationMode(); void setSelectionMode(); void zoom(const qreal zoom_factor); diff --git a/sources/qetdiagrameditor.cpp b/sources/qetdiagrameditor.cpp index 03359d200..0ab0a568e 100644 --- a/sources/qetdiagrameditor.cpp +++ b/sources/qetdiagrameditor.cpp @@ -47,6 +47,7 @@ #include "elementtextitem.h" #include "undocommand/rotateselectioncommand.h" #include "rotatetextscommand.h" +#include "diagramcommands.h" #include #include @@ -1261,23 +1262,25 @@ void QETDiagramEditor::selectionGroupTriggered(QAction *action) { QString value = action->data().toString(); DiagramView *dv = currentDiagram(); + Diagram *diagram = dv->diagram(); + DiagramContent dc(diagram); if (!dv || value.isEmpty()) return; if (value == "delete_selection") - dv->deleteSelection(); + { + diagram->clearSelection(); + diagram->undoStack().push(new DeleteQGraphicsItemCommand(diagram, dc)); + dv->adjustSceneRect(); + } else if (value == "rotate_selection") { - Diagram *d = dv->diagram(); - RotateSelectionCommand *c = new RotateSelectionCommand(d); + RotateSelectionCommand *c = new RotateSelectionCommand(diagram); if(c->isValid()) - d->undoStack().push(c); + diagram->undoStack().push(c); } else if (value == "rotate_selected_text") - { - Diagram *d = dv->diagram(); - d->undoStack().push(new RotateTextsCommand(d)); - } + diagram->undoStack().push(new RotateTextsCommand(diagram)); else if (value == "find_selected_element" && currentCustomElement()) findElementInPanel(currentCustomElement()->location()); else if (value == "edit_selected_element") @@ -1427,7 +1430,7 @@ void QETDiagramEditor::slot_updateComplexActions() //Action that need items (elements, conductors, texts...) selected, to be enabled bool copiable_items = dv->hasCopiableItems(); - bool deletable_items = dv->hasDeletableItems(); + bool deletable_items = dc.hasDeletableItems(); m_cut -> setEnabled(!ro && copiable_items); m_copy -> setEnabled(copiable_items); m_delete_selection -> setEnabled(!ro && deletable_items); diff --git a/sources/ui/dynamicelementtextmodel.cpp b/sources/ui/dynamicelementtextmodel.cpp index 5baf6c38f..5950e6287 100644 --- a/sources/ui/dynamicelementtextmodel.cpp +++ b/sources/ui/dynamicelementtextmodel.cpp @@ -452,7 +452,7 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem quc->setText(tr("Modifier la couleur d'un texte d'élément")); } - bool frame = text_qsi->child(4,1)->checkState() == Qt::Checked? frame=true : frame=false; + bool frame = text_qsi->child(4,1)->checkState() == Qt::Checked? true : false; if(frame != deti->frame()) { QUndoCommand *quc = new QPropertyUndoCommand(deti, "frame", QVariant(deti->frame()), QVariant(frame), undo); diff --git a/sources/undocommand/deleteqgraphicsitemcommand.cpp b/sources/undocommand/deleteqgraphicsitemcommand.cpp index e68d80017..2fdac7cca 100644 --- a/sources/undocommand/deleteqgraphicsitemcommand.cpp +++ b/sources/undocommand/deleteqgraphicsitemcommand.cpp @@ -22,6 +22,7 @@ #include "conductor.h" #include "conductortextitem.h" #include "elementtextitemgroup.h" +#include "addelementtextcommand.h" /** * @brief DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand @@ -44,6 +45,8 @@ DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand(Diagram *diagram, const D } + //When remove a deti we must to know his parent item, for re-add deti as child of the parent + //when undo this command for(DynamicElementTextItem *deti : m_removed_contents.m_element_texts) { if(deti->parentGroup()) @@ -52,6 +55,21 @@ DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand(Diagram *diagram, const D m_elmt_text_hash.insert(deti, deti->parentElement()); } + //If parent element of ElementTextItemGroup is also in @m_removed_content, + //we remove it, because when the element will be removed from the scene every child's will also be removed. + const QSet group_set = m_removed_contents.m_texts_groups; + for(ElementTextItemGroup *group : group_set) + { + if(m_removed_contents.m_elements.contains(group->parentElement())) + m_removed_contents.m_texts_groups.remove(group); + } + + //The deletion of the groups is not managed by this undo, but by a RemoveTextsGroupCommand + for(ElementTextItemGroup *group : m_removed_contents.m_texts_groups) { + new RemoveTextsGroupCommand(group->parentElement(), group, this);} + + m_removed_contents.m_texts_groups.clear(); + setText(QString(QObject::tr("supprimer %1", "undo caption - %1 is a sentence listing the removed content")).arg(m_removed_contents.sentence(DiagramContent::All))); m_diagram->qgiManager().manage(m_removed_contents.items(DiagramContent::All)); } @@ -87,6 +105,8 @@ void DeleteQGraphicsItemCommand::undo() elmt->addTextToGroup(deti, m_grp_texts_hash.value(deti)); } } + + QUndoCommand::undo(); } /** @@ -131,4 +151,6 @@ void DeleteQGraphicsItemCommand::redo() for(QGraphicsItem *item : m_removed_contents.items()) m_diagram->removeItem(item); + + QUndoCommand::redo(); }