• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
  • routing
RoutingProfilesModel.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2010 Niko Sams <niko.sams@gmail.com>
9 //
10 
11 #include "RoutingProfilesModel.h"
12 
13 #include "PluginManager.h"
14 #include "RoutingRunnerPlugin.h"
15 
16 namespace Marble
17 {
18 
19 RoutingProfilesModel::RoutingProfilesModel( const PluginManager* pluginManager, QObject *parent )
20  : QAbstractListModel( parent ), m_pluginManager( pluginManager )
21 {
22 }
23 
24 QVariant RoutingProfilesModel::data( const QModelIndex& index, int role ) const
25 {
26  if ( !index.isValid() ) { return QVariant(); }
27  if ( index.parent().isValid() ) { return QVariant(); }
28  if ( index.row() >= m_profiles.count() ) { return QVariant(); }
29  if ( ( role == Qt::DisplayRole || role == Qt::EditRole ) && index.column() == 0 ) {
30  return m_profiles.at( index.row() ).name();
31  }
32  return QVariant();
33 }
34 
35 int RoutingProfilesModel::rowCount( const QModelIndex& parent ) const
36 {
37  if ( parent.isValid() ) { return 0; }
38  return m_profiles.count();
39 }
40 
41 bool RoutingProfilesModel::removeRows( int row, int count, const QModelIndex& parent )
42 {
43  if ( parent.isValid() ) { return false; }
44  if ( row + count > m_profiles.count()) { return false; }
45  beginRemoveRows( parent, row, row + count );
46  for ( int i = 0; i < count; ++i) {
47  m_profiles.removeAt( row+i );
48  }
49  endRemoveRows();
50  return true;
51 }
52 
53 void RoutingProfilesModel::setProfiles( const QList<RoutingProfile>& profiles )
54 {
55  beginResetModel();
56  m_profiles = profiles;
57  endResetModel();
58 }
59 
60 QList<RoutingProfile> RoutingProfilesModel::profiles() const
61 {
62  return m_profiles;
63 }
64 
65 void RoutingProfilesModel::addProfile( const QString& name )
66 {
67  beginInsertRows( QModelIndex(), m_profiles.count(), m_profiles.count() );
68  m_profiles << RoutingProfile( name );
69  endInsertRows();
70 }
71 
72 bool RoutingProfilesModel::moveUp( int row )
73 {
74  if ( row < 1 ) { return false; }
75  if ( row >= m_profiles.count() ) { return false; }
76  if ( !beginMoveRows( QModelIndex(), row, row, QModelIndex(), row-1 ) ) {
77  Q_ASSERT( false );
78  return false;
79  }
80  m_profiles.swap( row, row-1 );
81  endMoveRows();
82  return true;
83 }
84 
85 bool RoutingProfilesModel::moveDown( int row )
86 {
87  return moveUp( row + 1 );
88 }
89 
90 bool RoutingProfilesModel::setProfileName( int row, const QString& name)
91 {
92  if ( row < 0 ) { return false; }
93  if ( row >= m_profiles.count() ) { return false; }
94  m_profiles[row].setName( name );
95  emit dataChanged( index( row, 0 ), index( row, 0 ) );
96  return true;
97 }
98 
99 bool RoutingProfilesModel::setProfilePluginSettings( int row, const QHash< QString, QHash< QString, QVariant > >& pluginSettings )
100 {
101  if ( row < 0 ) { return false; }
102  if ( row >= m_profiles.count() ) { return false; }
103  m_profiles[row].pluginSettings() = pluginSettings;
104  return true;
105 }
106 
107 QString templateName( RoutingProfilesModel::ProfileTemplate profileTemplate )
108 {
109  switch ( profileTemplate ) {
110  case RoutingProfilesModel::CarFastestTemplate:
111  return RoutingProfilesModel::tr( "Car (fastest)" );
112  case RoutingProfilesModel::CarShortestTemplate:
113  return RoutingProfilesModel::tr( "Car (shortest)" );
114  case RoutingProfilesModel::CarEcologicalTemplate:
115  return RoutingProfilesModel::tr( "Car (ecological)" );
116  case RoutingProfilesModel::BicycleTemplate:
117  return RoutingProfilesModel::tr( "Bicycle" );
118  case RoutingProfilesModel::PedestrianTemplate:
119  return RoutingProfilesModel::tr( "Pedestrian" );
120  case RoutingProfilesModel::LastTemplate:
121  break;
122  }
123  return RoutingProfilesModel::tr( "Unknown" );
124 }
125 
126 void RoutingProfilesModel::loadDefaultProfiles()
127 {
128  beginInsertRows( QModelIndex(), m_profiles.count(), m_profiles.count() + int( LastTemplate ) - 1 );
129  for ( int i=0; i < LastTemplate; ++i) {
130  ProfileTemplate tpl = static_cast<ProfileTemplate>( i );
131  RoutingProfile profile( templateName( tpl ) );
132  bool profileSupportedByAtLeastOnePlugin = false;
133  foreach( RoutingRunnerPlugin* plugin, m_pluginManager->routingRunnerPlugins() ) {
134  if ( plugin->supportsTemplate( tpl ) ) {
135  profileSupportedByAtLeastOnePlugin = true;
136  break;
137  }
138  }
139  if ( !profileSupportedByAtLeastOnePlugin ) {
140  continue;
141  }
142  foreach( RoutingRunnerPlugin* plugin, m_pluginManager->routingRunnerPlugins() ) {
143  if ( plugin->supportsTemplate( tpl ) ) {
144  profile.pluginSettings()[plugin->nameId()] = plugin->templateSettings( tpl );
145  }
146  }
147 
148  switch ( tpl ) {
149  case CarFastestTemplate:
150  case CarShortestTemplate:
151  case CarEcologicalTemplate:
152  profile.setTransportType( RoutingProfile::Motorcar );
153  break;
154  case BicycleTemplate:
155  profile.setTransportType( RoutingProfile::Bicycle );
156  break;
157  case PedestrianTemplate:
158  profile.setTransportType( RoutingProfile::Pedestrian );
159  break;
160  case LastTemplate:
161  Q_ASSERT( false && "LastTemplate is an internal enum not to be used as a profile template" );
162  break;
163  }
164 
165  m_profiles << profile;
166  }
167  endInsertRows();
168 }
169 
170 }
171 
172 #include "RoutingProfilesModel.moc"
QModelIndex
Marble::RoutingProfilesModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: RoutingProfilesModel.cpp:24
Marble::PluginManager
The class that handles Marble's plugins.
Definition: PluginManager.h:45
Marble::RoutingProfilesModel::CarEcologicalTemplate
Definition: RoutingProfilesModel.h:38
Marble::RoutingProfilesModel::profiles
QList< RoutingProfile > profiles() const
Definition: RoutingProfilesModel.cpp:60
Marble::templateName
QString templateName(RoutingProfilesModel::ProfileTemplate profileTemplate)
Definition: RoutingProfilesModel.cpp:107
QAbstractItemModel::beginMoveRows
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
Marble::RoutingRunnerPlugin::templateSettings
virtual QHash< QString, QVariant > templateSettings(RoutingProfilesModel::ProfileTemplate profileTemplate) const
Settings for the given routing profile template.
Definition: RoutingRunnerPlugin.cpp:91
Marble::RoutingProfile
Definition: RoutingProfile.h:24
Marble::RoutingProfilesModel::setProfiles
void setProfiles(const QList< RoutingProfile > &profiles)
Definition: RoutingProfilesModel.cpp:53
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
Marble::PluginManager::routingRunnerPlugins
QList< RoutingRunnerPlugin * > routingRunnerPlugins() const
Returns all routing runner plugins.
Definition: PluginManager.cpp:123
Marble::RoutingProfile::Pedestrian
Definition: RoutingProfile.h:32
QAbstractItemModel::beginResetModel
void beginResetModel()
QObject::name
const char * name() const
QModelIndex::isValid
bool isValid() const
Marble::RoutingProfilesModel::setProfilePluginSettings
bool setProfilePluginSettings(int row, const QHash< QString, QHash< QString, QVariant > > &pluginSettings)
Definition: RoutingProfilesModel.cpp:99
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
RoutingRunnerPlugin.h
QHash
QAbstractItemModel::endInsertRows
void endInsertRows()
QObject
QAbstractListModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
Marble::RoutingProfile::setTransportType
void setTransportType(TransportType transportType)
Definition: RoutingProfile.cpp:43
QAbstractListModel
QAbstractItemModel::beginRemoveRows
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex::row
int row() const
QAbstractItemModel::endMoveRows
void endMoveRows()
Marble::RoutingProfilesModel::CarShortestTemplate
Definition: RoutingProfilesModel.h:37
Marble::RoutingProfilesModel::moveDown
bool moveDown(int row)
Definition: RoutingProfilesModel.cpp:85
QString
QList
QModelIndex::parent
QModelIndex parent() const
Marble::RoutingProfilesModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: RoutingProfilesModel.cpp:35
Marble::PluginInterface::nameId
virtual QString nameId() const =0
Returns the unique name of the plugin.
Marble::RoutingRunnerPlugin::supportsTemplate
virtual bool supportsTemplate(RoutingProfilesModel::ProfileTemplate profileTemplate) const
True if the plugin supports the given routing profile template.
Definition: RoutingRunnerPlugin.cpp:86
Marble::RoutingProfilesModel::loadDefaultProfiles
void loadDefaultProfiles()
Definition: RoutingProfilesModel.cpp:126
Marble::RoutingProfilesModel::CarFastestTemplate
Definition: RoutingProfilesModel.h:36
Marble::RoutingProfilesModel::setProfileName
bool setProfileName(int row, const QString &name)
Definition: RoutingProfilesModel.cpp:90
QAbstractItemModel::beginInsertRows
void beginInsertRows(const QModelIndex &parent, int first, int last)
Marble::RoutingProfile::pluginSettings
const QHash< QString, QHash< QString, QVariant > > & pluginSettings() const
Definition: RoutingProfile.cpp:33
Marble::RoutingProfilesModel::LastTemplate
Definition: RoutingProfilesModel.h:42
Marble::RoutingProfilesModel::ProfileTemplate
ProfileTemplate
Definition: RoutingProfilesModel.h:35
Marble::RoutingProfilesModel::BicycleTemplate
Definition: RoutingProfilesModel.h:39
PluginManager.h
Marble::RoutingRunnerPlugin
A plugin for Marble to execute a routing task.
Definition: RoutingRunnerPlugin.h:33
QModelIndex::column
int column() const
Marble::RoutingProfilesModel::removeRows
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Definition: RoutingProfilesModel.cpp:41
QAbstractItemModel::endRemoveRows
void endRemoveRows()
Marble::RoutingProfilesModel::addProfile
void addProfile(const QString &name)
Definition: RoutingProfilesModel.cpp:65
Marble::RoutingProfilesModel::RoutingProfilesModel
RoutingProfilesModel(const PluginManager *pluginManager, QObject *parent=0)
Definition: RoutingProfilesModel.cpp:19
Marble::RoutingProfile::Motorcar
Definition: RoutingProfile.h:30
QAbstractItemModel::endResetModel
void endResetModel()
Marble::RoutingProfilesModel::PedestrianTemplate
Definition: RoutingProfilesModel.h:40
Marble::RoutingProfile::Bicycle
Definition: RoutingProfile.h:31
Marble::RoutingProfilesModel::moveUp
bool moveUp(int row)
Definition: RoutingProfilesModel.cpp:72
RoutingProfilesModel.h
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal