• 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
  • gmlFileFormat
GmlFileFormatPlugin.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 "GmlFileFormatPlugin.h"
22 
23 #include "Document.h"
24 #include "Pointer.h"
25 #include "DataStructure.h"
26 #include "DataStructureBackendManager.h"
27 #include "GmlGraphParsingHelper.h"
28 #include "GmlGrammar.h"
29 
30 #include <KAboutData>
31 #include <KGenericFactory>
32 #include <KUrl>
33 
34 #include <QFile>
35 
36 static const KAboutData aboutdata("rocs_gmlfileformat",
37  0,
38  ki18nc("@title Displayed plugin name", "GML File Backend"),
39  "0.1",
40  ki18n("Read and write Graph Markup Language (GML) files."),
41  KAboutData::License_GPL_V2);
42 
43 extern GmlParser::GmlGraphParsingHelper* phelper;
44 
45 K_PLUGIN_FACTORY(FilePLuginFactory, registerPlugin<GmlFileFormatPlugin>();)
46 K_EXPORT_PLUGIN(FilePLuginFactory(aboutdata))
47 
48 GmlFileFormatPlugin::GmlFileFormatPlugin(QObject *parent, const QList<QVariant>&) :
49  GraphFilePluginInterface(FilePLuginFactory(aboutdata, 0).componentData().aboutData(), parent)
50 {
51 }
52 
53 GmlFileFormatPlugin::~GmlFileFormatPlugin()
54 {
55 }
56 
57 const QStringList GmlFileFormatPlugin::extensions() const
58 {
59  return QStringList()
60  << i18n("*.gml|Graph Markup Language Format") + '\n';
61 }
62 
63 void GmlFileFormatPlugin::readFile()
64 {
65  Document * graphDoc = new Document(i18n("Import"));
66  DataStructureBackendManager::self().setBackend("Graph");
67 
68  QList < QPair<QString, QString> > edges;
69  QFile fileHandle(file().toLocalFile());
70  if (!fileHandle.open(QFile::ReadOnly)) {
71  setError(CouldNotOpenFile, i18n("Could not open file \"%1\" in read mode: %2", file().toLocalFile(), fileHandle.errorString()));
72  delete graphDoc;
73  return;
74  }
75  QString content = fileHandle.readAll();
76  if (!GmlParser::parse(content, graphDoc)) { //TODO change interface and pass graph structure
77  setError(EncodingProblem, i18n("Could not parse file \"%1\".", file().toLocalFile()));
78  delete graphDoc;
79  return;
80  }
81  setGraphDocument(graphDoc);
82  setError(None);
83 }
84 
85 
86 void GmlFileFormatPlugin::writeFile(Document& document)
87 {
88  DataStructurePtr graph = document.activeDataStructure();
89  //TODO make export graph selectable
90  if (!graph) {
91  setError(NoGraphFound, i18n("No active graph in this document."));
92  return;
93  }
94 
95  QFile fileHandle(file().toLocalFile());
96  QVariantList subgraphs;
97  if (!fileHandle.open(QFile::WriteOnly | QFile::Text)) {
98  setError(FileIsReadOnly, i18n("Cannot open file %1 to write document. Error: %2", file().fileName(), fileHandle.errorString()));
99  return;
100  } else {
101  QTextStream out(&fileHandle);
102 //FIXME uncommented following directed() check sind this is moved to subclass
103 //need to add toggle
104 // out << QString("graph [\n directed %1 \n").arg(g->directed()?"1":"0");
105  out << QString("id \"%1\" \n").arg(graph->name());
106 
107  foreach(DataPtr n, graph->dataListAll()) {
108  out << QString("node [\n id \"%1\" \n").arg(n->property("name").toString());
109 // foreach (QByteArray p, n->dynamicPropertyNames()){
110 // out << p << " " << n->property(p).toString() << "\n";
111 // }
112  out << processNode(n);
113  out << "]\n";
114 
115  }
116  foreach(PointerPtr e, graph->pointerListAll()) {
117  out << "edge [\n";
118 // foreach (QByteArray p, e->dynamicPropertyNames()){
119 // out << p << " " << e->property(p).toString() << "\n";
120 // }
121  out << processEdge(e);
122 
123  out << "]\n";
124  }
125  out << "]\n";
126  }
127  setError(None);
128  return;
129 }
130 
131 
132 QString const GmlFileFormatPlugin::processEdge(PointerPtr e) const
133 {
134  QString edge;
135  edge.append(QString("source \"%1\"\n target \"%2\"\n").arg(e->from()->property("name").toString(), e->to()->property("name").toString()));
136  edge.append(QString(" width \"%1\"\n").arg(e->width()));
137 // edge.append (QString(" color \"%1\"\n").arg(e->color())); //Problem with comments (both starts by '#')
138 
139  foreach(const QByteArray& p, e->dynamicPropertyNames()) {
140  edge.append(QString("%1 %2\n").arg(QString(p)).arg(e->property(p).toString()));
141  }
142 
143  return edge;
144 }
145 
146 
147 QString const GmlFileFormatPlugin::processNode(DataPtr n) const
148 {
149  QString node;
150  node.append(QString(" x %1 \n y %2 \n").arg(n->x()).arg(n->y()));
151  node.append(QString(" width %1\n").arg(n->width()));
152 // node.append (QString(" color \"%1\"\n").arg(n->color())); //Problem with comments (both starts by '#')
153  node.append(QString(" iconPackage \"%1\"\n").arg(n->dataStructure()->document()->iconPackage()));
154  node.append(QString(" icon \"%1\"\n").arg(n->icon()));
155 
156  foreach(const QByteArray& p, n->dynamicPropertyNames()) {
157  QString s = p ;
158 
159  node.append(QString("%1 \"%2\"\n").arg(s.replace('.', '_')).arg(n->property(p).toString()));
160  }
161 
162  return node;
163 }
164 
165 
166 #include "GmlFileFormatPlugin.moc"
GraphFilePluginInterface::None
Definition: GraphFilePluginInterface.h:51
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
GmlParser::document
Document * document
Definition: GmlGrammar.cpp:40
aboutdata
static const KAboutData aboutdata("rocs_gmlfileformat", 0, ki18nc("@title Displayed plugin name","GML File Backend"),"0.1", ki18n("Read and write Graph Markup Language (GML) files."), KAboutData::License_GPL_V2)
phelper
GmlParser::GmlGraphParsingHelper * phelper
Definition: DotGrammar.cpp:157
QObject
DataStructureBackendManager::self
static DataStructureBackendManager & self()
Returns self reference to backend manager.
Definition: DataStructureBackendManager.cpp:233
Document.h
GmlParser::parse
bool parse(const QString &content, Document *doc)
Definition: GmlGrammar.cpp:105
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
GmlFileFormatPlugin.h
DataStructure.h
GraphFilePluginInterface::setError
void setError(Error error, QString message=QString())
Definition: GraphFilePluginInterface.cpp:83
GmlFileFormatPlugin::readFile
virtual void readFile()
Open given file and imports it into internal format.
Definition: GmlFileFormatPlugin.cpp:63
GmlFileFormatPlugin::writeFile
virtual void writeFile(Document &document)
Writes given graph document to formerly specified file.
Definition: GmlFileFormatPlugin.cpp:86
Document::activeDataStructure
DataStructurePtr activeDataStructure() const
Definition: Document.cpp:431
GraphFilePluginInterface::NoGraphFound
Definition: GraphFilePluginInterface.h:55
GmlGrammar.h
Document
Definition: Document.h:41
GmlFileFormatPlugin
Definition: GmlFileFormatPlugin.h:31
GmlFileFormatPlugin::~GmlFileFormatPlugin
~GmlFileFormatPlugin()
Definition: GmlFileFormatPlugin.cpp:53
DataStructureBackendManager::setBackend
void setBackend(const QString &pluginIdentifier)
Change the active backend.
Definition: DataStructureBackendManager.cpp:240
Pointer.h
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
GraphFilePluginInterface::setGraphDocument
void setGraphDocument(Document *document)
Definition: GraphFilePluginInterface.cpp:108
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
GmlFileFormatPlugin::extensions
virtual const QStringList extensions() const
File extensions that are common for this file type.
Definition: GmlFileFormatPlugin.cpp:57
GmlParser::GmlGraphParsingHelper
Definition: GmlGraphParsingHelper.h:37
GmlGraphParsingHelper.h
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