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 "MarbleMap.h"
9#include "MarbleModel.h"
10#include "Routing.h"
11#include "routing/RouteRequest.h"
12#include "routing/RoutingManager.h"
13#include <GeoDataPlacemark.h>
14
15RouteRequestModel::RouteRequestModel(QObject *parent)
16 : QAbstractListModel(parent)
17{
18}
19
20RouteRequestModel::~RouteRequestModel() = default;
21
22int RouteRequestModel::rowCount(const QModelIndex &parent) const
23{
24 if (!parent.isValid() && m_request) {
25 return m_request->size();
26 }
27
28 return 0;
29}
30
31QHash<int, QByteArray> RouteRequestModel::roleNames() const
32{
33 return {
34 {Qt::DisplayRole, "name"},
35 {LongitudeRole, "longitude"},
36 {LatitudeRole, "latitude"},
37 };
38}
39
40QVariant RouteRequestModel::headerData(int section, Qt::Orientation orientation, int role) const
41{
42 if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0) {
43 return QStringLiteral("Waypoint");
44 }
45
46 return {};
47}
48
49QVariant RouteRequestModel::data(const QModelIndex &index, int role) const
50{
51 Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
52
53 switch (role) {
54 case Qt::DisplayRole: {
55 Marble::GeoDataPlacemark const &placemark = (*m_request)[index.row()];
56 if (!placemark.name().isEmpty()) {
57 return placemark.name();
58 }
59
60 if (!placemark.address().isEmpty()) {
61 return placemark.address();
62 }
63
65 }
66 case LongitudeRole:
67 return m_request->at(index.row()).longitude(Marble::GeoDataCoordinates::Degree);
68 case LatitudeRole:
69 return m_request->at(index.row()).latitude(Marble::GeoDataCoordinates::Degree);
70 default:
71 return {};
72 }
73}
74
75Marble::Routing *RouteRequestModel::routing()
76{
77 return m_routing;
78}
79
80void RouteRequestModel::setRouting(Marble::Routing *routing)
81{
82 if (routing == m_routing) {
83 return;
84 }
85 m_routing = routing;
86 updateMap();
87 connect(m_routing, &Marble::Routing::marbleMapChanged, this, &RouteRequestModel::updateMap);
88 Q_EMIT routingChanged();
89}
90
91void RouteRequestModel::updateMap()
92{
93 if (m_routing && m_routing->marbleMap()) {
94 m_request = m_routing->marbleMap()->model()->routingManager()->routeRequest();
95
96 connect(m_request, &Marble::RouteRequest::positionChanged, this, &RouteRequestModel::updateData, Qt::UniqueConnection);
97 connect(m_request, &Marble::RouteRequest::positionAdded, this, &RouteRequestModel::updateAfterAddition, Qt::UniqueConnection);
98 connect(m_request, &Marble::RouteRequest::positionRemoved, this, &RouteRequestModel::updateAfterRemoval, Qt::UniqueConnection);
99
101 }
102}
103
104void RouteRequestModel::updateData(int idx)
105{
106 QModelIndex affected = index(idx);
107 Q_EMIT dataChanged(affected, affected);
108}
109
110void RouteRequestModel::updateAfterRemoval(int idx)
111{
112 beginRemoveRows(QModelIndex(), idx, idx);
113 removeRow(idx);
115}
116
117void RouteRequestModel::updateAfterAddition(int idx)
118{
119 beginInsertRows(QModelIndex(), idx, idx);
120 insertRow(idx);
122}
123
124void RouteRequestModel::setPosition(int index, qreal longitude, qreal latitude, const QString &name)
125{
126 if (index >= 0 && index < m_request->size()) {
127 m_request->setPosition(index, Marble::GeoDataCoordinates(longitude, latitude, 0.0, Marble::GeoDataCoordinates::Degree), name);
128 }
129}
130
131void RouteRequestModel::reverse()
132{
134 m_request->reverse();
136}
137
138#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)
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.
int size() const
Number of points in the route.
void positionAdded(int index)
An element was added at the given position.
void positionChanged(int index, const GeoDataCoordinates &position)
The value of the n-th element was changed.
void setPosition(int index, const GeoDataCoordinates &position, const QString &name=QString())
Change the value of the element at the given position.
void positionRemoved(int index)
The element at the given position was removed.
GeoDataCoordinates at(int index) const
Accessor for the n-th position.
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options) const const
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
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
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 Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.