geo.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qdatastream.h>
00022 
00023 #include "geo.h"
00024 
00025 using namespace KABC;
00026 
00027 Geo::Geo()
00028   : mLatitude( 91 ), mLongitude( 181 ), mValidLat( false ), mValidLong( false )
00029 {
00030 }
00031 
00032 Geo::Geo( float latitude, float longitude )
00033 {
00034   setLatitude( latitude );
00035   setLongitude( longitude );
00036 }
00037 
00038 void Geo::setLatitude( float latitude )
00039 {
00040   if ( latitude >= -90 && latitude <= 90 ) {
00041     mLatitude = latitude;
00042     mValidLat = true;
00043   } else {
00044     mLatitude = 91;
00045     mValidLat = false;
00046   }
00047 }
00048 
00049 float Geo::latitude() const
00050 {
00051   return mLatitude;
00052 }
00053 
00054 void Geo::setLongitude( float longitude)
00055 {
00056   if ( longitude >= -180 && longitude <= 180 ) {
00057     mLongitude = longitude;
00058     mValidLong = true;
00059   } else {
00060     mLongitude = 181;
00061     mValidLong = false;
00062   }
00063 }
00064 
00065 float Geo::longitude() const
00066 {
00067   return mLongitude;
00068 }
00069 
00070 bool Geo::isValid() const
00071 {
00072   return mValidLat && mValidLong;
00073 }
00074 
00075 bool Geo::operator==( const Geo &g ) const
00076 {
00077   if ( !g.isValid() && !isValid() ) return true;
00078   if ( !g.isValid() || !isValid() ) return false;
00079   if ( g.mLatitude == mLatitude && g.mLongitude == mLongitude ) return true;
00080   return false;
00081 }
00082 
00083 bool Geo::operator!=( const Geo &g ) const
00084 {
00085   if ( !g.isValid() && !isValid() ) return false;
00086   if ( !g.isValid() || !isValid() ) return true;
00087   if ( g.mLatitude == mLatitude && g.mLongitude == mLongitude ) return false;
00088   return true;
00089 }
00090 
00091 QString Geo::asString() const
00092 {
00093   return "(" + QString::number(mLatitude) + "," + QString::number(mLongitude) + ")";
00094 }
00095 
00096 QDataStream &KABC::operator<<( QDataStream &s, const Geo &geo )
00097 {
00098     return s << (float)geo.mLatitude << (float)geo.mLongitude;
00099 }
00100 
00101 QDataStream &KABC::operator>>( QDataStream &s, Geo &geo )
00102 {
00103     s >> geo.mLatitude >> geo.mLongitude;
00104 
00105     geo.mValidLat = true;
00106     geo.mValidLong = true;
00107 
00108     return s;
00109 }
KDE Home | KDE Accessibility Home | Description of Access Keys