| 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 | |
| 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 | |
| 42 | Geo::Geo() |
| 43 | : d(new Private) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | Geo::Geo(float latitude, float longitude) |
| 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 | |
| 59 | Geo::~Geo() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | void 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 | |
| 74 | float Geo::latitude() const |
| 75 | { |
| 76 | return d->mLatitude; |
| 77 | } |
| 78 | |
| 79 | void 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 | |
| 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 | |
| 131 | QString Geo::toString() const |
| 132 | { |
| 133 | QString str = QLatin1String("Geo {\n"); |
| 134 | str += QStringLiteral(" Valid: %1\n").arg(a: isValid() ? QStringLiteral( "true") : QStringLiteral( "false")); |
| 135 | str += QStringLiteral(" Latitude: %1\n").arg(a: d->mLatitude); |
| 136 | str += QStringLiteral(" Longitude: %1\n").arg(a: d->mLongitude); |
| 137 | str += QLatin1String("}\n"); |
| 138 | |
| 139 | return str; |
| 140 | } |
| 141 | |
| 142 | void Geo::clear() |
| 143 | { |
| 144 | d->mValidLatitude = false; |
| 145 | d->mValidLongitude = false; |
| 146 | } |
| 147 | |
| 148 | // clang-format off |
| 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 | } |
| 162 | // clang-format on |
| 163 | |
| 164 | #include "moc_geo.cpp" |
| 165 |
