Marble

FileManager.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2006-2007 Torsten Rahn <[email protected]>
4 // SPDX-FileCopyrightText: 2007 Inge Wallin <[email protected]>
5 //
6 
7 #include "FileManager.h"
8 
9 #include <QFileInfo>
10 #include <QElapsedTimer>
11 
12 #include "FileLoader.h"
13 #include "MarbleDebug.h"
14 #include "MarbleModel.h"
15 #include "GeoDataTreeModel.h"
16 
17 #include "GeoDataLatLonAltBox.h"
18 #include "GeoDataStyle.h"
19 
20 
21 using namespace Marble;
22 
23 namespace Marble
24 {
25 class FileManagerPrivate
26 {
27 public:
28  FileManagerPrivate( GeoDataTreeModel *treeModel, const PluginManager *pluginManager, FileManager* parent ) :
29  q( parent ),
30  m_treeModel( treeModel ),
31  m_pluginManager( pluginManager )
32  {
33  }
34 
35  ~FileManagerPrivate()
36  {
37  for ( FileLoader *loader: m_loaderList ) {
38  if ( loader ) {
39  loader->wait();
40  }
41  }
42  }
43 
44  void appendLoader( FileLoader *loader );
45  void closeFile( const QString &key );
46  void cleanupLoader( FileLoader *loader );
47 
48  FileManager *const q;
49  GeoDataTreeModel *const m_treeModel;
50  const PluginManager *const m_pluginManager;
51 
52  QList<FileLoader*> m_loaderList;
54  GeoDataLatLonBox m_latLonBox;
55  QElapsedTimer m_timer;
56 };
57 }
58 
59 FileManager::FileManager( GeoDataTreeModel *treeModel, const PluginManager *pluginManager, QObject *parent )
60  : QObject( parent )
61  , d( new FileManagerPrivate( treeModel, pluginManager, this ) )
62 {
63 }
64 
65 
67 {
68  delete d;
69 }
70 
71 void FileManager::addFile( const QString& filepath, const QString& property, const GeoDataStyle::Ptr &style, DocumentRole role, int renderOrder, bool recenter )
72 {
73  if( d->m_fileItemHash.contains( filepath ) ) {
74  return; // already loaded
75  }
76 
77  for ( const FileLoader *loader: d->m_loaderList ) {
78  if ( loader->path() == filepath )
79  return; // currently loading
80  }
81 
82  mDebug() << "adding container:" << filepath;
83  mDebug() << "Starting placemark loading timer";
84  d->m_timer.start();
85  FileLoader* loader = new FileLoader( this, d->m_pluginManager, recenter, filepath, property, style, role, renderOrder );
86  d->appendLoader( loader );
87 }
88 
89 void FileManager::addData( const QString &name, const QString &data, DocumentRole role )
90 {
91  FileLoader* loader = new FileLoader( this, d->m_pluginManager, data, name, role );
92  d->appendLoader( loader );
93 }
94 
95 void FileManagerPrivate::appendLoader( FileLoader *loader )
96 {
97  QObject::connect( loader, SIGNAL(loaderFinished(FileLoader*)),
98  q, SLOT(cleanupLoader(FileLoader*)) );
99 
100  m_loaderList.append( loader );
101  loader->start();
102 }
103 
105 {
106  for ( FileLoader *loader: d->m_loaderList ) {
107  if ( loader->path() == key ) {
108  disconnect( loader, nullptr, this, nullptr );
109  loader->wait();
110  d->m_loaderList.removeAll( loader );
111  delete loader->document();
112  return;
113  }
114  }
115 
116  if( d->m_fileItemHash.contains( key ) ) {
117  d->closeFile( key );
118  }
119 
120  mDebug() << "could not identify " << key;
121 }
122 
123 void FileManagerPrivate::closeFile( const QString& key )
124 {
125  mDebug() << "FileManager::closeFile " << key;
126  if( m_fileItemHash.contains( key ) ) {
127  GeoDataDocument *doc = m_fileItemHash.value( key );
128  m_treeModel->removeDocument( doc );
129  emit q->fileRemoved( key );
130  delete doc;
131  m_fileItemHash.remove( key );
132  }
133 }
134 
135 void FileManager::closeFile( const GeoDataDocument *document )
136 {
137  QHash < QString, GeoDataDocument* >::iterator itpoint = d->m_fileItemHash.begin();
138  QHash < QString, GeoDataDocument* >::iterator const endpoint = d->m_fileItemHash.end();
139  for (; itpoint != endpoint; ++itpoint ) {
140  if( d->m_fileItemHash.value( itpoint.key() ) == document ) {
141  d->closeFile( itpoint.key() );
142  return;
143  }
144  }
145 }
146 
147 int FileManager::size() const
148 {
149  return d->m_fileItemHash.size();
150 }
151 
152 GeoDataDocument * FileManager::at( const QString &key )
153 {
154  if ( d->m_fileItemHash.contains( key ) ) {
155  return d->m_fileItemHash.value( key );
156  }
157  return nullptr;
158 }
159 
161 {
162  return d->m_loaderList.size();
163 }
164 
165 void FileManagerPrivate::cleanupLoader( FileLoader* loader )
166 {
167  GeoDataDocument *doc = loader->document();
168  m_loaderList.removeAll( loader );
169  if ( loader->isFinished() ) {
170  if ( doc ) {
171  if ( doc->name().isEmpty() && !doc->fileName().isEmpty() )
172  {
173  QFileInfo file( doc->fileName() );
174  doc->setName( file.baseName() );
175  }
176  m_treeModel->addDocument( doc );
177  m_fileItemHash.insert( loader->path(), doc );
178  emit q->fileAdded( loader->path() );
179  if( loader->recenter() ) {
180  m_latLonBox |= doc->latLonAltBox();
181  }
182  }
183  if ( !loader->error().isEmpty() ) {
184  qWarning() << "Failed to parse" << loader->path() << loader->error();
185  emit q->fileError(loader->path(), loader->error());
186  }
187  delete loader;
188  }
189  if ( m_loaderList.isEmpty() )
190  {
191  mDebug() << "Finished loading all placemarks " << m_timer.elapsed();
192 
193  if ( !m_latLonBox.isEmpty() ) {
194  emit q->centeredDocument( m_latLonBox );
195  }
196  m_latLonBox.clear();
197  }
198 }
199 
200 #include "moc_FileManager.cpp"
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void addData(const QString &name, const QString &data, DocumentRole role)
add Data containing KML code as string
Definition: FileManager.cpp:89
FileManager(GeoDataTreeModel *treeModel, const PluginManager *pluginManager, QObject *parent=nullptr)
Creates a new file manager.
Definition: FileManager.cpp:59
void setName(const QString &value)
Set a new name for this feature.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int pendingFiles() const
Returns the number of files being opened at the moment.
QString name() const
The name of the feature.
bool isEmpty() const const
A container for Features, Styles and in the future Schemas.
The representation of GeoData in a model This class represents all available data given by kml-data f...
A class that defines a 2D bounding box for geographic data.
~FileManager() override
Destroys the file manager.
Definition: FileManager.cpp:66
The class that handles Marble's plugins.
Definition: PluginManager.h:39
Binds a QML item to a specific geodetic location in screen coordinates.
This class is responsible for loading the different files into Geodata model.
Definition: FileManager.h:32
GeoDataLatLonAltBox latLonAltBox() const
A convenience function that returns the LatLonAltBox of all placemarks in this container.
void removeFile(const QString &fileName)
removes an existing file from the manager
void addFile(const QString &fileName, const QString &property, const GeoDataStyle::Ptr &style, DocumentRole role, int renderOrder=0, bool recenter=false)
Loads a new file into the manager.
Definition: FileManager.cpp:71
QString fileName() const
The filename of the document.
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
QVariant property(const char *name) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:12:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.