Marble

RouteRelationModel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2017 Sergey Popov <sergobot@protonmail.com>
4//
5
6#include "RouteRelationModel.h"
7
8#include "MarbleColors.h"
9#include "MarbleDirs.h"
10#include "osm/OsmPlacemarkData.h"
11#include "GeoDataColorStyle.h"
12
13namespace Marble
14{
15
16RouteRelationModel::RouteRelationModel(QObject *parent) :
17 QAbstractListModel(parent)
18{
19 m_networks[QStringLiteral("iwn")] = tr("International walking route");
20 m_networks[QStringLiteral("nwn")] = tr("National walking route");
21 m_networks[QStringLiteral("rwn")] = tr("Regional walking route");
22 m_networks[QStringLiteral("lwn")] = tr("Local walking route");
23 m_networks[QStringLiteral("icn")] = tr("International cycling route");
24 m_networks[QStringLiteral("ncn")] = tr("National cycling route");
25 m_networks[QStringLiteral("rcn")] = tr("Regional cycling route");
26 m_networks[QStringLiteral("lcn")] = tr("Local cycling route");
27 m_networks[QStringLiteral("US:TX:FM")] = tr("Farm to Market Road", "State or county road in Texas, USA");
28 m_networks[QStringLiteral("regional")] = tr("Regional route");
29 m_networks[QStringLiteral("national")] = tr("National route");
30 m_networks[QStringLiteral("municipal")] = tr("Municipal route");
31 m_networks[QStringLiteral("territorial")] = tr("Territorial route");
32 m_networks[QStringLiteral("local")] = tr("Local route");
33 m_networks[QStringLiteral("prefectural")] = tr("Prefectural route");
34 m_networks[QStringLiteral("US")] = tr("United States route");
35}
36
37void RouteRelationModel::setRelations(const QSet<const GeoDataRelation*> &relations)
38{
39 if (!m_relations.isEmpty()) {
40 beginRemoveRows(QModelIndex(), 0, m_relations.count() - 1);
41 m_relations.clear();
42 endRemoveRows();
43 }
44
45 if (!relations.isEmpty()) {
46 beginInsertRows(QModelIndex(), 0, relations.count() - 1);
47 m_relations.reserve(relations.size());
48 for (auto relation: relations) {
49 if (relation->relationType() >= GeoDataRelation::RouteRoad && relation->relationType() <= GeoDataRelation::RouteSled) {
50 m_relations << new GeoDataRelation(*relation);
51 }
52 }
53 std::sort(m_relations.begin(), m_relations.end(),
54 [](const GeoDataRelation * a, const GeoDataRelation * b) {
55 return *a < *b;
56 });
57 endInsertRows();
58 }
59}
60
61int RouteRelationModel::rowCount(const QModelIndex & parent) const
62{
63 return parent.isValid() ? 0 : m_relations.count();
64}
65
66QVariant RouteRelationModel::data(const QModelIndex & index, int role) const
67{
68 if (!index.isValid() || index.row() < 0 || index.row() >= m_relations.count()) {
69 return QVariant();
70 }
71
72 if (role == Qt::DisplayRole) {
73 return m_relations.at(index.row())->name();
74 } else if (role == IconSource) {
75 switch (m_relations.at(index.row())->relationType()) {
76 case GeoDataRelation::RouteRoad: return QStringLiteral("material/directions-car.svg");
77 case GeoDataRelation::RouteDetour: return QStringLiteral("material/directions-car.svg");
78 case GeoDataRelation::RouteFerry: return QStringLiteral("material/directions-boat.svg");
79 case GeoDataRelation::RouteTrain: return QStringLiteral("material/directions-railway.svg");
80 case GeoDataRelation::RouteSubway: return QStringLiteral("material/directions-subway.svg");
81 case GeoDataRelation::RouteTram: return QStringLiteral("material/directions-tram.svg");
82 case GeoDataRelation::RouteBus: return QStringLiteral("material/directions-bus.svg");
83 case GeoDataRelation::RouteTrolleyBus: return QStringLiteral("material/directions-bus.svg");
84 case GeoDataRelation::RouteBicycle: return QStringLiteral("material/directions-bike.svg");
85 case GeoDataRelation::RouteMountainbike: return QStringLiteral("material/directions-bike.svg");
86 case GeoDataRelation::RouteFoot: return QStringLiteral("material/directions-walk.svg");
87 case GeoDataRelation::RouteHiking: return QStringLiteral("thenounproject/204712-hiker.svg");
88 case GeoDataRelation::RouteHorse: return QStringLiteral("thenounproject/78374-horse-riding.svg");
89 case GeoDataRelation::RouteInlineSkates: return QStringLiteral("thenounproject/101965-inline-skater.svg");
90 case GeoDataRelation::RouteSkiDownhill: return QStringLiteral("thenounproject/2412-skiing-downhill.svg");
91 case GeoDataRelation::RouteSkiNordic: return QStringLiteral("thenounproject/30231-skiing-cross-country.svg");
92 case GeoDataRelation::RouteSkitour: return QStringLiteral("thenounproject/29366-skitour.svg");
93 case GeoDataRelation::RouteSled: return QStringLiteral("thenounproject/365217-sled.svg");
94 case GeoDataRelation::UnknownType: return QVariant(QString());
95 }
96 } else if (role == Description) {
97 return m_relations.at(index.row())->osmData().tagValue(QStringLiteral("description"));
98 } else if (role == Network) {
99 auto const network = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("network"));
100 auto iter = m_networks.find(network);
101 if (iter != m_networks.end()) {
102 return *iter;
103 }
104 auto const fields = network.split(':', QString::SkipEmptyParts);
105 for (auto const &field: fields) {
106 auto iter = m_networks.find(field);
107 if (iter != m_networks.end()) {
108 return *iter;
109 }
110 }
111 return network;
112 } else if (role == RouteColor) {
113 auto const color = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("colour"));
114 return color.isEmpty() ? QStringLiteral("white") : color;
115 } else if (role == TextColor) {
116 auto const colorValue = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("colour"));
117 auto const color = QColor(colorValue.isEmpty() ? QStringLiteral("white") : colorValue);
118 return GeoDataColorStyle::contrastColor(color);
119 } else if (role == RouteFrom) {
120 return m_relations.at(index.row())->osmData().tagValue(QStringLiteral("from"));
121 } else if (role == RouteTo) {
122 return m_relations.at(index.row())->osmData().tagValue(QStringLiteral("to"));
123 } else if (role == RouteRef) {
124 auto const ref = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("ref"));
125 return ref.isEmpty() ? m_relations.at(index.row())->name() : ref;
126 } else if (role == RouteVia) {
127 auto const viaValue = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("via"));
128 auto viaList = viaValue.split(';', QString::SkipEmptyParts);
129 for (auto &via: viaList) {
130 via = via.trimmed();
131 }
132 return viaList;
133 } else if (role == OsmId) {
134 return m_relations.at(index.row())->osmData().oid();
135 } else if (role == RouteVisible) {
136 return m_relations.at(index.row())->isVisible();
137 }
138
139 return QVariant();
140}
141
142QHash<int, QByteArray> RouteRelationModel::roleNames() const
143{
145 roles[Qt::DisplayRole] = "display";
146 roles[IconSource] = "iconSource";
147 roles[Description] = "description";
148 roles[Network] = "network";
149 roles[RouteColor] = "routeColor";
150 roles[TextColor] = "textColor";
151 roles[RouteFrom] = "routeFrom";
152 roles[RouteTo] = "routeTo";
153 roles[RouteRef] = "routeRef";
154 roles[RouteVia] = "routeVia";
155 roles[OsmId] = "oid";
156 roles[RouteVisible] = "routeVisible";
157 return roles;
158}
159
160QString RouteRelationModel::svgFile(const QString &path)
161{
162#ifdef Q_OS_ANDROID
163 return MarbleDirs::path(QStringLiteral("svg/%1").arg(path));
164#else
165 return QStringLiteral("file:///") + MarbleDirs::path(QStringLiteral("svg/%1").arg(path));
166#endif
167}
168
169}
170
171#include "moc_RouteRelationModel.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
bool isValid() const const
int row() const const
qsizetype count() const const
bool isEmpty() const const
qsizetype size() const const
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.