QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 QgsDataItemGuiProviderUtils::deleteConnections( vtConnectionItems, []( const QString &connectionName ) { QgsVectorTileProviderConnection::deleteConnection( connectionName ); }, context );
47 } );
48 menu->addAction( actionDelete );
49 }
50
51 if ( QgsVectorTileRootItem *rootItem = qobject_cast<QgsVectorTileRootItem *>( item ) )
52 {
53 QAction *actionNew = new QAction( tr( "New Generic Connection…" ), menu );
54 connect( actionNew, &QAction::triggered, this, [rootItem] { newConnection( rootItem ); } );
55 menu->addAction( actionNew );
56
57 QAction *actionNewArcGISConnection = new QAction( tr( "New ArcGIS Vector Tile Service Connection…" ), menu );
58 connect( actionNewArcGISConnection, &QAction::triggered, this, [rootItem] { newArcGISConnection( rootItem ); } );
59 menu->addAction( actionNewArcGISConnection );
60
61 menu->addSeparator();
62
63 QAction *actionSaveXyzTilesServers = new QAction( tr( "Save Connections…" ), menu );
64 connect( actionSaveXyzTilesServers, &QAction::triggered, this, [] { saveXyzTilesServers(); } );
65 menu->addAction( actionSaveXyzTilesServers );
66
67 QAction *actionLoadXyzTilesServers = new QAction( tr( "Load Connections…" ), menu );
68 connect( actionLoadXyzTilesServers, &QAction::triggered, this, [rootItem] { loadXyzTilesServers( rootItem ); } );
69 menu->addAction( actionLoadXyzTilesServers );
70 }
71}
72
73void QgsVectorTileDataItemGuiProvider::editConnection( QgsDataItem *item )
74{
75 const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( item->name() );
76 const QString uri = QgsVectorTileProviderConnection::encodedUri( connection );
77
78 switch ( connection.serviceType )
79 {
80 case QgsVectorTileProviderConnection::Generic:
81 {
82 QgsVectorTileConnectionDialog dlg;
83
84 dlg.setConnection( item->name(), uri );
85 if ( !dlg.exec() )
86 return;
87
88 QgsVectorTileProviderConnection::deleteConnection( item->name() );
89 const QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
90 QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
91 break;
92 }
93
94 case QgsVectorTileProviderConnection::ArcgisVectorTileService:
95 {
96 QgsArcgisVectorTileConnectionDialog dlg;
97
98 dlg.setConnection( item->name(), uri );
99 if ( !dlg.exec() )
100 return;
101
102 QgsVectorTileProviderConnection::deleteConnection( item->name() );
103 const QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
104 QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
105 break;
106 }
107 }
108
109 item->parent()->refreshConnections();
110}
111
112void QgsVectorTileDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
113{
114 const QString connectionName = item->name();
115 const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( connectionName );
116 const QStringList connections = QgsVectorTileProviderConnection::sTreeConnectionVectorTile->items();
117
118 const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );
119
120 QgsVectorTileProviderConnection::addConnection( newConnectionName, connection );
121 item->parent()->refreshConnections();
122}
123
124void QgsVectorTileDataItemGuiProvider::newConnection( QgsDataItem *item )
125{
126 QgsVectorTileConnectionDialog dlg;
127 if ( !dlg.exec() )
128 return;
129
130 const QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
131 QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
132
133 item->refreshConnections();
134}
135
136void QgsVectorTileDataItemGuiProvider::newArcGISConnection( QgsDataItem *item )
137{
138 QgsArcgisVectorTileConnectionDialog dlg;
139 if ( !dlg.exec() )
140 return;
141
142 const QgsVectorTileProviderConnection::Data conn = QgsVectorTileProviderConnection::decodedUri( dlg.connectionUri() );
143 QgsVectorTileProviderConnection::addConnection( dlg.connectionName(), conn );
144
145 item->refreshConnections();
146}
147
148void QgsVectorTileDataItemGuiProvider::saveXyzTilesServers()
149{
151 dlg.exec();
152}
153
154void QgsVectorTileDataItemGuiProvider::loadXyzTilesServers( QgsDataItem *item )
155{
156 const QString fileName = QFileDialog::getOpenFileName( nullptr, tr( "Load Connections" ), QDir::homePath(), tr( "XML files (*.xml *.XML)" ) );
157 if ( fileName.isEmpty() )
158 {
159 return;
160 }
161
163 if ( dlg.exec() == QDialog::Accepted )
164 item->refreshConnections();
165}
166
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.