[kgraphviewer-devel] [KGraphViewer/libkgraphviz] 460ade9: Fix external (command line) graph loading

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


commit 460ade9c80ab404a90a247ded45cf5845ca4a01b
branch libkgraphviz
Author: Kevin Funk <krf at electrostorm.net>
Date:   Fri Jan 14 13:01:01 2011 +0100

    Fix external (command line) graph loading
    
    Drop GraphExpoter::writeDot, this was merged to GraphIO in some older
    commit. Use GraphIO where applicable.
    
    This fixes subgraph handling during load and tests.

diff --git a/src/kgraphviz/dotgraph.cpp b/src/kgraphviz/dotgraph.cpp
index bdeadae..30bbdbf 100644
--- a/src/kgraphviz/dotgraph.cpp
+++ b/src/kgraphviz/dotgraph.cpp
@@ -295,16 +295,17 @@ bool DotGraph::parseDot(const QString& fileName)
 
 bool DotGraph::update()
 {
-  GraphExporter exporter;
+  Q_D(DotGraph);
   if (!useLibrary())
   {
-    kDebug() << "command";
-    QString str = exporter.writeDot(this);
-    return parseDot(str);
+    kDebug() << "using command line";
+    d->m_graphIO.updateDot(this);
+    return true;
   }
   else
   {
-    kDebug() << "library";
+    kDebug() << "using library";
+    GraphExporter exporter;
     graph_t* graph = exporter.exportToGraphviz(this);
 
     GVC_t* gvc = gvContext();
diff --git a/src/kgraphviz/graphexporter.cpp b/src/kgraphviz/graphexporter.cpp
index 4e79eb3..8876ee0 100644
--- a/src/kgraphviz/graphexporter.cpp
+++ b/src/kgraphviz/graphexporter.cpp
@@ -26,19 +26,16 @@
 
 
 #include "graphexporter.h"
+
 #include "dotgraph.h"
 
 #include <graphviz/gvc.h>
 
-#include <QFile>
-#include <QTextStream>
-
 #include <kdebug.h>
-#include <ktemporaryfile.h>
 
 namespace KGraphViz
 {
-  
+
 GraphExporter::GraphExporter()
 {
 }
@@ -47,90 +44,21 @@ GraphExporter::~GraphExporter()
 {
 }
 
-QString GraphExporter::writeDot(const DotGraph* graph, const QString& fileName)
-{
-  kDebug() << fileName;
-
-  QString actualFileName = fileName;
-
-  if (fileName.isEmpty())
-  {
-    KTemporaryFile tempFile;
-    tempFile.setSuffix(".dot");
-    if (!tempFile.open()) 
-    {
-      kError() << "Unable to open for temp file for writing " << tempFile.name() << endl;
-      exit(2);
-    }
-    actualFileName = tempFile.name();
-    kDebug() << "using " << actualFileName;
-  }
-  
-  QFile f(actualFileName);
-  if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
-  {
-    kError() << "Unable to open file for writing " << fileName << endl;
-    exit(2);
-  }
-  
-  QTextStream stream(&f);
-
-  stream << "digraph \"";
-  if (graph->id()!="\"\"")
-  {
-    stream <<graph->id();
-  }
-  stream <<"\" {\n";
-
-  stream << "graph [" << *graph <<"]" << endl;
-
-  /// @TODO Subgraph are not represented as needed in DotGraph, so it is not
-  /// possible to save them back : to be changed !
-//   kDebug() << "writing subgraphs";
-  GraphSubgraphMap::const_iterator sit;
-  for ( sit = graph->subgraphs().begin();
-  sit != graph->subgraphs().end(); ++sit )
-  {
-    const GraphSubgraph& s = **sit;
-    (stream) << s;
-  }
-
-//   kDebug() << "writing nodes";
-  GraphNodeMap::const_iterator nit;
-  for ( nit = graph->nodes().begin();
-        nit != graph->nodes().end(); ++nit )
-  {
-    (stream) << **nit;
-  }
-
-  kDebug() << "writing edges";
-  GraphEdgeMap::const_iterator eit;
-  for ( eit = graph->edges().begin();
-        eit != graph->edges().end(); ++eit )
-  {
-    kDebug() << "writing edge" << (*eit)->id();
-    stream << **eit;
-  }
-
-  stream << "}\n";
-
-  f.close();
-  return actualFileName;
-}
-
 graph_t* GraphExporter::exportToGraphviz(const DotGraph* graph)
 {
+  kDebug() << graph;
+
   int type = graph->directed()
       ?(graph->strict()?AGDIGRAPHSTRICT:AGDIGRAPH)
       :(graph->strict()?AGRAPHSTRICT:AGRAPH);
-  
-  graph_t* agraph = agopen((graph->id()!="\"\"")?graph->id().toUtf8().data():QString("unnamed").toUtf8().data(), type);
 
-  QTextStream stream;
+  graph_t* agraph = agopen((graph->id()!="\"\"")
+    ?graph->id().toUtf8().data()
+    :QString("unnamed").toUtf8().data(), type);
+
   graph->exportToGraphviz(agraph);
   /// @TODO Subgraph are not represented as needed in DotGraph, so it is not
   /// possible to save them back : to be changed !
-  //   kDebug() << "writing subgraphs";
   GraphSubgraphMap::const_iterator sit;
   for ( sit = graph->subgraphs().begin();
   sit != graph->subgraphs().end(); ++sit )
@@ -140,7 +68,6 @@ graph_t* GraphExporter::exportToGraphviz(const DotGraph* graph)
     s.exportToGraphviz(subgraph);
   }
   
-  //   kDebug() << "writing nodes";
   GraphNodeMap::const_iterator nit;
   foreach (GraphNode* n, graph->nodes())
   {
@@ -148,16 +75,14 @@ graph_t* GraphExporter::exportToGraphviz(const DotGraph* graph)
     n->exportToGraphviz(node);
   }
   
-  kDebug() << "writing edges";
   GraphEdgeMap::const_iterator eit;
   foreach (GraphEdge* e, graph->edges())
   {
-    kDebug() << "writing edge" << e->id();
     edge_t* edge = agedge(agraph, agnode(agraph, e->fromNode()->id().toUtf8().data()),
                           agnode(agraph, e->toNode()->id().toUtf8().data()));
     e->exportToGraphviz(edge);
   }
-  
+
   return agraph;
 }
 
diff --git a/src/kgraphviz/graphexporter.h b/src/kgraphviz/graphexporter.h
index 4623849..902763d 100644
--- a/src/kgraphviz/graphexporter.h
+++ b/src/kgraphviz/graphexporter.h
@@ -24,23 +24,16 @@
    License as published by the Free Software Foundation, version 2.
 */
 
-/*
- * Graph Exporter
- */
-
 #ifndef GRAPH_EXPORTER_H
 #define GRAPH_EXPORTER_H
 
-#include <QString>
-
 class Agraph_t;
 
-class KTemporaryFile;
-
 namespace KGraphViz
 {
+
 class DotGraph;
-  
+
 /**
  * GraphExporter
  *
@@ -52,13 +45,9 @@ public:
   GraphExporter();
   virtual ~GraphExporter();
 
-  QString writeDot(const DotGraph* graph, const QString& fileName = QString());
   Agraph_t* exportToGraphviz(const DotGraph* graph);
 };
 
 }
 
 #endif
-
-
-
diff --git a/src/kgraphviz/graphio.cpp b/src/kgraphviz/graphio.cpp
index b24b38c..4c2e3d9 100644
--- a/src/kgraphviz/graphio.cpp
+++ b/src/kgraphviz/graphio.cpp
@@ -91,11 +91,10 @@ QString GraphIOPrivate::toString(QProcess::ProcessError error)
 
 void GraphIOPrivate::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
 {
-  kDebug() << "Error message:" << toString(m_process.error()) << m_process.errorString();
-
-  // TODO: Better error handling here
   QByteArray result = m_process.readAll();
   if (exitCode != 0) {
+    kDebug() << "Error message:" << toString(m_process.error()) << m_process.errorString();
+    kWarning() << "Exit code not okay:" << exitCode;
     emit error(i18n("Failed to load from dot file: %1", result.data()));
     return;
   }
@@ -137,7 +136,7 @@ void GraphIOPrivate::processFinished(int exitCode, QProcess::ExitStatus exitStat
 
 void GraphIOPrivate::processError(QProcess::ProcessError error)
 {
-  kDebug();
+  kDebug() << toString(error) << '-' << m_process.errorString();
   emit(toString(error));
 }
 
@@ -190,53 +189,13 @@ void GraphIO::saveToDotFile(const DotGraph* dotGraph, const QString& fileName)
   kDebug() << fileName;
 
   QFile f(fileName);
-  if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
-  {
+  if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) {
     kError() << "Unable to open file for writing:" << fileName;
     return;
   }
 
   QTextStream stream(&f);
-  stream << "digraph \"";
-  if (dotGraph->id()!="\"\"")
-  {
-    stream <<dotGraph->id();
-  }
-  stream <<"\" {\n";
-
-  stream << "graph [" << *dotGraph <<"]" << endl;
-
-  /// @TODO Subgraph are not represented as needed in DotGraph, so it is not
-  /// possible to save them back : to be changed !
-//   kDebug() << "writing subgraphs";
-  GraphSubgraphMap::const_iterator sit;
-  for ( sit = dotGraph->subgraphs().begin();
-  sit != dotGraph->subgraphs().end(); ++sit )
-  {
-    const GraphSubgraph& s = **sit;
-    (stream) << s;
-  }
-
-  kDebug() << "writing nodes";
-  GraphNodeMap::const_iterator nit;
-  for ( nit = dotGraph->nodes().begin();
-        nit != dotGraph->nodes().end(); ++nit )
-  {
-    kDebug() << "writing node" << (*nit)->id();
-    (stream) << **nit;
-  }
-
-  kDebug() << "writing edges";
-  GraphEdgeMap::const_iterator eit;
-  for ( eit = dotGraph->edges().begin();
-        eit != dotGraph->edges().end(); ++eit )
-  {
-    kDebug() << "writing edge" << (*eit)->id();
-    stream << **eit;
-  }
-
-  stream << "}\n";
-
+  stream << *dotGraph;
   f.close();
 }
 


More information about the kgraphviewer-devel mailing list