| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtLocation module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #include "qgeoroutesegment.h" |
| 38 | #include "qgeoroutesegment_p.h" |
| 39 | |
| 40 | #include "qgeocoordinate.h" |
| 41 | #include <QDateTime> |
| 42 | |
| 43 | QT_BEGIN_NAMESPACE |
| 44 | |
| 45 | template<> |
| 46 | QGeoRouteSegmentPrivate *QExplicitlySharedDataPointer<QGeoRouteSegmentPrivate>::clone() |
| 47 | { |
| 48 | return d->clone(); |
| 49 | } |
| 50 | |
| 51 | /*! |
| 52 | \class QGeoRouteSegment |
| 53 | \inmodule QtLocation |
| 54 | \ingroup QtLocation-routing |
| 55 | \since 5.6 |
| 56 | |
| 57 | \brief The QGeoRouteSegment class represents a segment of a route. |
| 58 | |
| 59 | A QGeoRouteSegment instance has information about the physical layout |
| 60 | of the route segment, the length of the route and estimated time required |
| 61 | to traverse the route segment and an optional QGeoManeuver associated with |
| 62 | the beginning of the route segment. |
| 63 | |
| 64 | QGeoRouteSegment instances can be thought of as edges on a routing |
| 65 | graph, with QGeoManeuver instances as optional labels attached to the |
| 66 | vertices of the graph. |
| 67 | */ |
| 68 | |
| 69 | /*! |
| 70 | Constructs an invalid route segment object. |
| 71 | |
| 72 | The route segment will remain invalid until one of setNextRouteSegment(), |
| 73 | setTravelTime(), setDistance(), setPath() or setManeuver() is called. |
| 74 | */ |
| 75 | QGeoRouteSegment::QGeoRouteSegment() |
| 76 | : d_ptr(new QGeoRouteSegmentPrivateDefault()) {} |
| 77 | |
| 78 | /*! |
| 79 | Constructs a route segment object from the contents of \a other. |
| 80 | */ |
| 81 | QGeoRouteSegment::QGeoRouteSegment(const QGeoRouteSegment &other) |
| 82 | : d_ptr(other.d_ptr) {} |
| 83 | |
| 84 | /*! |
| 85 | \internal |
| 86 | */ |
| 87 | QGeoRouteSegment::QGeoRouteSegment(const QExplicitlySharedDataPointer<QGeoRouteSegmentPrivate> &dd) |
| 88 | : d_ptr(dd) {} |
| 89 | |
| 90 | /*! |
| 91 | Returns the private implementation. |
| 92 | */ |
| 93 | QExplicitlySharedDataPointer<QGeoRouteSegmentPrivate> &QGeoRouteSegment::d() |
| 94 | { |
| 95 | return d_ptr; |
| 96 | } |
| 97 | |
| 98 | /*! |
| 99 | Destroys this route segment object. |
| 100 | */ |
| 101 | QGeoRouteSegment::~QGeoRouteSegment() {} |
| 102 | |
| 103 | |
| 104 | /*! |
| 105 | Assigns \a other to this route segment object and then returns a |
| 106 | reference to this route segment object. |
| 107 | */ |
| 108 | QGeoRouteSegment &QGeoRouteSegment::operator= (const QGeoRouteSegment & other) |
| 109 | { |
| 110 | if (this == &other) |
| 111 | return *this; |
| 112 | |
| 113 | d_ptr = other.d_ptr; |
| 114 | return *this; |
| 115 | } |
| 116 | |
| 117 | /*! |
| 118 | Returns whether this route segment and \a other are equal. |
| 119 | |
| 120 | The value of nextRouteSegment() is not considered in the comparison. |
| 121 | */ |
| 122 | bool QGeoRouteSegment::operator ==(const QGeoRouteSegment &other) const |
| 123 | { |
| 124 | return ( (d_ptr.constData() == other.d_ptr.constData()) |
| 125 | || (*d_ptr) == (*other.d_ptr)); |
| 126 | } |
| 127 | |
| 128 | /*! |
| 129 | Returns whether this route segment and \a other are not equal. |
| 130 | |
| 131 | The value of nextRouteSegment() is not considered in the comparison. |
| 132 | */ |
| 133 | bool QGeoRouteSegment::operator !=(const QGeoRouteSegment &other) const |
| 134 | { |
| 135 | return !(operator==(other)); |
| 136 | } |
| 137 | |
| 138 | /*! |
| 139 | Returns whether this route segment is valid or not. |
| 140 | |
| 141 | If nextRouteSegment() is called on the last route segment of a route, the |
| 142 | returned value will be an invalid route segment. |
| 143 | */ |
| 144 | bool QGeoRouteSegment::isValid() const |
| 145 | { |
| 146 | return d_ptr->valid(); |
| 147 | } |
| 148 | |
| 149 | /*! |
| 150 | Returns whether this route segment is the last segment of a route leg. |
| 151 | |
| 152 | \since 5.12 |
| 153 | */ |
| 154 | bool QGeoRouteSegment::isLegLastSegment() const |
| 155 | { |
| 156 | if (!d_ptr->valid()) |
| 157 | return false; |
| 158 | |
| 159 | if (!d_ptr->nextRouteSegment()) |
| 160 | return true; |
| 161 | return d_ptr->isLegLastSegment(); |
| 162 | } |
| 163 | |
| 164 | /*! |
| 165 | Sets the next route segment in the route to \a routeSegment. |
| 166 | */ |
| 167 | void QGeoRouteSegment::setNextRouteSegment(const QGeoRouteSegment &routeSegment) |
| 168 | { |
| 169 | d_ptr->setValid(true); |
| 170 | d_ptr->setNextRouteSegment(routeSegment.d_ptr); |
| 171 | } |
| 172 | |
| 173 | /*! |
| 174 | Returns the next route segment in the route. |
| 175 | |
| 176 | Will return an invalid route segment if this is the last route |
| 177 | segment in the route. |
| 178 | */ |
| 179 | QGeoRouteSegment QGeoRouteSegment::nextRouteSegment() const |
| 180 | { |
| 181 | if (d_ptr->valid() && d_ptr->nextRouteSegment()) |
| 182 | return QGeoRouteSegment(d_ptr->nextRouteSegment()); |
| 183 | |
| 184 | return QGeoRouteSegment(); |
| 185 | } |
| 186 | |
| 187 | /*! |
| 188 | Sets the estimated amount of time it will take to traverse this segment of |
| 189 | the route, in seconds, to \a secs. |
| 190 | */ |
| 191 | void QGeoRouteSegment::setTravelTime(int secs) |
| 192 | { |
| 193 | d_ptr->setValid(true); |
| 194 | d_ptr->setTravelTime(secs); |
| 195 | } |
| 196 | |
| 197 | /*! |
| 198 | Returns the estimated amount of time it will take to traverse this segment |
| 199 | of the route, in seconds. |
| 200 | */ |
| 201 | int QGeoRouteSegment::travelTime() const |
| 202 | { |
| 203 | return d_ptr->travelTime(); |
| 204 | } |
| 205 | |
| 206 | /*! |
| 207 | Sets the distance covered by this segment of the route, in meters, to \a distance. |
| 208 | */ |
| 209 | void QGeoRouteSegment::setDistance(qreal distance) |
| 210 | { |
| 211 | d_ptr->setValid(true); |
| 212 | d_ptr->setDistance(distance); |
| 213 | } |
| 214 | |
| 215 | /*! |
| 216 | Returns the distance covered by this segment of the route, in meters. |
| 217 | */ |
| 218 | qreal QGeoRouteSegment::distance() const |
| 219 | { |
| 220 | return d_ptr->distance(); |
| 221 | } |
| 222 | |
| 223 | /*! |
| 224 | Sets the geometric shape of this segment of the route to \a path. |
| 225 | |
| 226 | The coordinates in \a path should be listed in the order in which they |
| 227 | would be traversed by someone traveling along this segment of the route. |
| 228 | */ |
| 229 | void QGeoRouteSegment::setPath(const QList<QGeoCoordinate> &path) |
| 230 | { |
| 231 | d_ptr->setValid(true); |
| 232 | d_ptr->setPath(path); |
| 233 | } |
| 234 | |
| 235 | /*! |
| 236 | Returns the geometric shape of this route segment of the route. |
| 237 | |
| 238 | The coordinates should be listed in the order in which they |
| 239 | would be traversed by someone traveling along this segment of the route. |
| 240 | */ |
| 241 | |
| 242 | QList<QGeoCoordinate> QGeoRouteSegment::path() const |
| 243 | { |
| 244 | return d_ptr->path(); |
| 245 | } |
| 246 | |
| 247 | /*! |
| 248 | Sets the maneuver for this route segment to \a maneuver. |
| 249 | */ |
| 250 | void QGeoRouteSegment::setManeuver(const QGeoManeuver &maneuver) |
| 251 | { |
| 252 | d_ptr->setValid(true); |
| 253 | d_ptr->setManeuver(maneuver); |
| 254 | } |
| 255 | |
| 256 | /*! |
| 257 | Returns the maneuver for this route segment. |
| 258 | |
| 259 | Will return an invalid QGeoManeuver if no information has been attached |
| 260 | to the starting point of this route segment. |
| 261 | */ |
| 262 | QGeoManeuver QGeoRouteSegment::maneuver() const |
| 263 | { |
| 264 | return d_ptr->maneuver(); |
| 265 | } |
| 266 | |
| 267 | /******************************************************************************* |
| 268 | *******************************************************************************/ |
| 269 | |
| 270 | QGeoRouteSegmentPrivate::QGeoRouteSegmentPrivate() {} |
| 271 | |
| 272 | QGeoRouteSegmentPrivate::QGeoRouteSegmentPrivate(const QGeoRouteSegmentPrivate &other) |
| 273 | : QSharedData(other), m_nextSegment(other.m_nextSegment) {} |
| 274 | |
| 275 | QGeoRouteSegmentPrivate::~QGeoRouteSegmentPrivate() |
| 276 | { |
| 277 | m_nextSegment.reset(); |
| 278 | } |
| 279 | |
| 280 | bool QGeoRouteSegmentPrivate::operator ==(const QGeoRouteSegmentPrivate &other) const |
| 281 | { |
| 282 | return equals(other); |
| 283 | } |
| 284 | |
| 285 | bool QGeoRouteSegmentPrivate::equals(const QGeoRouteSegmentPrivate &other) const |
| 286 | { |
| 287 | return ((valid() == other.valid()) |
| 288 | && (travelTime() == other.travelTime()) |
| 289 | && (distance() == other.distance()) |
| 290 | && (path() == other.path()) |
| 291 | && (maneuver() == other.maneuver())); |
| 292 | } |
| 293 | |
| 294 | bool QGeoRouteSegmentPrivate::valid() const |
| 295 | { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | void QGeoRouteSegmentPrivate::setValid(bool valid) |
| 300 | { |
| 301 | Q_UNUSED(valid); |
| 302 | } |
| 303 | |
| 304 | bool QGeoRouteSegmentPrivate::isLegLastSegment() const |
| 305 | { |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | void QGeoRouteSegmentPrivate::setLegLastSegment(bool lastSegment) |
| 310 | { |
| 311 | Q_UNUSED(lastSegment); |
| 312 | } |
| 313 | |
| 314 | int QGeoRouteSegmentPrivate::travelTime() const |
| 315 | { |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | void QGeoRouteSegmentPrivate::setTravelTime(int travelTime) |
| 320 | { |
| 321 | Q_UNUSED(travelTime); |
| 322 | } |
| 323 | |
| 324 | qreal QGeoRouteSegmentPrivate::distance() const |
| 325 | { |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | void QGeoRouteSegmentPrivate::setDistance(qreal distance) |
| 330 | { |
| 331 | Q_UNUSED(distance); |
| 332 | } |
| 333 | |
| 334 | QList<QGeoCoordinate> QGeoRouteSegmentPrivate::path() const |
| 335 | { |
| 336 | return QList<QGeoCoordinate>(); |
| 337 | } |
| 338 | |
| 339 | void QGeoRouteSegmentPrivate::setPath(const QList<QGeoCoordinate> &path) |
| 340 | { |
| 341 | Q_UNUSED(path); |
| 342 | } |
| 343 | |
| 344 | QGeoManeuver QGeoRouteSegmentPrivate::maneuver() const |
| 345 | { |
| 346 | return QGeoManeuver(); |
| 347 | } |
| 348 | |
| 349 | void QGeoRouteSegmentPrivate::setManeuver(const QGeoManeuver &maneuver) |
| 350 | { |
| 351 | Q_UNUSED(maneuver); |
| 352 | } |
| 353 | |
| 354 | QExplicitlySharedDataPointer<QGeoRouteSegmentPrivate> QGeoRouteSegmentPrivate::nextRouteSegment() const |
| 355 | { |
| 356 | return m_nextSegment; |
| 357 | } |
| 358 | |
| 359 | void QGeoRouteSegmentPrivate::setNextRouteSegment(const QExplicitlySharedDataPointer<QGeoRouteSegmentPrivate> &next) |
| 360 | { |
| 361 | m_nextSegment = next; |
| 362 | } |
| 363 | |
| 364 | QGeoRouteSegmentPrivate *QGeoRouteSegmentPrivate::get(QGeoRouteSegment &segment) |
| 365 | { |
| 366 | return segment.d_ptr.data(); |
| 367 | } |
| 368 | |
| 369 | /******************************************************************************* |
| 370 | *******************************************************************************/ |
| 371 | |
| 372 | QGeoRouteSegmentPrivateDefault::QGeoRouteSegmentPrivateDefault() |
| 373 | : m_valid(false), |
| 374 | m_travelTime(0), |
| 375 | m_distance(0.0) |
| 376 | { |
| 377 | |
| 378 | } |
| 379 | |
| 380 | QGeoRouteSegmentPrivateDefault::QGeoRouteSegmentPrivateDefault(const QGeoRouteSegmentPrivateDefault &other) |
| 381 | : QGeoRouteSegmentPrivate(other), |
| 382 | m_valid(other.m_valid), |
| 383 | m_travelTime(other.m_travelTime), |
| 384 | m_distance(other.m_distance), |
| 385 | m_path(other.m_path), |
| 386 | m_maneuver(other.m_maneuver) |
| 387 | { |
| 388 | |
| 389 | } |
| 390 | |
| 391 | QGeoRouteSegmentPrivateDefault::~QGeoRouteSegmentPrivateDefault() |
| 392 | { |
| 393 | |
| 394 | } |
| 395 | |
| 396 | QGeoRouteSegmentPrivate *QGeoRouteSegmentPrivateDefault::clone() |
| 397 | { |
| 398 | return new QGeoRouteSegmentPrivateDefault(*this); |
| 399 | } |
| 400 | |
| 401 | bool QGeoRouteSegmentPrivateDefault::operator ==(const QGeoRouteSegmentPrivateDefault &other) const |
| 402 | { |
| 403 | return QGeoRouteSegmentPrivate::operator ==(other); |
| 404 | } |
| 405 | |
| 406 | bool QGeoRouteSegmentPrivateDefault::valid() const |
| 407 | { |
| 408 | return m_valid; |
| 409 | } |
| 410 | |
| 411 | void QGeoRouteSegmentPrivateDefault::setValid(bool valid) |
| 412 | { |
| 413 | m_valid = valid; |
| 414 | } |
| 415 | |
| 416 | bool QGeoRouteSegmentPrivateDefault::isLegLastSegment() const |
| 417 | { |
| 418 | return m_legLastSegment; |
| 419 | } |
| 420 | |
| 421 | void QGeoRouteSegmentPrivateDefault::setLegLastSegment(bool lastSegment) |
| 422 | { |
| 423 | m_legLastSegment = lastSegment; |
| 424 | } |
| 425 | |
| 426 | int QGeoRouteSegmentPrivateDefault::travelTime() const |
| 427 | { |
| 428 | return m_travelTime; |
| 429 | } |
| 430 | |
| 431 | void QGeoRouteSegmentPrivateDefault::setTravelTime(int travelTime) |
| 432 | { |
| 433 | m_travelTime = travelTime; |
| 434 | } |
| 435 | |
| 436 | qreal QGeoRouteSegmentPrivateDefault::distance() const |
| 437 | { |
| 438 | return m_distance; |
| 439 | } |
| 440 | |
| 441 | void QGeoRouteSegmentPrivateDefault::setDistance(qreal distance) |
| 442 | { |
| 443 | m_distance = distance; |
| 444 | } |
| 445 | |
| 446 | QList<QGeoCoordinate> QGeoRouteSegmentPrivateDefault::path() const |
| 447 | { |
| 448 | return m_path; |
| 449 | } |
| 450 | |
| 451 | void QGeoRouteSegmentPrivateDefault::setPath(const QList<QGeoCoordinate> &path) |
| 452 | { |
| 453 | m_path = path; |
| 454 | } |
| 455 | |
| 456 | QGeoManeuver QGeoRouteSegmentPrivateDefault::maneuver() const |
| 457 | { |
| 458 | return m_maneuver; |
| 459 | } |
| 460 | |
| 461 | void QGeoRouteSegmentPrivateDefault::setManeuver(const QGeoManeuver &maneuver) |
| 462 | { |
| 463 | m_maneuver = maneuver; |
| 464 | } |
| 465 | |
| 466 | /******************************************************************************* |
| 467 | *******************************************************************************/ |
| 468 | |
| 469 | QT_END_NAMESPACE |
| 470 | |