From 0015f4a69463e3b3cc958bb77c4cd057695a1212 Mon Sep 17 00:00:00 2001 From: Claveau Joshua Date: Tue, 12 May 2020 11:17:25 +0200 Subject: [PATCH] Qet graphics table item can be removed from diagram --- .../ElementsCollection/ui/renamedialog.cpp | 2 +- sources/conductorproperties.cpp | 2 +- sources/conductorproperties.h | 1 + sources/diagramcontent.cpp | 109 ++++++++++-------- sources/qet.cpp | 12 +- sources/qet.h | 6 +- .../ViewItem/qetgraphicstableitem.cpp | 18 +++ .../ViewItem/qetgraphicstableitem.h | 1 + sources/ui/aboutqetdialog.cpp | 2 + .../deleteqgraphicsitemcommand.cpp | 45 ++++++++ .../undocommand/deleteqgraphicsitemcommand.h | 8 ++ 11 files changed, 154 insertions(+), 52 deletions(-) diff --git a/sources/ElementsCollection/ui/renamedialog.cpp b/sources/ElementsCollection/ui/renamedialog.cpp index ba08d0412..ccefd13ac 100644 --- a/sources/ElementsCollection/ui/renamedialog.cpp +++ b/sources/ElementsCollection/ui/renamedialog.cpp @@ -17,7 +17,7 @@ */ #include "renamedialog.h" -#include +#include #include "ui_renamedialog.h" RenameDialog::RenameDialog(QString path, QWidget *parent) : diff --git a/sources/conductorproperties.cpp b/sources/conductorproperties.cpp index 300c3e4e2..e6686281a 100644 --- a/sources/conductorproperties.cpp +++ b/sources/conductorproperties.cpp @@ -17,7 +17,7 @@ */ #include "conductorproperties.h" #include -#include +#include /** Constructeur par defaut diff --git a/sources/conductorproperties.h b/sources/conductorproperties.h index fdcbfc328..e392359bf 100644 --- a/sources/conductorproperties.h +++ b/sources/conductorproperties.h @@ -20,6 +20,7 @@ #include "qet.h" #include +#include class QPainter; diff --git a/sources/diagramcontent.cpp b/sources/diagramcontent.cpp index 4a3478fd2..62fb3efad 100644 --- a/sources/diagramcontent.cpp +++ b/sources/diagramcontent.cpp @@ -27,6 +27,7 @@ #include "diagram.h" #include "terminal.h" #include "conductortextitem.h" +#include "qetgraphicstableitem.h" /** * @brief DiagramContent::DiagramContent @@ -49,39 +50,41 @@ DiagramContent::DiagramContent(Diagram *diagram, bool selected) : item_list = diagram->items(); } - - for (QGraphicsItem *item : item_list) + for (auto item : item_list) { - if (Element *elmt = qgraphicsitem_cast(item)) - m_elements << elmt; - else if (IndependentTextItem *iti = qgraphicsitem_cast(item)) - m_text_fields << iti; - else if (Conductor *c = qgraphicsitem_cast(item)) + switch (item->type()) { - //Get the isolated selected conductor (= not movable, but deletable) - if (!c->terminal1->parentItem()->isSelected() &&\ - !c->terminal2->parentItem()->isSelected()) { - m_other_conductors << c; - } - - if (m_potential_conductors.isEmpty()) { - m_potential_conductors << c; - } - else + case Element::Type: { m_elements << qgraphicsitem_cast(item); break;} + case IndependentTextItem::Type: { m_text_fields << qgraphicsitem_cast(item); break;} + case Conductor::Type: { - if (!potentialIsManaged(c->relatedPotentialConductors(true).toList())) + auto c = qgraphicsitem_cast(item); + //Get the isolated selected conductor (= not movable, but deletable) + if (!c->terminal1->parentItem()->isSelected() &&\ + !c->terminal2->parentItem()->isSelected()) { + m_other_conductors << c; + } + + if (m_potential_conductors.isEmpty()) { m_potential_conductors << c; + } else { + if (!potentialIsManaged(c->relatedPotentialConductors(true).toList())) { + m_potential_conductors << c; + } + } + break; } + case DiagramImageItem::Type: { m_images << qgraphicsitem_cast(item); break;} + case QetShapeItem::Type: { m_shapes << qgraphicsitem_cast(item); break;} + case DynamicElementTextItem::Type: { m_element_texts << qgraphicsitem_cast(item); break;} + case QGraphicsItemGroup::Type: { + if (auto *group = dynamic_cast(item)) { + m_texts_groups << group; + } + break; + } + case QetGraphicsTableItem::Type: { m_tables << qgraphicsitem_cast(item); break;} } - else if (DiagramImageItem *dii = qgraphicsitem_cast(item)) - m_images << dii; - else if (QetShapeItem *dsi = qgraphicsitem_cast(item)) - m_shapes << dsi; - else if (DynamicElementTextItem *deti = qgraphicsitem_cast(item)) - m_element_texts << deti; - else if (QGraphicsItemGroup *group = qgraphicsitem_cast(item)) - if(ElementTextItemGroup *etig = dynamic_cast(group)) - m_texts_groups << etig; } @@ -100,7 +103,7 @@ DiagramContent::DiagramContent(Diagram *diagram, bool selected) : else other_terminal = conductor->terminal1; - //If the two elements of conductor are movable + //If the two elements of conductor are movable if (m_elements.contains(other_terminal -> parentElement())) { if (!m_conductors_to_move.contains(conductor)) m_conductors_to_move << conductor; @@ -129,7 +132,8 @@ DiagramContent::DiagramContent(const DiagramContent &other) : m_potential_conductors(other.m_potential_conductors), m_element_texts(other.m_element_texts), m_texts_groups(other.m_texts_groups), - m_selected_items(other.m_selected_items) + m_selected_items(other.m_selected_items), + m_tables(other.m_tables) {} DiagramContent::~DiagramContent() @@ -211,7 +215,8 @@ bool DiagramContent::hasDeletableItems() const qgi->type() == IndependentTextItem::Type || qgi->type() == QetShapeItem::Type || qgi->type() == DiagramImageItem::Type || - qgi->type() == DynamicElementTextItem::Type) + qgi->type() == DynamicElementTextItem::Type || + qgi->type() == QetGraphicsTableItem::Type) return true; if(qgi->type() == QGraphicsItemGroup::Type) if(dynamic_cast(qgi)) @@ -250,6 +255,7 @@ void DiagramContent::clear() m_element_texts.clear(); m_texts_groups.clear(); m_selected_items.clear(); + m_tables.clear(); } /** @@ -338,6 +344,10 @@ DiagramContent &DiagramContent::operator+=(const DiagramContent &other) m_potential_conductors << c; } } + + for (auto table : other.m_tables) + if (!m_tables.contains(table)) + m_tables << table; return *this; } @@ -386,12 +396,13 @@ QList DiagramContent::items(int filter) const for(QGraphicsItem *qgi : conductors(filter)) items_list << qgi; - if (filter & Elements) for(QGraphicsItem *qgi : m_elements) items_list << qgi; - if (filter & TextFields) for(QGraphicsItem *qgi : m_text_fields) items_list << qgi; - if (filter & Images) for(QGraphicsItem *qgi : m_images) items_list << qgi; - if (filter & Shapes) for(QGraphicsItem *qgi : m_shapes) items_list << qgi; - if (filter & ElementTextFields) for(QGraphicsItem *qgi : m_element_texts) items_list << qgi; - if (filter & TextGroup) for(QGraphicsItem *qgi : m_texts_groups) items_list << qgi; + if (filter & Elements) for(auto qgi : m_elements) items_list << qgi; + if (filter & TextFields) for(auto qgi : m_text_fields) items_list << qgi; + if (filter & Images) for(auto qgi : m_images) items_list << qgi; + if (filter & Shapes) for(auto qgi : m_shapes) items_list << qgi; + if (filter & ElementTextFields) for(auto qgi : m_element_texts) items_list << qgi; + if (filter & TextGroup) for(auto qgi : m_texts_groups) items_list << qgi; + if (filter & Tables) for(auto qgi : m_tables) items_list << qgi; if (filter & SelectedOnly) { for(QGraphicsItem *qgi : items_list) { @@ -410,15 +421,16 @@ int DiagramContent::count(int filter) const { int count = 0; if (filter & SelectedOnly) { - if (filter & Elements) for(Element *element : m_elements) { if (element -> isSelected()) ++ count; } - if (filter & TextFields) for(DiagramTextItem *dti : m_text_fields) { if (dti -> isSelected()) ++ count; } - if (filter & Images) for(DiagramImageItem *dii : m_images) { if (dii -> isSelected()) ++ count; } - if (filter & Shapes) for(QetShapeItem *dsi : m_shapes) { if (dsi -> isSelected()) ++ count; } - if (filter & ConductorsToMove) for(Conductor *conductor : m_conductors_to_move) { if (conductor -> isSelected()) ++ count; } - if (filter & ConductorsToUpdate) for(Conductor *conductor : m_conductors_to_update) { if (conductor -> isSelected()) ++ count; } - if (filter & OtherConductors) for(Conductor *conductor : m_other_conductors) { if (conductor -> isSelected()) ++ count; } - if (filter & ElementTextFields) for(DynamicElementTextItem *deti : m_element_texts) { if (deti -> isSelected()) ++ count; } - if (filter & TextGroup) for(ElementTextItemGroup *etig : m_texts_groups) { if (etig -> isSelected()) ++ count; } + if (filter & Elements) for(auto element : m_elements) { if (element -> isSelected()) ++ count; } + if (filter & TextFields) for(auto dti : m_text_fields) { if (dti -> isSelected()) ++ count; } + if (filter & Images) for(auto dii : m_images) { if (dii -> isSelected()) ++ count; } + if (filter & Shapes) for(auto dsi : m_shapes) { if (dsi -> isSelected()) ++ count; } + if (filter & ConductorsToMove) for(auto conductor : m_conductors_to_move) { if (conductor -> isSelected()) ++ count; } + if (filter & ConductorsToUpdate) for(auto conductor : m_conductors_to_update) { if (conductor -> isSelected()) ++ count; } + if (filter & OtherConductors) for(auto conductor : m_other_conductors) { if (conductor -> isSelected()) ++ count; } + if (filter & ElementTextFields) for(auto deti : m_element_texts) { if (deti -> isSelected()) ++ count; } + if (filter & TextGroup) for(auto etig : m_texts_groups) { if (etig -> isSelected()) ++ count; } + if (filter & Tables) for(auto table : m_tables) { if (table -> isSelected()) ++ count; } } else { if (filter & Elements) count += m_elements.count(); @@ -430,6 +442,7 @@ int DiagramContent::count(int filter) const if (filter & OtherConductors) count += m_other_conductors.count(); if (filter & ElementTextFields) count += m_element_texts.count(); if (filter & TextGroup) count += m_texts_groups.count(); + if (filter & Tables) count += m_tables.count(); } return(count); } @@ -448,6 +461,7 @@ QString DiagramContent::sentence(int filter) const int images_count = (filter & Images) ? m_images.count() : 0; int shapes_count = (filter & Shapes) ? m_shapes.count() : 0; int elmt_text_count = (filter & ElementTextFields) ? m_element_texts.count() : 0; + int tables_count = (filter & Tables) ? m_tables.count() : 0; return( QET::ElementsAndConductorsSentence( @@ -456,7 +470,8 @@ QString DiagramContent::sentence(int filter) const textfields_count, images_count, shapes_count, - elmt_text_count + elmt_text_count, + tables_count ) ); } @@ -468,7 +483,7 @@ QString DiagramContent::sentence(int filter) const * @return */ QDebug &operator<<(QDebug d, DiagramContent &content) { - Q_UNUSED(content); + Q_UNUSED(content) d << "DiagramContent {" << "\n"; /* FIXME Le double-heritage QObject / QGraphicsItem a casse cet operateur diff --git a/sources/qet.cpp b/sources/qet.cpp index b019324c0..83e716afe 100644 --- a/sources/qet.cpp +++ b/sources/qet.cpp @@ -23,6 +23,7 @@ #include #include #include +#include /** Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w") @@ -237,7 +238,8 @@ bool QET::attributeIsAReal(const QDomElement &e, const QString& nom_attribut, qr @return la proposition decrivant le nombre d'elements, de conducteurs et de textes */ -QString QET::ElementsAndConductorsSentence(int elements_count, int conductors_count, int texts_count, int images_count, int shapes_count, int element_text_count) { +QString QET::ElementsAndConductorsSentence(int elements_count, int conductors_count, int texts_count, int images_count, int shapes_count, int element_text_count, int tables_count) +{ QString text; if (elements_count) { text += QObject::tr( @@ -290,6 +292,14 @@ QString QET::ElementsAndConductorsSentence(int elements_count, int conductors_co "part of a sentence listing the content of a diagram", element_text_count); } + + if (tables_count) { + if (!text.isEmpty()) text += ", "; + text += QObject::tr( + "%n tableau(s)", + "part of a sentence listing the content of diagram", + tables_count); + } return(text); } diff --git a/sources/qet.h b/sources/qet.h index 5b4235e11..331271d06 100644 --- a/sources/qet.h +++ b/sources/qet.h @@ -17,7 +17,9 @@ */ #ifndef _QET_H #define _QET_H -#include + +#include +#include #include class QActionGroup; @@ -160,7 +162,7 @@ namespace QET { bool orthogonalProjection(const QPointF &, const QLineF &, QPointF * = nullptr); bool attributeIsAnInteger(const QDomElement &, const QString& , int * = nullptr); bool attributeIsAReal(const QDomElement &, const QString& , qreal * = nullptr); - QString ElementsAndConductorsSentence(int, int, int = 0, int = 0, int = 0, int = 0); + QString ElementsAndConductorsSentence(int elements=0, int conductors=0, int indi_texts=0, int images=0, int shapes=0, int element_text=0, int tables_count=0); QList findInDomElement(const QDomElement &, const QString &); QList findInDomElement(const QDomElement &, const QString &, const QString &); QList forbiddenCharacters(); diff --git a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp index 85574aa8e..2936ef74e 100644 --- a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp +++ b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.cpp @@ -544,6 +544,24 @@ bool QetGraphicsTableItem::sceneEventFilter(QGraphicsItem *watched, QEvent *even return false; } +/** + * @brief QetGraphicsTableItem::itemChange + * @param change + * @param value + * @return + */ +QVariant QetGraphicsTableItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) +{ + //item was removed from scene, we remove the handler + if (change == ItemSceneHasChanged) { + if (!scene() && m_handler_item.scene()) { + m_handler_item.scene()->removeItem(&m_handler_item); + } + } + + return QetGraphicsItem::itemChange(change, value); +} + void QetGraphicsTableItem::modelReseted() { dataChanged(m_model->index(0,0), m_model->index(0,0), QVector()); setToMinimumHeight(); diff --git a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h index 93a16ee28..017f76533 100644 --- a/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h +++ b/sources/qetgraphicsitem/ViewItem/qetgraphicstableitem.h @@ -83,6 +83,7 @@ class QetGraphicsTableItem : public QetGraphicsItem virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override; virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override; virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override; + virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; private: void modelReseted(); diff --git a/sources/ui/aboutqetdialog.cpp b/sources/ui/aboutqetdialog.cpp index 96911bc88..0c510dfbf 100644 --- a/sources/ui/aboutqetdialog.cpp +++ b/sources/ui/aboutqetdialog.cpp @@ -19,6 +19,8 @@ #include "ui_aboutqetdialog.h" #include "qet.h" +#include + AboutQETDialog::AboutQETDialog(QWidget *parent) : QDialog(parent), ui(new Ui::AboutQETDialog) diff --git a/sources/undocommand/deleteqgraphicsitemcommand.cpp b/sources/undocommand/deleteqgraphicsitemcommand.cpp index 1d6f4b25a..ab48ffe88 100644 --- a/sources/undocommand/deleteqgraphicsitemcommand.cpp +++ b/sources/undocommand/deleteqgraphicsitemcommand.cpp @@ -25,6 +25,7 @@ #include "addelementtextcommand.h" #include "terminal.h" #include "diagramcommands.h" +#include "qetgraphicstableitem.h" /** * @brief DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand @@ -73,6 +74,15 @@ DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand(Diagram *diagram, const D m_removed_contents.m_texts_groups.clear(); setPotentialsOfRemovedElements(); + + //Store some information about the tables + for (auto table : m_removed_contents.m_tables) + { + tableStatus status; + status.next = table->nextTable(); + status.previous = table->previousTable(); + m_tables_status.insert(table, status); + } 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)); @@ -229,6 +239,27 @@ void DeleteQGraphicsItemCommand::undo() elmt->addTextToGroup(deti, m_grp_texts_hash.value(deti)); } } + + for (auto table : m_removed_contents.m_tables) + { + auto pair = m_tables_status.value(table); + + if(pair.next && pair.previous) // Table is between two tables + { + pair.next->setPreviousTable(nullptr); + table->setPreviousTable(pair.previous); + pair.next->setPreviousTable(table); + } + else if (pair.next) //Table is the first table of linked tables + { + auto model = pair.next->model(); + pair.next->setPreviousTable(table); + table->setModel(model); + } + else if (pair.previous) { //Table is the last of linked tables + table->setPreviousTable(pair.previous); + } + } QUndoCommand::undo(); } @@ -272,6 +303,20 @@ void DeleteQGraphicsItemCommand::redo() deti->setParentItem(nullptr); } + for (auto table : m_removed_contents.m_tables) + { + auto pair = m_tables_status.value(table); + + if(pair.next && pair.previous) { // Table is between two tables + pair.next->setPreviousTable(pair.previous); //change the previous table of the current next table of @table + } else if (pair.next) { //Table is the first table of linked tables + pair.next->setPreviousTable(nullptr); //Next table haven't got model anymore + pair.next->setModel(table->model()); + } else if (pair.previous) { //Table is the last of linked tables + table->setPreviousTable(nullptr); //Remove the previous table @table + } + } + for(QGraphicsItem *item : m_removed_contents.items()) m_diagram->removeItem(item); diff --git a/sources/undocommand/deleteqgraphicsitemcommand.h b/sources/undocommand/deleteqgraphicsitemcommand.h index 172b45191..2a02ead1a 100644 --- a/sources/undocommand/deleteqgraphicsitemcommand.h +++ b/sources/undocommand/deleteqgraphicsitemcommand.h @@ -24,9 +24,16 @@ class Diagram; class ElementTextItemGroup; class Terminal; +class QetGraphicsTableItem; class DeleteQGraphicsItemCommand : public QUndoCommand { + private : + struct tableStatus { + QetGraphicsTableItem *previous = nullptr; + QetGraphicsTableItem *next = nullptr; + }; + public: DeleteQGraphicsItemCommand(Diagram *diagram, const DiagramContent &content, QUndoCommand * parent = nullptr); ~DeleteQGraphicsItemCommand() override; @@ -49,6 +56,7 @@ class DeleteQGraphicsItemCommand : public QUndoCommand QHash m_elmt_text_hash; /// Keep the parent element of each deleted dynamic element text item QHash m_grp_texts_hash; ///Keep the parent group of each deleted element text item QList > m_connected_terminals; + QHash m_tables_status; }; #endif // DELETEQGRAPHICSITEMCOMMAND_H