KContacts

geo.cpp
1 /*
2  This file is part of the KContacts framework.
3  SPDX-FileCopyrightText: 2001 Cornelius Schumacher <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "geo.h"
9 
10 #include <QDataStream>
11 #include <QSharedData>
12 
13 using namespace KContacts;
14 
15 class Q_DECL_HIDDEN Geo::Private : public QSharedData
16 {
17 public:
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 
48  : d(new Private)
49 {
50  setLatitude(latitude);
51  setLongitude(longitude);
52 }
53 
54 Geo::Geo(const Geo &other)
55  : d(other.d)
56 {
57 }
58 
60 {
61 }
62 
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 
74 float Geo::latitude() const
75 {
76  return d->mLatitude;
77 }
78 
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 
90 float Geo::longitude() const
91 {
92  return d->mLongitude;
93 }
94 
95 bool Geo::isValid() const
96 {
97  return d->mValidLatitude && d->mValidLongitude;
98 }
99 
100 bool 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 
117 bool Geo::operator!=(const Geo &other) const
118 {
119  return !(*this == other);
120 }
121 
122 Geo &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").
135  arg(isValid() ? QStringLiteral("true") : QStringLiteral("false"));
136  str += QStringLiteral(" Latitude: %1\n").arg(d->mLatitude);
137  str += QStringLiteral(" Longitude: %1\n").arg(d->mLongitude);
138  str += QLatin1String("}\n");
139 
140  return str;
141 }
142 
144 {
145  d->mValidLatitude = false;
146  d->mValidLongitude = false;
147 }
148 
149 QDataStream &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 
155 QDataStream &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 }
float longitude() const
Returns the longitude.
Geo()
Creates an invalid geographics position object.
Definition: geo.cpp:42
Geographic position.
Definition: geo.h:23
bool operator!=(const Geo &other) const
Not-Equal operator.
Definition: geo.cpp:117
void clear()
Clears the class, marking it as invalid.
Definition: geo.cpp:143
void setLongitude(float longitude)
Sets the longitude.
Definition: geo.cpp:79
~Geo()
Destroys the geographics position object.
Definition: geo.cpp:59
bool operator==(const Geo &other) const
Equality operator.
Definition: geo.cpp:100
QString toString() const
Returns string representation of geographical position.
Definition: geo.cpp:131
void setLatitude(float latitude)
Sets the latitude.
Definition: geo.cpp:63
bool isValid() const
Returns, whether this object contains a valid geographical position.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Geo & operator=(const Geo &other)
Assignment operator.
Definition: geo.cpp:122
float latitude() const
Returns the latitude.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Aug 8 2020 22:53:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.