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 <QDebug>
13#include <QTreeWidget>
14
15// Marble
16#include "GeoDataGeometry.h"
17#include "GeoDataPlacemark.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 connect(d->m_addTagButton, &QAbstractButton::pressed, this, &OsmTagEditorWidget::addSelectedTag);
40 connect(d->m_recommendedTagsList, &QTreeWidget::itemDoubleClicked, this, &OsmTagEditorWidget::addSelectedTag);
41 connect(d->m_removeTagButton, &QAbstractButton::pressed, this, &OsmTagEditorWidget::removeSelectedTag);
42 connect(d->m_currentTagsList, &QTreeWidget::itemChanged, this, &OsmTagEditorWidget::handleItemChanged);
43 connect(d->m_currentTagsList, &QTreeWidget::itemDoubleClicked, this, &OsmTagEditorWidget::handleDoubleClick);
44}
45
46OsmTagEditorWidget::~OsmTagEditorWidget()
47{
48 delete d;
49}
50
51void OsmTagEditorWidget::update()
52{
53 d->m_currentTagsList->clear();
54 d->m_recommendedTagsList->clear();
55 d->populatePresetTagsList();
56 d->populateCurrentTagsList();
57
58 Q_EMIT placemarkChanged(d->m_placemark);
59}
60
61OsmPlacemarkData OsmTagEditorWidget::placemarkData() const
62{
63 OsmPlacemarkData osmData;
64
65 for (int index = 0; index < d->m_currentTagsList->topLevelItemCount(); ++index) {
66 const QTreeWidgetItem *item = d->m_currentTagsList->topLevelItem(index);
67 osmData.addTag(item->text(0), item->text(1));
68 }
69
70 return osmData;
71}
72
73void OsmTagEditorWidget::addSelectedTag()
74{
75 QTreeWidgetItem *selectedTag = d->m_recommendedTagsList->currentItem();
76
77 if (!selectedTag) {
78 return;
79 }
80
81 // Adding the tag to the placemark's osmData
82 QString key = selectedTag->text(0);
83 QString value = selectedTag->text(1);
84
85 // If the value is <value>, the user has to type a value for that particular key
86 if (value == QLatin1Char('<') + tr("value") + QLatin1Char('>')) {
87 int lastIndex = d->m_currentTagsList->topLevelItemCount() - 1;
88 QTreeWidgetItem *adderItem = d->m_currentTagsList->topLevelItem(lastIndex);
89 adderItem->setText(0, key);
90 d->m_currentTagsList->editItem(adderItem, 1);
91 d->m_currentTagsList->setCurrentItem(adderItem);
92 } else {
93 d->m_placemark->osmData().addTag(key, value);
94
95 QTreeWidgetItem *newItem = d->tagWidgetItem(OsmTagEditorWidgetPrivate::OsmTag(key, value));
96 newItem->setFlags(newItem->flags() | Qt::ItemIsUserCheckable);
97 newItem->setCheckState(0, Qt::Unchecked);
98 d->m_currentTagsList->addTopLevelItem(newItem);
99 update();
100 }
101}
102
103void OsmTagEditorWidget::removeSelectedTag()
104{
105 QTreeWidgetItem *selectedTag = d->m_currentTagsList->currentItem();
106
107 if (!selectedTag) {
108 return;
109 }
110
111 // Adding the tag to the placemark's osmData
112 QString key = selectedTag->text(0);
113 d->m_placemark->osmData().removeTag(key);
114
115 update();
116}
117
118void OsmTagEditorWidget::handleItemChanged(QTreeWidgetItem *item, int column)
119{
120 Q_UNUSED(column);
121 QString key = item->text(0);
122 QString value = item->text(1);
123
124 // If any of the fields is still empty ( or the first field is "Add custom tag..."
125 // the editing is not yet finished.
126 if (key.isEmpty() || value.isEmpty() || key == d->m_customTagAdderText) {
127 return;
128 }
129
130 d->m_placemark->osmData().addTag(key, value);
131
132 update();
133}
134
135void OsmTagEditorWidget::handleDoubleClick(QTreeWidgetItem *item, int column)
136{
137 Q_UNUSED(column);
138 int index = d->m_currentTagsList->indexOfTopLevelItem(item);
139 int lastIndex = d->m_currentTagsList->topLevelItemCount() - 1;
140
141 // The user double-clicked on the "Add custom tag..." element, so the text is cleared
142 if (index == lastIndex) {
143 QString key = item->text(0);
144
145 if (key == d->m_customTagAdderText) {
146 item->setText(0, QString());
147 }
148 }
149 // The user double-clicked on a valid tag, so the tag is removed
150 else if (!item->isDisabled()) {
151 d->m_placemark->osmData().removeTag(item->text(0));
152 update();
153 }
154}
155
156}
157
158#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.
bool isEmpty() const const
Unchecked
ItemIsUserCheckable
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void itemChanged(QTreeWidgetItem *item, int column)
void itemDoubleClicked(QTreeWidgetItem *item, int column)
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 Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.