[kgraphviewer-devel] [KGraphViewer/libkgraphviz] b5ac967: Merge GraphExporter.writeDot to GraphIO, add tests

Kevin Funk krf at electrostorm.net
Mon Dec 13 18:18:33 CET 2010


	A	 tests/kgraphviz/utils.h	 [License: UNKNOWN]

commit b5ac967bbe351bc9f549898887aab774f187468f
Author: Kevin Funk <krf at electrostorm.net>
Date:   Mon Dec 13 17:22:26 2010 +0100

    Merge GraphExporter.writeDot to GraphIO, add tests

diff --git a/src/kgraphviz/graphio.cpp b/src/kgraphviz/graphio.cpp
index 5b1fc62..56dd15b 100644
--- a/src/kgraphviz/graphio.cpp
+++ b/src/kgraphviz/graphio.cpp
@@ -25,6 +25,7 @@
 
 #include <KDebug>
 #include <KLocale>
+#include <KTemporaryFile>
 
 #include "dotgraph.h"
 #include "graphexporter.h"
@@ -149,11 +150,70 @@ void GraphIO::loadFromDotFile(const QString& fileName, const QString& layoutComm
   d->m_process.start(layoutCommandForFile, args, QIODevice::ReadOnly);
 }
 
-/* TODO: Merge GraphExporter here */
 void GraphIO::saveToDotFile(const DotGraph* dotGraph, const QString& fileName)
 {
-  GraphExporter exporter;
-  exporter.writeDot(dotGraph, fileName);
+  kDebug() << fileName;
+
+  QFile f(fileName);
+  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 )
+  {
+    (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";
+
+  f.close();
+}
+
+void GraphIO::updateDot(const DotGraph* dotGraph)
+{
+  KTemporaryFile tempFile;
+  tempFile.setSuffix(".dot");
+
+  const QString fileName = tempFile.name();
+  kDebug() << "Using temporary file:" << fileName;
+
+  saveToDotFile(dotGraph, fileName);
+  loadFromDotFile(fileName);
 }
 
 QString GraphIO::internalLayoutCommandForFile(const QString& fileName)
diff --git a/src/kgraphviz/graphio.h b/src/kgraphviz/graphio.h
index f7b8038..72bb7b2 100644
--- a/src/kgraphviz/graphio.h
+++ b/src/kgraphviz/graphio.h
@@ -43,6 +43,8 @@ public:
   void loadFromDotFile(const QString& fileName, const QString& layoutCommand = QString());
   void saveToDotFile(const DotGraph* graph, const QString& fileName);
 
+  void updateDot(const DotGraph* dotGraph);
+
   static QString internalLayoutCommandForFile(const QString& fileName);
 
 Q_SIGNALS:
diff --git a/tests/kgraphviz/graphiotests.cpp b/tests/kgraphviz/graphiotests.cpp
index 78d1148..63591de 100644
--- a/tests/kgraphviz/graphiotests.cpp
+++ b/tests/kgraphviz/graphiotests.cpp
@@ -17,6 +17,8 @@
     Boston, MA 02110-1301, USA.
 */
 
+#include "utils.h"
+
 #include <kgraphviz/graphio.h>
 #include <kgraphviz/dotgraph.h>
 
@@ -35,6 +37,8 @@ private Q_SLOTS:
   void testImportGraphFromInvalidFile();
 
   void testExportImportGraph();
+
+  void testUpdateDotGraph();
 };
 
 using namespace KGraphViz;
@@ -97,5 +101,27 @@ void GraphIOTests::testExportImportGraph()
   QFile::remove(TEST_FILENAME);
 }
 
+void GraphIOTests::testUpdateDotGraph()
+{
+  // populate
+  QMap <QString, QString> map;
+  DotGraph graph;
+  map["id"] = "State1";
+  graph.addNewNode(map);
+  map["id"] = "State2";
+  graph.addNewNode(map);
+  map.clear();
+  graph.addNewEdge("State1", "State2", map);
+
+  GraphIO io;
+  io.updateDot(&graph);
+  QSignalSpy spy(&io, SIGNAL(finished()));
+  QVERIFY(QTest::kWaitForSignal(&io, SIGNAL(finished()), 3000) == true);
+
+  DotGraph* updatedGraph = io.readData();
+  QVERIFY(updatedGraph != 0);
+  QVERIFY(Utilities::haveSameElementCount(&graph, updatedGraph) == true);
+}
+
 QTEST_KDEMAIN_CORE(GraphIOTests)
 #include "graphiotests.moc"
diff --git a/tests/kgraphviz/utils.h b/tests/kgraphviz/utils.h
new file mode 100644
index 0000000..af19aee
--- /dev/null
+++ b/tests/kgraphviz/utils.h
@@ -0,0 +1,35 @@
+/*
+    Copyright (C) 2010 Kevin Funk <krf at electrostorm.net>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Library General Public
+    License as published by the Free Software Foundation; either
+    version 2 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU Library General Public License
+    along with this library; see the file COPYING.LIB.  If not, write to
+    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA 02110-1301, USA.
+*/
+
+#ifndef KGRAPHVIZ_TESTS_UTILS_H
+#define KGRAPHVIZ_TESTS_UTILS_H
+
+#include <kgraphviz/dotgraph.h>
+
+namespace Utilities
+{
+  bool haveSameElementCount(const KGraphViz::DotGraph* left,
+                            const KGraphViz::DotGraph* right)
+  {
+    return (left->nodes().size() == right->nodes().size())
+        && (left->edges().size() == right->edges().size());
+  }
+}
+
+#endif
\ No newline at end of file


More information about the kgraphviewer-devel mailing list