Commit f15dee44 authored by Volker Krause's avatar Volker Krause
Browse files

Add path model

On top of the list of path sections this computes relative turn directions
for adjacent sections.
parent 1c8b0256
Pipeline #74494 passed with stage
in 18 seconds
......@@ -97,6 +97,7 @@ target_sources(KPublicTransport PRIVATE
models/backendmodel.cpp
models/journeyquerymodel.cpp
models/locationquerymodel.cpp
models/pathmodel.cpp
models/stopoverquerymodel.cpp
models/vehiclelayoutquerymodel.cpp
......@@ -178,6 +179,7 @@ ecm_generate_headers(KPublicTransport_Models_FORWARDING_HEADERS
DepartureQueryModel
JourneyQueryModel
LocationQueryModel
PathModel
StopoverQueryModel
VehicleLayoutQueryModel
PREFIX KPublicTransport
......
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "pathmodel.h"
using namespace KPublicTransport;
PathModel::PathModel(QObject *parent)
: QAbstractListModel(parent)
{
}
PathModel::~PathModel() = default;
Path PathModel::path() const
{
return m_path;
}
void PathModel::setPath(const Path &path)
{
beginResetModel();
m_path = path;
endResetModel();
}
int PathModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return m_path.sections().size();
}
QVariant PathModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case PathSectionRole:
return m_path.sections()[index.row()];
case TurnDirectionRole:
{
const auto curDir = m_path.sections()[index.row()].direction();
if (index.row() == 0) {
return curDir;
}
const auto prevDir = m_path.sections()[index.row() - 1].direction();
if (prevDir >= 0 && curDir >= 0) {
return (360 + curDir - prevDir) % 360;
}
return curDir;
}
}
return {};
}
QHash<int, QByteArray> PathModel::roleNames() const
{
auto r = QAbstractListModel::roleNames();
r.insert(PathSectionRole, "section");
r.insert(TurnDirectionRole, "turnDirection");
return r;
}
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KPUBLICTRANSPORT_PATHMODEL_H
#define KPUBLICTRANSPORT_PATHMODEL_H
#include "kpublictransport_export.h"
#include <KPublicTransport/Path>
#include <QAbstractListModel>
namespace KPublicTransport {
/** Model representing a KPublicTransport::Path.
* Each row is a KPublicTransport::PathSection, the model computes
* additional information such as turn directions for each section.
*/
class KPUBLICTRANSPORT_EXPORT PathModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(KPublicTransport::Path path READ path WRITE setPath)
public:
explicit PathModel(QObject *parent = nullptr);
~PathModel();
enum Role {
PathSectionRole = Qt::UserRole,
TurnDirectionRole,
};
Path path() const;
void setPath(const Path &path);
int rowCount(const QModelIndex &parent = {}) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
private:
Path m_path;
};
}
#endif // KPUBLICTRANSPORT_PATHMODEL_H
......@@ -19,6 +19,8 @@
#include <KPublicTransport/LocationQueryModel>
#include <KPublicTransport/LocationRequest>
#include <KPublicTransport/Manager>
#include <KPublicTransport/Path>
#include <KPublicTransport/PathModel>
#include <KPublicTransport/Platform>
#include <KPublicTransport/RentalVehicle>
#include <KPublicTransport/StopoverRequest>
......@@ -47,6 +49,7 @@ void KPublicTransportQmlPlugin::registerTypes(const char*)
qmlRegisterUncreatableType<KPublicTransport::JourneySection>("org.kde.kpublictransport", 1, 0, "JourneySection", {});
qmlRegisterUncreatableType<KPublicTransport::Vehicle>("org.kde.kpublictransport", 1, 0, "Vehicle", {});
qmlRegisterUncreatableType<KPublicTransport::VehicleSection>("org.kde.kpublictransport", 1, 0, "VehicleSection", {});
qmlRegisterUncreatableType<KPublicTransport::PathSection>("org.kde.kpublictransport", 1, 0, "PathSection", {});
qmlRegisterUncreatableType<KPublicTransport::Platform>("org.kde.kpublictransport", 1, 0, "Platform", {});
qmlRegisterUncreatableType<KPublicTransport::RentalVehicle>("org.kde.kpublictransport", 1, 0, "RentalVehicle", {});
qmlRegisterUncreatableType<KPublicTransport::Equipment>("org.kde.kpublictransport", 1, 0, "Equipment", {});
......@@ -64,6 +67,7 @@ void KPublicTransportQmlPlugin::registerTypes(const char*)
qmlRegisterType<KPublicTransport::BackendModel>("org.kde.kpublictransport", 1, 0, "BackendModel");
qmlRegisterType<KPublicTransport::StopoverQueryModel>("org.kde.kpublictransport", 1, 0, "StopoverQueryModel");
qmlRegisterType<KPublicTransport::VehicleLayoutQueryModel>("org.kde.kpublictransport", 1, 0, "VehicleLayoutQueryModel");
qmlRegisterType<KPublicTransport::PathModel>("org.kde.kpublictransport", 1, 0, "PathModel");
// backward compat
qmlRegisterType<KPublicTransport::StopoverQueryModel>("org.kde.kpublictransport", 1, 0, "DepartureQueryModel");
......
......@@ -11,12 +11,13 @@ import org.kde.kirigami 2.12 as Kirigami
import org.kde.kpublictransport 1.0
Kirigami.ScrollablePage {
property var path
property alias path: pathModel.path
Component {
id: pathDelegate
Kirigami.AbstractListItem {
highlighted: false
property var section: model.section
GridLayout {
rows: 2
columns: 4
......@@ -26,7 +27,7 @@ Kirigami.ScrollablePage {
Layout.column: 0
Layout.rowSpan: 2
text: {
switch (modelData.maneuver) {
switch (section.maneuver) {
case PathSection.Stairs:
return "🪜";
case PathSection.Elevator:
......@@ -44,8 +45,8 @@ Kirigami.ScrollablePage {
Layout.row: 0
Layout.column: 1
Layout.rowSpan: 2
visible: modelData.floorLevelChange != 0
text: modelData.floorLevelChange < 0 ? "⬇️" : "⬆️"
visible: section.floorLevelChange != 0
text: section.floorLevelChange < 0 ? "⬇️" : "⬆️"
font.pixelSize: 32
}
......@@ -53,12 +54,13 @@ Kirigami.ScrollablePage {
Layout.row: 0
Layout.column: 2
Layout.fillWidth: true
text: modelData.description
text: section.description
}
QQC2.Label {
Layout.row: 1
Layout.column: 2
text: modelData.distance + "m"
visible: section.distance > 0
text: section.distance + "m"
}
QQC2.Label {
......@@ -66,16 +68,20 @@ Kirigami.ScrollablePage {
Layout.column: 3
Layout.rowSpan: 2
text: "⬆️"
visible: modelData.direction >= 0
visible: model.turnDirection >= 0
font.pixelSize: 32
rotation: modelData.direction
rotation: model.turnDirection
}
}
}
}
PathModel {
id: pathModel
}
ListView {
model: path.sections
model: pathModel
delegate: pathDelegate
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment