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 ), m_pluginManager( pluginManager )
16{
17}
18
19QVariant RoutingProfilesModel::data( const QModelIndex& index, int role ) const
20{
21 if ( !index.isValid() ) { return QVariant(); }
22 if ( index.parent().isValid() ) { return QVariant(); }
23 if ( index.row() >= m_profiles.count() ) { return QVariant(); }
24 if ( ( role == Qt::DisplayRole || role == Qt::EditRole ) && index.column() == 0 ) {
25 return m_profiles.at( index.row() ).name();
26 }
27 return QVariant();
28}
29
30int RoutingProfilesModel::rowCount( const QModelIndex& parent ) const
31{
32 if ( parent.isValid() ) { return 0; }
33 return m_profiles.count();
34}
35
36bool RoutingProfilesModel::removeRows( int row, int count, const QModelIndex& parent )
37{
38 if ( parent.isValid() ) { return false; }
39 if ( row + count > m_profiles.count()) { return false; }
40 beginRemoveRows( parent, row, row + count );
41 for ( int i = 0; i < count; ++i) {
42 m_profiles.removeAt( row+i );
43 }
44 endRemoveRows();
45 return true;
46}
47
48void RoutingProfilesModel::setProfiles( const QList<RoutingProfile>& profiles )
49{
50 beginResetModel();
51 m_profiles = profiles;
52 endResetModel();
53}
54
55QList<RoutingProfile> RoutingProfilesModel::profiles() const
56{
57 return m_profiles;
58}
59
60void RoutingProfilesModel::addProfile( const QString& name )
61{
62 beginInsertRows( QModelIndex(), m_profiles.count(), m_profiles.count() );
63 m_profiles << RoutingProfile( name );
64 endInsertRows();
65}
66
67bool RoutingProfilesModel::moveUp( int row )
68{
69 if ( row < 1 ) { return false; }
70 if ( row >= m_profiles.count() ) { return false; }
71 if ( !beginMoveRows( QModelIndex(), row, row, QModelIndex(), row-1 ) ) {
72 Q_ASSERT( false );
73 return false;
74 }
75#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
76 m_profiles.swapItemsAt( row, row-1 );
77#else
78 m_profiles.swap( row, row-1 );
79#endif
80 endMoveRows();
81 return true;
82}
83
84bool RoutingProfilesModel::moveDown( int row )
85{
86 return moveUp( row + 1 );
87}
88
89bool RoutingProfilesModel::setProfileName( int row, const QString& name)
90{
91 if ( row < 0 ) { return false; }
92 if ( row >= m_profiles.count() ) { return false; }
93 m_profiles[row].setName( name );
94 emit dataChanged( index( row, 0 ), index( row, 0 ) );
95 return true;
96}
97
98bool RoutingProfilesModel::setProfilePluginSettings( int row, const QHash< QString, QHash< QString, QVariant > >& pluginSettings )
99{
100 if ( row < 0 ) { return false; }
101 if ( row >= m_profiles.count() ) { return false; }
102 m_profiles[row].pluginSettings() = pluginSettings;
103 return true;
104}
105
106QString templateName( RoutingProfilesModel::ProfileTemplate profileTemplate )
107{
108 switch ( profileTemplate ) {
109 case RoutingProfilesModel::CarFastestTemplate:
110 return RoutingProfilesModel::tr( "Car (fastest)" );
111 case RoutingProfilesModel::CarShortestTemplate:
112 return RoutingProfilesModel::tr( "Car (shortest)" );
113 case RoutingProfilesModel::CarEcologicalTemplate:
114 return RoutingProfilesModel::tr( "Car (ecological)" );
115 case RoutingProfilesModel::BicycleTemplate:
116 return RoutingProfilesModel::tr( "Bicycle" );
117 case RoutingProfilesModel::PedestrianTemplate:
118 return RoutingProfilesModel::tr( "Pedestrian" );
119 case RoutingProfilesModel::LastTemplate:
120 break;
121 }
122 return RoutingProfilesModel::tr( "Unknown" );
123}
124
125void RoutingProfilesModel::loadDefaultProfiles()
126{
127 beginInsertRows( QModelIndex(), m_profiles.count(), m_profiles.count() + int( LastTemplate ) - 1 );
128 for ( int i=0; i < LastTemplate; ++i) {
129 ProfileTemplate tpl = static_cast<ProfileTemplate>( i );
130 RoutingProfile profile( templateName( tpl ) );
131 bool profileSupportedByAtLeastOnePlugin = false;
132 for( RoutingRunnerPlugin* plugin: m_pluginManager->routingRunnerPlugins() ) {
133 if ( plugin->supportsTemplate( tpl ) ) {
134 profileSupportedByAtLeastOnePlugin = true;
135 break;
136 }
137 }
138 if ( !profileSupportedByAtLeastOnePlugin ) {
139 continue;
140 }
141 for( RoutingRunnerPlugin* plugin: m_pluginManager->routingRunnerPlugins() ) {
142 if ( plugin->supportsTemplate( tpl ) ) {
143 profile.pluginSettings()[plugin->nameId()] = plugin->templateSettings( tpl );
144 }
145 }
146
147 switch ( tpl ) {
148 case CarFastestTemplate:
149 case CarShortestTemplate:
150 case CarEcologicalTemplate:
151 profile.setTransportType( RoutingProfile::Motorcar );
152 break;
153 case BicycleTemplate:
154 profile.setTransportType( RoutingProfile::Bicycle );
155 break;
156 case PedestrianTemplate:
157 profile.setTransportType( RoutingProfile::Pedestrian );
158 break;
159 case LastTemplate:
160 Q_ASSERT( false && "LastTemplate is an internal enum not to be used as a profile template" );
161 break;
162 }
163
164 m_profiles << profile;
165 }
166 endInsertRows();
167}
168
169}
170
171#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 Tue Mar 26 2024 11:18:17 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.