QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsvectortiledataitemguiprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectortiledataitemguiprovider.cpp
3 --------------------------------------
4 Date : March 2020
5 Copyright : (C) 2020 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsvectortiledataitemguiprovider.cpp"
18
25
26#include <QFileDialog>
27#include <QMessageBox>
28
30
31void QgsVectorTileDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &selection, QgsDataItemGuiContext context )
32{
33 if ( QgsVectorTileLayerItem *layerItem = qobject_cast< QgsVectorTileLayerItem * >( item ) )
34 {
35 QAction *actionEdit = new QAction( tr( "Edit Connection…" ), menu );
36 connect( actionEdit, &QAction::triggered, this, [layerItem] { editConnection( layerItem ); } );
37 menu->addAction( actionEdit );
38
39 QAction *actionDuplicate = new QAction( tr( "Duplicate Connection" ), menu );
40 connect( actionDuplicate, &QAction::triggered, this, [layerItem] { duplicateConnection( layerItem ); } );
41 menu->addAction( actionDuplicate );
42
43 const QList< QgsVectorTileLayerItem * > vtConnectionItems = QgsDataItem::filteredItems<QgsVectorTileLayerItem>( selection );
44 QAction *actionDelete = new QAction( vtConnectionItems.size() > 1 ? tr( "Remove Connections…" ) : tr( "Remove Connection…" ), menu );
45 connect( actionDelete, &QAction::triggered, this, [vtConnectionItems, context]
46 {
47 QgsDataItemGuiProviderUtils::deleteConnections( vtConnectionItems, []( const QString & connectionName )
48 {
49 QgsVectorTileProviderConnection::deleteConnection( connectionName );
50 }, context );
51 } );
52 menu->addAction( actionDelete );
53 }
54
55 if ( QgsVectorTileRootItem *rootItem = qobject_cast< QgsVectorTileRootItem * >( item ) )
56 {
57 QAction *actionNew = new QAction( tr( "New Generic Connection…" ), menu );
58 connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
59 menu->addAction( actionNew );
60
61 QAction *actionNewArcGISConnection = new QAction( tr( "New ArcGIS Vector Tile Service Connection…" ), menu );
62 connect( actionNewArcGISConnection, &QAction::triggered, this, [rootItem] { newArcGISConnection( rootItem ); } );
63 menu->addAction( actionNewArcGISConnection );
64
65 menu->addSeparator();
66
67 QAction *actionSaveXyzTilesServers = new QAction( tr( "Save Connections…" ), menu );
68 connect( actionSaveXyzTilesServers, &QAction::triggered, this, [] { saveXyzTilesServers(); } );
69 menu->addAction( actionSaveXyzTilesServers );
70
71 QAction *actionLoadXyzTilesServers = new QAction( tr( "Load Connections…" ), menu );
72 connect( actionLoadXyzTilesServers, &QAction::triggered, this, [rootItem] { loadXyzTilesServers( rootItem ); } );
73 menu->addAction( actionLoadXyzTilesServers );
74 }
75}
76
77void QgsVectorTileDataItemGuiProvider::editConnection( QgsDataItem *item )
78{
79 const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( item->name() );
80 const QString uri = QgsVectorTileProviderConnection::encodedUri( connection );
81
82 switch ( connection.serviceType )
83 {
84 case QgsVectorTileProviderConnection::Generic:
85 {
86 QgsVectorTileConnectionDialog dlg;
87
88 dlg.setConnection( item->name(), uri );
89 if ( !dlg.exec() )
90 return;
91
92 QgsVectorTileProviderConnection::deleteConnection( item->name() );
93 const QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
94 QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
95 break;
96 }
97
98 case QgsVectorTileProviderConnection::ArcgisVectorTileService:
99 {
100 QgsArcgisVectorTileConnectionDialog dlg;
101
102 dlg.setConnection( item->name(), uri );
103 if ( !dlg.exec() )
104 return;
105
106 QgsVectorTileProviderConnection::deleteConnection( item->name() );
107 const QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
108 QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
109 break;
110 }
111 }
112
113 item->parent()->refreshConnections();
114}
115
116void QgsVectorTileDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
117{
118 const QString connectionName = item->name();
119 const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( connectionName );
120 const QStringList connections = QgsVectorTileProviderConnection::sTreeConnectionVectorTile->items();
121
122 const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );
123
124 QgsVectorTileProviderConnection::addConnection( newConnectionName, connection );
125 item->parent()->refreshConnections();
126}
127
128void QgsVectorTileDataItemGuiProvider::newConnection( QgsDataItem *item )
129{
130 QgsVectorTileConnectionDialog dlg;
131 if ( !dlg.exec() )
132 return;
133
134 const QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
135 QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
136
137 item->refreshConnections();
138}
139
140void QgsVectorTileDataItemGuiProvider::newArcGISConnection( QgsDataItem *item )
141{
142 QgsArcgisVectorTileConnectionDialog dlg;
143 if ( !dlg.exec() )
144 return;
145
146 const QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
147 QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
148
149 item->refreshConnections();
150}
151
152void QgsVectorTileDataItemGuiProvider::saveXyzTilesServers()
153{
155 dlg.exec();
156}
157
158void QgsVectorTileDataItemGuiProvider::loadXyzTilesServers( QgsDataItem *item )
159{
160 const QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(),
161 tr( "XML files (*.xml *.XML)" ) );
162 if ( fileName.isEmpty() )
163 {
164 return;
165 }
166
168 if ( dlg.exec() == QDialog::Accepted )
169 item->refreshConnections();
170}
171
Encapsulates the context in which a QgsDataItem is shown within the application GUI.
static const QString uniqueName(const QString &name, const QStringList &connectionNames)
Check if connection with name exists in connectionNames list and then try to append a number to it to...
static void deleteConnections(const QList< T * > &items, const std::function< void(const QString &) > &deleteConnection, QgsDataItemGuiContext context)
Handles deletion of a list of connection items.
Base class for all items in the model.
Definition qgsdataitem.h:46
QString name() const
Returns the name of the item (the displayed text for the item).
virtual void refreshConnections(const QString &providerKey=QString())
Causes a data item provider to refresh all registered connections.
QgsDataItem * parent() const
Gets item parent.