KPublicTransport

vehiclelayoutreply.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "vehiclelayoutreply.h"
8#include "reply_p.h"
9#include "vehiclelayoutrequest.h"
10#include "logging.h"
11#include "backends/abstractbackend.h"
12#include "backends/cache.h"
13
14#include <KPublicTransport/Platform>
15#include <KPublicTransport/Stopover>
16#include <KPublicTransport/Vehicle>
17
18#include <QDebug>
19
20using namespace KPublicTransport;
21
22namespace KPublicTransport {
23class VehicleLayoutReplyPrivate: public ReplyPrivate {
24public:
25 void finalizeResult() override {}
26
28 Stopover stopover;
29};
30}
31
32VehicleLayoutReply::VehicleLayoutReply(const VehicleLayoutRequest &req, QObject *parent)
33 : Reply(new VehicleLayoutReplyPrivate, parent)
34{
36 d->request = req;
37 d->stopover = req.stopover();
38}
39
40VehicleLayoutReply::~VehicleLayoutReply() = default;
41
43{
45 return d->request;
46}
47
49{
51 return d->stopover;
52}
53
54static bool isOneSidedCar(VehicleSection::Type type)
55{
56 return type == VehicleSection::PowerCar || type == VehicleSection::ControlCar;
57}
58
59void VehicleLayoutReply::addResult(const Stopover &stopover)
60{
62 d->stopover = Stopover::merge(d->stopover, stopover);
63
64 if (!d->stopover.vehicleLayout().sections().empty()) {
65 // normalize section order
66 auto vehicle = d->stopover.vehicleLayout();
67 auto sections = vehicle.takeSections();
68 std::sort(sections.begin(), sections.end(), [](const auto &lhs, const auto &rhs) {
69 return lhs.platformPositionBegin() < rhs.platformPositionBegin();
70 });
71
72 // we have no connections at the ends
73 sections.front().setConnectedSides(sections.front().connectedSides() & ~VehicleSection::Front);
74 sections.back().setConnectedSides(sections.back().connectedSides() & ~VehicleSection::Back);
75
76 // if the leading car in driving direction is a PassengerCar, turn it into a ControlCar
77 if (vehicle.direction() == Vehicle::Forward && sections.front().type() == VehicleSection::PassengerCar) {
78 sections.front().setType(VehicleSection::ControlCar);
79 } else if (vehicle.direction() == Vehicle::Backward && sections.back().type() == VehicleSection::PassengerCar) {
80 sections.back().setType(VehicleSection::ControlCar);
81 }
82
83 for (auto it = sections.begin(); it != sections.end(); ++it) {
84 // engines and power cars have no connections either
85 if ((*it).type() == VehicleSection::Engine) {
86 (*it).setConnectedSides(VehicleSection::NoSide);
87 }
88
89 if (it == sections.begin()) {
90 continue;
91 }
92
93 // connect control cars in the middle of the train to the correct side
94 // only do that when two cars back isn't a control car either, ie. the preceeding car
95 // actually has a connection to the front. Otherwise trains consisting of e.g. 4 consecutive
96 // control cars get layouted wrongly.
97 if (isOneSidedCar((*(it - 1)).type()) && isOneSidedCar((*it).type()) && ((*(it - 1)).connectedSides() & VehicleSection::Front)) {
98 (*it).setConnectedSides((*it).connectedSides() & ~VehicleSection::Front);
99 }
100
101 // make sure connections are symmetric
102 if (((*(it - 1)).connectedSides() & VehicleSection::Back) == 0) {
103 (*it).setConnectedSides((*it).connectedSides() & ~VehicleSection::Front);
104 }
105 if (((*it).connectedSides() & VehicleSection::Front) == 0) {
106 (*(it - 1)).setConnectedSides((*(it - 1)).connectedSides() & ~VehicleSection::Back);
107 }
108 }
109
110 vehicle.setSections(std::move(sections));
111 d->stopover.setVehicleLayout(std::move(vehicle));
112 }
113
114 d->pendingOps--;
115 d->emitFinishedIfDone(this);
116}
117
118void VehicleLayoutReply::addError(const AbstractBackend *backend, Reply::Error error, const QString &errorMsg)
119{
121 // TODO add negative cache entry
122 } else {
123 qCDebug(Log) << backend->backendId() << error << errorMsg;
124 }
125 Reply::addError(error, errorMsg);
126}
Query response base class.
Definition reply.h:25
Error error() const
Error code.
Definition reply.cpp:46
Error
Error types.
Definition reply.h:31
@ NotFoundError
The requested journey/departure/place could not be found.
Definition reply.h:34
Information about an arrival and/or departure of a vehicle at a stop area.
Definition stopover.h:26
static Stopover merge(const Stopover &lhs, const Stopover &rhs)
Merge two departure instances.
Definition stopover.cpp:205
Reply to a vehicle layout query.
VehicleLayoutRequest request() const
The request this is the reply for.
Stopover stopover() const
The requested Stopover information, including the vehicle and platform layout.
Describes a query for vehicle layout information.
KPublicTransport::Stopover stopover
The stopover vehicle and platform layout information are requested for.
Query operations and data types for accessing realtime public transport information from online servi...
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.