File indexing completed on 2025-01-05 03:59:23

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