• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
  • LoadSave
  • Plugins
  • dotFileFormat
DotFileFormatPlugin.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2010 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2010 Wagner Reck <wagner.reck@gmail.com>
5  Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "DotFileFormatPlugin.h"
22 
23 #include <KAboutData>
24 #include <KGenericFactory>
25 #include <KUrl>
26 #include <QFile>
27 #include <QHash>
28 
29 #include <DataStructure.h>
30 #include <Document.h>
31 #include <DataStructureBackendManager.h>
32 #include <Modifiers/Topology.h>
33 #include "DataStructures/Graph/GraphStructure.h"
34 #include "DotGraphParsingHelper.h"
35 #include "DotGrammar.h"
36 #include "CoreTypes.h"
37 #include <Group.h>
38 
39 static const KAboutData aboutdata("rocs_dotfileformat",
40  0,
41  ki18nc("@title Displayed plugin name", "Graphviz Graph File Backend"),
42  "0.1",
43  ki18n("Read and write Graphviz graph files."),
44  KAboutData::License_GPL_V2);
45 
46 extern DotParser::DotGraphParsingHelper* phelper;
47 
48 K_PLUGIN_FACTORY(FilePluginFactory, registerPlugin<DotFileFormatPlugin>();)
49 K_EXPORT_PLUGIN(FilePluginFactory(aboutdata))
50 
51 
52 DotFileFormatPlugin::~DotFileFormatPlugin()
53 {
54 }
55 
56 DotFileFormatPlugin::DotFileFormatPlugin(QObject* parent, const QList< QVariant >&) :
57  GraphFilePluginInterface(FilePluginFactory(aboutdata,0).componentData().aboutData(), parent)
58 {
59 }
60 
61 
62 const QStringList DotFileFormatPlugin::extensions() const
63 {
64  return QStringList()
65  << i18n("*.dot *.gv|Graphviz Format") + '\n';
66 }
67 
68 
69 void DotFileFormatPlugin::readFile()
70 {
71  Document * graphDoc = new Document(i18n("Import"));
72  DataStructureBackendManager::self().setBackend("Graph");
73 
74  QList < QPair<QString, QString> > edges;
75  QFile fileHandle(file().toLocalFile());
76  if (!fileHandle.open(QFile::ReadOnly)) {
77  setError(CouldNotOpenFile, i18n("Could not open file \"%1\" in read mode: %2", file().toLocalFile(), fileHandle.errorString()));
78  delete graphDoc;
79  return;
80  }
81  QString content = fileHandle.readAll();
82  if (!DotParser::parse(content.toStdString(), graphDoc)) {
83  setError(EncodingProblem, i18n("Could not parse file \"%1\".", file().toLocalFile()));
84  delete graphDoc;
85  return;
86  }
87  Topology layouter;
88  layouter.directedGraphDefaultTopology(graphDoc->activeDataStructure());
89  setGraphDocument(graphDoc);
90  setError(None);
91 }
92 
93 
94 void DotFileFormatPlugin::writeFile(Document &document)
95 {
96  DataStructurePtr graph = document.activeDataStructure();
97  //TODO make export graph selectable
98  if (!graph) {
99  setError(NoGraphFound, i18n("No active graph in this document."));
100  return;
101  }
102 
103  // prepare file handle for output
104  QFile fileHandle(file().toLocalFile());
105  QVariantList subgraphs;
106  if (!fileHandle.open(QFile::WriteOnly | QFile::Text)) {
107  setError(FileIsReadOnly, i18n("Cannot open file %1 to write document. Error: %2", file().fileName(), fileHandle.errorString()));
108  return;
109  }
110  QTextStream out(&fileHandle);
111 
112  // create fast access list of already processed nodes: serialize each node only once
113  QHash<int, bool> processedData;
114 
115  // process groups
116  foreach(GroupPtr group, graph->groups()) {
117  out << QString("subgraph %1 {\n").arg(group->name());
118  foreach(DataPtr data, group->dataList()) {
119  out << processNode(data);
120  processedData.insert(data->identifier(), true);
121  }
122  out << "}\n";
123  }
124 
125  // process all data elements
126  foreach(int type, document.dataTypeList()) {
127  if (type == document.groupType()) { // do not process groups
128  continue;
129  }
130  foreach(DataPtr n, graph->dataList(type)) {
131  if (processedData.contains(n->identifier())) {
132  continue;
133  }
134  out << processNode(n);
135  }
136  }
137 
138  // process all edges
139  foreach(int type, document.pointerTypeList()) {
140  foreach(PointerPtr edge, graph->pointers(type)) {
141  out << processEdge(edge);
142  }
143  }
144  out << "}\n";
145  setError(None);
146  return;
147 }
148 
149 QString const DotFileFormatPlugin::processEdge(PointerPtr edge) const
150 {
151  QString edgeStr;
152  edgeStr.append(QString(" %1 -> %2 ")
153  .arg(edge->from()->identifier())
154  .arg(edge->to()->identifier()));
155 
156  // process properties if present
157  bool firstProperty = true;
158  if (!edge->property("name").toString().isEmpty()) {
159  firstProperty = false;
160  edgeStr.append("[");
161  edgeStr.append(QString(" label = \"%2\" ").arg(edge->property("name").toString()));
162  }
163  foreach(const QByteArray& property, edge->dynamicPropertyNames()) {
164  if (firstProperty == true) {
165  firstProperty = false;
166  edgeStr.append("[");
167  } else {
168  edgeStr.append(", ");
169  }
170  edgeStr.append(QString(" %1 = \"%2\" ").arg(QString(property)).arg(edge->property(property).toString()));
171  }
172  if (!firstProperty) { // at least one property was inserted
173  edgeStr.append("]");
174  }
175  return edgeStr.append(";\n");
176 }
177 
178 QString const DotFileFormatPlugin::processNode(DataPtr node) const
179 {
180  QString nodeStr;
181 
182  // use identifier for unique identification, store name as argument "label"
183  nodeStr = QString("%1").arg(node->identifier());
184  nodeStr.append(QString(" [label=%1 ").arg(node->property("name").toString()));
185 
186  foreach(const QByteArray& property, node->dynamicPropertyNames()) {
187  nodeStr.append(", ");
188  nodeStr.append(QString(" %1 = \"%2\" ").arg(QString(property)).arg(node->property(property).toString()));
189  }
190 
191  // at least one property was inserted
192  nodeStr.append("]");
193  return nodeStr.append(";\n");
194 }
195 
196 #include "DotFileFormatPlugin.moc"
aboutdata
static const KAboutData aboutdata("rocs_dotfileformat", 0, ki18nc("@title Displayed plugin name","Graphviz Graph File Backend"),"0.1", ki18n("Read and write Graphviz graph files."), KAboutData::License_GPL_V2)
GraphFilePluginInterface::None
Definition: GraphFilePluginInterface.h:51
Topology.h
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
GmlParser::document
Document * document
Definition: GmlGrammar.cpp:40
Group.h
Topology::directedGraphDefaultTopology
void directedGraphDefaultTopology(DataStructurePtr dataStructure)
applies a default topology for undirected graphs
Definition: Topology.cpp:214
CoreTypes.h
QObject
phelper
DotParser::DotGraphParsingHelper * phelper
Definition: DotGrammar.cpp:157
DataStructureBackendManager::self
static DataStructureBackendManager & self()
Returns self reference to backend manager.
Definition: DataStructureBackendManager.cpp:233
Document.h
DotGraphParsingHelper.h
GraphStructure.h
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
DataStructure.h
GraphFilePluginInterface::setError
void setError(Error error, QString message=QString())
Definition: GraphFilePluginInterface.cpp:83
Document::activeDataStructure
DataStructurePtr activeDataStructure() const
Definition: Document.cpp:431
Topology
this class provides topology modifiers for data structures
Definition: Topology.h:36
DotParser::DotGraphParsingHelper
Definition: DotGraphParsingHelper.h:35
DotGrammar.h
GraphFilePluginInterface::NoGraphFound
Definition: GraphFilePluginInterface.h:55
Document
Definition: Document.h:41
DotFileFormatPlugin::DotFileFormatPlugin
DotFileFormatPlugin(QObject *parent, const QList< QVariant > &)
Definition: DotFileFormatPlugin.cpp:56
DataStructureBackendManager::setBackend
void setBackend(const QString &pluginIdentifier)
Change the active backend.
Definition: DataStructureBackendManager.cpp:240
GroupPtr
boost::shared_ptr< Group > GroupPtr
Definition: CoreTypes.h:39
GraphFilePluginInterface::FileIsReadOnly
Definition: GraphFilePluginInterface.h:53
GraphFilePluginInterface::EncodingProblem
Definition: GraphFilePluginInterface.h:56
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
GraphFilePluginInterface::CouldNotOpenFile
Definition: GraphFilePluginInterface.h:54
Document::pointerTypeList
QList< int > pointerTypeList() const
Getter for all registered pointer types.
Definition: Document.cpp:165
Document::groupType
int groupType()
Returns data type for element groups.
Definition: Document.cpp:217
Document::dataTypeList
QList< int > dataTypeList() const
Getter for all registered data types.
Definition: Document.cpp:160
DotFileFormatPlugin::extensions
virtual const QStringList extensions() const
File extensions that are common for this file type.
Definition: DotFileFormatPlugin.cpp:62
DotParser::parse
bool parse(const std::string &str, Document *graphDoc)
Parse the given string str that represents the textual respresentation of a graph in DOT/Graphviz for...
Definition: DotGrammar.cpp:474
GraphFilePluginInterface::setGraphDocument
void setGraphDocument(Document *document)
Definition: GraphFilePluginInterface.cpp:108
DotFileFormatPlugin
Definition: DotFileFormatPlugin.h:28
DotFileFormatPlugin::writeFile
virtual void writeFile(Document &graph)
Writes given graph document to formerly specified file.
Definition: DotFileFormatPlugin.cpp:94
DotFileFormatPlugin.h
DataStructureBackendManager.h
GraphFilePluginInterface::file
const KUrl & file() const
Definition: GraphFilePluginInterface.cpp:120
GraphFilePluginInterface
This class provides an interface for graph file format plugins.
Definition: GraphFilePluginInterface.h:42
DotFileFormatPlugin::readFile
virtual void readFile()
Open given file and imports it into internal format.
Definition: DotFileFormatPlugin.cpp:69
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:25 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal