Marble

OsmTagEditorWidget.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2015 Stanciu Marius-Valeriu <stanciumarius94@gmail.com>
4//
5
6// Self
7#include "OsmTagEditorWidget.h"
8#include "OsmTagEditorWidget_p.h"
9#include "ui_OsmTagEditorWidget.h"
10
11// Qt
12#include <QTreeWidget>
13#include <QDebug>
14
15// Marble
16#include "GeoDataPlacemark.h"
17#include "GeoDataGeometry.h"
18#include "OsmPlacemarkData.h"
19
20namespace Marble
21{
22
23OsmTagEditorWidget::OsmTagEditorWidget( GeoDataPlacemark *placemark, QWidget *parent )
24 : QWidget( parent ),
25 d( new OsmTagEditorWidgetPrivate )
26{
27 d->m_placemark = placemark;
28 d->setupUi( this );
29 d->populatePresetTagsList();
30 d->populateCurrentTagsList();
31 d->m_recommendedTagsList->setSelectionBehavior( QAbstractItemView::SelectRows );
32 d->m_recommendedTagsList->setSelectionMode( QAbstractItemView::SingleSelection );
33 d->m_recommendedTagsList->setRootIsDecorated( false );
34
35 d->m_currentTagsList->setSelectionBehavior( QAbstractItemView::SelectRows );
36 d->m_currentTagsList->setSelectionMode( QAbstractItemView::SingleSelection );
37 d->m_currentTagsList->setRootIsDecorated( false );
38
39 QObject::connect( d->m_addTagButton, SIGNAL(pressed()),
40 this, SLOT(addSelectedTag()) );
41 QObject::connect( d->m_recommendedTagsList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
42 this, SLOT(addSelectedTag()) );
43 QObject::connect( d->m_removeTagButton, SIGNAL(pressed()),
44 this, SLOT(removeSelectedTag()) );
45 QObject::connect( d->m_currentTagsList, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
46 this, SLOT(handleItemChanged(QTreeWidgetItem*,int)) );
47 QObject::connect( d->m_currentTagsList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
48 this, SLOT(handleDoubleClick(QTreeWidgetItem*,int)) );
49}
50
51OsmTagEditorWidget::~OsmTagEditorWidget()
52{
53 delete d;
54}
55
56
57void OsmTagEditorWidget::update()
58{
59 d->m_currentTagsList->clear();
60 d->m_recommendedTagsList->clear();
61 d->populatePresetTagsList();
62 d->populateCurrentTagsList();
63
64 emit placemarkChanged( d->m_placemark );
65}
66
67OsmPlacemarkData OsmTagEditorWidget::placemarkData() const
68{
69 OsmPlacemarkData osmData;
70
71 for (int index = 0; index < d->m_currentTagsList->topLevelItemCount(); ++index) {
72 const QTreeWidgetItem *item = d->m_currentTagsList->topLevelItem( index );
73 osmData.addTag(item->text(0), item->text(1));
74 }
75
76 return osmData;
77}
78
79void OsmTagEditorWidget::addSelectedTag()
80{
81 QTreeWidgetItem *selectedTag = d->m_recommendedTagsList->currentItem();
82
83 if ( !selectedTag ) {
84 return;
85 }
86
87 // Adding the tag to the placemark's osmData
88 QString key = selectedTag->text( 0 );
89 QString value = selectedTag->text( 1 );
90
91 // If the value is <value>, the user has to type a value for that particular key
92 if (value == QLatin1Char('<') + tr("value") + QLatin1Char('>')) {
93 int lastIndex = d->m_currentTagsList->topLevelItemCount() - 1;
94 QTreeWidgetItem *adderItem = d->m_currentTagsList->topLevelItem( lastIndex );
95 adderItem->setText( 0, key );
96 d->m_currentTagsList->editItem( adderItem, 1 );
97 d->m_currentTagsList->setCurrentItem( adderItem );
98 }
99 else {
100 d->m_placemark->osmData().addTag( key, value );
101
102 QTreeWidgetItem *newItem = d->tagWidgetItem( OsmTagEditorWidgetPrivate::OsmTag( key, value ) );
103 newItem->setFlags( newItem->flags() | Qt::ItemIsUserCheckable );
104 newItem->setCheckState( 0, Qt::Unchecked );
105 d->m_currentTagsList->addTopLevelItem( newItem );
106 update();
107 }
108
109}
110
111void OsmTagEditorWidget::removeSelectedTag()
112{
113 QTreeWidgetItem *selectedTag = d->m_currentTagsList->currentItem();
114
115 if ( !selectedTag ) {
116 return;
117 }
118
119 // Adding the tag to the placemark's osmData
120 QString key = selectedTag->text( 0 );
121 d->m_placemark->osmData().removeTag( key );
122
123 update();
124}
125
126void OsmTagEditorWidget::handleItemChanged( QTreeWidgetItem *item, int column )
127{
128 Q_UNUSED( column );
129 QString key = item->text( 0 );
130 QString value = item->text( 1 );
131
132 // If any of the fields is still empty ( or the first field is "Add custom tag..."
133 // the editing is not yet finished.
134 if ( key.isEmpty() || value.isEmpty() || key == d->m_customTagAdderText ) {
135 return;
136 }
137
138 d->m_placemark->osmData().addTag( key, value );
139
140 update();
141}
142
143void OsmTagEditorWidget::handleDoubleClick( QTreeWidgetItem *item, int column )
144{
145 Q_UNUSED( column );
146 int index = d->m_currentTagsList->indexOfTopLevelItem( item );
147 int lastIndex = d->m_currentTagsList->topLevelItemCount() - 1;
148
149 // The user double-clicked on the "Add custom tag..." element, so the text is cleared
150 if ( index == lastIndex ) {
151 QString key = item->text( 0 );
152
153 if ( key == d->m_customTagAdderText ) {
154 item->setText( 0, QString() );
155 }
156 }
157 // The user double-clicked on a valid tag, so the tag is removed
158 else if ( !item->isDisabled() ) {
159 d->m_placemark->osmData().removeTag( item->text( 0 ) );
160 update();
161 }
162
163}
164
165}
166
167#include "moc_OsmTagEditorWidget.cpp"
void update(Part *part, const QByteArray &data, qint64 dataSize)
Binds a QML item to a specific geodetic location in screen coordinates.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
Unchecked
ItemIsUserCheckable
Qt::ItemFlags flags() const const
bool isDisabled() const const
void setCheckState(int column, Qt::CheckState state)
void setFlags(Qt::ItemFlags flags)
void setText(int column, const QString &text)
QString text(int column) const const
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.