Marble

OsmRelationManagerWidget.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 "OsmRelationManagerWidget.h"
8#include "OsmRelationManagerWidget_p.h"
9#include "ui_OsmRelationManagerWidget.h"
10
11// Qt
12#include <QTreeWidget>
13#include <QMenu>
14#include <QPointer>
15#include <QDebug>
16
17// Marble
18#include "GeoDataPlacemark.h"
19#include "GeoDataGeometry.h"
20#include "osm/OsmPlacemarkData.h"
21#include "osm/OsmObjectManager.h"
22#include "osm/OsmRelationEditorDialog.h"
23
24namespace Marble
25{
26
27OsmRelationManagerWidget::OsmRelationManagerWidget( GeoDataPlacemark *placemark,
28 const QHash<qint64, OsmPlacemarkData> *relations,
29 QWidget *parent )
30 : QWidget( parent ),
31 d( new OsmRelationManagerWidgetPrivate )
32{
33 d->m_placemark = placemark;
34 d->m_allRelations = relations;
35 d->setupUi( this );
36 d->populateRelationsList();
37 d->m_relationDropMenu = new QMenu( d->m_addRelation );
38 d->m_currentRelations->setRootIsDecorated( false );
39 d->m_currentRelations->setEditTriggers( QTreeWidget::DoubleClicked );
40 d->m_currentRelations->setContextMenuPolicy( Qt::CustomContextMenu );
41 d->m_currentRelations->setMinimumWidth( d->m_currentRelations->columnCount() * d->m_currentRelations->columnWidth( 0 ) + 10 );
42
43 d->m_addRelation->setMenu( d->m_relationDropMenu );
44 d->populateDropMenu();
45
46 QObject::connect( d->m_currentRelations, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
47 this, SLOT(handleDoubleClick(QTreeWidgetItem*,int)) );
48 QObject::connect( d->m_currentRelations, SIGNAL(customContextMenuRequested(QPoint)),
49 this, SLOT(handleRelationContextMenuRequest(QPoint)) );
50 QObject::connect( d->m_relationDropMenu, SIGNAL(triggered(QAction*)),
51 this, SLOT(addRelation(QAction*)) );
52 QObject::connect( d->m_currentRelations, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
53 this, SLOT(handleItemChange(QTreeWidgetItem*,int)) );
54}
55
56OsmRelationManagerWidget::~OsmRelationManagerWidget()
57{
58 delete d;
59}
60
61void OsmRelationManagerWidget::addRelation( QAction *relationAction )
62{
63 // The QAction::text() adds a '&' for some reason
64 QString relationText = relationAction->text().remove(QLatin1Char('&'));
65
66 if ( relationText == tr( "New Relation" ) ) {
67 OsmPlacemarkData relationData;
68 QPointer<OsmRelationEditorDialog> relationEditor = new OsmRelationEditorDialog( &relationData );
69 const int result = relationEditor->exec();
70 delete relationEditor;
71 if (result == QDialog::Rejected) {
72 return;
73 }
74
75 QTreeWidgetItem *newRelationItem = new QTreeWidgetItem();
76 newRelationItem->setText(Column::Name, relationData.tagValue(QStringLiteral("name")));
77 newRelationItem->setText(Column::Type, relationData.tagValue(QStringLiteral("type")));
78 newRelationItem->setData( Column::Name, Qt::UserRole, relationData.id() );
79 d->m_currentRelations->addTopLevelItem( newRelationItem );
80
81 // Make the user complete the role column
82 newRelationItem->setFlags( newRelationItem->flags() | Qt::ItemIsEditable );
83 d->m_currentRelations->editItem( newRelationItem, Column::Role );
84
85 // This tells the annotate plugin to add the new relation to its list
86 emit relationCreated( relationData );
87 }
88 else {
89 qint64 id = relationAction->data().toLongLong();
90 OsmPlacemarkData relationData = d->m_allRelations->value( id );
91 QTreeWidgetItem *newRelationItem = new QTreeWidgetItem();
92 newRelationItem->setText(Column::Name, relationData.tagValue(QStringLiteral("name")));
93 newRelationItem->setText(Column::Type, relationData.tagValue(QStringLiteral("type")));
94 newRelationItem->setData( Column::Name, Qt::UserRole, relationData.id() );
95 d->m_currentRelations->addTopLevelItem( newRelationItem );
96
97 // Make the user complete the role column
98 newRelationItem->setFlags( newRelationItem->flags() | Qt::ItemIsEditable );
99 d->m_currentRelations->editItem( newRelationItem, Column::Role );
100 }
101}
102
103void OsmRelationManagerWidget::update()
104{
105 d->populateRelationsList();
106 d->populateDropMenu();
107}
108
109void OsmRelationManagerWidget::handleDoubleClick( QTreeWidgetItem * item, int column )
110{
111 Qt::ItemFlags flags = item->flags();
112
113 // Only the "role" column should be editable
114 if ( column == Column::Role ) {
115 item->setFlags( flags | Qt::ItemIsEditable );
116 // If the double click didn't occur on the "role" column, and the item
117 // is editable make it uneditable
118 } else if ( flags & Qt::ItemIsEditable ) {
119 item->setFlags( flags ^ Qt::ItemIsEditable );
120 }
121}
122
123void OsmRelationManagerWidget::handleItemChange( QTreeWidgetItem *item, int column )
124{
125 // Only the role column should be editable
126 if ( column != Column::Role ) {
127 return;
128 }
129
130 QString role = item->text( Column::Role );
131 qint64 id = item->data( Column::Name, Qt::UserRole ).toLongLong();
132
133 d->m_placemark->osmData().addRelation( id, OsmType::Way, role );
134 update();
135}
136
137void OsmRelationManagerWidget::handleRelationContextMenuRequest( const QPoint& point )
138{
139 QMenu relationEditMenu;
140 relationEditMenu.addAction( tr( "Remove" ) );
141 relationEditMenu.addAction( tr( "Edit" ) );
142 QAction* selectedItem = relationEditMenu.exec( d->m_currentRelations->mapToGlobal( point ) );
143 if ( selectedItem ) {
144
145 QTreeWidgetItem *requestedItem = d->m_currentRelations->itemAt( point );
146 qint64 id = requestedItem->data( Column::Name, Qt::UserRole ).toLongLong();
147
148 if ( selectedItem->text() == tr( "Remove" ) ) {
149 d->m_placemark->osmData().removeRelation( id );
150 update();
151 }
152 else if ( selectedItem->text() == tr( "Edit" ) ) {
153 OsmPlacemarkData relationData = d->m_allRelations->value( id );
154 QPointer<OsmRelationEditorDialog> relationEditor = new OsmRelationEditorDialog( &relationData );
155 const int result = relationEditor->exec();
156 delete relationEditor;
157 if (result == QDialog::Rejected) {
158 return;
159 }
160
161 emit relationCreated( relationData );
162 update();
163 }
164 }
165}
166
167}
168
169#include "moc_OsmRelationManagerWidget.cpp"
This class is used to encapsulate the osm data fields kept within a placemark's extendedData.
QString tagValue(const QString &key) const
tagValue returns the value of the tag that has key as key or an empty qstring if there is no such tag
The OsmRelationEditorDialog class small dialog that is used to edit relations The dialog has a line e...
void update(Part *part, const QByteArray &data, qint64 dataSize)
Binds a QML item to a specific geodetic location in screen coordinates.
QVariant data() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * exec()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
CustomContextMenu
UserRole
ItemIsEditable
virtual QVariant data(int column, int role) const const
Qt::ItemFlags flags() const const
virtual void setData(int column, int role, const QVariant &value)
void setFlags(Qt::ItemFlags flags)
void setText(int column, const QString &text)
QString text(int column) const const
qlonglong toLongLong(bool *ok) const const
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.