[kgraphviewer-devel] [KGraphViewer/libkgraphviz] b3d03a3: Introduce GraphIO for import/export of graphs
Kevin Funk
krf at electrostorm.net
Mon Dec 13 18:18:33 CET 2010
A src/kgraphviz/graphio_p.h [License: UNKNOWN]
A src/kgraphviz/graphio.h [License: UNKNOWN]
A src/kgraphviz/graphio.cpp [License: UNKNOWN]
commit b3d03a3cc7db8bc8d7909e75fb40537983ac9d4f
Author: Kevin Funk <krf at electrostorm.net>
Date: Mon Dec 13 16:41:46 2010 +0100
Introduce GraphIO for import/export of graphs
Work in progress: not yet used in code.
diff --git a/src/kgraphviz/CMakeLists.txt b/src/kgraphviz/CMakeLists.txt
index 65b2ce6..b0b9996 100644
--- a/src/kgraphviz/CMakeLists.txt
+++ b/src/kgraphviz/CMakeLists.txt
@@ -20,6 +20,7 @@ set( kgraphvz_LIB_SRCS
graphedge.cpp
graphexporter.cpp
graphelement.cpp
+ graphio.cpp
graphnode.cpp
graphsubgraph.cpp
dotgraph.cpp
diff --git a/src/kgraphviz/graphio.cpp b/src/kgraphviz/graphio.cpp
new file mode 100644
index 0000000..5b1fc62
--- /dev/null
+++ b/src/kgraphviz/graphio.cpp
@@ -0,0 +1,182 @@
+/*
+ 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.
+*/
+
+#include "graphio.h"
+#include "graphio_p.h"
+
+#include <QFile>
+#include <QProcess>
+
+#include <KDebug>
+#include <KLocale>
+
+#include "dotgraph.h"
+#include "graphexporter.h"
+#include "support/dotgrammar.h"
+#include "support/dotgraphparsinghelper.h"
+
+#include <boost/spirit/include/classic_assign_actor.hpp>
+#include <boost/spirit/include/classic_confix.hpp>
+#include <boost/spirit/include/classic_distinct.hpp>
+
+using namespace boost;
+using namespace boost::spirit::classic;
+
+using namespace KGraphViz;
+
+extern KGraphViz::DotGraphParsingHelper* phelper;
+
+const distinct_parser<> keyword_p("0-9a-zA-Z_");
+
+GraphIOPrivate::GraphIOPrivate(QObject* parent)
+ : QObject(parent)
+ , m_dotGraph(0)
+{
+ connect(&m_process,
+ SIGNAL(finished(int, QProcess::ExitStatus)),
+ SLOT(processFinished(int, QProcess::ExitStatus)));
+}
+
+GraphIOPrivate::~GraphIOPrivate()
+{
+}
+
+void GraphIOPrivate::reset()
+{
+ if (m_dotGraph != 0) {
+ delete m_dotGraph;
+ m_dotGraph = 0;
+ }
+}
+
+void GraphIOPrivate::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
+{
+ qDebug() << "Exit status:" << exitStatus;
+
+ if (exitCode != 0) {
+ emit error(i18n("Failed to load from dot file"));
+ return;
+ }
+
+ QByteArray result = m_process.readAll();
+ result.replace("\\\n", "");
+
+ kDebug() << "String size is:" << result.size();
+ kDebug() << "String content is:" << result;
+ const std::string content = result.data();
+
+ if (phelper != 0) {
+ delete phelper;
+ phelper = 0;
+ }
+
+ DotGraph graph;
+ phelper = new DotGraphParsingHelper;
+ phelper->graph = &graph;
+ phelper->z = 1;
+ phelper->maxZ = 1;
+ phelper->uniq = 0;
+
+ kDebug() << "Parsing dot";
+ const bool parsingResult = parse(content);
+ delete phelper;
+ phelper = 0;
+
+ if (parsingResult)
+ {
+ m_dotGraph = new DotGraph;
+ m_dotGraph->updateWithGraph(graph);
+ }
+ else
+ {
+ kError() << "parsing failed";
+ }
+
+ emit(finished());
+}
+
+GraphIO::GraphIO(QObject* parent)
+ : QObject(parent)
+ , d_ptr(new GraphIOPrivate)
+{
+ Q_D(const GraphIO);
+ connect(d, SIGNAL(finished()), SIGNAL(finished()));
+ connect(d, SIGNAL(error(QString)), SIGNAL(error(QString)));
+}
+
+GraphIO::~GraphIO()
+{
+ delete d_ptr;
+}
+
+DotGraph* GraphIO::readData()
+{
+ Q_D(GraphIO);
+
+ DotGraph* graph = d->m_dotGraph;
+ d->m_dotGraph = 0;
+ return graph;
+}
+
+void GraphIO::loadFromDotFile(const QString& fileName, const QString& layoutCommand)
+{
+ Q_D(GraphIO);
+
+ const QString layoutCommandForFile = (layoutCommand.isEmpty()
+ ? internalLayoutCommandForFile(fileName)
+ : layoutCommand);
+
+ QStringList args;
+ args << fileName;
+
+ kDebug() << "Loading from" << fileName << "with" << layoutCommandForFile << "executable.";
+ 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);
+}
+
+QString GraphIO::internalLayoutCommandForFile(const QString& fileName)
+{
+ QFile file(fileName);
+
+ if (!file.open(QIODevice::ReadOnly)) {
+ kWarning() << "Can't test dot file. Will try to use the dot command on the file:" << fileName;
+ return "dot"; // -Txdot";
+ }
+
+ QByteArray fileContent = file.readAll();
+ if (fileContent.isEmpty()) return "";
+ std::string s = fileContent.data();
+ std::string cmd = "dot";
+ parse(s.c_str(),
+ (
+ !(keyword_p("strict")) >> (keyword_p("graph")[assign_a(cmd,"neato")])
+ ), (space_p|comment_p("/*", "*/")) );
+
+ return QString::fromStdString(cmd); // + " -Txdot" ;
+}
+
+
+#include "graphio.moc"
+#include "graphio_p.moc"
diff --git a/src/kgraphviz/graphio.h b/src/kgraphviz/graphio.h
new file mode 100644
index 0000000..f7b8038
--- /dev/null
+++ b/src/kgraphviz/graphio.h
@@ -0,0 +1,59 @@
+/*
+ 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 GRAPHIO_H
+#define GRAPHIO_H
+
+#include <QObject>
+
+#include "kgraphviz_export.h"
+
+namespace KGraphViz
+{
+
+class DotGraph;
+class GraphIOPrivate;
+
+class KGRAPHVIZ_EXPORT GraphIO : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit GraphIO(QObject* parent = 0);
+ virtual ~GraphIO();
+
+ DotGraph* readData();
+
+ void loadFromDotFile(const QString& fileName, const QString& layoutCommand = QString());
+ void saveToDotFile(const DotGraph* graph, const QString& fileName);
+
+ static QString internalLayoutCommandForFile(const QString& fileName);
+
+Q_SIGNALS:
+ void finished();
+ void error(QString);
+
+private:
+ Q_DECLARE_PRIVATE(GraphIO)
+ GraphIOPrivate* const d_ptr;
+};
+
+}
+
+#endif // GRAPHIO_H
diff --git a/src/kgraphviz/graphio_p.h b/src/kgraphviz/graphio_p.h
new file mode 100644
index 0000000..01666d6
--- /dev/null
+++ b/src/kgraphviz/graphio_p.h
@@ -0,0 +1,50 @@
+/*
+ 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 GRAPHIO_P_H
+#define GRAPHIO_P_H
+
+#include "graphio.h"
+
+#include <QProcess>
+
+class KGraphViz::GraphIOPrivate
+ : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit GraphIOPrivate(QObject* parent = 0);
+ virtual ~GraphIOPrivate();
+
+ void reset();
+
+ DotGraph* m_dotGraph;
+
+ QProcess m_process;
+
+Q_SIGNALS:
+ void finished();
+ void error(QString);
+
+private Q_SLOTS:
+ void processFinished(int, QProcess::ExitStatus);
+};
+
+#endif
More information about the kgraphviewer-devel
mailing list