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

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
FileManager.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2006-2007 Torsten Rahn <tackat@kde.org>
9 // Copyright 2007 Inge Wallin <ingwa@kde.org>
10 //
11 
12 #include "FileManager.h"
13 
14 #include <QDir>
15 #include <QFileInfo>
16 #include <QTime>
17 #include <QMessageBox>
18 
19 #include "FileLoader.h"
20 #include "MarbleDebug.h"
21 #include "MarbleModel.h"
22 #include "GeoDataTreeModel.h"
23 
24 #include "GeoDataDocument.h"
25 #include "GeoDataLatLonAltBox.h"
26 #include "GeoDataStyle.h"
27 #include "GeoWriter.h"
28 #include <KmlElementDictionary.h>
29 
30 
31 using namespace Marble;
32 
33 namespace Marble
34 {
35 class FileManagerPrivate
36 {
37 public:
38  FileManagerPrivate( MarbleModel* model, FileManager* parent )
39  : m_model( model ),
40  q( parent )
41  {
42  }
43 
44  ~FileManagerPrivate()
45  {
46  foreach ( FileLoader *loader, m_loaderList ) {
47  if ( loader ) {
48  loader->wait();
49  }
50  }
51  }
52 
53  void appendLoader( FileLoader *loader );
54  void closeFile( const QString &key );
55  void cleanupLoader( FileLoader *loader );
56 
57  MarbleModel* const m_model;
58 
59  FileManager * const q;
60  QList<FileLoader*> m_loaderList;
61  QHash < QString, GeoDataDocument* > m_fileItemHash;
62  GeoDataLatLonBox m_latLonBox;
63  QTime m_timer;
64 };
65 }
66 
67 FileManager::FileManager( MarbleModel *model, QObject *parent )
68  : QObject( parent )
69  , d( new FileManagerPrivate( model, this ) )
70 {
71 }
72 
73 
74 FileManager::~FileManager()
75 {
76  delete d;
77 }
78 
79 void FileManager::addFile( const QString& filepath, const QString& property, const GeoDataStyle* style, DocumentRole role, bool recenter )
80 {
81  if( d->m_fileItemHash.contains( filepath ) ) {
82  return; // already loaded
83  }
84 
85  foreach ( const FileLoader *loader, d->m_loaderList ) {
86  if ( loader->path() == filepath )
87  return; // currently loading
88  }
89 
90  mDebug() << "adding container:" << filepath;
91  mDebug() << "Starting placemark loading timer";
92  d->m_timer.start();
93  FileLoader* loader = new FileLoader( this, d->m_model, recenter, filepath, property, style, role );
94  d->appendLoader( loader );
95 }
96 
97 void FileManager::addFile( const QStringList& filepaths, const QStringList& propertyList, const QList<const GeoDataStyle*>& styles, DocumentRole role )
98 {
99  for (int i = 0 ; i < filepaths.size(); ++i ) {
100  addFile( filepaths.at(i), propertyList.at(i), styles.at(i), role );
101  }
102 }
103 
104 void FileManager::addData( const QString &name, const QString &data, DocumentRole role )
105 {
106  FileLoader* loader = new FileLoader( this, d->m_model, data, name, role );
107  d->appendLoader( loader );
108 }
109 
110 void FileManagerPrivate::appendLoader( FileLoader *loader )
111 {
112  QObject::connect( loader, SIGNAL(loaderFinished(FileLoader*)),
113  q, SLOT(cleanupLoader(FileLoader*)) );
114 
115  m_loaderList.append( loader );
116  loader->start();
117 }
118 
119 void FileManager::removeFile( const QString& key )
120 {
121  foreach ( FileLoader *loader, d->m_loaderList ) {
122  if ( loader->path() == key ) {
123  disconnect( loader, 0, this, 0 );
124  loader->wait();
125  d->m_loaderList.removeAll( loader );
126  delete loader->document();
127  return;
128  }
129  }
130 
131  if( d->m_fileItemHash.contains( key ) ) {
132  d->closeFile( key );
133  }
134 
135  mDebug() << "could not identify " << key;
136 }
137 
138 void FileManagerPrivate::closeFile( const QString& key )
139 {
140  mDebug() << "FileManager::closeFile " << key;
141  if( m_fileItemHash.contains( key ) ) {
142  GeoDataDocument *doc = m_fileItemHash.value( key );
143  m_model->treeModel()->removeDocument( doc );
144  emit q->fileRemoved( key );
145  delete doc;
146  m_fileItemHash.remove( key );
147  }
148 }
149 
150 void FileManager::saveFile( const QString &fileName, const GeoDataDocument *document )
151 {
152  GeoWriter writer;
153  writer.setDocumentType( kml::kmlTag_nameSpaceOgc22 );
154 
155  QFile file( fileName );
156  if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) {
157  return;
158  }
159 
160  writer.write( &file, document );
161  file.close();
162 }
163 
164 void FileManager::closeFile( const GeoDataDocument *document )
165 {
166  QHash < QString, GeoDataDocument* >::iterator itpoint = d->m_fileItemHash.begin();
167  QHash < QString, GeoDataDocument* >::iterator const endpoint = d->m_fileItemHash.end();
168  for (; itpoint != endpoint; ++itpoint ) {
169  if( d->m_fileItemHash.value( itpoint.key() ) == document ) {
170  d->closeFile( itpoint.key() );
171  return;
172  }
173  }
174 }
175 
176 int FileManager::size() const
177 {
178  return d->m_fileItemHash.size();
179 }
180 
181 GeoDataDocument * FileManager::at( const QString &key )
182 {
183  if ( d->m_fileItemHash.contains( key ) ) {
184  return d->m_fileItemHash.value( key );
185  }
186  return 0;
187 }
188 
189 int FileManager::pendingFiles() const
190 {
191  return d->m_loaderList.size();
192 }
193 
194 void FileManagerPrivate::cleanupLoader( FileLoader* loader )
195 {
196  GeoDataDocument *doc = loader->document();
197  m_loaderList.removeAll( loader );
198  if ( loader->isFinished() ) {
199  if ( doc ) {
200  if ( doc->name().isEmpty() && !doc->fileName().isEmpty() )
201  {
202  QFileInfo file( doc->fileName() );
203  doc->setName( file.baseName() );
204  }
205  m_model->treeModel()->addDocument( doc );
206  m_fileItemHash.insert( loader->path(), doc );
207  emit q->fileAdded( loader->path() );
208  if( loader->recenter() ) {
209  m_latLonBox |= doc->latLonAltBox();
210  }
211  }
212  if ( !loader->error().isEmpty() ) {
213  QMessageBox errorBox;
214  errorBox.setWindowTitle( QObject::tr("File Parsing Error"));
215  errorBox.setText( loader->error() );
216  errorBox.setIcon( QMessageBox::Warning );
217  errorBox.exec();
218  qWarning() << "File Parsing error " << loader->error();
219  }
220  delete loader;
221  }
222  if ( m_loaderList.isEmpty() )
223  {
224  mDebug() << "Finished loading all placemarks " << m_timer.elapsed();
225 
226  if ( !m_latLonBox.isEmpty() ) {
227  emit q->centeredDocument( m_latLonBox );
228  }
229  m_latLonBox.clear();
230  }
231 }
232 
233 #include "FileManager.moc"
GeoDataDocument.h
FileManager.h
Marble::FileLoader::path
QString path() const
Definition: FileLoader.cpp:123
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:65
Marble::FileManager::pendingFiles
int pendingFiles() const
Returns the number of files being opened at the moment.
Definition: FileManager.cpp:189
Marble::FileManager::~FileManager
~FileManager()
Destroys the file manager.
Definition: FileManager.cpp:74
Marble::FileLoader::document
GeoDataDocument * document()
Definition: FileLoader.cpp:128
MarbleModel.h
This file contains the headers for MarbleModel.
QList::at
const T & at(int i) const
GeoDataStyle.h
Marble::FileLoader
Definition: FileLoader.h:25
FileLoader.h
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QTime
MarbleDebug.h
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
Marble::FileLoader::error
QString error() const
Definition: FileLoader.cpp:133
QFile
QList::size
int size() const
GeoWriter.h
QThread::start
void start(Priority priority)
QHash::iterator
Marble::GeoDataStyle
an addressable style group
Definition: GeoDataStyle.h:55
QThread::isFinished
bool isFinished() const
Marble::FileManager::addData
void addData(const QString &name, const QString &data, DocumentRole role)
add Data containing KML code as string
Definition: FileManager.cpp:104
Marble::GeoDataFeature::setName
void setName(const QString &value)
Set a new name for this feature.
Definition: GeoDataFeature.cpp:549
QMessageBox
Marble::FileManager::closeFile
void closeFile(const GeoDataDocument *document)
Definition: FileManager.cpp:164
QHash
KmlElementDictionary.h
QMessageBox::setWindowTitle
void setWindowTitle(const QString &title)
QObject
Marble::FileManager
This class is responsible for loading the different files into Geodata model.
Definition: FileManager.h:36
QString::isEmpty
bool isEmpty() const
QMessageBox::setText
void setText(const QString &text)
Marble::FileManager::removeFile
void removeFile(const QString &fileName)
removes an existing file from the manager
Definition: FileManager.cpp:119
Marble::GeoWriter
Standard Marble way of writing XML This class is intended to be a standardised way of writing XML for...
Definition: GeoWriter.h:29
Marble::FileManager::size
int size() const
Definition: FileManager.cpp:176
QMessageBox::setIcon
void setIcon(Icon)
QString
QList
Marble::GeoDataContainer::latLonAltBox
GeoDataLatLonAltBox latLonAltBox() const
A convenience function that returns the LatLonAltBox of all placemarks in this container.
Definition: GeoDataContainer.cpp:160
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
GeoDataTreeModel.h
QStringList
QFileInfo
Marble::kml::kmlTag_nameSpaceOgc22
const char * kmlTag_nameSpaceOgc22
Definition: KmlElementDictionary.cpp:34
QMessageBox::exec
int exec()
Marble::FileLoader::recenter
bool recenter() const
Definition: FileLoader.cpp:214
Marble::GeoWriter::setDocumentType
void setDocumentType(const QString &documentType)
Set the current document type.
Definition: GeoWriter.cpp:79
Marble::FileManager::FileManager
FileManager(MarbleModel *model, QObject *parent=0)
Creates a new file manager.
Definition: FileManager.cpp:67
QFile::close
virtual void close()
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:97
QThread::wait
bool wait(unsigned long time)
Marble::GeoDataDocument::fileName
QString fileName() const
The filename of the document.
Definition: GeoDataDocument.cpp:101
Marble::FileManager::at
GeoDataDocument * at(const QString &key)
Definition: FileManager.cpp:181
GeoDataLatLonAltBox.h
Marble::GeoDataFeature::name
QString name() const
The name of the feature.
Definition: GeoDataFeature.cpp:544
Marble::GeoDataContainer::remove
void remove(int index)
Definition: GeoDataContainer.cpp:280
Marble::FileManager::saveFile
void saveFile(const QString &fileName, const GeoDataDocument *document)
Definition: FileManager.cpp:150
Marble::GeoWriter::write
bool write(QIODevice *device, const GeoNode *feature)
The main API call to use the XML writer.
Definition: GeoWriter.cpp:28
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
Marble::DocumentRole
DocumentRole
Definition: GeoDataDocument.h:39
Marble::GeoDataLatLonBox
A class that defines a 2D bounding box for geographic data.
Definition: GeoDataLatLonBox.h:51
Marble::FileManager::addFile
void addFile(const QString &fileName, const QString &property, const GeoDataStyle *style, DocumentRole role, bool recenter=false)
Loads a new file into the manager.
Definition: FileManager.cpp:79
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • 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