[kgraphviewer-devel] [KGraphViewer/libkgraphviz] d4e4e20: Partly fix global stream operators for element

Kevin Funk krf at electrostorm.net
Fri Jan 14 18:01:35 CET 2011


commit d4e4e2092f27a93915147e3fc288eabd20fb2d6b
branch libkgraphviz
Author: Kevin Funk <krf at electrostorm.net>
Date:   Fri Jan 14 09:45:12 2011 +0100

    Partly fix global stream operators for element
    
    * Add DotGraph::operator<< to serialize a complete graph.
    * Export stream operators

diff --git a/src/kgraphviz/dotgraph.cpp b/src/kgraphviz/dotgraph.cpp
index 9ea588b..bdeadae 100644
--- a/src/kgraphviz/dotgraph.cpp
+++ b/src/kgraphviz/dotgraph.cpp
@@ -111,11 +111,11 @@ DotGraph::DotGraph(const QString& layoutCommand, const QString& fileName) :
   d->m_layoutCommand = layoutCommand;
 }
 
-DotGraph::~DotGraph()  
+DotGraph::~DotGraph()
 {
   kDebug();
-  
-  Q_D(DotGraph); 
+
+  Q_D(DotGraph);
   GraphNodeMap::iterator itn, itn_end;
   itn = d->m_nodesMap.begin(); itn_end = d->m_nodesMap.end();
   for (; itn != itn_end; itn++)
@@ -312,7 +312,7 @@ bool DotGraph::update()
     gvRender (gvc, graph, "xdot", NULL);
 
     updateWithGraph(graph);
-    
+
     gvFreeLayout(gvc, graph);
     agclose(graph);
     bool result = (gvFreeContext(gvc) == 0);
@@ -323,13 +323,13 @@ bool DotGraph::update()
 unsigned int DotGraphPrivate::cellNumber(int x, int y) const
 {
 /*  kDebug() << "x= " << x << ", y= " << y << ", m_width= " << m_width << ", m_height= " << m_height << ", m_horizCellFactor= " << m_horizCellFactor << ", m_vertCellFactor= " << m_vertCellFactor  << ", m_wdhcf= " << m_wdhcf << ", m_hdvcf= " << m_hdvcf;*/
-  
+
   const unsigned int nx = (unsigned int)(( x - ( x % int(m_wdhcf) ) ) / m_wdhcf);
   const unsigned int ny = (unsigned int)(( y - ( y % int(m_hdvcf) ) ) / m_hdvcf);
 /*  kDebug() << "nx = " << (unsigned int)(( x - ( x % int(m_wdhcf) ) ) / m_wdhcf);
   kDebug() << "ny = " << (unsigned int)(( y - ( y % int(m_hdvcf) ) ) / m_hdvcf);
   kDebug() << "res = " << ny * m_horizCellFactor + nx;*/
-  
+
   const unsigned int res = ny * m_horizCellFactor + nx;
   return res;
 }
@@ -340,7 +340,7 @@ void DotGraphPrivate::computeCells()
 {
   kWarning() << "Not implemented";
   return;
-  
+
 /* FIXME: Is this used?
   kDebug() << m_width << m_height << endl;
   m_horizCellFactor = m_vertCellFactor = 1;
@@ -352,7 +352,7 @@ void DotGraphPrivate::computeCells()
     stop = true;
     m_cells.clear();
 //     m_cells.resize(m_horizCellFactor * m_vertCellFactor);
-    
+
     GraphNodeMap::iterator it, it_end;
     it = m_nodesMap.begin(); it_end = m_nodesMap.end();
     for (; it != it_end; it++)
@@ -367,7 +367,7 @@ void DotGraphPrivate::computeCells()
         m_cells.resize(cellNum+1);
       }
       m_cells[cellNum].insert(gn);
-      
+
       kDebug() << "after insert";
       if ( m_cells[cellNum].size() > MAXCELLWEIGHT )
       {
@@ -427,12 +427,12 @@ void DotGraph::saveTo(const QString& fileName)
 void DotGraph::updateWithGraph(graph_t* newGraph)
 {
   Q_D(DotGraph);
-  
+
   kDebug() << newGraph;
   QList<QString> drawingAttributes;
   drawingAttributes << "_draw_" << "_ldraw_";
   importFromGraphviz(newGraph, drawingAttributes);
-  
+
   // copy subgraphs
   for (edge_t* e = agfstout(newGraph->meta_node->graph, newGraph->meta_node); e;
       e = agnxtout(newGraph->meta_node->graph, e))
@@ -631,7 +631,7 @@ void DotGraph::removeNodeNamed(const QString& nodeName)
     kError() << "No such node " << nodeName;
     return;
   }
-  
+
   GraphEdgeMap::iterator it, it_end;
   it = d->m_edgesMap.begin(); it_end = d->m_edgesMap.end();
   while (it != it_end)
@@ -683,7 +683,7 @@ void DotGraph::removeNodeFromSubgraph(
     kError() << "No such subgraph " << subgraphName;
     return;
   }
-  
+
   subgraph->removeElement(node);
   if (subgraph->content().isEmpty())
   {
@@ -869,10 +869,14 @@ void DotGraph::addNewSubgraph(QMap<QString,QString> attribs)
 void DotGraph::addNewNodeToSubgraph(QMap<QString,QString> attribs, QString subgraph)
 {
   kDebug() << attribs << "to" << subgraph;
+  if (!subgraphs().contains(subgraph)) {
+    kWarning() << "Invalid subgraph:" << subgraph;
+    return;
+  }
+
   GraphNode* newNode = new GraphNode();
   newNode->attributes() = attribs;
   subgraphs()[subgraph]->content().push_back(newNode);
-
   kDebug() << "node added as" << newNode->id() << "in" << subgraph;
 }
 
@@ -971,7 +975,7 @@ void DotGraph::addNewEdge(QString src, QString tgt, QMap<QString,QString> attrib
   {
     tgtElement = elementNamed(QString("cluster_")+tgt);
   }
-  
+
   if (srcElement == 0 || tgtElement == 0)
   {
     kError() << src << "or" << tgt << "missing";
@@ -1024,4 +1028,23 @@ QString DotGraph::backColor() const
   }
 }
 
+QTextStream& KGraphViz::operator<<(QTextStream& s, const KGraphViz::DotGraph& graph)
+{
+  if (graph.strict())
+    s << "strict ";
+
+  s << (graph.directed() ? "digraph " : "graph ") << graph.id() << " {" << endl;
+  foreach(const GraphSubgraph* subgraph, graph.subgraphs()) {
+    s << *subgraph;
+  }
+  foreach(const GraphNode* node, graph.nodes()) {
+    s << *node;
+  }
+  foreach(const GraphEdge* edge, graph.edges()) {
+    s << *edge;
+  }
+  s << "}" << endl;
+  return s;
+}
+
 #include "dotgraph.moc"
diff --git a/src/kgraphviz/dotgraph.h b/src/kgraphviz/dotgraph.h
index 1183b45..5247c1e 100644
--- a/src/kgraphviz/dotgraph.h
+++ b/src/kgraphviz/dotgraph.h
@@ -133,6 +133,7 @@ private:
   Q_PRIVATE_SLOT(d_func(), void graphIOError(QString));
 };
 
+KGRAPHVIZ_EXPORT QTextStream& operator<<(QTextStream& s, const DotGraph& n);
 }
 
 #endif
diff --git a/src/kgraphviz/graphedge.h b/src/kgraphviz/graphedge.h
index 68b3716..936d446 100644
--- a/src/kgraphviz/graphedge.h
+++ b/src/kgraphviz/graphedge.h
@@ -97,15 +97,10 @@ private:
   QList< DotRenderOp > m_arrowheads;
 };
 
-
 /** A map associating the bounds nodes of a graph's edges to these edges */
 typedef QMap<QString, GraphEdge*> GraphEdgeMap;
 
-QTextStream& operator<<(QTextStream& s, const GraphEdge& e);
-
+KGRAPHVIZ_EXPORT QTextStream& operator<<(QTextStream& s, const GraphEdge& e);
 }
 
 #endif
-
-
-
diff --git a/src/kgraphviz/graphelement.h b/src/kgraphviz/graphelement.h
index e4d51d8..1c2cd0e 100644
--- a/src/kgraphviz/graphelement.h
+++ b/src/kgraphviz/graphelement.h
@@ -116,7 +116,7 @@ private:
   Q_DECLARE_PRIVATE(GraphElement);
 };
 
