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 "GeoDataColorStyle.h"
9#include "MarbleDirs.h"
10#include "osm/OsmPlacemarkData.h"
11#include <QColor>
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(), [](const GeoDataRelation *a, const GeoDataRelation *b) {
54 return *a < *b;
55 });
56 endInsertRows();
57 }
58}
59
60int RouteRelationModel::rowCount(const QModelIndex &parent) const
61{
62 return parent.isValid() ? 0 : m_relations.count();
63}
64
65QVariant RouteRelationModel::data(const QModelIndex &index, int role) const
66{
67 if (!index.isValid() || index.row() < 0 || index.row() >= m_relations.count()) {
68 return {};
69 }
70
71 if (role == Qt::DisplayRole) {
72 return m_relations.at(index.row())->name();
73 } else if (role == IconSource) {
74 switch (m_relations.at(index.row())->relationType()) {
75 case GeoDataRelation::RouteRoad:
76 return QStringLiteral("material/directions-car.svg");
77 case GeoDataRelation::RouteDetour:
78 return QStringLiteral("material/directions-car.svg");
79 case GeoDataRelation::RouteFerry:
80 return QStringLiteral("material/directions-boat.svg");
81 case GeoDataRelation::RouteTrain:
82 return QStringLiteral("material/directions-railway.svg");
83 case GeoDataRelation::RouteSubway:
84 return QStringLiteral("material/directions-subway.svg");
85 case GeoDataRelation::RouteTram:
86 return QStringLiteral("material/directions-tram.svg");
87 case GeoDataRelation::RouteBus:
88 return QStringLiteral("material/directions-bus.svg");
89 case GeoDataRelation::RouteTrolleyBus:
90 return QStringLiteral("material/directions-bus.svg");
91 case GeoDataRelation::RouteBicycle:
92 return QStringLiteral("material/directions-bike.svg");
93 case GeoDataRelation::RouteMountainbike:
94 return QStringLiteral("material/directions-bike.svg");
95 case GeoDataRelation::RouteFoot:
96 return QStringLiteral("material/directions-walk.svg");
97 case GeoDataRelation::RouteHiking:
98 return QStringLiteral("thenounproject/204712-hiker.svg");
99 case GeoDataRelation::RouteHorse:
100 return QStringLiteral("thenounproject/78374-horse-riding.svg");
101 case GeoDataRelation::RouteInlineSkates:
102 return QStringLiteral("thenounproject/101965-inline-skater.svg");
103 case GeoDataRelation::RouteSkiDownhill:
104 return QStringLiteral("thenounproject/2412-skiing-downhill.svg");
105 case GeoDataRelation::RouteSkiNordic:
106 return QStringLiteral("thenounproject/30231-skiing-cross-country.svg");
107 case GeoDataRelation::RouteSkitour:
108 return QStringLiteral("thenounproject/29366-skitour.svg");
109 case GeoDataRelation::RouteSled:
110 return QStringLiteral("thenounproject/365217-sled.svg");
111 case GeoDataRelation::UnknownType:
112 return QVariant(QString());
113 }
114 } else if (role == Description) {
115 return m_relations.at(index.row())->osmData().tagValue(QStringLiteral("description"));
116 } else if (role == Network) {
117 auto const network = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("network"));
118 auto iter = m_networks.find(network);
119 if (iter != m_networks.end()) {
120 return *iter;
121 }
122 auto const fields = network.split(QLatin1Char(':'), Qt::SkipEmptyParts);
123 for (auto const &field : fields) {
124 auto iter = m_networks.find(field);
125 if (iter != m_networks.end()) {
126 return *iter;
127 }
128 }
129 return network;
130 } else if (role == RouteColor) {
131 auto const color = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("colour"));
132 return color.isEmpty() ? QStringLiteral("white") : color;
133 } else if (role == TextColor) {
134 auto const colorValue = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("colour"));
135 auto const color = QColor(colorValue.isEmpty() ? QStringLiteral("white") : colorValue);
136 return GeoDataColorStyle::contrastColor(color);
137 } else if (role == RouteFrom) {
138 return m_relations.at(index.row())->osmData().tagValue(QStringLiteral("from"));
139 } else if (role == RouteTo) {
140 return m_relations.at(index.row())->osmData().tagValue(QStringLiteral("to"));
141 } else if (role == RouteRef) {
142 auto const ref = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("ref"));
143 return ref.isEmpty() ? m_relations.at(index.row())->name() : ref;
144 } else if (role == RouteVia) {
145 auto const viaValue = m_relations.at(index.row())->osmData().tagValue(QStringLiteral("via"));
146 auto viaList = viaValue.split(QLatin1Char(';'), Qt::SkipEmptyParts);
147 for (auto &via : viaList) {
148 via = via.trimmed();
149 }
150 return viaList;
151 } else if (role == OsmId) {
152 return m_relations.at(index.row())->osmData().oid();
153 } else if (role == RouteVisible) {
154 return m_relations.at(index.row())->isVisible();
155 }
156
157 return {};
158}
159
160QHash<int, QByteArray> RouteRelationModel::roleNames() const
161{
163 roles[Qt::DisplayRole] = "display";
164 roles[IconSource] = "iconSource";
165 roles[Description] = "description";
166 roles[Network] = "network";
167 roles[RouteColor] = "routeColor";
168 roles[TextColor] = "textColor";
169 roles[RouteFrom] = "routeFrom";
170 roles[RouteTo] = "routeTo";
171 roles[RouteRef] = "routeRef";
172 roles[RouteVia] = "routeVia";
173 roles[OsmId] = "oid";
174 roles[RouteVisible] = "routeVisible";
175 return roles;
176}
177
178QString RouteRelationModel::svgFile(const QString &path)
179{
180#ifdef Q_OS_ANDROID
181 return MarbleDirs::path(QStringLiteral("svg/%1").arg(path));
182#else
183 return QStringLiteral("file:///") + MarbleDirs::path(QStringLiteral("svg/%1").arg(path));
184#endif
185}
186
187}
188
189#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
SkipEmptyParts
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.