File indexing completed on 2024-05-12 15:29:53

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2015 Stanciu Marius-Valeriu <stanciumarius94@gmail.com>
0004 //
0005 
0006 // Self
0007 #include "OsmRelationManagerWidget_p.h"
0008 #include "OsmRelationManagerWidget.h"
0009 
0010 // Marble
0011 #include "GeoDataPlacemark.h"
0012 #include "GeoDataStyle.h"
0013 #include "OsmPlacemarkData.h"
0014 #include "MarbleDebug.h"
0015 
0016 // Qt
0017 #include <QTreeWidget>
0018 #include <QMenu>
0019 
0020 namespace Marble
0021 {
0022 
0023 OsmRelationManagerWidgetPrivate::OsmRelationManagerWidgetPrivate()
0024 {
0025     // nothing to do
0026 }
0027 
0028 OsmRelationManagerWidgetPrivate::~OsmRelationManagerWidgetPrivate()
0029 {
0030     // nothing to do
0031 }
0032 
0033 void OsmRelationManagerWidgetPrivate::populateRelationsList()
0034 {
0035     m_currentRelations->clear();
0036 
0037     // This shouldn't happen
0038     if ( !m_allRelations ) {
0039         return;
0040     }
0041 
0042     if ( m_placemark->hasOsmData() ) {
0043         const OsmPlacemarkData &osmData = m_placemark->osmData();
0044         auto it = osmData.relationReferencesBegin();
0045         const auto end = osmData.relationReferencesEnd();
0046 
0047         for ( ; it != end; ++it ) {
0048 
0049             if ( !m_allRelations->contains( it.key().id ) ) {
0050                 mDebug()<< QString( "Relation %1 is not loaded in the Annotate Plugin" ).arg( it.key().id );
0051                 continue;
0052             }
0053 
0054             const OsmPlacemarkData &relationData = m_allRelations->value( it.key().id );
0055 
0056             QTreeWidgetItem *newItem = new QTreeWidgetItem();
0057             QString name = relationData.tagValue(QStringLiteral("name"));
0058             QString type = relationData.tagValue(QStringLiteral("type"));
0059             QString role = it.value();
0060             newItem->setText( Column::Name, name );
0061             newItem->setText( Column::Type, type );
0062             newItem->setText( Column::Role, role );
0063             newItem->setData( Column::Name, Qt::UserRole, relationData.id() );
0064             m_currentRelations->addTopLevelItem( newItem );
0065 
0066         }
0067     }
0068 }
0069 
0070 void OsmRelationManagerWidgetPrivate::populateDropMenu()
0071 {
0072     m_relationDropMenu->clear();
0073 
0074     m_addRelation->setIcon(QIcon(QStringLiteral(":marble/list-add.png")));
0075 
0076     // The new relation adder
0077     m_relationDropMenu->addAction( QObject::tr( "New Relation" ) );
0078     m_relationDropMenu->addSeparator();
0079 
0080     // This shouldn't happen
0081     Q_ASSERT( m_allRelations );
0082 
0083     // Suggesting existing relations
0084     for ( const OsmPlacemarkData &relationData: m_allRelations->values() ) {
0085         const QString relationText = relationData.tagValue("name") + QLatin1String(" (") + relationData.tagValue("type") + QLatin1Char(')');
0086 
0087         // Don't suggest relations the placemark is already part of
0088         if ( m_placemark->hasOsmData() && m_placemark->osmData().containsRelation( relationData.id() ) ) {
0089             continue;
0090         }
0091         QAction *newAction = new QAction( m_relationDropMenu );
0092         newAction->setText( relationText );
0093         newAction->setData( relationData.id() );
0094         m_relationDropMenu->addAction( newAction );
0095     }
0096 }
0097 
0098 }