[kgraphviewer-devel] [KGraphViewer/libkgraphviz] e381376: Hide GraphEdge internals (pimpl)

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


commit e3813767fbaf2f137ef0f0c189a64fe75e6d94cd
branch libkgraphviz
Author: Kevin Funk <krf at electrostorm.net>
Date:   Fri Jan 14 16:38:15 2011 +0100

    Hide GraphEdge internals (pimpl)

diff --git a/src/kgraphviz/canvasedge.cpp b/src/kgraphviz/canvasedge.cpp
index df33099..d79696a 100644
--- a/src/kgraphviz/canvasedge.cpp
+++ b/src/kgraphviz/canvasedge.cpp
@@ -28,7 +28,7 @@
 #include "canvasedge.h"
 #include "canvasedge_p.h"
 
-#include "canvaselement.h"
+#include "graphnode.h"
 #include "dotgraphview.h"
 #include "graphedge.h"
 #include "support/dotdefaults.h"
diff --git a/src/kgraphviz/dotgraph.cpp b/src/kgraphviz/dotgraph.cpp
index afff64f..b659a9b 100644
--- a/src/kgraphviz/dotgraph.cpp
+++ b/src/kgraphviz/dotgraph.cpp
@@ -20,6 +20,8 @@
 #include "dotgraph_p.h"
 
 #include "graphexporter.h"
+#include "graphedge.h"
+#include "graphnode.h"
 #include "canvasedge.h"
 #include "canvasnode.h"
 #include "canvassubgraph.h"
@@ -515,14 +517,14 @@ void DotGraph::updateWithGraph(graph_t* newGraph)
             //       kDebug() << "new created";
             nodes().insert(nge->tail->name, newgn);
           }
-          newEdge->setFromNode(elementNamed(nge->tail->name));
+          newEdge->setFromNode(nodeNamed(nge->tail->name));
           if (elementNamed(nge->head->name) == 0)
           {
             GraphNode* newgn = new GraphNode();
             //       kDebug() << "new created";
             nodes().insert(nge->head->name, newgn);
           }
-          newEdge->setToNode(elementNamed(nge->head->name));
+          newEdge->setToNode(nodeNamed(nge->head->name));
           edges().insert(edgeName, newEdge);
         }
       }
@@ -610,8 +612,8 @@ void DotGraph::updateWithGraph(const DotGraph& newGraph)
         GraphEdge* newEdge = new GraphEdge();
         newEdge->setId(nge->id());
         newEdge->updateWithEdge(*nge);
-        newEdge->setFromNode(elementNamed(nge->fromNode()->id()));
-        newEdge->setToNode(elementNamed(nge->toNode()->id()));
+        newEdge->setFromNode(nodeNamed(nge->fromNode()->id()));
+        newEdge->setToNode(nodeNamed(nge->toNode()->id()));
         edges().insert(nge->id(), newEdge);
       }
     }
