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 "MarbleModel.h"
10#include "MarbleQuickItem.h"
11#include "PluginManager.h"
12#include "PositionProviderPlugin.h"
13#include "PositionTracking.h"
14
15namespace Marble
16{
17
18PositionSource::PositionSource(QObject *parent)
19 : QObject(parent)
20 , m_active(false)
21 , m_hasPosition(false)
22 , m_position(0)
23 , m_marbleQuickItem(nullptr)
24 , m_speed(0.0)
25{
26 // nothing to do
27}
28
29PositionSource::~PositionSource() = default;
30
31bool PositionSource::active() const
32{
33 return m_active;
34}
35
36void PositionSource::setActive(bool active)
37{
38 if (active != m_active) {
39 if (active) {
40 start();
41 } else if (m_marbleQuickItem) {
42 PositionTracking *tracking = m_marbleQuickItem->model()->positionTracking();
43 tracking->setPositionProviderPlugin(nullptr);
44 }
45
46 if (m_hasPosition) {
47 m_hasPosition = false;
48 Q_EMIT hasPositionChanged();
49 }
50
51 m_active = active;
52 Q_EMIT activeChanged();
53 }
54}
55
56QString PositionSource::source() const
57{
58 return m_source;
59}
60
61void PositionSource::setSource(const QString &source)
62{
63 if (source != m_source) {
64 m_source = source;
65 if (m_hasPosition) {
66 m_hasPosition = false;
67 Q_EMIT hasPositionChanged();
68 }
69
70 if (active()) {
71 start();
72 }
73 Q_EMIT sourceChanged();
74 }
75}
76
77bool PositionSource::hasPosition() const
78{
79 return m_hasPosition;
80}
81
82Coordinate *PositionSource::position()
83{
84 return &m_position;
85}
86
87void PositionSource::start()
88{
89 if (!m_marbleQuickItem) {
90 return;
91 }
92
93 const PluginManager *pluginManager = m_marbleQuickItem->model()->pluginManager();
94 for (const Marble::PositionProviderPlugin *plugin : pluginManager->positionProviderPlugins()) {
95 if (m_source.isEmpty() || plugin->nameId() == m_source) {
96 PositionProviderPlugin *instance = plugin->newInstance();
97 PositionTracking *tracking = m_marbleQuickItem->model()->positionTracking();
98 tracking->setPositionProviderPlugin(instance);
99 break;
100 }
101 }
102}
103
104MarbleQuickItem *PositionSource::map()
105{
106 return m_marbleQuickItem;
107}
108
109void PositionSource::setMap(MarbleQuickItem *map)
110{
111 if (map != m_marbleQuickItem) {
112 m_marbleQuickItem = map;
113
114 if (m_marbleQuickItem) {
115 connect(m_marbleQuickItem->model()->positionTracking(), SIGNAL(gpsLocation(GeoDataCoordinates, qreal)), this, SLOT(updatePosition()));
116 connect(m_marbleQuickItem->model()->positionTracking(), SIGNAL(statusChanged(PositionProviderStatus)), this, SLOT(updatePosition()));
117
118 Q_EMIT mapChanged();
119 }
120
121 if (active()) {
122 start();
123 }
124 }
125}
126
127qreal PositionSource::speed() const
128{
129 return m_speed;
130}
131
132void PositionSource::updatePosition()
133{
134 if (m_marbleQuickItem) {
135 bool const hasPosition = m_marbleQuickItem->model()->positionTracking()->status() == Marble::PositionProviderStatusAvailable;
136
137 if (hasPosition) {
138 Marble::GeoDataCoordinates position = m_marbleQuickItem->model()->positionTracking()->currentLocation();
139 m_position.setLongitude(position.longitude(Marble::GeoDataCoordinates::Degree));
140 m_position.setLatitude(position.latitude(Marble::GeoDataCoordinates::Degree));
141 m_position.setAltitude(position.altitude());
142 }
143
144 m_speed = m_marbleQuickItem->model()->positionTracking()->speed() * Marble::METER2KM / Marble::SEC2HOUR;
145 Q_EMIT speedChanged();
146
147 if (hasPosition != m_hasPosition) {
148 m_hasPosition = hasPosition;
149 Q_EMIT hasPositionChanged();
150 }
151
152 if (hasPosition) {
153 Q_EMIT positionChanged();
154 }
155 }
156}
157
158}
159
160#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
void setLongitude(qreal lon, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian)
set the longitude in a GeoDataCoordinates object
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 Mon Nov 18 2024 12:15:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.