Marble

RoutingProfilesModel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2010 Niko Sams <niko.sams@gmail.com>
4//
5
6#include "RoutingProfilesModel.h"
7
8#include "PluginManager.h"
9#include "RoutingRunnerPlugin.h"
10
11namespace Marble
12{
13
14RoutingProfilesModel::RoutingProfilesModel(const PluginManager *pluginManager, QObject *parent)
15 : QAbstractListModel(parent)
16 , m_pluginManager(pluginManager)
17{
18}
19
20QVariant RoutingProfilesModel::data(const QModelIndex &index, int role) const
21{
22 if (!index.isValid()) {
23 return {};
24 }
25 if (index.parent().isValid()) {
26 return {};
27 }
28 if (index.row() >= m_profiles.count()) {
29 return {};
30 }
31 if ((role == Qt::DisplayRole || role == Qt::EditRole) && index.column() == 0) {
32 return m_profiles.at(index.row()).name();
33 }
34 return {};
35}
36
37int RoutingProfilesModel::rowCount(const QModelIndex &parent) const
38{
39 if (parent.isValid()) {
40 return 0;
41 }
42 return m_profiles.count();
43}
44
45bool RoutingProfilesModel::removeRows(int row, int count, const QModelIndex &parent)
46{
47 if (parent.isValid()) {
48 return false;
49 }
50 if (row + count > m_profiles.count()) {
51 return false;
52 }
53 beginRemoveRows(parent, row, row + count);
54 for (int i = 0; i < count; ++i) {
55 m_profiles.removeAt(row + i);
56 }
57 endRemoveRows();
58 return true;
59}
60
61void RoutingProfilesModel::setProfiles(const QList<RoutingProfile> &profiles)
62{
63 beginResetModel();
64 m_profiles = profiles;
65 endResetModel();
66}
67
68QList<RoutingProfile> RoutingProfilesModel::profiles() const
69{
70 return m_profiles;
71}
72
73void RoutingProfilesModel::addProfile(const QString &name)
74{
75 beginInsertRows(QModelIndex(), m_profiles.count(), m_profiles.count());
76 m_profiles << RoutingProfile(name);
77 endInsertRows();
78}
79
80bool RoutingProfilesModel::moveUp(int row)
81{
82 if (row < 1) {
83 return false;
84 }
85 if (row >= m_profiles.count()) {
86 return false;
87 }
88 if (!beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1)) {
89 Q_ASSERT(false);
90 return false;
91 }
92 m_profiles.swapItemsAt(row, row - 1);
93 endMoveRows();
94 return true;
95}
96
97bool RoutingProfilesModel::moveDown(int row)
98{
99 return moveUp(row + 1);
100}
101
102bool RoutingProfilesModel::setProfileName(int row, const QString &name)
103{
104 if (row < 0) {
105 return false;
106 }
107 if (row >= m_profiles.count()) {
108 return false;
109 }
110 m_profiles[row].setName(name);
111 Q_EMIT dataChanged(index(row, 0), index(row, 0));
112 return true;
113}
114
115bool RoutingProfilesModel::setProfilePluginSettings(int row, const QHash<QString, QHash<QString, QVariant>> &pluginSettings)
116{
117 if (row < 0) {
118 return false;
119 }
120 if (row >= m_profiles.count()) {
121 return false;
122 }
123 m_profiles[row].pluginSettings() = pluginSettings;
124 return true;
125}
126
127QString templateName(RoutingProfilesModel::ProfileTemplate profileTemplate)
128{
129 switch (profileTemplate) {
130 case RoutingProfilesModel::CarFastestTemplate:
131 return RoutingProfilesModel::tr("Car (fastest)");
132 case RoutingProfilesModel::CarShortestTemplate:
133 return RoutingProfilesModel::tr("Car (shortest)");
134 case RoutingProfilesModel::CarEcologicalTemplate:
135 return RoutingProfilesModel::tr("Car (ecological)");
136 case RoutingProfilesModel::BicycleTemplate:
137 return RoutingProfilesModel::tr("Bicycle");
138 case RoutingProfilesModel::PedestrianTemplate:
139 return RoutingProfilesModel::tr("Pedestrian");
140 case RoutingProfilesModel::LastTemplate:
141 break;
142 }
143 return RoutingProfilesModel::tr("Unknown");
144}
145
146void RoutingProfilesModel::loadDefaultProfiles()
147{
148 beginInsertRows(QModelIndex(), m_profiles.count(), m_profiles.count() + int(LastTemplate) - 1);
149 for (int i = 0; i < LastTemplate; ++i) {
150 auto tpl = static_cast<ProfileTemplate>(i);
151 RoutingProfile profile(templateName(tpl));
152 bool profileSupportedByAtLeastOnePlugin = false;
153 for (RoutingRunnerPlugin *plugin : m_pluginManager->routingRunnerPlugins()) {
154 if (plugin->supportsTemplate(tpl)) {
155 profileSupportedByAtLeastOnePlugin = true;
156 break;
157 }
158 }
159 if (!profileSupportedByAtLeastOnePlugin) {
160 continue;
161 }
162 for (RoutingRunnerPlugin *plugin : m_pluginManager->routingRunnerPlugins()) {
163 if (plugin->supportsTemplate(tpl)) {
164 profile.pluginSettings()[plugin->nameId()] = plugin->templateSettings(tpl);
165 }
166 }
167
168 switch (tpl) {
169 case CarFastestTemplate:
170 case CarShortestTemplate:
171 case CarEcologicalTemplate:
172 profile.setTransportType(RoutingProfile::Motorcar);
173 break;
174 case BicycleTemplate:
175 profile.setTransportType(RoutingProfile::Bicycle);
176 break;
177 case PedestrianTemplate:
178 profile.setTransportType(RoutingProfile::Pedestrian);
179 break;
180 case LastTemplate:
181 Q_ASSERT(false && "LastTemplate is an internal enum not to be used as a profile template");
182 break;
183 }
184
185 m_profiles << profile;
186 }
187 endInsertRows();
188}
189
190}
191
192#include "moc_RoutingProfilesModel.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
int column() const const
bool isValid() const const
QModelIndex parent() const const
int row() const const
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.