File indexing completed on 2024-04-28 03:49:30
0001 // SPDX-License-Identifier: LGPL-2.1-or-later 0002 // 0003 // SPDX-FileCopyrightText: 2010 Niko Sams <niko.sams@gmail.com> 0004 // 0005 0006 #include "RoutingProfilesModel.h" 0007 0008 #include "PluginManager.h" 0009 #include "RoutingRunnerPlugin.h" 0010 0011 namespace Marble 0012 { 0013 0014 RoutingProfilesModel::RoutingProfilesModel( const PluginManager* pluginManager, QObject *parent ) 0015 : QAbstractListModel( parent ), m_pluginManager( pluginManager ) 0016 { 0017 } 0018 0019 QVariant RoutingProfilesModel::data( const QModelIndex& index, int role ) const 0020 { 0021 if ( !index.isValid() ) { return QVariant(); } 0022 if ( index.parent().isValid() ) { return QVariant(); } 0023 if ( index.row() >= m_profiles.count() ) { return QVariant(); } 0024 if ( ( role == Qt::DisplayRole || role == Qt::EditRole ) && index.column() == 0 ) { 0025 return m_profiles.at( index.row() ).name(); 0026 } 0027 return QVariant(); 0028 } 0029 0030 int RoutingProfilesModel::rowCount( const QModelIndex& parent ) const 0031 { 0032 if ( parent.isValid() ) { return 0; } 0033 return m_profiles.count(); 0034 } 0035 0036 bool RoutingProfilesModel::removeRows( int row, int count, const QModelIndex& parent ) 0037 { 0038 if ( parent.isValid() ) { return false; } 0039 if ( row + count > m_profiles.count()) { return false; } 0040 beginRemoveRows( parent, row, row + count ); 0041 for ( int i = 0; i < count; ++i) { 0042 m_profiles.removeAt( row+i ); 0043 } 0044 endRemoveRows(); 0045 return true; 0046 } 0047 0048 void RoutingProfilesModel::setProfiles( const QList<RoutingProfile>& profiles ) 0049 { 0050 beginResetModel(); 0051 m_profiles = profiles; 0052 endResetModel(); 0053 } 0054 0055 QList<RoutingProfile> RoutingProfilesModel::profiles() const 0056 { 0057 return m_profiles; 0058 } 0059 0060 void RoutingProfilesModel::addProfile( const QString& name ) 0061 { 0062 beginInsertRows( QModelIndex(), m_profiles.count(), m_profiles.count() ); 0063 m_profiles << RoutingProfile( name ); 0064 endInsertRows(); 0065 } 0066 0067 bool RoutingProfilesModel::moveUp( int row ) 0068 { 0069 if ( row < 1 ) { return false; } 0070 if ( row >= m_profiles.count() ) { return false; } 0071 if ( !beginMoveRows( QModelIndex(), row, row, QModelIndex(), row-1 ) ) { 0072 Q_ASSERT( false ); 0073 return false; 0074 } 0075 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) 0076 m_profiles.swapItemsAt( row, row-1 ); 0077 #else 0078 m_profiles.swap( row, row-1 ); 0079 #endif 0080 endMoveRows(); 0081 return true; 0082 } 0083 0084 bool RoutingProfilesModel::moveDown( int row ) 0085 { 0086 return moveUp( row + 1 ); 0087 } 0088 0089 bool RoutingProfilesModel::setProfileName( int row, const QString& name) 0090 { 0091 if ( row < 0 ) { return false; } 0092 if ( row >= m_profiles.count() ) { return false; } 0093 m_profiles[row].setName( name ); 0094 emit dataChanged( index( row, 0 ), index( row, 0 ) ); 0095 return true; 0096 } 0097 0098 bool RoutingProfilesModel::setProfilePluginSettings( int row, const QHash< QString, QHash< QString, QVariant > >& pluginSettings ) 0099 { 0100 if ( row < 0 ) { return false; } 0101 if ( row >= m_profiles.count() ) { return false; } 0102 m_profiles[row].pluginSettings() = pluginSettings; 0103 return true; 0104 } 0105 0106 QString templateName( RoutingProfilesModel::ProfileTemplate profileTemplate ) 0107 { 0108 switch ( profileTemplate ) { 0109 case RoutingProfilesModel::CarFastestTemplate: 0110 return RoutingProfilesModel::tr( "Car (fastest)" ); 0111 case RoutingProfilesModel::CarShortestTemplate: 0112 return RoutingProfilesModel::tr( "Car (shortest)" ); 0113 case RoutingProfilesModel::CarEcologicalTemplate: 0114 return RoutingProfilesModel::tr( "Car (ecological)" ); 0115 case RoutingProfilesModel::BicycleTemplate: 0116 return RoutingProfilesModel::tr( "Bicycle" ); 0117 case RoutingProfilesModel::PedestrianTemplate: 0118 return RoutingProfilesModel::tr( "Pedestrian" ); 0119 case RoutingProfilesModel::LastTemplate: 0120 break; 0121 } 0122 return RoutingProfilesModel::tr( "Unknown" ); 0123 } 0124 0125 void RoutingProfilesModel::loadDefaultProfiles() 0126 { 0127 beginInsertRows( QModelIndex(), m_profiles.count(), m_profiles.count() + int( LastTemplate ) - 1 ); 0128 for ( int i=0; i < LastTemplate; ++i) { 0129 ProfileTemplate tpl = static_cast<ProfileTemplate>( i ); 0130 RoutingProfile profile( templateName( tpl ) ); 0131 bool profileSupportedByAtLeastOnePlugin = false; 0132 for( RoutingRunnerPlugin* plugin: m_pluginManager->routingRunnerPlugins() ) { 0133 if ( plugin->supportsTemplate( tpl ) ) { 0134 profileSupportedByAtLeastOnePlugin = true; 0135 break; 0136 } 0137 } 0138 if ( !profileSupportedByAtLeastOnePlugin ) { 0139 continue; 0140 } 0141 for( RoutingRunnerPlugin* plugin: m_pluginManager->routingRunnerPlugins() ) { 0142 if ( plugin->supportsTemplate( tpl ) ) { 0143 profile.pluginSettings()[plugin->nameId()] = plugin->templateSettings( tpl ); 0144 } 0145 } 0146 0147 switch ( tpl ) { 0148 case CarFastestTemplate: 0149 case CarShortestTemplate: 0150 case CarEcologicalTemplate: 0151 profile.setTransportType( RoutingProfile::Motorcar ); 0152 break; 0153 case BicycleTemplate: 0154 profile.setTransportType( RoutingProfile::Bicycle ); 0155 break; 0156 case PedestrianTemplate: 0157 profile.setTransportType( RoutingProfile::Pedestrian ); 0158 break; 0159 case LastTemplate: 0160 Q_ASSERT( false && "LastTemplate is an internal enum not to be used as a profile template" ); 0161 break; 0162 } 0163 0164 m_profiles << profile; 0165 } 0166 endInsertRows(); 0167 } 0168 0169 } 0170 0171 #include "moc_RoutingProfilesModel.cpp"