From d0e0efc0b963122cdf46c328047503f846c276ab Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Tue, 24 Aug 2021 13:44:09 +0200
Subject: [PATCH] Fix re-loading

---
 .../LocationGraphEditorWidget.ui              |  15 ++-
 .../LocationGraphEditorWidgetController.cpp   | 110 ++++++++++++++----
 .../LocationGraphEditorWidgetController.h     |   8 +-
 .../widgets/EdgeTableWidget.cpp               |   7 ++
 .../widgets/EdgeTableWidget.h                 |   2 +-
 .../widgets/VertexDataWidget.cpp              |  11 ++
 .../widgets/VertexDataWidget.h                |   1 +
 .../widgets/VertexTableWidget.cpp             |  12 ++
 .../widgets/VertexTableWidget.h               |   2 +
 .../widgets/graph_scene/Scene.cpp             |   3 +-
 10 files changed, 144 insertions(+), 27 deletions(-)

diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidget.ui b/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidget.ui
index 63dbb942..161687fa 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidget.ui
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidget.ui
@@ -50,6 +50,16 @@
         </property>
        </widget>
       </item>
+      <item>
+       <widget class="QPushButton" name="commitGraphButton">
+        <property name="toolTip">
+         <string>Draws the selected scene</string>
+        </property>
+        <property name="text">
+         <string>Commit</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
@@ -161,8 +171,7 @@
        </item>
       </layout>
      </widget>
-    
-         <widget class="QFrame" name="graphFrame">
+     <widget class="QFrame" name="graphFrame">
       <property name="frameShape">
        <enum>QFrame::NoFrame</enum>
       </property>
@@ -186,7 +195,7 @@
         <layout class="QVBoxLayout" name="graphSceneLayout"/>
        </item>
       </layout>
-     </widget> 
+     </widget>
     </widget>
    </item>
    <item>
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidgetController.cpp b/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidgetController.cpp
index 50e45488..77332863 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidgetController.cpp
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidgetController.cpp
@@ -127,6 +127,7 @@ namespace armarx::nav::locgrapheditor
         connect(widget.refreshGraphsButton, &QPushButton::pressed, this, &This::updateGraphList);
 
         connect(widget.loadGraphButton, &QPushButton::pressed, this, &This::loadGraph);
+        connect(widget.commitGraphButton, &QPushButton::pressed, this, &This::commitGraph);
 
 
         // Update views
@@ -286,12 +287,20 @@ namespace armarx::nav::locgrapheditor
             fromAron(dto, nav);
         }
 
+        model.graphEntityID = entityID;
         setGraph(nav);
     }
 
 
+    void LocationGraphEditorWidgetController::commitGraph()
+    {
+
+    }
+
+
     void LocationGraphEditorWidgetController::setGraph(graph::Graph& nav)
     {
+#if 0
         // Store vertex highlighting.
         std::set<std::string> highlightedVertices;
         for (auto vertex : model.graph.vertices())
@@ -301,8 +310,12 @@ namespace armarx::nav::locgrapheditor
                 highlightedVertices.insert(vertex.attrib().getName());
             }
         }
+#endif
+
+        // Build the gui graph (model).
+        const bool initialBlocked = this->signalsBlocked();
+        blockSignals(true);
 
-        // Build graph.
         clearGraph();
         for (auto vertex : nav.vertices())
         {
@@ -313,6 +326,7 @@ namespace armarx::nav::locgrapheditor
             addEdge(edge);
         }
 
+#if 0
         // Restore vertex highlighting.
         for (auto vertex : model.graph.vertices())
         {
@@ -321,7 +335,11 @@ namespace armarx::nav::locgrapheditor
                 vertex.attrib().highlighted = true;
             }
         }
+#endif
+
+        blockSignals(initialBlocked);
 
+        // Trigger a view update.
         emit graphChanged();
     }
 
@@ -464,43 +482,89 @@ namespace armarx::nav::locgrapheditor
     }
 
 
