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
42Geo::Geo()
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
59Geo::~Geo() = default;
60
61void Geo::setLatitude(float latitude)
62{
63 if (latitude >= -90 && latitude <= 90) {
64 d->mLatitude = latitude;
65 d->mValidLatitude = true;
66 } else {
67 d->mLatitude = 91;
68 d->mValidLatitude = false;
69 }
70}
71
72float Geo::latitude() const
73{
74 return d->mLatitude;
75}
76
77void Geo::setLongitude(float longitude)
78{
79 if (longitude >= -180 && longitude <= 180) {
80 d->mLongitude = longitude;
81 d->mValidLongitude = true;
82 } else {
83 d->mLongitude = 181;
84 d->mValidLongitude = false;
85 }
86}
87
88float Geo::longitude() const
89{
90 return d->mLongitude;
91}
92
93bool Geo::isValid() const
94{
95 return d->mValidLatitude && d->mValidLongitude;
96}
97
98bool Geo::operator==(const Geo &other) const
99{
100 if (!other.isValid() && !isValid()) {
101 return true;
102 }
103
104 if (!other.isValid() || !isValid()) {
105 return false;
106 }
107
108 if (other.d->mLatitude == d->mLatitude && other.d->mLongitude == d->mLongitude) {
109 return true;
110 }
111
112 return false;
113}
114
115bool Geo::operator!=(const Geo &other) const
116{
117 return !(*this == other);
118}
119
120Geo &Geo::operator=(const Geo &other)
121{
122 if (this != &other) {
123 d = other.d;
124 }
125
126 return *this;
127}
128
129QString Geo::toString() const
130{
131 QString str = QLatin1String("Geo {\n");
132 str += QStringLiteral(" Valid: %1\n").arg(a: isValid() ? QStringLiteral("true") : QStringLiteral("false"));
133 str += QStringLiteral(" Latitude: %1\n").arg(a: d->mLatitude);
134 str += QStringLiteral(" Longitude: %1\n").arg(a: d->mLongitude);
135 str += QLatin1String("}\n");
136
137 return str;
138}
139
140void Geo::clear()
141{
142 d->mValidLatitude = false;
143 d->mValidLongitude = false;
144}
145
146// clang-format off
147QDataStream &KContacts::operator<<(QDataStream &s, const Geo &geo)
148{
149 return s << geo.d->mLatitude << geo.d->mValidLatitude
150 << geo.d->mLongitude << geo.d->mValidLongitude;
151}
152
153QDataStream &KContacts::operator>>(QDataStream &s, Geo &geo)
154{
155 s >> geo.d->mLatitude >> geo.d->mValidLatitude
156 >> geo.d->mLongitude >> geo.d->mValidLongitude;
157
158 return s;
159}
160// clang-format on
161
162#include "moc_geo.cpp"
163

source code of kcontacts/src/geo.cpp