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 "OsmTagEditorWidget.h"
0008 #include "OsmTagEditorWidget_p.h"
0009 #include "ui_OsmTagEditorWidget.h"
0010 
0011 // Qt
0012 #include <QTreeWidget>
0013 #include <QDebug>
0014 
0015 // Marble
0016 #include "GeoDataPlacemark.h"
0017 #include "GeoDataGeometry.h"
0018 #include "OsmPlacemarkData.h"
0019 
0020 namespace Marble
0021 {
0022 
0023 OsmTagEditorWidget::OsmTagEditorWidget( GeoDataPlacemark *placemark, QWidget *parent )
0024     : QWidget( parent ),
0025       d( new OsmTagEditorWidgetPrivate )
0026 {
0027     d->m_placemark = placemark;
0028     d->setupUi( this );
0029     d->populatePresetTagsList();
0030     d->populateCurrentTagsList();
0031     d->m_recommendedTagsList->setSelectionBehavior( QAbstractItemView::SelectRows );
0032     d->m_recommendedTagsList->setSelectionMode( QAbstractItemView::SingleSelection );
0033     d->m_recommendedTagsList->setRootIsDecorated( false );
0034 
0035     d->m_currentTagsList->setSelectionBehavior( QAbstractItemView::SelectRows );
0036     d->m_currentTagsList->setSelectionMode( QAbstractItemView::SingleSelection );
0037     d->m_currentTagsList->setRootIsDecorated( false );
0038 
0039     QObject::connect( d->m_addTagButton, SIGNAL(pressed()),
0040                       this, SLOT(addSelectedTag()) );
0041     QObject::connect( d->m_recommendedTagsList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
0042                       this, SLOT(addSelectedTag()) );
0043     QObject::connect( d->m_removeTagButton, SIGNAL(pressed()),
0044                       this, SLOT(removeSelectedTag()) );
0045     QObject::connect( d->m_currentTagsList, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
0046                       this, SLOT(handleItemChanged(QTreeWidgetItem*,int)) );
0047     QObject::connect( d->m_currentTagsList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
0048                       this, SLOT(handleDoubleClick(QTreeWidgetItem*,int)) );
0049 }
0050 
0051 OsmTagEditorWidget::~OsmTagEditorWidget()
0052 {
0053     delete d;
0054 }
0055 
0056 
0057 void OsmTagEditorWidget::update()
0058 {
0059     d->m_currentTagsList->clear();
0060     d->m_recommendedTagsList->clear();
0061     d->populatePresetTagsList();
0062     d->populateCurrentTagsList();
0063 
0064     emit placemarkChanged( d->m_placemark );
0065 }
0066 
0067 OsmPlacemarkData OsmTagEditorWidget::placemarkData() const
0068 {
0069     OsmPlacemarkData osmData;
0070 
0071     for (int index = 0; index < d->m_currentTagsList->topLevelItemCount(); ++index) {
0072         const QTreeWidgetItem *item = d->m_currentTagsList->topLevelItem( index );
0073         osmData.addTag(item->text(0), item->text(1));
0074     }
0075 
0076     return osmData;
0077 }
0078 
0079 void OsmTagEditorWidget::addSelectedTag()
0080 {
0081     QTreeWidgetItem *selectedTag = d->m_recommendedTagsList->currentItem();
0082 
0083     if ( !selectedTag ) {
0084         return;
0085     }
0086 
0087     // Adding the tag to the placemark's osmData
0088     QString key = selectedTag->text( 0 );
0089     QString value = selectedTag->text( 1 );
0090 
0091     // If the value is <value>, the user has to type a value for that particular key
0092     if (value == QLatin1Char('<') + tr("value") + QLatin1Char('>')) {
0093         int lastIndex = d->m_currentTagsList->topLevelItemCount() - 1;
0094         QTreeWidgetItem *adderItem = d->m_currentTagsList->topLevelItem( lastIndex );
0095         adderItem->setText( 0, key );
0096         d->m_currentTagsList->editItem( adderItem, 1 );
0097         d->m_currentTagsList->setCurrentItem( adderItem );
0098     }
0099     else {
0100         d->m_placemark->osmData().addTag( key, value );
0101 
0102         QTreeWidgetItem *newItem = d->tagWidgetItem( OsmTagEditorWidgetPrivate::OsmTag( key, value ) );
0103         newItem->setFlags( newItem->flags() | Qt::ItemIsUserCheckable );
0104         newItem->setCheckState( 0, Qt::Unchecked );
0105         d->m_currentTagsList->addTopLevelItem( newItem );
0106         update();
0107     }
0108 
0109 }
0110 
0111 void OsmTagEditorWidget::removeSelectedTag()
0112 {
0113     QTreeWidgetItem *selectedTag = d->m_currentTagsList->currentItem();
0114 
0115     if ( !selectedTag ) {
0116         return;
0117     }
0118 
0119     // Adding the tag to the placemark's osmData
0120     QString key = selectedTag->text( 0 );
0121     d->m_placemark->osmData().removeTag( key );
0122 
0123     update();
0124 }
0125 
0126 void OsmTagEditorWidget::handleItemChanged( QTreeWidgetItem *item, int column )
0127 {
0128     Q_UNUSED( column );
0129     QString key = item->text( 0 );
0130     QString value = item->text( 1 );
0131 
0132     // If any of the fields is still empty ( or the first field is "Add custom tag..."
0133     // the editing is not yet finished.
0134     if ( key.isEmpty() || value.isEmpty() || key == d->m_customTagAdderText ) {
0135         return;
0136     }
0137 
0138     d->m_placemark->osmData().addTag( key, value );
0139 
0140     update();
0141 }
0142 
0143 void OsmTagEditorWidget::handleDoubleClick( QTreeWidgetItem *item, int column )
0144 {
0145     Q_UNUSED( column );
0146     int index = d->m_currentTagsList->indexOfTopLevelItem( item );
0147     int lastIndex = d->m_currentTagsList->topLevelItemCount() - 1;
0148 
0149     // The user double-clicked on the "Add custom tag..." element, so the text is cleared
0150     if ( index == lastIndex ) {
0151         QString key = item->text( 0 );
0152 
0153         if ( key == d->m_customTagAdderText ) {
0154             item->setText( 0, QString() );
0155         }
0156     }
0157     // The user double-clicked on a valid tag, so the tag is removed
0158     else if ( !item->isDisabled() ) {
0159         d->m_placemark->osmData().removeTag( item->text( 0 ) );
0160         update();
0161     }
0162 
0163 }
0164 
0165 }
0166 
0167 #include "moc_OsmTagEditorWidget.cpp"