• 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.14
  • 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("%1|Graphviz Format", QString("*.dot")) + '\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  out << "digraph {\n";
113 
114  // create fast access list of already processed nodes: serialize each node only once
115  QHash<int, bool> processedData;
116 
117  // process groups
118  foreach(GroupPtr group, graph->groups()) {
119  out << QString("subgraph %1 {\n").arg(group->name());
120  foreach(DataPtr data, group->dataList()) {
121  out << processNode(data);
122  processedData.insert(data->identifier(), true);
123  }
124  out << "}\n";
125  }
126 
127  // process all data elements
128  foreach(int type, document.dataTypeList()) {
129  if (type == document.groupType()) { // do not process groups
130  continue;
131  }
132  foreach(DataPtr n, graph->dataList(type)) {
133  if (processedData.contains(n->identifier())) {
134  continue;
135  }
136  out << processNode(n);
137  }
138  }
139 
140  // process all edges
141  foreach(int type, document.pointerTypeList()) {
142  foreach(PointerPtr edge, graph->pointers(type)) {
143  out << processEdge(edge);
144  }
145  }
146  out << "}\n";
147  setError(None);
148  return;
149 }
150 
151 QString const DotFileFormatPlugin::processEdge(PointerPtr edge) const
152 {
153  QString edgeStr;
154  edgeStr.append(QString(" %1 -> %2 ")
155  .arg(edge->from()->identifier())
156  .arg(edge->to()->identifier()));
157 
158  // process properties if present
159  bool firstProperty = true;
160  if (!edge->property("name").toString().isEmpty()) {
161  firstProperty = false;
162  edgeStr.append("[");
163  edgeStr.append(QString(" label = \"%2\" ").arg(edge->property("name").toString()));
164  }
165  foreach(const QByteArray& property, edge->dynamicPropertyNames()) {
166  if (firstProperty == true) {
167  firstProperty = false;
168  edgeStr.append("[");
169  } else {
170  edgeStr.append(", ");
171  }
172  edgeStr.append(QString(" %1 = \"%2\" ").arg(QString(property)).arg(edge->property(property).toString()));
173  }
174  if (!firstProperty) { // at least one property was inserted
175  edgeStr.append("]");
176  }
177  return edgeStr.append(";\n");
178 }
179 
180 QString const DotFileFormatPlugin::processNode(DataPtr node) const
181 {
182  QString nodeStr;
183 
184  // use identifier for unique identification, store name as argument "label"
185  nodeStr = QString("%1").arg(node->identifier());
186  nodeStr.append(QString(" [label=%1 ").arg(node->property("name").toString()));
187 
188  foreach(const QByteArray& property, node->dynamicPropertyNames()) {
189  nodeStr.append(", ");
190  nodeStr.append(QString(" %1 = \"%2\" ").arg(QString(property)).arg(node->property(property).toString()));
191  }
192 
193  // at least one property was inserted
194  nodeStr.append("]");
195  return nodeStr.append(";\n");
196 }
197 
198 #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)
QString::toStdString
std::string toStdString() const
QString::append
QString & append(QChar ch)
QHash::insert
iterator insert(const Key &key, const T &value)
GraphFilePluginInterface::None
Definition: GraphFilePluginInterface.h:51
Topology.h
QByteArray
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
GmlParser::document
Document * document
Definition: GmlGrammar.cpp:40
Group.h
QIODevice::errorString
QString errorString() const
Topology::directedGraphDefaultTopology
void directedGraphDefaultTopology(DataStructurePtr dataStructure)
applies a default topology for undirected graphs
Definition: Topology.cpp:214
CoreTypes.h
phelper
DotParser::DotGraphParsingHelper * phelper
Definition: DotGrammar.cpp:157
DataStructureBackendManager::self
static DataStructureBackendManager & self()
Returns self reference to backend manager.
Definition: DataStructureBackendManager.cpp:233
QFile
QTextStream
Document.h
DotGraphParsingHelper.h
GraphStructure.h
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
DataStructure.h
QObject::property
QVariant property(const char *name) const
QHash
QObject
GraphFilePluginInterface::setError
void setError(Error error, QString message=QString())
Definition: GraphFilePluginInterface.cpp:83
QIODevice::readAll
QByteArray readAll()
Document::activeDataStructure
DataStructurePtr activeDataStructure() const
Definition: Document.cpp:431
Topology
this class provides topology modifiers for data structures
Definition: Topology.h:36
QString
QList
DotParser::DotGraphParsingHelper
Definition: DotGraphParsingHelper.h:35
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
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
QHash::contains
bool contains(const Key &key) const
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
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
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-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:18 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
  • 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