+    void LocationGraphEditorWidgetController::clearGraph()
+    {
+        const bool initialBlocked = this->signalsBlocked();
+        blockSignals(true);
+
+        clearEdges();
+        clearVertices();
+
+        blockSignals(initialBlocked);
+
+        // Clear data structure
+        ARMARX_CHECK_EQUAL(model.graph.numEdges(), 0);
+        ARMARX_CHECK_EQUAL(model.graph.numVertices(), 0);
+
+        emit graphChanged();
+    }
+
+
     void LocationGraphEditorWidgetController::clearEdges()
     {
-        // Remove from graphics scene
-        for (auto edge : model.graph.edges())
+        // Remove in reverse order to be more array-friendly.
+        std::vector<GuiGraph::Edge> edges { model.graph.edges().begin(), model.graph.edges().end() };
+        for (auto it = edges.rbegin(); it != edges.rend(); ++it)
         {
-            view.graph->scene()->removeEdge(edge.attrib().graphicsItem);
+            GuiGraph::Edge edge = *it;
+            removeEdge(edge);
         }
 
-        // Clear table widget
-        view.edgeTable->clearContents();
-        view.edgeTable->setRowCount(0);
+        ARMARX_CHECK_EQUAL(view.edgeTable->rowCount(), 0);
+        ARMARX_CHECK_EQUAL(model.graph.numEdges(), 0);
 
-        // Clear data structure
-        while (model.graph.edges().begin() != model.graph.edges().end())
+        emit graphChanged();
+    }
+
+
+    void LocationGraphEditorWidgetController::clearVertices()
+    {
+        ARMARX_CHECK_EQUAL(model.graph.numEdges(), 0)
+                << "The graph may not have any edges when clearing the vertices.";
+
+        // Remove in reverse order to be more array-friendly.
+        std::vector<GuiGraph::Vertex> vertices { model.graph.vertices().begin(), model.graph.vertices().end() };
+        for (auto it = vertices.rbegin(); it != vertices.rend(); ++it)
         {
-            model.graph.removeEdge(*model.graph.edges().begin());
+            GuiGraph::Vertex vertex = *it;
+            removeVertex(vertex);
         }
 
+        ARMARX_CHECK_EQUAL(view.vertexTable->rowCount(), 0);
+        ARMARX_CHECK_EQUAL(model.graph.numVertices(), 0);
+
         emit graphChanged();
     }
 
 
-    void LocationGraphEditorWidgetController::clearGraph()
+    void LocationGraphEditorWidgetController::removeEdge(GuiGraph::Edge& edge)
     {
-        // Clear scene
-        view.graph->scene()->clear();
+        // Remove view elements
+        view.graph->scene()->removeEdge(edge.attrib().graphicsItem);
+        view.edgeTable->removeEdge(edge);
 
-        // Clear table widgets
-        view.edgeTable->clearContents();
-        view.edgeTable->setRowCount(0);
-        view.vertexTable->clearContents();
-        view.vertexTable->setRowCount(0);
+        // Remove from model
+        model.graph.removeEdge(edge);
+    }
 
-        // Clear data structure
-        model.graph.clear();
 
-        emit graphChanged();
+    void LocationGraphEditorWidgetController::removeVertex(GuiGraph::Vertex& vertex)
+    {
+        ARMARX_CHECK_EQUAL(vertex.inDegree(), 0)
+                << "A vertex may not have any edges before being removed. " << vertex.attrib().getName();
+        ARMARX_CHECK_EQUAL(vertex.outDegree(), 0)
+                << "A vertex may not have any edges before being removed. "  << vertex.attrib().getName();
+
+        // Remove view elements
+        view.graph->scene()->removeVertex(vertex.attrib().graphicsItem);
+        view.vertexTable->removeVertex(vertex);
+        if (view.vertexData->vertex().has_value() and view.vertexData->vertex().value() == vertex)
+        {
+            view.vertexData->clearVertex();
+        }
+
+        // Remove from model
+        model.graph.removeVertex(vertex);
     }
 
 
@@ -562,6 +626,10 @@ namespace armarx::nav::locgrapheditor
 
     void LocationGraphEditorWidgetController::selectVertex(QTableWidgetItem* vertexItem)
     {
+        if (vertexItem == nullptr)
+        {
+            view.vertexData->clearVertex();
+        }
         if (auto vertex = model.graph.getVertexFromTableItem(vertexItem))
         {
             selectVertex(vertex.value());
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidgetController.h b/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidgetController.h
index 3a7d9dd3..825ea418 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidgetController.h
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/LocationGraphEditorWidgetController.h
@@ -138,6 +138,7 @@ namespace armarx::nav::locgrapheditor
         void queryGraphs();
         void updateGraphList();
         void loadGraph();
+        void commitGraph();
 
         void setGraph(graph::Graph& nav);
         GuiGraph::Vertex addVertex(graph::Graph::ConstVertex vertex);
@@ -145,8 +146,12 @@ namespace armarx::nav::locgrapheditor
         GuiGraph::Edge addEdge(GuiGraph::ConstVertex source, GuiGraph::ConstVertex target,
                                const EdgeData& defaultAttribs);
 
-        void clearEdges();
         void clearGraph();
+        void clearEdges();
+        void clearVertices();
+
+        void removeEdge(GuiGraph::Edge& edge);
+        void removeVertex(GuiGraph::Vertex& vertex);
 
         void addEdges(QList<QPair<QTableWidgetItem*, QTableWidgetItem*>> vertexItems);
 
@@ -197,6 +202,7 @@ namespace armarx::nav::locgrapheditor
         struct Model
         {
             armem::wm::Memory memory;
+            armem::MemoryID graphEntityID;
             GuiGraph graph;
         };
         Model model;
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/EdgeTableWidget.cpp b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/EdgeTableWidget.cpp
index 9e7a16ff..081091c5 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/EdgeTableWidget.cpp
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/EdgeTableWidget.cpp
@@ -84,6 +84,13 @@ namespace armarx::nav::locgrapheditor
     }
 
 
+    void EdgeTableWidget::removeEdge(GuiGraph::Edge& edge)
+    {
+        this->removeRow(row(edge.attrib().tableWidgetItem));
+        edge.attrib().tableWidgetItem = nullptr;
+    }
+
+
     QList<QTableWidgetItem*>
     EdgeTableWidget::selectedEdgeItems()
     {
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/EdgeTableWidget.h b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/EdgeTableWidget.h
index 296e6a6f..0cd2cbee 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/EdgeTableWidget.h
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/EdgeTableWidget.h
@@ -55,7 +55,7 @@ namespace armarx::nav::locgrapheditor
 
         void updateEdge(GuiGraph::Edge edge);
 
-        void removeEdge(GuiGraph::Edge edge);
+        void removeEdge(GuiGraph::Edge& edge);
 
         void clearEdges();
 
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexDataWidget.cpp b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexDataWidget.cpp
index d34dab04..ba63ea0f 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexDataWidget.cpp
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexDataWidget.cpp
@@ -125,9 +125,20 @@ namespace armarx::nav::locgrapheditor
 
     void VertexDataWidget::setVertex(GuiGraph::Vertex vertex)
     {
+        blockSignals(true);
+
         _vertex = vertex;
         _setFromVertex(vertex);
         setEnabled(true);
+
+        blockSignals(false);
+    }
+
+
+    void VertexDataWidget::clearVertex()
+    {
+        _vertex = std::nullopt;
+        setEnabled(false);
     }
 
 
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexDataWidget.h b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexDataWidget.h
index 64b7971e..cb14f0c5 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexDataWidget.h
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexDataWidget.h
@@ -49,6 +49,7 @@ namespace armarx::nav::locgrapheditor
 
         std::optional<GuiGraph::Vertex> vertex();
         void setVertex(GuiGraph::Vertex vertex);
+        void clearVertex();
 
         Eigen::Vector3d xyz() const;
         Eigen::Vector3d rpyDeg() const;
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexTableWidget.cpp b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexTableWidget.cpp
index 692e18b8..6722f828 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexTableWidget.cpp
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexTableWidget.cpp
@@ -103,6 +103,18 @@ namespace armarx::nav::locgrapheditor
     }
 
 
+    void
+    VertexTableWidget::removeVertex(GuiGraph::Vertex& vertex)
+    {
+        if (currentItem() == vertex.attrib().tableWidgetItem)
+        {
+            setCurrentItem(nullptr);
+        }
+        this->removeRow(row(vertex.attrib().tableWidgetItem));
+        vertex.attrib().tableWidgetItem = nullptr;
+    }
+
+
     QList<QTableWidgetItem*>
     VertexTableWidget::selectedVertexItems()
     {
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexTableWidget.h b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexTableWidget.h
index 6d9a227f..379c224d 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexTableWidget.h
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/VertexTableWidget.h
@@ -46,6 +46,8 @@ namespace armarx::nav::locgrapheditor
 
         void updateVertex(GuiGraph::Vertex vertex);
 
+        void removeVertex(GuiGraph::Vertex& vertex);
+
 
         QList<QTableWidgetItem*> selectedVertexItems();
 
diff --git a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/graph_scene/Scene.cpp b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/graph_scene/Scene.cpp
index cfa598c2..330a15dc 100644
--- a/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/graph_scene/Scene.cpp
+++ b/source/armarx/navigation/gui-plugins/LocationGraphEditor/widgets/graph_scene/Scene.cpp
@@ -79,7 +79,8 @@ namespace armarx::nav::locgrapheditor::graph_scene
     void Scene::updateEdge(GuiGraph::Edge& edge)
     {
         QGraphicsLineItem* item = edge.attrib().graphicsItem;
-        ARMARX_CHECK_NOT_NULL(item);
+        ARMARX_CHECK_NOT_NULL(item)
+                << edge.source().attrib().getName() << " -> " << edge.target().attrib().getName();
 
         Eigen::Matrix4d sourcePose = edge.source().attrib().getPose().cast<qreal>();
         Eigen::Matrix4d targetPose = edge.target().attrib().getPose().cast<qreal>();
-- 
GitLab