Marble

GeoItem.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2019 Torsten Rahn <rahn@kde.org>
4//
5
6#include "GeoItem.h"
7#include "MarbleQuickItem.h"
8#include "Coordinate.h"
9
10#include "MarbleGlobal.h"
11
13using Marble::EARTH_RADIUS;
14using Marble::DEG2RAD;
15
16namespace Marble
17{
18 GeoItem::GeoItem(QQuickItem *parent ) :
19 QQuickItem( parent ),
20 m_map(nullptr),
21 m_observable(false),
22 m_visible(true)
23 {
24 connect(this, &QQuickItem::parentChanged, this, &GeoItem::setMapToParentOnInit);
25 connect(this, &QQuickItem::widthChanged, this, &GeoItem::updateScreenPosition);
26 connect(this, &QQuickItem::heightChanged, this, &GeoItem::updateScreenPosition);
27 }
28
29 bool GeoItem::moveToScreenCoordinates(qreal x, qreal y)
30 {
31 bool valid = m_map->screenCoordinatesToGeoDataCoordinates(QPoint(x,y), m_coordinate);
32 if (valid) {
33 updateScreenPosition();
34 emit longitudeChanged();
35 emit latitudeChanged();
36 }
37 return valid;
38 }
39
40 qreal GeoItem::longitude() const
41 {
42 return m_coordinate.longitude( GeoDataCoordinates::Degree );
43 }
44
45 void GeoItem::setLongitude( qreal lon )
46 {
47 if (m_coordinate.longitude(GeoDataCoordinates::Degree) != lon) {
48 m_coordinate.setLongitude( lon, GeoDataCoordinates::Degree );
49 updateScreenPosition();
50 emit longitudeChanged();
51 }
52 }
53
54 qreal GeoItem::latitude() const
55 {
56 return m_coordinate.latitude( GeoDataCoordinates::Degree );
57 }
58
59 void GeoItem::setLatitude( qreal lat )
60 {
61 if (m_coordinate.latitude(GeoDataCoordinates::Degree) != lat) {
62 m_coordinate.setLatitude( lat, GeoDataCoordinates::Degree );
63 updateScreenPosition();
64 emit latitudeChanged();
65 }
66 }
67
68 qreal GeoItem::altitude() const
69 {
70 return m_coordinate.altitude();
71 }
72
73 void GeoItem::setAltitude( qreal alt )
74 {
75 if (m_coordinate.altitude() != alt) {
76 m_coordinate.setAltitude( alt );
77 updateScreenPosition();
78 emit altitudeChanged();
79 }
80 }
81
82 GeoDataCoordinates GeoItem::coordinates() const
83 {
84 return m_coordinate;
85 }
86
87 void GeoItem::setCoordinates( const GeoDataCoordinates &coordinates )
88 {
89 if (m_coordinate != coordinates) {
90 m_coordinate = coordinates;
91 updateScreenPosition();
92 }
93 }
94
95 MarbleQuickItem *GeoItem::map() const
96 {
97 return m_map;
98 }
99
100 void GeoItem::setMap(MarbleQuickItem *map)
101 {
102 if (m_map == map)
103 return;
104
105 m_map = map;
106
107 connect(m_map, &MarbleQuickItem::geoItemUpdateRequested, this, &GeoItem::updateScreenPosition);
108 emit mapChanged(m_map);
109 }
110
111 void GeoItem::updateScreenPosition() {
112 if (m_map) {
113 QPointF relativePoint = m_map->screenCoordinatesFromGeoDataCoordinates(m_coordinate);
114 bool observable = !relativePoint.isNull();
115 if (observable != m_observable) {
116 m_observable = observable;
117 emit observableChanged(m_observable);
118 }
119 if (!m_coordinate.isValid()) {
120 setPosition(QPointF(-childrenRect().width(), -childrenRect().height()));
121 }
122 else if (observable) {
123 setPosition(QPointF(0.0,0.0));
124 QPointF screenPoint = mapFromItem(m_map, relativePoint);
125 screenPoint -= QPointF(width()/2.0, height()/2.0);
126 setPosition(screenPoint);
127 emit readonlyXChanged(readonlyX()) ;
128 emit readonlyYChanged(readonlyY()) ;
129 }
130 QQuickItem::setVisible(m_visible && m_observable);
131 }
132 }
133
134 void GeoItem::setMapToParentOnInit()
135 {
136 MarbleQuickItem * visualParent = qobject_cast<MarbleQuickItem*>(parentItem());
137 if (visualParent) {
138 disconnect(this, &QQuickItem::parentChanged, this, &GeoItem::setMapToParentOnInit);
139 setMap(visualParent);
140 }
141 }
142
143 bool GeoItem::observable() const
144 {
145 return m_observable;
146 }
147
148 bool GeoItem::visObservable() const
149 {
150 return m_visible;
151 }
152
153 void GeoItem::setVisObservable(bool visible)
154 {
155 if (m_visible == visible)
156 return;
157
158 m_visible = visible;
159 QQuickItem::setVisible(m_visible && m_observable);
160 emit visObservableChanged(m_visible);
161 }
162}
163
164#include "moc_GeoItem.cpp"
A 3d point representation.
Binds a QML item to a specific geodetic location in screen coordinates.
bool isNull() const const
void heightChanged()
void parentChanged(QQuickItem *)
void setVisible(bool)
void widthChanged()
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 Tue Mar 26 2024 11:18:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.