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

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • lib
  • marble
  • routing
  • instructions
WaypointParser.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 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
11 #include "WaypointParser.h"
12 
13 #include <QDebug>
14 #include <QStringList>
15 
16 namespace Marble
17 {
18 
19 // Template specialization to avoid shifting a QString through a QVariant and back
20 template<>
21 QString WaypointParser::readField<QString>( Field field, const QStringList &fields, const QString &defaultValue ) const {
22  int index = m_fieldIndices[field];
23  if ( index >= 0 && index < fields.size() ) {
24  return fields[index];
25  }
26 
27  return defaultValue;
28 }
29 
30 // The default values are suitable for older versions of gosmore (the one shipped with Ubuntu Lucid Lynx)
31 WaypointParser::WaypointParser() : m_lineSeparator( '\n' ), m_fieldSeparator( ',' )
32 {
33  setFieldIndex( Latitude, 0 );
34  setFieldIndex( Longitude, 1 );
35  setFieldIndex( JunctionType, 2 );
36  setFieldIndex( RoadName, 4 );
37 }
38 
39 void WaypointParser::setFieldIndex( Field field, int index )
40 {
41  m_fieldIndices[field] = index;
42 }
43 
44 void WaypointParser::setLineSeparator( const QString &separator )
45 {
46  m_lineSeparator = separator;
47 }
48 
49 void WaypointParser::setFieldSeparator( const QChar &separator )
50 {
51  m_fieldSeparator = separator;
52 }
53 
54 void WaypointParser::addJunctionTypeMapping( const QString &key, RoutingWaypoint::JunctionType value )
55 {
56  m_junctionTypeMapping[key] = value;
57 }
58 
59 RoutingWaypoints WaypointParser::parse( QTextStream &stream ) const
60 {
61  RoutingWaypoints result;
62  QString input = stream.readAll();
63  QStringList lines = input.split( m_lineSeparator );
64  foreach( const QString &line, lines ) {
65  if ( !line.trimmed().isEmpty() &&
66  !line.trimmed().startsWith('#') &&
67  !line.startsWith( QLatin1String( "Content-Type: text/plain" ) ) ) {
68  QStringList entries = line.split( m_fieldSeparator );
69  if ( entries.size() >= 1 + m_fieldIndices[RoadName] ) {
70  qreal lon = readField<qreal>( Longitude, entries );
71  qreal lat = readField<qreal>( Latitude, entries );
72  RoutingPoint point( lon, lat );
73  QString junctionTypeRaw = readField<QString>( JunctionType, entries, QString() );
74  RoutingWaypoint::JunctionType junctionType = RoutingWaypoint::Other;
75  if ( m_junctionTypeMapping.contains( junctionTypeRaw ) ) {
76  junctionType = m_junctionTypeMapping[junctionTypeRaw];
77  }
78  QString roadType = readField<QString>( RoadType, entries, QString() );
79  int secondsRemaining = readField<int>( TotalSecondsRemaining, entries, -1 );
80  QString roadName = readField<QString>( RoadName, entries, QString() );
81 
82  // Road names may contain the field separator
83  for (int i = 2 + m_fieldIndices[RoadName]; i<entries.size(); ++i)
84  {
85  roadName += m_fieldSeparator + entries.at(i);
86  }
87 
88  RoutingWaypoint item( point, junctionType, junctionTypeRaw, roadType, secondsRemaining, roadName );
89  result.push_back( item );
90  } else {
91  qDebug() << "Cannot parse " << line << "(detected " << entries.size() << " fields)";
92  }
93  }
94  }
95 
96  return result;
97 }
98 
99 } // namespace Marble
Marble::WaypointParser::RoadName
Definition: WaypointParser.h:33
Marble::WaypointParser::TotalSecondsRemaining
Definition: WaypointParser.h:34
Marble::WaypointParser::RoadType
Definition: WaypointParser.h:35
Marble::WaypointParser::setLineSeparator
void setLineSeparator(const QString &separator)
The line separator used in the stream passed to parse.
Definition: WaypointParser.cpp:44
Marble::WaypointParser::parse
RoutingWaypoints parse(QTextStream &stream) const
Parses the given stream and returns the extracted waypoint list.
Definition: WaypointParser.cpp:59
Marble::WaypointParser::Field
Field
Fields which can be parsed.
Definition: WaypointParser.h:29
Marble::WaypointParser::addJunctionTypeMapping
void addJunctionTypeMapping(const QString &key, RoutingWaypoint::JunctionType value)
Associate the given string key with the given junction type.
Definition: WaypointParser.cpp:54
Marble::WaypointParser::Latitude
Definition: WaypointParser.h:31
Marble::WaypointParser::JunctionType
Definition: WaypointParser.h:32
Marble::WaypointParser::setFieldIndex
void setFieldIndex(Field field, int index)
Associate the zero-based field no index with the given semantic type.
Definition: WaypointParser.cpp:39
WaypointParser.h
Marble::WaypointParser::setFieldSeparator
void setFieldSeparator(const QChar &separator)
The field separator.
Definition: WaypointParser.cpp:49
Marble::WaypointParser::Longitude
Definition: WaypointParser.h:30
Marble::WaypointParser::WaypointParser
WaypointParser()
Constructor.
Definition: WaypointParser.cpp:31
Marble::RoutingPoint
There are many Point classes, but this is mine.
Definition: RoutingPoint.h:24
Marble::RoutingWaypoint::Other
Definition: RoutingWaypoint.h:32
Marble::RoutingWaypoints
QVector< RoutingWaypoint > RoutingWaypoints
Definition: RoutingWaypoint.h:75
Marble::RoutingWaypoint
Stores one line of gosmore/routino output.
Definition: RoutingWaypoint.h:25
Marble::RoutingWaypoint::JunctionType
JunctionType
Junction types that affect instructions.
Definition: RoutingWaypoint.h:29
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:53 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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