Marble

PositionSource.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2011 Dennis Nienh├╝ser <nienhueser@kde.org>
4// SPDX-FileCopyrightText: 2011 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
5//
6
7#include "PositionSource.h"
8
9#include "MarbleQuickItem.h"
10#include "MarbleModel.h"
11#include "PluginManager.h"
12#include "PositionTracking.h"
13#include "PositionProviderPlugin.h"
14
15namespace Marble {
16
17PositionSource::PositionSource( QObject* parent) : QObject( parent ),
18 m_active( false ),
19 m_hasPosition( false ),
20 m_position( 0 ),
21 m_marbleQuickItem( nullptr ),
22 m_speed( 0.0 )
23{
24 // nothing to do
25}
26
27bool PositionSource::active() const
28{
29 return m_active;
30}
31
32void PositionSource::setActive( bool active )
33{
34 if ( active != m_active ) {
35 if ( active ) {
36 start();
37 } else if ( m_marbleQuickItem ) {
38 PositionTracking *tracking = m_marbleQuickItem->model()->positionTracking();
39 tracking->setPositionProviderPlugin( nullptr );
40 }
41
42 if ( m_hasPosition ) {
43 m_hasPosition = false;
44 emit hasPositionChanged();
45 }
46
47 m_active = active;
48 emit activeChanged();
49 }
50}
51
52QString PositionSource::source() const
53{
54 return m_source;
55}
56
57void PositionSource::setSource( const QString &source )
58{
59 if ( source != m_source ) {
60 m_source = source;
61 if ( m_hasPosition ) {
62 m_hasPosition = false;
63 emit hasPositionChanged();
64 }
65
66 if ( active() ) {
67 start();
68 }
69 emit sourceChanged();
70 }
71}
72
73bool PositionSource::hasPosition() const
74{
75 return m_hasPosition;
76}
77
78Coordinate* PositionSource::position()
79{
80 return &m_position;
81}
82
83void PositionSource::start()
84{
85 if ( !m_marbleQuickItem ) {
86 return;
87 }
88
89 const PluginManager* pluginManager = m_marbleQuickItem->model()->pluginManager();
90 for( const Marble::PositionProviderPlugin *plugin: pluginManager->positionProviderPlugins() ) {
91 if ( m_source.isEmpty() || plugin->nameId() == m_source ) {
92 PositionProviderPlugin* instance = plugin->newInstance();
93 PositionTracking *tracking = m_marbleQuickItem->model()->positionTracking();
94 tracking->setPositionProviderPlugin( instance );
95 break;
96 }
97 }
98}
99
100MarbleQuickItem *PositionSource::map()
101{
102 return m_marbleQuickItem;
103}
104
105void PositionSource::setMap( MarbleQuickItem *map )
106{
107 if ( map != m_marbleQuickItem ) {
108 m_marbleQuickItem = map;
109
110 if ( m_marbleQuickItem ) {
111 connect( m_marbleQuickItem->model()->positionTracking(), SIGNAL(gpsLocation(GeoDataCoordinates,qreal)),
112 this, SLOT(updatePosition()) );
113 connect( m_marbleQuickItem->model()->positionTracking(), SIGNAL(statusChanged(PositionProviderStatus)),
114 this, SLOT(updatePosition()) );
115
116 emit mapChanged();
117 }
118
119 if ( active() ) {
120 start();
121 }
122 }
123}
124
125qreal PositionSource::speed() const
126{
127 return m_speed;
128}
129
130void PositionSource::updatePosition()
131{
132 if ( m_marbleQuickItem ) {
133 bool const hasPosition = m_marbleQuickItem->model()->positionTracking()->status() == Marble::PositionProviderStatusAvailable;
134
135 if ( hasPosition ) {
136 Marble::GeoDataCoordinates position = m_marbleQuickItem->model()->positionTracking()->currentLocation();
137 m_position.setLongitude( position.longitude( Marble::GeoDataCoordinates::Degree ) );
138 m_position.setLatitude( position.latitude( Marble::GeoDataCoordinates::Degree ) );
139 m_position.setAltitude( position.altitude() );
140 }
141
142 m_speed = m_marbleQuickItem->model()->positionTracking()->speed() * Marble::METER2KM / Marble::SEC2HOUR;
143 emit speedChanged();
144
145 if ( hasPosition != m_hasPosition ) {
146 m_hasPosition = hasPosition;
147 emit hasPositionChanged();
148 }
149
150 if ( hasPosition ) {
151 emit positionChanged();
152 }
153 }
154}
155
156}
157
158#include "moc_PositionSource.cpp"
This file contains the headers for MarbleModel.
Represents a coordinate with the properties of a name and coordinates.
Definition Coordinate.h:19
A 3d point representation.
qreal altitude() const
return the altitude of the Point in meters
qreal longitude(GeoDataCoordinates::Unit unit) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
void setLongitude(qreal lon, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian)
set the longitude in a GeoDataCoordinates object
qreal latitude(GeoDataCoordinates::Unit unit) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
The abstract class that provides position information.
Q_SCRIPTABLE Q_NOREPLY void start()
Binds a QML item to a specific geodetic location in screen coordinates.
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
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.