KContacts

geo.cpp
1/*
2 This file is part of the KContacts framework.
3 SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "geo.h"
9
10#include <QDataStream>
11#include <QSharedData>
12
13using namespace KContacts;
14
15class Q_DECL_HIDDEN Geo::Private : public QSharedData
16{
17public:
18 Private()
19 : mLatitude(91)
20 , mLongitude(181)
21 , mValidLatitude(false)
22 , mValidLongitude(false)
23 {
24 }
25
26 Private(const Private &other)
27 : QSharedData(other)
28 {
29 mLatitude = other.mLatitude;
30 mLongitude = other.mLongitude;
31 mValidLatitude = other.mValidLatitude;
32 mValidLongitude = other.mValidLongitude;
33 }
34
35 float mLatitude;
36 float mLongitude;
37
38 bool mValidLatitude;
39 bool mValidLongitude;
40};
41
43 : d(new Private)
44{
45}
46
47Geo::Geo(float latitude, float longitude)
48 : d(new Private)
49{
50 setLatitude(latitude);
51 setLongitude(longitude);
52}
53
54Geo::Geo(const Geo &other)
55 : d(other.d)
56{
57}
58
60{
61}
62
63void Geo::setLatitude(float latitude)
64{
65 if (latitude >= -90 && latitude <= 90) {
66 d->mLatitude = latitude;
67 d->mValidLatitude = true;
68 } else {
69 d->mLatitude = 91;
70 d->mValidLatitude = false;
71 }
72}
73
74float Geo::latitude() const
75{
76 return d->mLatitude;
77}
78
79void Geo::setLongitude(float longitude)
80{
81 if (longitude >= -180 && longitude <= 180) {
82 d->mLongitude = longitude;
83 d->mValidLongitude = true;
84 } else {
85 d->mLongitude = 181;
86 d->mValidLongitude = false;
87 }
88}
89
90float Geo::longitude() const
91{
92 return d->mLongitude;
93}
94
95bool Geo::isValid() const
96{
97 return d->mValidLatitude && d->mValidLongitude;
98}
99
100bool Geo::operator==(const Geo &other) const
101{
102 if (!other.isValid() && !isValid()) {
103 return true;
104 }
105
106 if (!other.isValid() || !isValid()) {
107 return false;
108 }
109
110 if (other.d->mLatitude == d->mLatitude && other.d->mLongitude == d->mLongitude) {
111 return true;
112 }
113
114 return false;
115}
116
117bool Geo::operator!=(const Geo &other) const
118{
119 return !(*this == other);
120}
121
122Geo &Geo::operator=(const Geo &other)
123{
124 if (this != &other) {
125 d = other.d;
126 }
127
128 return *this;
129}
130
132{
133 QString str = QLatin1String("Geo {\n");
134 str += QStringLiteral(" Valid: %1\n").arg(isValid() ? QStringLiteral("true") : QStringLiteral("false"));
135 str += QStringLiteral(" Latitude: %1\n").arg(d->mLatitude);
136 str += QStringLiteral(" Longitude: %1\n").arg(d->mLongitude);
137 str += QLatin1String("}\n");
138
139 return str;
140}
141
143{
144 d->mValidLatitude = false;
145 d->mValidLongitude = false;
146}
147
148// clang-format off
149QDataStream &KContacts::operator<<(QDataStream &s, const Geo &geo)
150{
151 return s << geo.d->mLatitude << geo.d->mValidLatitude
152 << geo.d->mLongitude << geo.d->mValidLongitude;
153}
154
155QDataStream &KContacts::operator>>(QDataStream &s, Geo &geo)
156{
157 s >> geo.d->mLatitude >> geo.d->mValidLatitude
158 >> geo.d->mLongitude >> geo.d->mValidLongitude;
159
160 return s;
161}
162// clang-format on
163
164#include "moc_geo.cpp"
Geographic position.
Definition geo.h:25
~Geo()
Destroys the geographics position object.
Definition geo.cpp:59
void clear()
Clears the class, marking it as invalid.
Definition geo.cpp:142
bool operator!=(const Geo &other) const
Not-Equal operator.
Definition geo.cpp:117
bool operator==(const Geo &other) const
Equality operator.
Definition geo.cpp:100
Geo()
Creates an invalid geographics position object.
Definition geo.cpp:42
void setLatitude(float latitude)
Sets the latitude.
Definition geo.cpp:63
Geo & operator=(const Geo &other)
Assignment operator.
Definition geo.cpp:122
QString toString() const
Returns string representation of geographical position.
Definition geo.cpp:131
void setLongitude(float longitude)
Sets the longitude.
Definition geo.cpp:79
QString arg(Args &&... args) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 11:58:37 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.