@@ -706,8 +708,9 @@ void DotGraph::removeSubgraphNamed(const QString& subgraphName)
   it = d->m_edgesMap.begin(); it_end = d->m_edgesMap.end();
   while (it != it_end)
   {
-    if ( it.value()->fromNode() == subgraph
-        || it.value()->toNode() == subgraph )
+    GraphElement* element = subgraph;
+    if ( it.value()->fromNode() == element
+        || it.value()->toNode() == element )
     {
       GraphEdge* edge = it.value();
       if (edge->canvasElement() != 0)
@@ -841,6 +844,22 @@ GraphElement* DotGraph::elementNamed(const QString& id) const
   return 0;
 }
 
+GraphNode* DotGraph::nodeNamed(const QString& id) const
+{
+  Q_D(const DotGraph);
+  GraphNode* ret = 0;
+  if ((ret = d->m_nodesMap.value(id, 0))) {
+    return ret;
+  }
+  foreach(GraphSubgraph* subGraph, subgraphs()) {
+    GraphNode* node = 0;
+    if ((ret = dynamic_cast<GraphNode*>(subGraph->elementNamed(id)))) {
+      return ret;
+    }
+  }
+  return 0;
+}
+
 void DotGraph::setGraphAttributes(QMap<QString,QString> attribs)
 {
   kDebug() << attribs;
@@ -965,15 +984,15 @@ void DotGraph::addNewEdge(QString src, QString tgt, QMap<QString,QString> attrib
   kDebug() << src << tgt << attribs;
   GraphEdge* newEdge = new GraphEdge();
   newEdge->attributes() = attribs;
-  GraphElement* srcElement = elementNamed(src);
+  GraphNode* srcElement = nodeNamed(src);
   if (srcElement == 0)
   {
-    srcElement = elementNamed(QString("cluster_")+src);
+    srcElement = nodeNamed(QString("cluster_")+src);
   }
-  GraphElement* tgtElement = elementNamed(tgt);
+  GraphNode* tgtElement = nodeNamed(tgt);
   if (tgtElement == 0)
   {
-    tgtElement = elementNamed(QString("cluster_")+tgt);
+    tgtElement = nodeNamed(QString("cluster_")+tgt);
   }
 
   if (srcElement == 0 || tgtElement == 0)
diff --git a/src/kgraphviz/dotgraph.h b/src/kgraphviz/dotgraph.h
index 5247c1e..9cebab3 100644
--- a/src/kgraphviz/dotgraph.h
+++ b/src/kgraphviz/dotgraph.h
@@ -100,6 +100,7 @@ public:
   void setAttribute(const QString& elementId, const QString& attributeName, const QString& attributeValue);
 
   GraphElement* elementNamed(const QString& id) const;
+  GraphNode* nodeNamed(const QString& id) const;
 
   void setUseLibrary(bool value);
   bool useLibrary() const;
diff --git a/src/kgraphviz/graphedge.cpp b/src/kgraphviz/graphedge.cpp
index 20e9441..126d44d 100644
--- a/src/kgraphviz/graphedge.cpp
+++ b/src/kgraphviz/graphedge.cpp
@@ -25,6 +25,8 @@
 */
 
 #include "graphedge.h"
+#include "graphedge_p.h"
+
 #include "graphnode.h"
 #include "graphsubgraph.h"
 #include "canvasedge.h"
@@ -35,49 +37,114 @@
 
 #include <KDebug>
 
-namespace KGraphViz
+using namespace KGraphViz;
+
+GraphEdgePrivate::GraphEdgePrivate()
+  : m_fromNode(0)
+  , m_toNode(0)
+{
+}
+
+GraphEdgePrivate::GraphEdgePrivate(const GraphEdgePrivate& other)
+  : m_fromNode(other.m_fromNode)
+  , m_toNode(other.m_toNode)
+  , m_arrowHeads(other.m_arrowHeads)
 {
+}
+
+GraphEdgePrivate::~GraphEdgePrivate()
+{
+  kDebug();
+}
 
-GraphEdge::GraphEdge() : 
-    m_fromNode(0),
-    m_toNode(0),
-    m_dir(DOT_DEFAULT_EDGE_DIR)
+GraphEdge::GraphEdge()
+  : d_ptr(new GraphEdgePrivate)
 {
-//   kDebug() ;
+  kDebug();
 }
 
 GraphEdge::~GraphEdge()
 {
   kDebug();
+
+  delete d_ptr;
+}
+
+GraphEdge::GraphEdge(const GraphEdge& other)
+  : GraphElement(other)
+  , d_ptr(new GraphEdgePrivate(*other.d_ptr))
+{
+}
+
+GraphEdge::GraphEdge(Agedge_t* edge)
+  : d_ptr(new GraphEdgePrivate)
+{
+  kDebug();
+
+  updateWithEdge(edge);
 }
 
-GraphEdge::GraphEdge(const GraphEdge& edge) :
-  GraphElement(edge)
+QList< DotRenderOp >& GraphEdge::arrowheads()
 {
-    m_fromNode = 0;
-    m_toNode = 0;
-    m_colors = edge.m_colors;
-    m_dir = edge.m_dir;
-    m_arrowheads = edge.m_arrowheads;
+  Q_D(GraphEdge);
+  return d->m_arrowHeads;
+}
+
+const QList< DotRenderOp >& GraphEdge::arrowheads() const
+{
+  Q_D(const GraphEdge);
+  return d->m_arrowHeads;
+}
+
+const QStringList& GraphEdge::colors() const
+{
+  Q_D(const GraphEdge);
+  return d->m_colors;
 }
 
 void GraphEdge::colors(const QString& cs)
 {
-  m_colors = QStringList::split(":", cs);
-//   kDebug() << fromNode()->id() << " -> " << toNode()->id() << ": nb colors: " << m_colors.size();
+  Q_D(GraphEdge);
+  d->m_colors = QStringList::split(":", cs);
+//   kDebug() << fromNode()->id() << " -> " << toNode()->id() << ": nb colors: " << d->m_colors.size();
+}
+
+GraphNode* GraphEdge::fromNode() const
+{
+  Q_D(const GraphEdge);
+  return d->m_fromNode;
+}
+
+void GraphEdge::setFromNode(GraphNode* node)
+{
+  Q_D(GraphEdge);
+  d->m_fromNode = node;
+}
+
+void GraphEdge::setToNode(GraphNode* node)
+{
+  Q_D(GraphEdge);
+  d->m_toNode = node;
+}
+
+GraphNode* GraphEdge::toNode() const
+{
+  Q_D(const GraphEdge);
+  return d->m_toNode;
 }
 
 const QString GraphEdge::color(uint i) 
 {
-  if (i >= (uint)m_colors.count() && m_attributes.find("color") != m_attributes.end())
+  Q_D(const GraphEdge);
+  if (i >= (uint)d->m_colors.count() && m_attributes.find("color") != m_attributes.end())
   {
     colors(m_attributes["color"]);
   }
-  if (i < (uint)m_colors.count())
+  if (i < (uint)d->m_colors.count())
   {
-//     std::cerr << "edge color " << i << " is " << m_colors[i] << std::endl;
-//     kDebug() << fromNode()->id() << " -> " << toNode()->id() << "color" << i << "is" << m_colors[i];
-    return m_colors[i];
+//     std::cerr << "edge color " << i << " is " << d->m_colors[i] << std::endl;
+//     kDebug() << fromNode()->id() << " -> " << toNode()->id() << "color" << i << "is" << d->m_colors[i];
+    return d->m_colors[i];
   }
   else
   {
@@ -88,11 +155,11 @@ const QString GraphEdge::color(uint i)
 
 void GraphEdge::updateWithEdge(const GraphEdge& edge)
 {
-  m_arrowheads = edge.arrowheads();
-  m_colors = edge.colors();
-  m_dir = edge.dir();
-  m_fromNode = edge.m_fromNode;
-  m_toNode = edge.m_toNode;
+  Q_D(GraphEdge);
+  d->m_arrowHeads = edge.arrowheads();
+  d->m_colors = edge.colors();
+  d->m_fromNode = edge.fromNode();
+  d->m_toNode = edge.toNode();
 
   GraphElement::updateWithElement(edge);
 }
@@ -104,7 +171,7 @@ void GraphEdge::updateWithEdge(edge_t* edge)
   importFromGraphviz(edge, drawingAttributes);
 }
 
-QTextStream& operator<<(QTextStream& s, const GraphEdge& e)
+QTextStream& KGraphViz::operator<<(QTextStream& s, const GraphEdge& e)
 {
   QString srcLabel = e.fromNode()->id();
   if (dynamic_cast<const GraphSubgraph*>(e.fromNode()))
@@ -122,6 +189,4 @@ QTextStream& operator<<(QTextStream& s, const GraphEdge& e)
   return s;
 }
 
-}
-
 #include "graphedge.moc"
diff --git a/src/kgraphviz/graphedge.h b/src/kgraphviz/graphedge.h
index 936d446..8ae8b7c 100644
--- a/src/kgraphviz/graphedge.h
+++ b/src/kgraphviz/graphedge.h
@@ -24,26 +24,20 @@
    License as published by the Free Software Foundation, version 2.
 */
 
-
-/*
- * Graph Edge
- */
-
 #ifndef GRAPH_EDGE_H
 #define GRAPH_EDGE_H
 
 #include "graphelement.h"
 
-#include <QStringList>
+#include <QString>
 #include <QMap>
 
 class Agedge_t;
 
 namespace KGraphViz
 {
-  
-class CanvasEdge;
-class CanvasNode;
+
+class GraphEdgePrivate;
 class GraphNode;
 
 class KGRAPHVIZ_EXPORT GraphEdge : public GraphElement
@@ -57,17 +51,17 @@ public:
 
   virtual ~GraphEdge();
 
-  GraphElement* fromNode() const { return m_fromNode; }
-  GraphElement* toNode() const { return m_toNode; }
-
-  void setFromNode(GraphElement* n) { m_fromNode = n; }
-  void setToNode(GraphElement* n) { m_toNode = n; }
+  GraphNode* fromNode() const;
+  void setFromNode(GraphNode* node);
+  
+  GraphNode* toNode() const;
+  void setToNode(GraphNode* node);
 
 //   inline const QVector< QPair< float, float > >& edgePoints() const {return m_edgePoints;}
 //   inline QVector< QPair< float, float > >& edgePoints() {return m_edgePoints;}
 //   inline void edgePoints(const QVector< QPair< float, float > >& ep) {m_edgePoints = ep;}
   
-  inline const QStringList& colors() const {return m_colors;}
+  const QStringList& colors() const;
   const QString color(uint i);
   void colors(const QString& cs); 
   
@@ -75,26 +69,18 @@ public:
   inline void labelY(float y) {m_labelY = y;}
   inline float labelX() const {return m_labelX;}
   inline float labelY() const {return m_labelY;}*/
-  
-  inline const QString& dir() const {return m_dir;}
-  inline void dir(const QString& dir) {m_dir = dir;}
 
-  inline QList< DotRenderOp >&  arrowheads() {return m_arrowheads;}
-  inline const QList< DotRenderOp >&  arrowheads() const {return m_arrowheads;}
+  QList<DotRenderOp>& arrowheads();
+  const QList<DotRenderOp>& arrowheads() const;
 
   virtual void updateWithEdge(const GraphEdge& edge);
   virtual void updateWithEdge(Agedge_t* edge);
 
+protected:
+  GraphEdgePrivate* const d_ptr;
+
 private:
-  // we have a _ce *and* _from/_to because for collapsed edges,
-  // only _to or _from will be unequal NULL
-  GraphElement *m_fromNode, *m_toNode;
-  QStringList m_colors;
-  QString m_dir;
-//   QVector< QPair< float, float > > m_edgePoints;
-//   float m_labelX, m_labelY;
-  
-  QList< DotRenderOp > m_arrowheads;
+  Q_DECLARE_PRIVATE(GraphEdge);
 };
 
 /** A map associating the bounds nodes of a graph's edges to these edges */
diff --git a/src/kgraphviz/graphelement.cpp b/src/kgraphviz/graphelement.cpp
index 754812b..739d8c9 100644
--- a/src/kgraphviz/graphelement.cpp
+++ b/src/kgraphviz/graphelement.cpp
@@ -30,8 +30,7 @@
 #include <QRegExp>
 #include <graphviz/gvc.h>
 
-namespace KGraphViz
-{
+using namespace KGraphViz;
 
 GraphElementPrivate::GraphElementPrivate() :
   m_z(1.0),
@@ -66,10 +65,10 @@ GraphElement::GraphElement() :
   setFontSize(DOT_DEFAULT_FONTSIZE);
 }
 
-GraphElement::GraphElement(const GraphElement& element) :
-  d_ptr(new GraphElementPrivate(*element.d_ptr))
+GraphElement::GraphElement(const GraphElement& element)
+  : d_ptr(new GraphElementPrivate(*element.d_ptr))
 {
-  kDebug() ;
+  kDebug();
   updateWithElement(element);
 }
 
@@ -254,7 +253,7 @@ void GraphElement::importFromGraphviz(void* element, QList<QString> drawingAttri
   }
 }
 
-QTextStream& operator<<(QTextStream& s, const GraphElement& n)
+QTextStream& KGraphViz::operator<<(QTextStream& s, const GraphElement& n)
 {
   QMap<QString,QString>::const_iterator it, it_end;
   it = n.attributes().begin(); it_end = n.attributes().end();
@@ -286,6 +285,4 @@ QTextStream& operator<<(QTextStream& s, const GraphElement& n)
   return s;
 }
 
-}
-
 #include "graphelement.moc"
diff --git a/src/kgraphviz/graphnode.cpp b/src/kgraphviz/graphnode.cpp
index 7ef1463..fce1fac 100644
--- a/src/kgraphviz/graphnode.cpp
+++ b/src/kgraphviz/graphnode.cpp
@@ -37,8 +37,7 @@
 
 #include <kdebug.h>
 
-namespace KGraphViz
-{
+using namespace KGraphViz;
 
 GraphNode::GraphNode()
 {
@@ -78,14 +77,15 @@ void GraphNode::updateWithNode(node_t* node)
 {
   kDebug() << node->name;
   m_attributes["id"] = node->name;
-  m_attributes["label"] = ND_label(node)->text;
+  if (ND_label(node))
+    m_attributes["label"] = ND_label(node)->text;
 
   QList<QString> drawingAttributes;
   drawingAttributes << "_draw_" << "_ldraw_";
   importFromGraphviz(node, drawingAttributes);
 }
 
-QTextStream& operator<<(QTextStream& s, const GraphNode& n)
+QTextStream& KGraphViz::operator<<(QTextStream& s, const GraphNode& n)
 {
   s << n.id() << " ["
     << dynamic_cast<const GraphElement&>(n)
@@ -93,6 +93,4 @@ QTextStream& operator<<(QTextStream& s, const GraphNode& n)
   return s;
 }
 
-}
-
 #include "graphnode.moc"
diff --git a/src/kgraphviz/support/dotgraphparsinghelper.cpp b/src/kgraphviz/support/dotgraphparsinghelper.cpp
index 2cd0cf2..4245a18 100644
--- a/src/kgraphviz/support/dotgraphparsinghelper.cpp
+++ b/src/kgraphviz/support/dotgraphparsinghelper.cpp
@@ -267,7 +267,7 @@ void DotGraphParsingHelper::createedges()
     }
 //     kDebug() << QString::fromStdString(node1Name) << ", " << QString::fromStdString(node2Name);
     ge = new GraphEdge();
-    GraphElement* gn1 = graph->elementNamed(QString::fromStdString(node1Name));
+    GraphNode* gn1 = graph->nodeNamed(QString::fromStdString(node1Name));
     if (gn1 == 0)
     {
 //       kDebug() << "new node 1";
@@ -275,7 +275,7 @@ void DotGraphParsingHelper::createedges()
       gn1->setId(QString::fromStdString(node1Name));
       graph->nodes()[QString::fromStdString(node1Name)] = dynamic_cast<GraphNode*>(gn1);
     }
-    GraphElement* gn2 = graph->elementNamed(QString::fromStdString(node2Name));
+    GraphNode* gn2 = graph->nodeNamed(QString::fromStdString(node2Name));
     if (gn2 == 0)
     {
 //       kDebug() << "new node 2";
diff --git a/tests/kgraphviz/graphiotests.cpp b/tests/kgraphviz/graphiotests.cpp
index fc8ea60..34be266 100644
--- a/tests/kgraphviz/graphiotests.cpp
+++ b/tests/kgraphviz/graphiotests.cpp
@@ -68,7 +68,7 @@ void GraphIOTests::testImportSubGraph()
   QVERIFY(graph != 0);
 
   // test root
-  QCOMPARE(graph->nodes().size(), 2);
+  QCOMPARE(graph->nodes().size(), 2); // 2 local ones
   QCOMPARE(graph->edges().size(), 13);
   QCOMPARE(graph->subgraphs().size(), 2);
 


More information about the kgraphviewer-devel mailing list