File indexing completed on 2024-05-05 03:49:19

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include "RouteRequestModel.h"
0007 
0008 #include "routing/RoutingManager.h"
0009 #include "routing/RouteRequest.h"
0010 #include "MarbleMap.h"
0011 #include "MarbleModel.h"
0012 #include "Routing.h"
0013 #include <GeoDataPlacemark.h>
0014 
0015 RouteRequestModel::RouteRequestModel( QObject *parent ) :
0016     QAbstractListModel( parent ),
0017     m_request( nullptr ),
0018     m_routing( nullptr )
0019 {
0020     QHash<int,QByteArray> roles;
0021     roles[Qt::DisplayRole] = "name";
0022     roles[LongitudeRole] = "longitude";
0023     roles[LatitudeRole] = "latitude";
0024     m_roleNames = roles;
0025 }
0026 
0027 RouteRequestModel::~RouteRequestModel()
0028 {
0029     // nothing to do
0030 }
0031 
0032 int RouteRequestModel::rowCount ( const QModelIndex &parent ) const
0033 {
0034     if ( !parent.isValid() && m_request ) {
0035         return m_request->size();
0036     }
0037 
0038     return 0;
0039 }
0040 
0041 QHash<int, QByteArray> RouteRequestModel::roleNames() const
0042 {
0043     return m_roleNames;
0044 }
0045 
0046 QVariant RouteRequestModel::headerData ( int section, Qt::Orientation orientation, int role ) const
0047 {
0048     if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0 ) {
0049         return QStringLiteral("Waypoint");
0050     }
0051 
0052     return QVariant();
0053 }
0054 
0055 QVariant RouteRequestModel::data ( const QModelIndex &index, int role ) const
0056 {
0057     if ( index.isValid() && m_request && index.row() >= 0 && index.row() < m_request->size() ) {
0058         switch ( role ) {
0059         case Qt::DisplayRole: {
0060             Marble::GeoDataPlacemark const & placemark = (*m_request)[index.row()];
0061             if (!placemark.name().isEmpty()) {
0062                 return placemark.name();
0063             }
0064 
0065             if (!placemark.address().isEmpty()) {
0066                 return placemark.address();
0067             }
0068 
0069             return placemark.coordinate().toString(Marble::GeoDataCoordinates::Decimal).trimmed();
0070         }
0071         case LongitudeRole: return m_request->at( index.row() ).longitude( Marble::GeoDataCoordinates::Degree );
0072         case LatitudeRole: return m_request->at( index.row() ).latitude( Marble::GeoDataCoordinates::Degree );
0073         }
0074     }
0075 
0076     return QVariant();
0077 }
0078 
0079 Marble::Routing *RouteRequestModel::routing()
0080 {
0081     return m_routing;
0082 }
0083 
0084 void RouteRequestModel::setRouting( Marble::Routing *routing )
0085 {
0086     if ( routing != m_routing ) {
0087         m_routing = routing;
0088         updateMap();
0089         connect( m_routing, SIGNAL(marbleMapChanged()), this, SLOT(updateMap()) );
0090         emit routingChanged();
0091     }
0092 }
0093 
0094 void RouteRequestModel::updateMap()
0095 {
0096     if ( m_routing && m_routing->marbleMap() ) {
0097         m_request = m_routing->marbleMap()->model()->routingManager()->routeRequest();
0098 
0099         connect( m_request, SIGNAL(positionChanged(int,GeoDataCoordinates)),
0100                  this, SLOT(updateData(int)),  Qt::UniqueConnection );
0101         connect( m_request, SIGNAL(positionAdded(int)),
0102                  this, SLOT(updateAfterAddition(int)),  Qt::UniqueConnection );
0103         connect( m_request, SIGNAL(positionRemoved(int)),
0104                  this, SLOT(updateAfterRemoval(int)),  Qt::UniqueConnection );
0105 
0106         emit layoutChanged();
0107     }
0108 }
0109 
0110 void RouteRequestModel::updateData( int idx )
0111 {
0112     QModelIndex affected = index( idx );
0113     emit dataChanged( affected, affected );
0114 }
0115 
0116 void RouteRequestModel::updateAfterRemoval( int idx )
0117 {
0118     beginRemoveRows( QModelIndex(), idx, idx );
0119     removeRow( idx );
0120     endRemoveRows();
0121     emit rowCountChanged();
0122 }
0123 
0124 void RouteRequestModel::updateAfterAddition( int idx )
0125 {
0126     beginInsertRows( QModelIndex(), idx, idx );
0127     insertRow( idx );
0128     endInsertRows();
0129     emit rowCountChanged();
0130 }
0131 
0132 void RouteRequestModel::setPosition ( int index, qreal longitude, qreal latitude )
0133 {
0134     if ( index >= 0 && index < m_request->size() ) {
0135         m_request->setPosition( index, Marble::GeoDataCoordinates( longitude, latitude, 0.0, Marble::GeoDataCoordinates::Degree ) );
0136     }
0137 }
0138 
0139 #include "moc_RouteRequestModel.cpp"