-QTextStream& operator<<(QTextStream& s, const GraphElement& n);
+KGRAPHVIZ_EXPORT QTextStream& operator<<(QTextStream& s, const GraphElement& n);
 
 }
 
diff --git a/src/kgraphviz/graphnode.cpp b/src/kgraphviz/graphnode.cpp
index 0619e14..2a085fa 100644
--- a/src/kgraphviz/graphnode.cpp
+++ b/src/kgraphviz/graphnode.cpp
@@ -96,7 +96,7 @@ void GraphNode::updateWithNode(node_t* node)
 
 QTextStream& operator<<(QTextStream& s, const GraphNode& n)
 {
-  s << n.id() << "  ["
+  s << n.id() << " ["
     << dynamic_cast<const GraphElement&>(n)
     <<"];"<<endl;
   return s;
diff --git a/src/kgraphviz/graphnode.h b/src/kgraphviz/graphnode.h
index f53651e..b2ba079 100644
--- a/src/kgraphviz/graphnode.h
+++ b/src/kgraphviz/graphnode.h
@@ -67,8 +67,7 @@ public:
 /** A map associating the ids of a graph's nodes to these nodes */
 typedef QMap<QString, GraphNode*> GraphNodeMap;
 
-QTextStream& operator<<(QTextStream& s, const GraphNode& n);
-
+KGRAPHVIZ_EXPORT QTextStream& operator<<(QTextStream& s, const GraphNode& n);
 }
 
 #endif
