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

KDE's Doxygen guidelines are available online.