journey.cpp 8.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
    Copyright (C) 2018 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "journey.h"
#include "datatypes_p.h"

21 22
#include <QVariant>

23 24 25 26 27 28 29
using namespace KPublicTransport;

namespace KPublicTransport {

class JourneySectionPrivate : public QSharedData
{
public:
30
    JourneySection::Mode mode = JourneySection::Invalid;
Volker Krause's avatar
Volker Krause committed
31 32 33 34
    QDateTime scheduledDepartureTime;
    QDateTime expectedDepartureTime;
    QDateTime scheduledArrivalTime;
    QDateTime expectedArrivalTime;
35 36 37
    Location from;
    Location to;
    Route route;
38 39 40 41
    QString scheduledDeparturePlatform;
    QString expectedDeparturePlatform;
    QString scheduledArrivalPlatform;
    QString expectedArrivalPlatform;
42 43 44 45 46 47 48 49 50 51 52 53
};

class JourneyPrivate : public QSharedData
{
public:
    std::vector<JourneySection> sections;
};

}

KPUBLICTRANSPORT_MAKE_GADGET(JourneySection)

54 55 56 57 58 59 60 61 62 63 64
JourneySection::Mode JourneySection::mode() const
{
    return d->mode;
}

void JourneySection::setMode(JourneySection::Mode mode)
{
    d.detach();
    d->mode = mode;
}

Volker Krause's avatar
Volker Krause committed
65
QDateTime JourneySection::scheduledDepartureTime() const
66
{
Volker Krause's avatar
Volker Krause committed
67
    return d->scheduledDepartureTime;
68 69
}

Volker Krause's avatar
Volker Krause committed
70
void JourneySection::setScheduledDepartureTime(const QDateTime &dt)
71 72
{
    d.detach();
Volker Krause's avatar
Volker Krause committed
73
    d->scheduledDepartureTime = dt;
74 75
}

Volker Krause's avatar
Volker Krause committed
76
QDateTime JourneySection::expectedDepartureTime() const
77
{
Volker Krause's avatar
Volker Krause committed
78
    return d->expectedDepartureTime;
79 80
}

Volker Krause's avatar
Volker Krause committed
81
void JourneySection::setExpectedDepartureTime(const QDateTime &dt)
82 83
{
    d.detach();
Volker Krause's avatar
Volker Krause committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    d->expectedDepartureTime = dt;
}

bool JourneySection::hasExpectedDepartureTime() const
{
    return d->expectedDepartureTime.isValid();
}

int JourneySection::departureDelay() const
{
    if (hasExpectedDepartureTime()) {
        return d->scheduledDepartureTime.secsTo(d->expectedDepartureTime) / 60;
    }
    return 0;
}

QDateTime JourneySection::scheduledArrivalTime() const
{
    return d->scheduledArrivalTime;
}

void JourneySection::setScheduledArrivalTime(const QDateTime &dt)
{
    d.detach();
    d->scheduledArrivalTime = dt;
}

QDateTime JourneySection::expectedArrivalTime() const
{
    return d->expectedArrivalTime;
}

void JourneySection::setExpectedArrivalTime(const QDateTime &dt)
{
    d.detach();
    d->expectedArrivalTime = dt;
}

bool JourneySection::hasExpectedArrivalTime() const
{
    return d->expectedArrivalTime.isValid();
}

int JourneySection::arrivalDelay() const
{
    if (hasExpectedArrivalTime()) {
        return d->scheduledArrivalTime.secsTo(d->expectedArrivalTime) / 60;
    }
    return 0;
133 134
}

