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