Merge branch 'master' of https://vcs.maemo.org/git/situare
[situare] / src / routing / routesegment.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QDebug>
23 #include <QRegExp>
24
25 #include "routingcommon.h"
26
27 #include "routesegment.h"
28
29 RouteSegment::RouteSegment()
30     : m_timeSeconds(ROUTING_VALUE_UNDEFINED),
31       m_positionIndex(ROUTING_VALUE_UNDEFINED),
32       m_azimuth(ROUTING_VALUE_UNDEFINED),
33       m_length(ROUTING_VALUE_UNDEFINED),
34       m_turnAngle(ROUTING_VALUE_UNDEFINED)
35 {
36     qDebug() << __PRETTY_FUNCTION__;
37 }
38
39 qreal RouteSegment::azimuth() const
40 {
41     qDebug() << __PRETTY_FUNCTION__;
42
43     return m_azimuth;
44 }
45
46 const QString& RouteSegment::earthDirection() const
47 {
48     qDebug() << __PRETTY_FUNCTION__;
49
50     return m_earthDirection;
51 }
52
53 const QString& RouteSegment::instruction() const
54 {
55     qDebug() << __PRETTY_FUNCTION__;
56
57     return m_instruction;
58 }
59
60 qreal RouteSegment::length() const
61 {
62     qDebug() << __PRETTY_FUNCTION__;
63
64     return m_length;
65 }
66
67 const QString& RouteSegment::lengthCaption() const
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70
71     return m_lengthCaption;
72 }
73
74 int RouteSegment::positionIndex() const
75 {
76     qDebug() << __PRETTY_FUNCTION__;
77
78     return m_positionIndex;
79 }
80
81 void RouteSegment::setAzimuth(qreal azimuth)
82 {
83     qDebug() << __PRETTY_FUNCTION__;
84
85     m_azimuth = azimuth;
86 }
87
88 void RouteSegment::setEarthDirection(const QString &direction)
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     m_earthDirection = direction;
93 }
94
95 void RouteSegment::setInstruction(const QString &instruction)
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98
99     m_instruction = instruction;
100 }
101
102 void RouteSegment::setLength(qreal meters)
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     m_length = meters;
107 }
108
109 void RouteSegment::setLengthCaption(const QString &length)
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     m_lengthCaption = length;
114 }
115
116 void RouteSegment::setPositionIndex(int index)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     m_positionIndex = index;
121 }
122
123 void RouteSegment::setTime(int seconds)
124 {
125     qDebug() << __PRETTY_FUNCTION__;
126
127     m_timeSeconds = seconds;
128 }
129
130 void RouteSegment::setTurnAngle(qreal degrees)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     m_turnAngle = degrees;
135 }
136
137 void RouteSegment::setTurnType(const QString &type)
138 {
139     qDebug() << __PRETTY_FUNCTION__;
140
141     m_turnType = type;
142 }
143
144 QString RouteSegment::street() const
145 {
146     qDebug() << __PRETTY_FUNCTION__;
147
148     // turn type codes
149     const QString TURN_TYPE_CONTINUE = "C";
150     const QString TURN_TYPE_LEFT = "TL";
151     const QString TURN_TYPE_SLIGHT_LEFT = "TSLL";
152     const QString TURN_TYPE_SHARP_LEFT = "TSHL";
153     const QString TURN_TYPE_RIGHT = "TR";
154     const QString TURN_TYPE_SLIGHT_RIGHT = "TSLR";
155     const QString TURN_TYPE_SHARP_RIGHT = "TSHR";
156     const QString TURN_TYPE_U_TURN = "TU";
157     const QString TURN_TYPE_ROUNDABOUT = "EXIT";
158
159     // regular expressions for matching/replacing instructions
160     const QString REGEXP_1ST_SEGMENT
161         = "^Head (north|northeast|east|southeast|south|southwest|west|northwest)";
162     const QString REGEXP_CONTINUE = "^Continue";
163     const QString REGEXP_ROUNDABOUT
164         = "^At the roundabout, take the \\d(st|nd|rd|th) exit";
165     const QString REGEXP_TURNS = "^(Turn|Slight|Sharp) (left|right)";
166     const QString REGEXP_U_TURN = "^Make a U-turn";
167
168     QString result = m_instruction;
169
170     // cut beginning of the instruction
171     // (but only if it matches with the expected value)
172
173     if (m_turnType.isEmpty()) {
174         // first segment (without turn type code)
175         QRegExp regexp(REGEXP_1ST_SEGMENT);
176         result.replace(regexp, "");
177
178     } else if (m_turnType == TURN_TYPE_CONTINUE) {
179         // continue straight
180         QRegExp regexp(REGEXP_CONTINUE);
181         result.replace(regexp, "");
182
183     } else if ((m_turnType == TURN_TYPE_LEFT) || (m_turnType == TURN_TYPE_SLIGHT_LEFT)
184                || (m_turnType == TURN_TYPE_SHARP_LEFT) || (m_turnType == TURN_TYPE_RIGHT)
185                || (m_turnType == TURN_TYPE_SLIGHT_RIGHT) || (m_turnType == TURN_TYPE_SHARP_RIGHT)) {
186         // turns
187         QRegExp regexp(REGEXP_TURNS);
188         result.replace(regexp, "");
189
190     } else if (m_turnType == TURN_TYPE_U_TURN) {
191         // u-turn
192         QRegExp regexp(REGEXP_U_TURN);
193         result.replace(regexp, "");
194
195     } else if (m_turnType.startsWith(TURN_TYPE_ROUNDABOUT)) {
196         // roundabout
197         QRegExp regexp(REGEXP_ROUNDABOUT);
198         result.replace(regexp, "");
199
200     }
201     // else: do nothing
202
203     // replace on/at/onto words at the beginning of the result
204     // with zero or one leading space and one following space
205     QRegExp regexp("^ ?(on|at|onto) ");
206     result.replace(regexp, "");
207
208     return result;
209 }
210
211 int RouteSegment::time() const
212 {
213     qDebug() << __PRETTY_FUNCTION__;
214
215     return m_timeSeconds;
216 }
217
218 qreal RouteSegment::turnAngle() const
219 {
220     qDebug() << __PRETTY_FUNCTION__;
221
222     return m_turnAngle;
223 }
224
225 const QString& RouteSegment::turnType() const
226 {
227     qDebug() << __PRETTY_FUNCTION__;
228
229     return m_turnType;
230 }