135 136
int JourneySection::duration() const
{
Volker Krause's avatar
Volker Krause committed
137
    return d->scheduledDepartureTime.secsTo(d->scheduledArrivalTime);
138 139
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
Location JourneySection::from() const
{
    return d->from;
}

void JourneySection::setFrom(const Location &from)
{
    d.detach();
    d->from = from;
}

Location JourneySection::to() const
{
    return d->to;
}

void JourneySection::setTo(const Location &to)
{
    d.detach();
    d->to = to;
}

Route JourneySection::route() const
{
    return d->route;
}

void JourneySection::setRoute(const Route &route)
{
    d.detach();
    d->route = route;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
QString JourneySection::scheduledDeparturePlatform() const
{
    return d->scheduledDeparturePlatform;
}

void JourneySection::setScheduledDeparturePlatform(const QString &platform)
{
    d.detach();
    d->scheduledDeparturePlatform = platform;
}

QString JourneySection::expectedDeparturePlatform() const
{
    return d->expectedDeparturePlatform;
}

void JourneySection::setExpectedDeparturePlatform(const QString &platform)
{
    d.detach();
    d->expectedDeparturePlatform = platform;
}

bool JourneySection::hasExpectedDeparturePlatform() const
{
    return !d->expectedDeparturePlatform.isEmpty();
}

bool JourneySection::departurePlatformChanged() const
{
    return hasExpectedDeparturePlatform() && d->expectedDeparturePlatform != d->scheduledDeparturePlatform;
}

QString JourneySection::scheduledArrivalPlatform() const
{
    return d->scheduledArrivalPlatform;
}

void JourneySection::setScheduledArrivalPlatform(const QString &platform)
{
    d.detach();
    d->scheduledArrivalPlatform = platform;
}

QString JourneySection::expectedArrivalPlatform() const
{
    return d->expectedArrivalPlatform;
}

void JourneySection::setExpectedArrivalPlatform(const QString &platform)
{
    d.detach();
    d->expectedArrivalPlatform = platform;
}

bool JourneySection::hasExpectedArrivalPlatform() const
{
    return !d->expectedArrivalPlatform.isEmpty();
}

bool JourneySection::arrivalPlatformChanged() const
{
    return hasExpectedArrivalPlatform() && d->scheduledArrivalPlatform != d->expectedArrivalPlatform;
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
bool JourneySection::isSame(const JourneySection &lhs, const JourneySection &rhs)
{
    return lhs.d->mode == rhs.d->mode
        && lhs.d->scheduledDepartureTime == rhs.d->scheduledDepartureTime
        && lhs.d->scheduledArrivalTime == rhs.d->scheduledArrivalTime
        && Location::isSame(lhs.d->from, rhs.d->from)
        && Location::isSame(lhs.d->to, rhs.d->to)
        && Route::isSame(lhs.d->route, rhs.d->route);
        // ### platforms relevant?
}

JourneySection JourneySection::merge(const JourneySection &lhs, const JourneySection &rhs)
{
    auto res = lhs;
    if (!res.expectedDepartureTime().isValid()) {
        res.setExpectedDepartureTime(rhs.expectedDepartureTime());
    }
    if (res.expectedDeparturePlatform().isEmpty()) {
        res.setExpectedDeparturePlatform(rhs.expectedDeparturePlatform());
    }
    if (!res.expectedArrivalTime().isValid()) {
        res.setExpectedArrivalTime(rhs.expectedArrivalTime());
    }
    if (res.expectedArrivalPlatform().isEmpty()) {
        res.setExpectedArrivalPlatform(rhs.expectedArrivalPlatform());
    }
    res.setFrom(Location::merge(lhs.from(), rhs.from()));
    res.setTo(Location::merge(lhs.to(), rhs.to()));
    res.setRoute(Route::merge(lhs.route(), rhs.route()));

    return res;
}

270 271 272 273 274 275 276 277

KPUBLICTRANSPORT_MAKE_GADGET(Journey)

const std::vector<JourneySection>& Journey::sections() const
{
    return d->sections;
}

278 279 280 281 282
std::vector<JourneySection>&& Journey::takeSections()
{
    return std::move(d->sections);
}

283 284 285 286 287
void Journey::setSections(std::vector<JourneySection> &&sections)
{
    d.detach();
    d->sections = std::move(sections);
}
288

289 290 291 292 293 294 295 296
QVariantList Journey::sectionsVariant() const
{
    QVariantList l;
    l.reserve(d->sections.size());
    std::transform(d->sections.begin(), d->sections.end(), std::back_inserter(l), [](const auto &sec) { return QVariant::fromValue(sec); });
    return l;
}

Volker Krause's avatar
Volker Krause committed
297
QDateTime Journey::scheduledDepartureTime() const
298 299
{
    if (!d->sections.empty()) {
Volker Krause's avatar
Volker Krause committed
300
        return d->sections.front().scheduledDepartureTime();
301 302 303 304
    }
    return {};
}

Volker Krause's avatar
Volker Krause committed
305
QDateTime Journey::scheduledArrivalTime() const
306 307
{
    if (!d->sections.empty()) {
Volker Krause's avatar
Volker Krause committed
308
        return d->sections.back().scheduledArrivalTime();
309 310 311 312 313 314
    }
    return {};
}

int Journey::duration() const
{
Volker Krause's avatar
Volker Krause committed
315
    return scheduledDepartureTime().secsTo(scheduledArrivalTime());
316 317
}

Volker Krause's avatar
Volker Krause committed
318 319 320 321 322
int Journey::numberOfChanges() const
{
    return std::count_if(d->sections.begin(), d->sections.end(), [](const auto &section) { return section.mode() == JourneySection::PublicTransport; });
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
bool Journey::isSame(const Journey &lhs, const Journey &rhs)
{
    // ### we can make this more clever by ignoring transit elements for example
    // doing that will need changes below too!
    if (lhs.sections().size() != rhs.sections().size()) {
        return false;
    }
    return std::equal(lhs.sections().begin(), lhs.sections().end(), rhs.sections().begin(), [](const auto &lhs, const auto &rhs) {
        return JourneySection::isSame(lhs, rhs);
    });
}

Journey Journey::merge(const Journey &lhs, const Journey &rhs)
{
    // ### see above
    std::vector<JourneySection> sections;
    sections.reserve(lhs.sections().size());
    for (auto lit = lhs.sections().begin(), rit = rhs.sections().begin(); lit != lhs.sections().end(); ++lit, ++rit) {
        sections.push_back(JourneySection::merge(*lit, *rit));
    }

    Journey res;
    res.setSections(std::move(sections));
    return res;
}

349
#include "moc_journey.cpp"