1 | /* |
2 | This file is part of libkabc. |
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "geo.h" |
22 | |
23 | #include <QtCore/QDataStream> |
24 | #include <QtCore/QSharedData> |
25 | |
26 | using namespace KABC; |
27 | |
28 | class Geo::Private : public QSharedData |
29 | { |
30 | public: |
31 | Private() |
32 | : mLatitude( 91 ), mLongitude( 181 ), |
33 | mValidLatitude( false ), mValidLongitude( false ) |
34 | { |
35 | } |
36 | |
37 | Private( const Private &other ) |
38 | : QSharedData( other ) |
39 | { |
40 | mLatitude = other.mLatitude; |
41 | mLongitude = other.mLongitude; |
42 | mValidLatitude = other.mValidLatitude; |
43 | mValidLongitude = other.mValidLongitude; |
44 | } |
45 | |
46 | float mLatitude; |
47 | float mLongitude; |
48 | |
49 | bool mValidLatitude; |
50 | bool mValidLongitude; |
51 | }; |
52 | |
53 | Geo::Geo() |
54 | : d( new Private ) |
55 | { |
56 | } |
57 | |
58 | Geo::Geo( float latitude, float longitude ) |
59 | : d( new Private ) |
60 | { |
61 | setLatitude( latitude ); |
62 | setLongitude( longitude ); |
63 | } |
64 | |
65 | Geo::Geo( const Geo &other ) |
66 | : d( other.d ) |
67 | { |
68 | } |
69 | |
70 | Geo::~Geo() |
71 | { |
72 | } |
73 | |
74 | void Geo::setLatitude( float latitude ) |
75 | { |
76 | if ( latitude >= -90 && latitude <= 90 ) { |
77 | d->mLatitude = latitude; |
78 | d->mValidLatitude = true; |
79 | } else { |
80 | d->mLatitude = 91; |
81 | d->mValidLatitude = false; |
82 | } |
83 | } |
84 | |
85 | float Geo::latitude() const |
86 | { |
87 | return d->mLatitude; |
88 | } |
89 | |
90 | void Geo::setLongitude( float longitude ) |
91 | { |
92 | if ( longitude >= -180 && longitude <= 180 ) { |
93 | d->mLongitude = longitude; |
94 | d->mValidLongitude = true; |
95 | } else { |
96 | d->mLongitude = 181; |
97 | d->mValidLongitude = false; |
98 | } |
99 | } |
100 | |
101 | float Geo::longitude() const |
102 | { |
103 | return d->mLongitude; |
104 | } |
105 | |
106 | bool Geo::isValid() const |
107 | { |
108 | return d->mValidLatitude && d->mValidLongitude; |
109 | } |
110 | |
111 | bool Geo::operator==( const Geo &other ) const |
112 | { |
113 | if ( !other.isValid() && !isValid() ) { |
114 | return true; |
115 | } |
116 | |
117 | if ( !other.isValid() || !isValid() ) { |
118 | return false; |
119 | } |
120 | |
121 | if ( other.d->mLatitude == d->mLatitude && other.d->mLongitude == d->mLongitude ) { |
122 | return true; |
123 | } |
124 | |
125 | return false; |
126 | } |
127 | |
128 | bool Geo::operator!=( const Geo &other ) const |
129 | { |
130 | return !( *this == other ); |
131 | } |
132 | |
133 | Geo &Geo::operator=( const Geo &other ) |
134 | { |
135 | if ( this != &other ) { |
136 | d = other.d; |
137 | } |
138 | |
139 | return *this; |
140 | } |
141 | |
142 | QString Geo::toString() const |
143 | { |
144 | QString str; |
145 | |
146 | str += QLatin1String( "Geo {\n" ); |
147 | str += QString::fromLatin1( " Valid: %1\n" ). |
148 | arg( isValid() ? QLatin1String( "true" ) : QLatin1String( "false" ) ); |
149 | str += QString::fromLatin1( " Latitude: %1\n" ).arg( d->mLatitude ); |
150 | str += QString::fromLatin1( " Longitude: %1\n" ).arg( d->mLongitude ); |
151 | str += QLatin1String( "}\n" ); |
152 | |
153 | return str; |
154 | } |
155 | |
156 | QDataStream &KABC::operator<<( QDataStream &s, const Geo &geo ) |
157 | { |
158 | return s << geo.d->mLatitude << geo.d->mValidLatitude |
159 | << geo.d->mLongitude << geo.d->mValidLongitude; |
160 | } |
161 | |
162 | QDataStream &KABC::operator>>( QDataStream &s, Geo &geo ) |
163 | { |
164 | s >> geo.d->mLatitude >> geo.d->mValidLatitude |
165 | >> geo.d->mLongitude >> geo.d->mValidLongitude; |
166 | |
167 | return s; |
168 | } |
169 | |