Marble

RouteRequestModel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2011 Dennis Nienhüser <nienhueser@kde.org>
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
15RouteRequestModel::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
27RouteRequestModel::~RouteRequestModel()
28{
29 // nothing to do
30}
31
32int 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
41QHash<int, QByteArray> RouteRequestModel::roleNames() const
42{
43 return m_roleNames;
44}
45
46QVariant 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
55QVariant 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
79Marble::Routing *RouteRequestModel::routing()
80{
81 return m_routing;
82}
83
84void 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
94void 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
107 }
108}
109
110void RouteRequestModel::updateData( int idx )
111{
112 QModelIndex affected = index( idx );
113 emit dataChanged( affected, affected );
114}
115
116void RouteRequestModel::updateAfterRemoval( int idx )
117{
119 removeRow( idx );
121 emit rowCountChanged();
122}
123
124void RouteRequestModel::updateAfterAddition( int idx )
125{
127 insertRow( idx );
129 emit rowCountChanged();
130}
131
132void 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"
This file contains the headers for MarbleMap.
This file contains the headers for MarbleModel.
A 3d point representation.
@ Decimal
"Decimal" notation (base-10)
qreal longitude(GeoDataCoordinates::Unit unit) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
qreal latitude(GeoDataCoordinates::Unit unit) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
QString toString() const
return a string representation of the coordinate this is a convenience function which uses the defaul...
QString name() const
The name of the feature.
QString address() const
Return the address of the feature.
a class representing a point of interest on the map
GeoDataCoordinates coordinate(const QDateTime &dateTime=QDateTime(), bool *iconAtCoordinates=nullptr) const
Return the coordinates of the placemark at time dateTime as a GeoDataCoordinates.
MarbleModel * model() const
Return the model that this view shows.
int size() const
Number of points in the route.
void setPosition(int index, const GeoDataCoordinates &position, const QString &name=QString())
Change the value of the element at the given position.
GeoDataCoordinates at(int index) const
Accessor for the n-th position.
RouteRequest * routeRequest()
Returns the current route request.
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
bool insertRow(int row, const QModelIndex &parent)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
bool removeRow(int row, const QModelIndex &parent)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
bool isEmpty() const const
QString trimmed() const const
UniqueConnection
DisplayRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.