Marble

FileManager.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2006-2007 Torsten Rahn <tackat@kde.org>
4// SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
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
21using namespace Marble;
22
23namespace Marble
24{
25class FileManagerPrivate
26{
27public:
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;
53 QHash < QString, GeoDataDocument* > m_fileItemHash;
54 GeoDataLatLonBox m_latLonBox;
55 QElapsedTimer m_timer;
56};
57}
58
59FileManager::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
71void 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
89void 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
95void 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
123void 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
135void 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
147int FileManager::size() const
148{
149 return d->m_fileItemHash.size();
150}
151
152GeoDataDocument * 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
165void 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"
This file contains the headers for MarbleModel.
This class is responsible for loading the different files into Geodata model.
Definition FileManager.h:33
int pendingFiles() const
Returns the number of files being opened at the moment.
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.
FileManager(GeoDataTreeModel *treeModel, const PluginManager *pluginManager, QObject *parent=nullptr)
Creates a new file manager.
void removeFile(const QString &fileName)
removes an existing file from the manager
void addData(const QString &name, const QString &data, DocumentRole role)
add Data containing KML code as string
~FileManager() override
Destroys the file manager.
GeoDataLatLonAltBox latLonAltBox() const
A convenience function that returns the LatLonAltBox of all placemarks in this container.
A container for Features, Styles and in the future Schemas.
QString fileName() const
The filename of the document.
QString name() const
The name of the feature.
void setName(const QString &value)
Set a new name for this feature.
A class that defines a 2D bounding box for geographic data.
virtual bool isEmpty() const
Indicates whether the bounding box is not initialised (and contains nothing).
virtual void clear()
Resets the bounding box to its uninitialised state (and thus contains nothing).
The representation of GeoData in a model This class represents all available data given by kml-data f...
The class that handles Marble's plugins.
Binds a QML item to a specific geodetic location in screen coordinates.
qint64 elapsed() const const
void append(QList< T > &&value)
bool isEmpty() const const
qsizetype removeAll(const AT &t)
qsizetype size() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
bool isEmpty() const const
bool isFinished() const const
void start(Priority priority)
bool wait(QDeadlineTimer deadline)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.