• 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
RouteRequest.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 "RouteRequest.h"
12 
13 #include "GeoDataLineString.h"
14 #include "GeoDataPlacemark.h"
15 #include "GeoDataData.h"
16 #include "GeoDataExtendedData.h"
17 #include "MarbleDirs.h"
18 
19 #include <QMap>
20 #include <QPainter>
21 
22 namespace Marble
23 {
24 
25 struct PixmapElement
26 {
27  int index;
28 
29  int size;
30 
31  explicit PixmapElement( int index=-1, int size=0 );
32 
33  bool operator < ( const PixmapElement &other ) const;
34 };
35 
36 class RouteRequestPrivate
37 {
38 public:
39  QVector<GeoDataPlacemark> m_route;
40 
41  QMap<PixmapElement, QPixmap> m_pixmapCache;
42 
43  RoutingProfile m_routingProfile;
44 
46  int viaIndex( const GeoDataCoordinates &position ) const;
47 };
48 
49 PixmapElement::PixmapElement( int index_, int size_ ) :
50  index( index_ ), size( size_ )
51 {
52  // nothing to do
53 }
54 
55 bool PixmapElement::operator <(const PixmapElement &other) const
56 {
57  return index < other.index || size < other.size;
58 }
59 
60 int RouteRequestPrivate::viaIndex( const GeoDataCoordinates &position ) const
61 {
64  // Iterates over all ordered trip point pairs (P,Q) and finds the triple
65  // (P,position,Q) or (P,Q,position) with minimum length
66  qreal minLength = -1.0;
67  int result = 0;
68  GeoDataLineString viaFirst;
69  GeoDataLineString viaSecond;
70  for ( int i = 0; i < m_route.size(); ++i ) {
71  Q_ASSERT( viaFirst.size() < 4 && viaSecond.size() < 4 );
72  if ( viaFirst.size() == 3 ) {
73  viaFirst.remove( 0 );
74  viaFirst.remove( 0 );
75  }
76 
77  if ( viaSecond.size() == 3 ) {
78  viaSecond.remove( 0 );
79  viaSecond.remove( 0 );
80  }
81 
82  if ( viaFirst.size() == 1 ) {
83  viaFirst.append( position );
84  }
85 
86  viaFirst.append( m_route[i].coordinate() );
87  viaSecond.append( m_route[i].coordinate() );
88 
89  if ( viaSecond.size() == 2 ) {
90  viaSecond.append( position );
91  }
92 
93  if ( viaFirst.size() == 3 ) {
94  qreal len = viaFirst.length( EARTH_RADIUS );
95  if ( minLength < 0.0 || len < minLength ) {
96  minLength = len;
97  result = i;
98  }
99  }
100 
102  if ( viaSecond.size() == 3 && i + 1 < m_route.size() ) {
103  qreal len = viaSecond.length( EARTH_RADIUS );
104  if ( minLength < 0.0 || len < minLength ) {
105  minLength = len;
106  result = i + 1;
107  }
108  }
109  }
110 
111  Q_ASSERT( 0 <= result && result <= m_route.size() );
112  return result;
113 }
114 
115 RouteRequest::RouteRequest( QObject *parent ) :
116  QObject( parent ), d( new RouteRequestPrivate )
117 {
118  // nothing to do
119 }
120 
121 RouteRequest::~RouteRequest()
122 {
123  delete d;
124 }
125 
126 int RouteRequest::size() const
127 {
128  return d->m_route.size();
129 }
130 
131 GeoDataCoordinates RouteRequest::source() const
132 {
133  GeoDataCoordinates result;
134  if ( d->m_route.size() ) {
135  result = d->m_route.first().coordinate();
136  }
137  return result;
138 }
139 
140 GeoDataCoordinates RouteRequest::destination() const
141 {
142  GeoDataCoordinates result;
143  if ( d->m_route.size() ) {
144  result = d->m_route.last().coordinate();
145  }
146  return result;
147 }
148 
149 GeoDataCoordinates RouteRequest::at( int position ) const
150 {
151  return d->m_route.at( position ).coordinate();
152 }
153 
154 QPixmap RouteRequest::pixmap(int position, int size, int margin ) const
155 {
156  PixmapElement const element( position, size );
157  if ( d->m_pixmapCache.contains( element ) ) {
158  return d->m_pixmapCache[element];
159  }
160 
161  // Transparent background
162  bool smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
163  int const imageSize = size > 0 ? size : ( smallScreen ? 32 : 16 );
164  QImage result( imageSize, imageSize, QImage::Format_ARGB32_Premultiplied );
165  result.fill( qRgba( 0, 0, 0, 0 ) );
166 
167  // Paint a colored circle
168  QPainter painter( &result );
169  painter.setRenderHint( QPainter::Antialiasing, true );
170  painter.setPen( QColor( Qt::black ) );
171  bool const isVisited = visited( position );
172  QColor const backgroundColor = isVisited ? Oxygen::aluminumGray4 : Oxygen::forestGreen4;
173  painter.setBrush( QBrush( backgroundColor ) );
174  painter.setPen( Qt::black );
175  int const iconSize = imageSize - 2 * margin;
176  painter.drawEllipse( margin, margin, iconSize, iconSize );
177 
178  char text = char( 'A' + position );
179 
180  // Choose a suitable font size
181  QFont font = painter.font();
182  int fontSize = 20;
183  while ( fontSize-- > 0 ) {
184  font.setPointSize( fontSize );
185  QFontMetrics fontMetric( font );
186  if ( fontMetric.width( text ) <= iconSize && fontMetric.height( ) <= iconSize ) {
187  break;
188  }
189  }
190 
191  Q_ASSERT( fontSize );
192  font.setPointSize( fontSize );
193  painter.setFont( font );
194 
195  // Paint a character denoting the position (0=A, 1=B, 2=C, ...)
196  painter.drawText( 0, 0, imageSize, imageSize, Qt::AlignCenter, QString( text ) );
197 
198  d->m_pixmapCache.insert( element, QPixmap::fromImage( result ) );
199  return pixmap( position, size );
200 }
201 
202 void RouteRequest::clear()
203 {
204  for ( int i=d->m_route.size()-1; i>=0; --i ) {
205  remove( i );
206  }
207 }
208 
209 void RouteRequest::insert( int index, const GeoDataCoordinates &coordinates, const QString &name )
210 {
211  GeoDataPlacemark placemark;
212  placemark.setCoordinate( coordinates );
213  d->m_route.insert( index, placemark );
214  setName( index, name );
215  emit positionAdded( index );
216 }
217 
218 void RouteRequest::append( const GeoDataCoordinates &coordinates, const QString &name )
219 {
220  GeoDataPlacemark placemark;
221  placemark.setCoordinate( coordinates );
222  placemark.setName( name );
223  append( placemark );
224 }
225 
226 void RouteRequest::append( const GeoDataPlacemark &placemark )
227 {
228  d->m_route.append( placemark );
229  emit positionAdded( d->m_route.size()-1 );
230 }
231 
232 void RouteRequest::remove( int index )
233 {
234  if ( index >= 0 && index < d->m_route.size() ) {
235  d->m_route.remove( index );
236  emit positionRemoved( index );
237  }
238 }
239 
240 void RouteRequest::addVia( const GeoDataCoordinates &position )
241 {
242  int index = d->viaIndex( position );
243  GeoDataPlacemark placemark;
244  placemark.setCoordinate( position );
245  d->m_route.insert( index, placemark );
246  emit positionAdded( index );
247 }
248 
249 void RouteRequest::setPosition( int index, const GeoDataCoordinates &position, const QString &name )
250 {
251  if ( index >= 0 && index < d->m_route.size() ) {
252  GeoDataPlacemark placemark;
253  placemark.setCoordinate( position );
254  d->m_route[index] = placemark;
255  setName( index, name );
256  setVisited( index, false );
257  emit positionChanged( index, position );
258  }
259 }
260 
261 void RouteRequest::setName( int index, const QString &name )
262 {
263  if ( index >= 0 && index < d->m_route.size() ) {
264  d->m_route[index].setName( name );
265  }
266 }
267 
268 QString RouteRequest::name( int index ) const
269 {
270  QString result;
271  if ( index >= 0 && index < d->m_route.size() ) {
272  result = d->m_route[index].name();
273  }
274  return result;
275 }
276 
277 void RouteRequest::setVisited( int index, bool visited )
278 {
279  if ( index >= 0 && index < d->m_route.size() ) {
280  d->m_route[index].extendedData().addValue( GeoDataData( "routingVisited", visited ) );
281  QMap<PixmapElement, QPixmap>::iterator iter = d->m_pixmapCache.begin();
282  while ( iter != d->m_pixmapCache.end() ) {
283  if ( iter.key().index == index ) {
284  iter = d->m_pixmapCache.erase( iter );
285  } else {
286  ++iter;
287  }
288  }
289  emit positionChanged( index, d->m_route[index].coordinate() );
290  }
291 }
292 
293 bool RouteRequest::visited( int index ) const
294 {
295  bool visited = false;
296  if ( index >= 0 && index < d->m_route.size() ) {
297  if ( d->m_route[index].extendedData().contains( "routingVisited" ) ) {
298  visited = d->m_route[index].extendedData().value( "routingVisited" ).value().toBool();
299  }
300  }
301  return visited;
302 }
303 
304 void RouteRequest::reverse()
305 {
306  int const total = d->m_route.size();
307  int const upper = total / 2;
308  for( int i=0; i<upper; ++i ) {
309  qSwap( d->m_route[i], d->m_route[total-i-1] );
310  setVisited( i, false );
311  setVisited( total-i-1, false );
312  }
313 }
314 
315 void RouteRequest::setRoutingProfile( const RoutingProfile &profile )
316 {
317  d->m_routingProfile = profile;
318  emit routingProfileChanged();
319 }
320 
321 RoutingProfile RouteRequest::routingProfile() const
322 {
323  return d->m_routingProfile;
324 }
325 
326 GeoDataPlacemark &RouteRequest::operator []( int index )
327 {
328  return d->m_route[index];
329 }
330 
331 const GeoDataPlacemark &RouteRequest::operator [](int index) const
332 {
333  return d->m_route[index];
334 }
335 
336 } // namespace Marble
337 
338 #include "RouteRequest.moc"
Marble::RouteRequest::size
int size() const
Number of points in the route.
Definition: RouteRequest.cpp:126
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
QFont::setPointSize
void setPointSize(int pointSize)
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
Marble::RouteRequest::positionAdded
void positionAdded(int index)
An element was added at the given position.
Marble::RouteRequest::clear
void clear()
Remove all elements.
Definition: RouteRequest.cpp:202
QPainter::font
const QFont & font() const
QFont
Marble::RouteRequest::~RouteRequest
~RouteRequest()
Destructor.
Definition: RouteRequest.cpp:121
QMap< PixmapElement, QPixmap >
Marble::RouteRequest::pixmap
QPixmap pixmap(int index, int size=-1, int margin=2) const
Returns a pixmap which indicates the position of the element.
Definition: RouteRequest.cpp:154
Marble::RouteRequest::positionRemoved
void positionRemoved(int index)
The element at the given position was removed.
Marble::RouteRequest::insert
void insert(int index, const GeoDataCoordinates &coordinates, const QString &name=QString())
Add the given element at the given position.
Definition: RouteRequest.cpp:209
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
Marble::RouteRequest::setVisited
void setVisited(int index, bool visited)
Definition: RouteRequest.cpp:277
QBrush
Marble::RouteRequest::destination
GeoDataCoordinates destination() const
The last point, or a default constructed if empty.
Definition: RouteRequest.cpp:140
QFontMetrics
Marble::RouteRequest::append
void append(const GeoDataCoordinates &coordinates, const QString &name=QString())
Add the given element to the end.
Definition: RouteRequest.cpp:218
GeoDataExtendedData.h
Marble::GeoDataPlacemark::setCoordinate
void setCoordinate(qreal longitude, qreal latitude, qreal altitude=0, GeoDataCoordinates::Unit _unit=GeoDataCoordinates::Radian)
Set the coordinate of the placemark in longitude and latitude.
Definition: GeoDataPlacemark.cpp:215
Marble::RoutingProfile
Definition: RoutingProfile.h:24
Marble::RouteRequest::positionChanged
void positionChanged(int index, const GeoDataCoordinates &position)
The value of the n-th element was changed.
QObject::name
const char * name() const
QPainter::setFont
void setFont(const QFont &font)
Marble::RouteRequest::routingProfile
RoutingProfile routingProfile() const
Definition: RouteRequest.cpp:321
Marble::EARTH_RADIUS
const qreal EARTH_RADIUS
Definition: MarbleGlobal.h:257
Marble::GeoDataFeature::setName
void setName(const QString &value)
Set a new name for this feature.
Definition: GeoDataFeature.cpp:549
Marble::Oxygen::aluminumGray4
QColor const aluminumGray4
Definition: MarbleColors.h:92
QImage::fill
void fill(uint pixelValue)
Marble::RouteRequest::RouteRequest
RouteRequest(QObject *parent=0)
Constructor.
Definition: RouteRequest.cpp:115
QObject
QPainter::setPen
void setPen(const QColor &color)
QPainter::drawEllipse
void drawEllipse(const QRectF &rectangle)
QPainter
MarbleDirs.h
GeoDataLineString.h
Marble::RouteRequest::operator[]
GeoDataPlacemark & operator[](int index)
Definition: RouteRequest.cpp:326
Marble::RouteRequest::setRoutingProfile
void setRoutingProfile(const RoutingProfile &profile)
Definition: RouteRequest.cpp:315
QPainter::setBrush
void setBrush(const QBrush &brush)
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
QString
Marble::RouteRequest::visited
bool visited(int index) const
Definition: RouteRequest.cpp:293
QColor
GeoDataPlacemark.h
QPixmap
iconSize
const QSize iconSize(16, 16)
QFontMetrics::width
int width(const QString &text, int len) const
Marble::MarbleGlobal::SmallScreen
Definition: MarbleGlobal.h:287
QImage
Marble::imageSize
const QSize imageSize(28, 28)
Marble::MarbleGlobal::getInstance
static MarbleGlobal * getInstance()
Definition: MarbleGlobal.cpp:37
QMap::key
const Key key(const T &value) const
Marble::GeoDataData
Definition: GeoDataData.h:26
GeoDataData.h
Marble::RouteRequest::remove
void remove(int index)
Remove the element at the given position.
Definition: RouteRequest.cpp:232
QVector
Marble::RouteRequest::setName
void setName(int index, const QString &name)
Definition: RouteRequest.cpp:261
QFontMetrics::height
int height() const
Marble::MarbleGlobal::profiles
Profiles profiles() const
Definition: MarbleGlobal.cpp:48
Marble::Oxygen::forestGreen4
QColor const forestGreen4
Definition: MarbleColors.h:74
RouteRequest.h
Marble::RouteRequest::setPosition
void setPosition(int index, const GeoDataCoordinates &position, const QString &name=QString())
Change the value of the element at the given position.
Definition: RouteRequest.cpp:249
Marble::RouteRequest::addVia
void addVia(const GeoDataCoordinates &position)
Insert a via point.
Definition: RouteRequest.cpp:240
Marble::RouteRequest::routingProfileChanged
void routingProfileChanged()
The routing profile was changed.
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:54
Marble::RouteRequest::source
GeoDataCoordinates source() const
The first point, or a default constructed if empty.
Definition: RouteRequest.cpp:131
coordinate
Coordinate coordinate
Definition: tools/osm-addresses/OsmParser.cpp:39
Marble::RouteRequest::reverse
void reverse()
Definition: RouteRequest.cpp:304
Marble::RouteRequest::at
GeoDataCoordinates at(int index) const
Accessor for the n-th position.
Definition: RouteRequest.cpp:149
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