00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "geo.h"
00022
00023 #include <QtCore/QDataStream>
00024 #include <QtCore/QSharedData>
00025
00026 using namespace KABC;
00027
00028 class Geo::Private : public QSharedData
00029 {
00030 public:
00031 Private()
00032 : mLatitude( 91 ), mLongitude( 181 ),
00033 mValidLatitude( false ), mValidLongitude( false )
00034 {
00035 }
00036
00037 Private( const Private &other )
00038 : QSharedData( other )
00039 {
00040 mLatitude = other.mLatitude;
00041 mLongitude = other.mLongitude;
00042 mValid = other.mValid;
00043 mValidLatitude = other.mValidLatitude;
00044 mValidLongitude = other.mValidLongitude;
00045 }
00046
00047 float mLatitude;
00048 float mLongitude;
00049
00050 bool mValid;
00051 bool mValidLatitude;
00052 bool mValidLongitude;
00053 };
00054
00055 Geo::Geo()
00056 : d( new Private )
00057 {
00058 }
00059
00060 Geo::Geo( float latitude, float longitude )
00061 : d( new Private )
00062 {
00063 setLatitude( latitude );
00064 setLongitude( longitude );
00065 }
00066
00067 Geo::Geo( const Geo &other )
00068 : d( other.d )
00069 {
00070 }
00071
00072 Geo::~Geo()
00073 {
00074 }
00075
00076 void Geo::setLatitude( float latitude )
00077 {
00078 if ( latitude >= -90 && latitude <= 90 ) {
00079 d->mLatitude = latitude;
00080 d->mValidLatitude = true;
00081 } else {
00082 d->mLatitude = 91;
00083 d->mValidLatitude = false;
00084 }
00085 }
00086
00087 float Geo::latitude() const
00088 {
00089 return d->mLatitude;
00090 }
00091
00092 void Geo::setLongitude( float longitude )
00093 {
00094 if ( longitude >= -180 && longitude <= 180 ) {
00095 d->mLongitude = longitude;
00096 d->mValidLongitude = true;
00097 } else {
00098 d->mLongitude = 181;
00099 d->mValidLongitude = false;
00100 }
00101 }
00102
00103 float Geo::longitude() const
00104 {
00105 return d->mLongitude;
00106 }
00107
00108 bool Geo::isValid() const
00109 {
00110 return d->mValidLatitude && d->mValidLongitude;
00111 }
00112
00113 bool Geo::operator==( const Geo &other ) const
00114 {
00115 if ( !other.isValid() && !isValid() ) {
00116 return true;
00117 }
00118
00119 if ( !other.isValid() || !isValid() ) {
00120 return false;
00121 }
00122
00123 if ( other.d->mLatitude == d->mLatitude && other.d->mLongitude == d->mLongitude ) {
00124 return true;
00125 }
00126
00127 return false;
00128 }
00129
00130 bool Geo::operator!=( const Geo &other ) const
00131 {
00132 return !( *this == other );
00133 }
00134
00135 Geo &Geo::operator=( const Geo &other )
00136 {
00137 if ( this != &other ) {
00138 d = other.d;
00139 }
00140
00141 return *this;
00142 }
00143
00144 QString Geo::toString() const
00145 {
00146 QString str;
00147
00148 str += QLatin1String( "Geo {\n" );
00149 str += QString::fromLatin1( " Valid: %1\n" ).
00150 arg( isValid() ? QLatin1String( "true" ) : QLatin1String( "false" ) );
00151 str += QString::fromLatin1( " Latitude: %1\n" ).arg( d->mLatitude );
00152 str += QString::fromLatin1( " Longitude: %1\n" ).arg( d->mLongitude );
00153 str += QLatin1String( "}\n" );
00154
00155 return str;
00156 }
00157
00158 QDataStream &KABC::operator<<( QDataStream &s, const Geo &geo )
00159 {
00160 return s << geo.d->mLatitude << geo.d->mValidLatitude
00161 << geo.d->mLongitude << geo.d->mValidLongitude;
00162 }
00163
00164 QDataStream &KABC::operator>>( QDataStream &s, Geo &geo )
00165 {
00166 s >> geo.d->mLatitude >> geo.d->mValidLatitude
00167 >> geo.d->mLongitude >> geo.d->mValidLongitude;
00168
00169 return s;
00170 }