diff --git a/src/kgraphviz/graphsubgraph.cpp b/src/kgraphviz/graphsubgraph.cpp
index 757af4a..68a5f8c 100644
--- a/src/kgraphviz/graphsubgraph.cpp
+++ b/src/kgraphviz/graphsubgraph.cpp
@@ -113,8 +113,7 @@ void GraphSubgraph::updateWithSubgraph(graph_t* subgraph)
   m_attributes["id"] = subgraph->name;
   if (GD_label(subgraph))
     m_attributes["label"] = GD_label(subgraph)->text;
-  
-  
+
   renderOperations().clear();
   if (agget(subgraph, (char*)"_draw_") != NULL)
   {
@@ -135,7 +134,6 @@ void GraphSubgraph::updateWithSubgraph(graph_t* subgraph)
     attr = agnxtattr(subgraph,attr);
   }
 
-
   for (edge_t* e = agfstout(subgraph->meta_node->graph, subgraph->meta_node); e;
       e = agnxtout(subgraph->meta_node->graph, e))
   {
@@ -160,7 +158,6 @@ void GraphSubgraph::updateWithSubgraph(graph_t* subgraph)
       subgraphs().insert(sg->name, newsg);
       //       kDebug() << "new inserted";
     }
-    
   }
 }
 
@@ -286,13 +283,14 @@ void GraphSubgraph::retrieveSelectedElementsIds(QList<QString> selection)
 
 QTextStream& operator<<(QTextStream& s, const GraphSubgraph& sg)
 {
-  s << "subgraph " << sg.id() << "  {"
-    << dynamic_cast<const GraphElement&>(sg);
+  s << "subgraph " << sg.id() << " {" << endl;
+  // TODO: Fix me, produces invalid dot syntax because of the ',' separators
+  //s << dynamic_cast<const GraphElement&>(sg);
   foreach (const GraphElement* el, sg.content())
   {
     s << *(dynamic_cast<const GraphNode*>(el));
   }
-  s <<"}"<<endl;
+  s << "}" <<endl;
   return s;
 }
 
diff --git a/src/kgraphviz/graphsubgraph.h b/src/kgraphviz/graphsubgraph.h
index d263904..4b61d73 100644
--- a/src/kgraphviz/graphsubgraph.h
+++ b/src/kgraphviz/graphsubgraph.h
@@ -86,7 +86,7 @@ private:
   GraphSubgraphMap m_subgraphsMap;
 };
 
-QTextStream& operator<<(QTextStream& stream, const GraphSubgraph& s);
+KGRAPHVIZ_EXPORT QTextStream& operator<<(QTextStream& stream, const GraphSubgraph& s);
 
 }
 


More information about the kgraphviewer-devel mailing list