• 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
ReverseGeocodingRunnerManager.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 2008 Henry de Valence <hdevalence@gmail.com>
9 // Copyright 2010 Dennis Nienhüser <earthwings@gentoo.org>
10 // Copyright 2010-2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
11 // Copyright 2011 Thibaut Gridel <tgridel@free.fr>
12 
13 #include "ReverseGeocodingRunnerManager.h"
14 
15 #include "MarbleDebug.h"
16 #include "MarbleModel.h"
17 #include "GeoDataCoordinates.h"
18 #include "GeoDataPlacemark.h"
19 #include "Planet.h"
20 #include "PluginManager.h"
21 #include "ReverseGeocodingRunnerPlugin.h"
22 #include "RunnerTask.h"
23 
24 #include <QList>
25 #include <QThreadPool>
26 #include <QTimer>
27 
28 namespace Marble
29 {
30 
31 class MarbleModel;
32 
33 class ReverseGeocodingRunnerManager::Private
34 {
35 public:
36  Private( ReverseGeocodingRunnerManager *parent, const MarbleModel *marbleModel );
37 
38  QList<const ReverseGeocodingRunnerPlugin *> plugins( const QList<const ReverseGeocodingRunnerPlugin *> &plugins ) const;
39 
40  void addReverseGeocodingResult( const GeoDataCoordinates &coordinates, const GeoDataPlacemark &placemark );
41  void cleanupReverseGeocodingTask( ReverseGeocodingTask *task );
42 
43  ReverseGeocodingRunnerManager *const q;
44  const MarbleModel *const m_marbleModel;
45  const PluginManager* m_pluginManager;
46  QList<ReverseGeocodingTask*> m_reverseTasks;
47  QList<GeoDataCoordinates> m_reverseGeocodingResults;
48  QString m_reverseGeocodingResult;
49 };
50 
51 ReverseGeocodingRunnerManager::Private::Private( ReverseGeocodingRunnerManager *parent, const MarbleModel *marbleModel ) :
52  q( parent ),
53  m_marbleModel( marbleModel ),
54  m_pluginManager( marbleModel->pluginManager() )
55 {
56  qRegisterMetaType<GeoDataPlacemark>( "GeoDataPlacemark" );
57  qRegisterMetaType<GeoDataCoordinates>( "GeoDataCoordinates" );
58 }
59 
60 QList<const ReverseGeocodingRunnerPlugin *> ReverseGeocodingRunnerManager::Private::plugins( const QList<const ReverseGeocodingRunnerPlugin *> &plugins ) const
61 {
62  QList<const ReverseGeocodingRunnerPlugin *> result;
63 
64  foreach( const ReverseGeocodingRunnerPlugin *plugin, plugins ) {
65  if ( ( m_marbleModel && m_marbleModel->workOffline() && !plugin->canWorkOffline() ) ) {
66  continue;
67  }
68 
69  if ( !plugin->canWork() ) {
70  continue;
71  }
72 
73  if ( m_marbleModel && !plugin->supportsCelestialBody( m_marbleModel->planet()->id() ) )
74  {
75  continue;
76  }
77 
78  result << plugin;
79  }
80 
81  return result;
82 }
83 
84 void ReverseGeocodingRunnerManager::Private::addReverseGeocodingResult( const GeoDataCoordinates &coordinates, const GeoDataPlacemark &placemark )
85 {
86  if ( !m_reverseGeocodingResults.contains( coordinates ) && !placemark.address().isEmpty() ) {
87  m_reverseGeocodingResults.push_back( coordinates );
88  m_reverseGeocodingResult = placemark.address();
89  emit q->reverseGeocodingFinished( coordinates, placemark );
90  }
91 
92  if ( m_reverseTasks.isEmpty() ) {
93  emit q->reverseGeocodingFinished();
94  }
95 }
96 
97 void ReverseGeocodingRunnerManager::Private::cleanupReverseGeocodingTask( ReverseGeocodingTask *task )
98 {
99  m_reverseTasks.removeAll( task );
100  mDebug() << "removing task " << m_reverseTasks.size() << " " << (quintptr)task;
101  if ( m_reverseTasks.isEmpty() ) {
102  emit q->reverseGeocodingFinished();
103  }
104 }
105 
106 ReverseGeocodingRunnerManager::ReverseGeocodingRunnerManager( const MarbleModel *marbleModel, QObject *parent ) :
107  QObject( parent ),
108  d( new Private( this, marbleModel ) )
109 {
110  if ( QThreadPool::globalInstance()->maxThreadCount() < 4 ) {
111  QThreadPool::globalInstance()->setMaxThreadCount( 4 );
112  }
113 }
114 
115 ReverseGeocodingRunnerManager::~ReverseGeocodingRunnerManager()
116 {
117  delete d;
118 }
119 
120 void ReverseGeocodingRunnerManager::reverseGeocoding( const GeoDataCoordinates &coordinates )
121 {
122  d->m_reverseTasks.clear();
123  d->m_reverseGeocodingResult.clear();
124  d->m_reverseGeocodingResults.removeAll( coordinates );
125  QList<const ReverseGeocodingRunnerPlugin*> plugins = d->plugins( d->m_pluginManager->reverseGeocodingRunnerPlugins() );
126  foreach( const ReverseGeocodingRunnerPlugin* plugin, plugins ) {
127  ReverseGeocodingTask* task = new ReverseGeocodingTask( plugin->newRunner(), this, d->m_marbleModel, coordinates );
128  connect( task, SIGNAL(finished(ReverseGeocodingTask*)), this, SLOT(cleanupReverseGeocodingTask(ReverseGeocodingTask*)) );
129  mDebug() << "reverse task " << plugin->nameId() << " " << (quintptr)task;
130  d->m_reverseTasks << task;
131  }
132 
133  foreach( ReverseGeocodingTask* task, d->m_reverseTasks ) {
134  QThreadPool::globalInstance()->start( task );
135  }
136 
137  if ( plugins.isEmpty() ) {
138  GeoDataPlacemark anonymous;
139  anonymous.setCoordinate( coordinates );
140  emit reverseGeocodingFinished( coordinates, anonymous );
141  d->cleanupReverseGeocodingTask( 0 );
142  }
143 }
144 
145 QString ReverseGeocodingRunnerManager::searchReverseGeocoding( const GeoDataCoordinates &coordinates, int timeout ) {
146  QEventLoop localEventLoop;
147  QTimer watchdog;
148  watchdog.setSingleShot(true);
149  connect( &watchdog, SIGNAL(timeout()),
150  &localEventLoop, SLOT(quit()));
151  connect(this, SIGNAL(reverseGeocodingFinished()),
152  &localEventLoop, SLOT(quit()), Qt::QueuedConnection );
153 
154  watchdog.start( timeout );
155  reverseGeocoding( coordinates );
156  localEventLoop.exec();
157  return d->m_reverseGeocodingResult;
158 }
159 
160 }
161 
162 #include "ReverseGeocodingRunnerManager.moc"
Marble::ReverseGeocodingRunnerManager::reverseGeocodingFinished
void reverseGeocodingFinished()
Emitted whenever all runners are finished for the query.
GeoDataCoordinates.h
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
QEventLoop
QList::push_back
void push_back(const T &value)
RunnerTask.h
MarbleModel.h
This file contains the headers for MarbleModel.
Marble::ReverseGeocodingTask
A RunnerTask that executes reverse geocoding.
Definition: RunnerTask.h:58
QThreadPool::globalInstance
QThreadPool * globalInstance()
Marble::ReverseGeocodingRunnerManager::Private
friend class Private
Definition: ReverseGeocodingRunnerManager.h:73
Marble::ReverseGeocodingRunnerPlugin::newRunner
virtual ReverseGeocodingRunner * newRunner() const =0
Plugin factory method to create a new runner instance.
Marble::GeoDataPlacemark::setCoordinate
void setCoordinate(qreal longitude, qreal latitude, qreal altitude=0, GeoDataCoordinates::Unit _unit=GeoDataCoordinates::Radian)
Set the coordinate of the placemark in longitude and latitude.
Definition: GeoDataPlacemark.cpp:215
Planet.h
MarbleDebug.h
Marble::ReverseGeocodingRunnerManager::ReverseGeocodingRunnerManager
ReverseGeocodingRunnerManager(const MarbleModel *marbleModel, QObject *parent=0)
Constructor.
Definition: ReverseGeocodingRunnerManager.cpp:106
QTimer
QThreadPool::setMaxThreadCount
void setMaxThreadCount(int maxThreadCount)
QEventLoop::exec
int exec(QFlags< QEventLoop::ProcessEventsFlag > flags)
Marble::ReverseGeocodingRunnerPlugin
A plugin for Marble to execute a reverse geocoding task.
Definition: ReverseGeocodingRunnerPlugin.h:27
QObject
QList::isEmpty
bool isEmpty() const
ReverseGeocodingRunnerManager.h
QString
QList
GeoDataPlacemark.h
Marble::PluginInterface::nameId
virtual QString nameId() const =0
Returns the unique name of the plugin.
Marble::ReverseGeocodingRunnerManager::searchReverseGeocoding
QString searchReverseGeocoding(const GeoDataCoordinates &coordinates, int timeout=30000)
Definition: ReverseGeocodingRunnerManager.cpp:145
Marble::ReverseGeocodingRunnerManager::~ReverseGeocodingRunnerManager
~ReverseGeocodingRunnerManager()
Definition: ReverseGeocodingRunnerManager.cpp:115
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:97
PluginManager.h
Marble::ReverseGeocodingRunnerManager::reverseGeocoding
void reverseGeocoding(const GeoDataCoordinates &coordinates)
Find the address and other meta information for a given geoposition.
Definition: ReverseGeocodingRunnerManager.cpp:120
QTimer::start
void start(int msec)
QThreadPool::start
void start(QRunnable *runnable, int priority)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
ReverseGeocodingRunnerPlugin.h
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:54
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
QTimer::setSingleShot
void setSingleShot(bool singleShot)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:41 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