• 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
  • kmlFileFormat
KmlFileFormatPlugin.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 "KmlFileFormatPlugin.h"
22 
23 #include "Document.h"
24 #include "DataStructure.h"
25 #include <DataStructureBackendManager.h>
26 #include "Data.h"
27 #include "KmlHandler.h"
28 
29 #include <KDebug>
30 #include <KAboutData>
31 #include <KGenericFactory>
32 #include <KUrl>
33 
34 #include <QFile>
35 #include <QXmlResultItems>
36 #include <QXmlNodeModelIndex>
37 
38 static const KAboutData aboutdata("rocs_kmlfileformat",
39  0,
40  ki18nc("@title Displayed plugin name", "KML File Backend"),
41  "0.1",
42  ki18n("Read and write Keyhole Markup Language (KML) files."),
43  KAboutData::License_GPL_V2);
44 
45 K_PLUGIN_FACTORY(FilePLuginFactory, registerPlugin<KmlFileFormatPlugin>();)
46 K_EXPORT_PLUGIN(FilePLuginFactory(aboutdata))
47 
48 
49 KmlFileFormatPlugin::KmlFileFormatPlugin(QObject *parent, const QList< QVariant >&)
50  : GraphFilePluginInterface(FilePLuginFactory(aboutdata, 0).componentData().aboutData(), parent)
51 {
52 }
53 
54 KmlFileFormatPlugin::~KmlFileFormatPlugin()
55 {
56 }
57 
58 
59 const QStringList KmlFileFormatPlugin::extensions() const
60 {
61  return QStringList()
62  << i18n("*.kml|Keyhole Markup Language Format") + '\n';
63 }
64 
65 
66 void KmlFileFormatPlugin::writeFile(Document& document)
67 {
68  // TODO allow selection which data structure shall be exported
69  QFile fileHandle(file().toLocalFile());
70  DataStructurePtr graph = document.activeDataStructure();
71  if (!fileHandle.open(QIODevice::WriteOnly | QIODevice::Text)) {
72  setError(FileIsReadOnly, i18n("Cannot open file %1: %2", file().fileName(), fileHandle.errorString()));
73  return;
74  }
75  QXmlStreamWriter xmlWriter(&fileHandle);
76 
77  xmlWriter.setAutoFormatting(true);
78  xmlWriter.writeStartDocument();
79  xmlWriter.writeStartElement("kml");
80  xmlWriter.writeNamespace("http://www.opengis.net/kml/2.2");
81  xmlWriter.writeStartElement("Document");
82  //FIXME only default data type considered
83  if (graph->pointers(0).isEmpty()) {
84  foreach(DataPtr n, graph->dataList(0)) {
85  xmlWriter.writeStartElement("Placemark");
86  xmlWriter.writeStartElement("name");
87  xmlWriter.writeCharacters(n->property("name").toString());
88  if (n->property("description").isValid()) {
89  xmlWriter.writeCharacters(n->property("description").toString());
90  }
91  xmlWriter.writeStartElement("Point");
92  xmlWriter.writeStartElement("coordinates");
93 
94  if (n->property("Longitude").isValid()) {
95  xmlWriter.writeCharacters(QString("%1,%2,%3").arg(n->property("Longitude").toString(),
96  n->property("Latitude").toString(),
97  n->property("Elevation").toString()));
98  } else {
99  xmlWriter.writeCharacters(QString("%1,%2,%3").arg(n->property("x").toString(),
100  n->property("y").toString())
101  .arg(0));
102  }
103  }
104  } else {
105  xmlWriter.writeStartElement("Placemark");
106  xmlWriter.writeStartElement("name");
107  {
108  QString s = graph->dataList(0).at(0)->property("name").toString();
109  s.chop(2);
110  xmlWriter.writeCharacters(s);
111  }
112  xmlWriter.writeEndElement();
113  xmlWriter.writeStartElement("description");
114 
115  if (graph->dataList(0).at(0)->property("description").isValid()) {
116  xmlWriter.writeCharacters(graph->dataList(0).at(0)->property("description").toString());
117  }
118  xmlWriter.writeEndElement();
119 
120  xmlWriter.writeStartElement("LineString");
121  xmlWriter.writeStartElement("coordinates");
122 
123  foreach(DataPtr n, graph->dataList(0)) {
124  if (n->property("Longitude").isValid()) {
125  xmlWriter.writeCharacters(QString("%1,%2,%3\n").arg(n->property("Longitude").toString(),
126  n->property("Latitude").toString(),
127  n->property("Elevation").toString()));
128  } else {
129  xmlWriter.writeCharacters(QString("%1,%2,%3").arg(n->property("x").toString(),
130  n->property("y").toString())
131  .arg(0));
132  }
133  }
134 
135  }
136  xmlWriter.writeEndDocument();
137  setError(None);
138  return;
139 }
140 
141 
142 void KmlFileFormatPlugin::readFile()
143 {
144  Document * graphDoc = new Document(i18n("Import"));
145  DataStructureBackendManager::self().setBackend("Graph");
146  DataStructurePtr graph = graphDoc->addDataStructure();
147 
148  KmlHandler handler(graph);
149  QFile fileHandle(file().toLocalFile());
150  QXmlInputSource source(&fileHandle);
151  QXmlSimpleReader xmlReader;
152  xmlReader.setContentHandler(&handler);
153  xmlReader.setErrorHandler(&handler);
154  if (!xmlReader.parse(&source)) {
155  setError(EncodingProblem, handler.errorString());
156  delete graphDoc;
157  return;
158  }
159 
160  setGraphDocument(graphDoc);
161  return;
162 }
163 
164 #include "KmlFileFormatPlugin.moc"
165 
KmlHandler.h
GraphFilePluginInterface::None
Definition: GraphFilePluginInterface.h:51
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
GmlParser::document
Document * document
Definition: GmlGrammar.cpp:40
QObject
Data.h
DataStructureBackendManager::self
static DataStructureBackendManager & self()
Returns self reference to backend manager.
Definition: DataStructureBackendManager.cpp:233
KmlFileFormatPlugin::~KmlFileFormatPlugin
~KmlFileFormatPlugin()
Definition: KmlFileFormatPlugin.cpp:54
Document.h
KmlHandler::errorString
QString errorString() const
Definition: KmlHandler.cpp:114
DataStructure.h
GraphFilePluginInterface::setError
void setError(Error error, QString message=QString())
Definition: GraphFilePluginInterface.cpp:83
Document::addDataStructure
DataStructurePtr addDataStructure(const QString &name=QString())
Add data structure to graph document with name name.
Definition: Document.cpp:333
Document::activeDataStructure
DataStructurePtr activeDataStructure() const
Definition: Document.cpp:431
KmlFileFormatPlugin
class KmlFileFormatPlugin: Export and Import Plugin for KML files
Definition: KmlFileFormatPlugin.h:30
KmlFileFormatPlugin::extensions
virtual const QStringList extensions() const
File extensions that are common for this file type.
Definition: KmlFileFormatPlugin.cpp:59
aboutdata
static const KAboutData aboutdata("rocs_kmlfileformat", 0, ki18nc("@title Displayed plugin name","KML File Backend"),"0.1", ki18n("Read and write Keyhole Markup Language (KML) files."), KAboutData::License_GPL_V2)
Document
Definition: Document.h:41
DataStructureBackendManager::setBackend
void setBackend(const QString &pluginIdentifier)
Change the active backend.
Definition: DataStructureBackendManager.cpp:240
GraphFilePluginInterface::FileIsReadOnly
Definition: GraphFilePluginInterface.h:53
GraphFilePluginInterface::EncodingProblem
Definition: GraphFilePluginInterface.h:56
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
KmlFileFormatPlugin::readFile
virtual void readFile()
Open given file and imports it into internal format.
Definition: KmlFileFormatPlugin.cpp:142
KmlHandler
Definition: KmlHandler.h:29
GraphFilePluginInterface::setGraphDocument
void setGraphDocument(Document *document)
Definition: GraphFilePluginInterface.cpp:108
KmlFileFormatPlugin::writeFile
virtual void writeFile(Document &graph)
Writes given graph document to formerly specified file.
Definition: KmlFileFormatPlugin.cpp:66
KmlFileFormatPlugin.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
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:26 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