File indexing completed on 2025-01-19 03:39:51
0001 /* 0002 This file is part of the KContacts framework. 0003 SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "geo.h" 0009 0010 #include <QDataStream> 0011 #include <QSharedData> 0012 0013 using namespace KContacts; 0014 0015 class Q_DECL_HIDDEN Geo::Private : public QSharedData 0016 { 0017 public: 0018 Private() 0019 : mLatitude(91) 0020 , mLongitude(181) 0021 , mValidLatitude(false) 0022 , mValidLongitude(false) 0023 { 0024 } 0025 0026 Private(const Private &other) 0027 : QSharedData(other) 0028 { 0029 mLatitude = other.mLatitude; 0030 mLongitude = other.mLongitude; 0031 mValidLatitude = other.mValidLatitude; 0032 mValidLongitude = other.mValidLongitude; 0033 } 0034 0035 float mLatitude; 0036 float mLongitude; 0037 0038 bool mValidLatitude; 0039 bool mValidLongitude; 0040 }; 0041 0042 Geo::Geo() 0043 : d(new Private) 0044 { 0045 } 0046 0047 Geo::Geo(float latitude, float longitude) 0048 : d(new Private) 0049 { 0050 setLatitude(latitude); 0051 setLongitude(longitude); 0052 } 0053 0054 Geo::Geo(const Geo &other) 0055 : d(other.d) 0056 { 0057 } 0058 0059 Geo::~Geo() 0060 { 0061 } 0062 0063 void Geo::setLatitude(float latitude) 0064 { 0065 if (latitude >= -90 && latitude <= 90) { 0066 d->mLatitude = latitude; 0067 d->mValidLatitude = true; 0068 } else { 0069 d->mLatitude = 91; 0070 d->mValidLatitude = false; 0071 } 0072 } 0073 0074 float Geo::latitude() const 0075 { 0076 return d->mLatitude; 0077 } 0078 0079 void Geo::setLongitude(float longitude) 0080 { 0081 if (longitude >= -180 && longitude <= 180) { 0082 d->mLongitude = longitude; 0083 d->mValidLongitude = true; 0084 } else { 0085 d->mLongitude = 181; 0086 d->mValidLongitude = false; 0087 } 0088 } 0089 0090 float Geo::longitude() const 0091 { 0092 return d->mLongitude; 0093 } 0094 0095 bool Geo::isValid() const 0096 { 0097 return d->mValidLatitude && d->mValidLongitude; 0098 } 0099 0100 bool Geo::operator==(const Geo &other) const 0101 { 0102 if (!other.isValid() && !isValid()) { 0103 return true; 0104 } 0105 0106 if (!other.isValid() || !isValid()) { 0107 return false; 0108 } 0109 0110 if (other.d->mLatitude == d->mLatitude && other.d->mLongitude == d->mLongitude) { 0111 return true; 0112 } 0113 0114 return false; 0115 } 0116 0117 bool Geo::operator!=(const Geo &other) const 0118 { 0119 return !(*this == other); 0120 } 0121 0122 Geo &Geo::operator=(const Geo &other) 0123 { 0124 if (this != &other) { 0125 d = other.d; 0126 } 0127 0128 return *this; 0129 } 0130 0131 QString Geo::toString() const 0132 { 0133 QString str = QLatin1String("Geo {\n"); 0134 str += QStringLiteral(" Valid: %1\n").arg(isValid() ? QStringLiteral("true") : QStringLiteral("false")); 0135 str += QStringLiteral(" Latitude: %1\n").arg(d->mLatitude); 0136 str += QStringLiteral(" Longitude: %1\n").arg(d->mLongitude); 0137 str += QLatin1String("}\n"); 0138 0139 return str; 0140 } 0141 0142 void Geo::clear() 0143 { 0144 d->mValidLatitude = false; 0145 d->mValidLongitude = false; 0146 } 0147 0148 // clang-format off 0149 QDataStream &KContacts::operator<<(QDataStream &s, const Geo &geo) 0150 { 0151 return s << geo.d->mLatitude << geo.d->mValidLatitude 0152 << geo.d->mLongitude << geo.d->mValidLongitude; 0153 } 0154 0155 QDataStream &KContacts::operator>>(QDataStream &s, Geo &geo) 0156 { 0157 s >> geo.d->mLatitude >> geo.d->mValidLatitude 0158 >> geo.d->mLongitude >> geo.d->mValidLongitude; 0159 0160 return s; 0161 } 0162 // clang-format on 0163 0164 #include "moc_geo.cpp"