Marble

RouteRequestModel.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2011 Dennis Nienhüser <[email protected]>
4 //
5 
6 #include "RouteRequestModel.h"
7 
8 #include "routing/RoutingManager.h"
9 #include "routing/RouteRequest.h"
10 #include "MarbleMap.h"
11 #include "MarbleModel.h"
12 #include "Routing.h"
13 #include <GeoDataPlacemark.h>
14 
15 RouteRequestModel::RouteRequestModel( QObject *parent ) :
16  QAbstractListModel( parent ),
17  m_request( nullptr ),
18  m_routing( nullptr )
19 {
21  roles[Qt::DisplayRole] = "name";
22  roles[LongitudeRole] = "longitude";
23  roles[LatitudeRole] = "latitude";
24  m_roleNames = roles;
25 }
26 
27 RouteRequestModel::~RouteRequestModel()
28 {
29  // nothing to do
30 }
31 
32 int RouteRequestModel::rowCount ( const QModelIndex &parent ) const
33 {
34  if ( !parent.isValid() && m_request ) {
35  return m_request->size();
36  }
37 
38  return 0;
39 }
40 
41 QHash<int, QByteArray> RouteRequestModel::roleNames() const
42 {
43  return m_roleNames;
44 }
45 
46 QVariant RouteRequestModel::headerData ( int section, Qt::Orientation orientation, int role ) const
47 {
48  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0 ) {
49  return QStringLiteral("Waypoint");
50  }
51 
52  return QVariant();
53 }
54 
55 QVariant RouteRequestModel::data ( const QModelIndex &index, int role ) const
56 {
57  if ( index.isValid() && m_request && index.row() >= 0 && index.row() < m_request->size() ) {
58  switch ( role ) {
59  case Qt::DisplayRole: {
60  Marble::GeoDataPlacemark const & placemark = (*m_request)[index.row()];
61  if (!placemark.name().isEmpty()) {
62  return placemark.name();
63  }
64 
65  if (!placemark.address().isEmpty()) {
66  return placemark.address();
67  }
68 
70  }
71  case LongitudeRole: return m_request->at( index.row() ).longitude( Marble::GeoDataCoordinates::Degree );
72  case LatitudeRole: return m_request->at( index.row() ).latitude( Marble::GeoDataCoordinates::Degree );
73  }
74  }
75 
76  return QVariant();
77 }
78 
79 Marble::Routing *RouteRequestModel::routing()
80 {
81  return m_routing;
82 }
83 
84 void RouteRequestModel::setRouting( Marble::Routing *routing )
85 {
86  if ( routing != m_routing ) {
87  m_routing = routing;
88  updateMap();
89  connect( m_routing, SIGNAL(marbleMapChanged()), this, SLOT(updateMap()) );
90  emit routingChanged();
91  }
92 }
93 
94 void RouteRequestModel::updateMap()
95 {
96  if ( m_routing && m_routing->marbleMap() ) {
97  m_request = m_routing->marbleMap()->model()->routingManager()->routeRequest();
98 
99  connect( m_request, SIGNAL(positionChanged(int,GeoDataCoordinates)),
100  this, SLOT(updateData(int)), Qt::UniqueConnection );
101  connect( m_request, SIGNAL(positionAdded(int)),
102  this, SLOT(updateAfterAddition(int)), Qt::UniqueConnection );
103  connect( m_request, SIGNAL(positionRemoved(int)),
104  this, SLOT(updateAfterRemoval(int)), Qt::UniqueConnection );
105 
106  emit layoutChanged();
107  }
108 }
109 
110 void RouteRequestModel::updateData( int idx )
111 {
112  QModelIndex affected = index( idx );
113  emit dataChanged( affected, affected );
114 }
115 
116 void RouteRequestModel::updateAfterRemoval( int idx )
117 {
118  beginRemoveRows( QModelIndex(), idx, idx );
119  removeRow( idx );
120  endRemoveRows();
121  emit rowCountChanged();
122 }
123 
124 void RouteRequestModel::updateAfterAddition( int idx )
125 {
126  beginInsertRows( QModelIndex(), idx, idx );
127  insertRow( idx );
128  endInsertRows();
129  emit rowCountChanged();
130 }
131 
132 void RouteRequestModel::setPosition ( int index, qreal longitude, qreal latitude )
133 {
134  if ( index >= 0 && index < m_request->size() ) {
135  m_request->setPosition( index, Marble::GeoDataCoordinates( longitude, latitude, 0.0, Marble::GeoDataCoordinates::Degree ) );
136  }
137 }
138 
139 #include "moc_RouteRequestModel.cpp"
QString toString() const
return a string representation of the coordinate this is a convenience function which uses the defaul...
A 3d point representation.
DisplayRole
QString trimmed() const const
int size() const const
QString name() const
The name of the feature.
Orientation
bool isEmpty() const const
@ Decimal
"Decimal" notation (base-10)
GeoDataCoordinates coordinate(const QDateTime &dateTime=QDateTime(), bool *iconAtCoordinates=nullptr) const
Return the coordinates of the placemark at time dateTime as a GeoDataCoordinates.
UniqueConnection
bool isValid() const const
a class representing a point of interest on the map
int row() const const
const QChar at(int position) const const
QString address() const
Return the address of the feature.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Oct 2 2023 03:52:09 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.