QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsvectortilesourceselect.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectortilesourceselect.cpp
3 ---------------------------------
4 begin : April 2020
5 copyright : (C) 2020 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgshelp.h"
19#include "qgsgui.h"
22#include "moc_qgsvectortilesourceselect.cpp"
26#include "qgsprovidermetadata.h"
27#include "qgsproviderutils.h"
28
29#include <QFileDialog>
30#include <QMessageBox>
31#include <QPushButton>
32#include <QMenu>
33#include <QAction>
34
36
37QgsVectorTileSourceSelect::QgsVectorTileSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
38 : QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
39{
40 setupUi( this );
41
43
44 setWindowTitle( tr( "Add Vector Tile Layer" ) );
45
46 mRadioSourceService->setChecked( true );
47 mStackedWidget->setCurrentIndex( 1 );
48
49 connect( mRadioSourceFile, &QRadioButton::toggled, this, [this] {
50 mStackedWidget->setCurrentIndex( 0 );
51
52 emit enableButtons( !mFileWidget->filePath().isEmpty() );
53 } );
54 connect( mRadioSourceService, &QRadioButton::toggled, this, [this] {
55 mStackedWidget->setCurrentIndex( 1 );
56
57 emit enableButtons( !cmbConnections->currentText().isEmpty() );
58 } );
59
60 btnNew->setPopupMode( QToolButton::InstantPopup );
61 QMenu *newMenu = new QMenu( btnNew );
62
63 QAction *actionNew = new QAction( tr( "New Generic Connection…" ), this );
64 connect( actionNew, &QAction::triggered, this, &QgsVectorTileSourceSelect::btnNew_clicked );
65 newMenu->addAction( actionNew );
66
67 QAction *actionNewArcGISConnection = new QAction( tr( "New ArcGIS Vector Tile Service Connection…" ), this );
68 connect( actionNewArcGISConnection, &QAction::triggered, this, &QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection );
69 newMenu->addAction( actionNewArcGISConnection );
70
71 btnNew->setMenu( newMenu );
72
73 connect( btnEdit, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnEdit_clicked );
74 connect( btnDelete, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnDelete_clicked );
75 connect( btnSave, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnSave_clicked );
76 connect( btnLoad, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnLoad_clicked );
77 connect( cmbConnections, &QComboBox::currentTextChanged, this, &QgsVectorTileSourceSelect::cmbConnections_currentTextChanged );
78 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorTileSourceSelect::showHelp );
79 setupButtons( buttonBox );
80
81 populateConnectionList();
82
83 mFileWidget->setDialogTitle( tr( "Open Vector Tile Dataset" ) );
84 mFileWidget->setFilter( QgsProviderRegistry::instance()->fileVectorTileFilters() );
85 mFileWidget->setStorageMode( QgsFileWidget::GetFile );
86 mFileWidget->setOptions( QFileDialog::HideNameFilterDetails );
87 connect( mFileWidget, &QgsFileWidget::fileChanged, this, [=]( const QString &path ) {
88 emit enableButtons( !path.isEmpty() );
89 } );
90}
91
92void QgsVectorTileSourceSelect::btnNew_clicked()
93{
94 QgsVectorTileConnectionDialog nc( this );
95 if ( nc.exec() )
96 {
97 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
98 populateConnectionList();
99 QgsVectorTileProviderConnection::setSelectedConnection( nc.connectionName() );
100 setConnectionListPosition();
101 emit connectionsChanged();
102 }
103}
104
105void QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection()
106{
107 QgsArcgisVectorTileConnectionDialog nc( this );
108 if ( nc.exec() )
109 {
110 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
111 populateConnectionList();
112 emit connectionsChanged();
113 }
114}
115
116void QgsVectorTileSourceSelect::btnEdit_clicked()
117{
118 const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( cmbConnections->currentText() );
119 const QString uri = QgsVectorTileProviderConnection::encodedUri( connection );
120
121 switch ( connection.serviceType )
122 {
123 case QgsVectorTileProviderConnection::Generic:
124 {
125 QgsVectorTileConnectionDialog nc( this );
126 nc.setConnection( cmbConnections->currentText(), uri );
127 if ( nc.exec() )
128 {
129 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
130 populateConnectionList();
131 emit connectionsChanged();
132 }
133 break;
134 }
135
136 case QgsVectorTileProviderConnection::ArcgisVectorTileService:
137 {
138 QgsArcgisVectorTileConnectionDialog nc( this );
139
140 nc.setConnection( cmbConnections->currentText(), uri );
141 if ( nc.exec() )
142 {
143 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
144 populateConnectionList();
145 emit connectionsChanged();
146 }
147 break;
148 }
149 }
150}
151
152void QgsVectorTileSourceSelect::btnDelete_clicked()
153{
154 const QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
155 .arg( cmbConnections->currentText() );
156 if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No ) )
157 return;
158
159 QgsVectorTileProviderConnection::deleteConnection( cmbConnections->currentText() );
160
161 populateConnectionList();
162 emit connectionsChanged();
163}
164
165void QgsVectorTileSourceSelect::btnSave_clicked()
166{
168 dlg.exec();
169}
170
171void QgsVectorTileSourceSelect::btnLoad_clicked()
172{
173 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(), tr( "XML files (*.xml *.XML)" ) );
174 if ( fileName.isEmpty() )
175 {
176 return;
177 }
178
180 dlg.exec();
181 populateConnectionList();
182}
183
184void QgsVectorTileSourceSelect::addButtonClicked()
185{
186 if ( mRadioSourceService->isChecked() )
187 {
188 const QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( cmbConnections->currentText() ) );
190 emit addVectorTileLayer( uri, cmbConnections->currentText() );
192 emit addLayer( Qgis::LayerType::VectorTile, uri, cmbConnections->currentText(), QString() );
193 }
194 else if ( mRadioSourceFile->isChecked() )
195 {
196 const QString filePath = mFileWidget->filePath();
197 const QList<QgsProviderRegistry::ProviderCandidateDetails> providers = QgsProviderRegistry::instance()->preferredProvidersForUri( filePath );
198 QString providerKey;
199 for ( const QgsProviderRegistry::ProviderCandidateDetails &details : providers )
200 {
201 if ( details.layerTypes().contains( Qgis::LayerType::VectorTile ) )
202 {
203 providerKey = details.metadata()->key();
204 }
205 }
206
207 QVariantMap parts;
208 parts.insert( QStringLiteral( "path" ), filePath );
209 const QString uri = QgsProviderRegistry::instance()->encodeUri( providerKey, parts );
210
212 emit addVectorTileLayer( uri, QgsProviderUtils::suggestLayerNameFromFilePath( filePath ) );
214 emit addLayer( Qgis::LayerType::VectorTile, uri, cmbConnections->currentText(), QString() );
215 }
216}
217
218void QgsVectorTileSourceSelect::populateConnectionList()
219{
220 cmbConnections->blockSignals( true );
221 cmbConnections->clear();
222 cmbConnections->addItems( QgsVectorTileProviderConnection::connectionList() );
223 cmbConnections->blockSignals( false );
224
225 btnEdit->setDisabled( cmbConnections->count() == 0 );
226 btnDelete->setDisabled( cmbConnections->count() == 0 );
227 btnSave->setDisabled( cmbConnections->count() == 0 );
228 cmbConnections->setDisabled( cmbConnections->count() == 0 );
229
230 setConnectionListPosition();
231}
232
233void QgsVectorTileSourceSelect::setConnectionListPosition()
234{
235 const QString toSelect = QgsVectorTileProviderConnection::selectedConnection();
236
237 cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
238
239 if ( cmbConnections->currentIndex() < 0 )
240 {
241 if ( toSelect.isNull() )
242 cmbConnections->setCurrentIndex( 0 );
243 else
244 cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
245 }
246
247 emit enableButtons( !cmbConnections->currentText().isEmpty() );
248}
249
250void QgsVectorTileSourceSelect::cmbConnections_currentTextChanged( const QString &text )
251{
252 QgsVectorTileProviderConnection::setSelectedConnection( text );
253 emit enableButtons( !text.isEmpty() );
254}
255
256void QgsVectorTileSourceSelect::showHelp()
257{
258 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#using-vector-tiles-services" ) );
259}
260
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Abstract base Data Source Widget to create connections and add layers This class provides common func...
@ GetFile
Select a single file.
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:210
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
Contains information pertaining to a candidate provider.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
WidgetMode
Different ways a source select dialog can be used.
QList< QgsProviderRegistry::ProviderCandidateDetails > preferredProvidersForUri(const QString &uri) const
Returns the details for the preferred provider(s) for opening the specified uri.
QString encodeUri(const QString &providerKey, const QVariantMap &parts)
Reassembles a provider data source URI from its component paths (e.g.
static QString suggestLayerNameFromFilePath(const QString &path)
Suggests a suitable layer name given only a file path.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6643
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6642