QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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"
25#include "qgsprovidermetadata.h"
26#include "qgsproviderutils.h"
27
28#include <QFileDialog>
29#include <QMessageBox>
30#include <QPushButton>
31#include <QMenu>
32#include <QAction>
33
35
36QgsVectorTileSourceSelect::QgsVectorTileSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
37 : QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
38{
39 setupUi( this );
40
42
43 setWindowTitle( tr( "Add Vector Tile Layer" ) );
44
45 mRadioSourceService->setChecked( true );
46 mStackedWidget->setCurrentIndex( 1 );
47
48 connect( mRadioSourceFile, &QRadioButton::toggled, this, [this]
49 {
50 mStackedWidget->setCurrentIndex( 0 );
51
52 emit enableButtons( !mFileWidget->filePath().isEmpty() );
53 } );
54 connect( mRadioSourceService, &QRadioButton::toggled, this, [this]
55 {
56 mStackedWidget->setCurrentIndex( 1 );
57
58 emit enableButtons( !cmbConnections->currentText().isEmpty() );
59 } );
60
61 btnNew->setPopupMode( QToolButton::InstantPopup );
62 QMenu *newMenu = new QMenu( btnNew );
63
64 QAction *actionNew = new QAction( tr( "New Generic Connection…" ), this );
65 connect( actionNew, &QAction::triggered, this, &QgsVectorTileSourceSelect::btnNew_clicked );
66 newMenu->addAction( actionNew );
67
68 QAction *actionNewArcGISConnection = new QAction( tr( "New ArcGIS Vector Tile Service Connection…" ), this );
69 connect( actionNewArcGISConnection, &QAction::triggered, this, &QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection );
70 newMenu->addAction( actionNewArcGISConnection );
71
72 btnNew->setMenu( newMenu );
73
74 connect( btnEdit, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnEdit_clicked );
75 connect( btnDelete, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnDelete_clicked );
76 connect( btnSave, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnSave_clicked );
77 connect( btnLoad, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnLoad_clicked );
78 connect( cmbConnections, &QComboBox::currentTextChanged, this, &QgsVectorTileSourceSelect::cmbConnections_currentTextChanged );
79 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorTileSourceSelect::showHelp );
80 setupButtons( buttonBox );
81
82 populateConnectionList();
83
84 mFileWidget->setDialogTitle( tr( "Open Vector Tile Dataset" ) );
85 mFileWidget->setFilter( QgsProviderRegistry::instance()->fileVectorTileFilters() );
86 mFileWidget->setStorageMode( QgsFileWidget::GetFile );
87 mFileWidget->setOptions( QFileDialog::HideNameFilterDetails );
88 connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
89 {
90 emit enableButtons( !path.isEmpty() );
91 } );
92}
93
94void QgsVectorTileSourceSelect::btnNew_clicked()
95{
96 QgsVectorTileConnectionDialog nc( this );
97 if ( nc.exec() )
98 {
99 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
100 populateConnectionList();
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(),
174 tr( "XML files (*.xml *.XML)" ) );
175 if ( fileName.isEmpty() )
176 {
177 return;
178 }
179
181 dlg.exec();
182 populateConnectionList();
183}
184
185void QgsVectorTileSourceSelect::addButtonClicked()
186{
187 if ( mRadioSourceService->isChecked() )
188 {
189 const QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( cmbConnections->currentText() ) );
191 emit addVectorTileLayer( uri, cmbConnections->currentText() );
193 emit addLayer( Qgis::LayerType::VectorTile, uri, cmbConnections->currentText(), QString() );
194 }
195 else if ( mRadioSourceFile->isChecked() )
196 {
197 const QString filePath = mFileWidget->filePath();
198 const QList< QgsProviderRegistry::ProviderCandidateDetails > providers = QgsProviderRegistry::instance()->preferredProvidersForUri( filePath );
199 QString providerKey;
200 for ( const QgsProviderRegistry::ProviderCandidateDetails &details : providers )
201 {
202 if ( details.layerTypes().contains( Qgis::LayerType::VectorTile ) )
203 {
204 providerKey = details.metadata()->key();
205 }
206 }
207
208 QVariantMap parts;
209 parts.insert( QStringLiteral( "path" ), filePath );
210 const QString uri = QgsProviderRegistry::instance()->encodeUri( providerKey, parts );
211
213 emit addVectorTileLayer( uri, QgsProviderUtils::suggestLayerNameFromFilePath( filePath ) );
215 emit addLayer( Qgis::LayerType::VectorTile, uri, cmbConnections->currentText(), QString() );
216 }
217}
218
219void QgsVectorTileSourceSelect::populateConnectionList()
220{
221 cmbConnections->blockSignals( true );
222 cmbConnections->clear();
223 cmbConnections->addItems( QgsVectorTileProviderConnection::connectionList() );
224 cmbConnections->blockSignals( false );
225
226 btnEdit->setDisabled( cmbConnections->count() == 0 );
227 btnDelete->setDisabled( cmbConnections->count() == 0 );
228 btnSave->setDisabled( cmbConnections->count() == 0 );
229 cmbConnections->setDisabled( cmbConnections->count() == 0 );
230
231 setConnectionListPosition();
232}
233
234void QgsVectorTileSourceSelect::setConnectionListPosition()
235{
236 const QString toSelect = QgsVectorTileProviderConnection::selectedConnection();
237
238 cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
239
240 if ( cmbConnections->currentIndex() < 0 )
241 {
242 if ( toSelect.isNull() )
243 cmbConnections->setCurrentIndex( 0 );
244 else
245 cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
246 }
247
248 emit enableButtons( !cmbConnections->currentText().isEmpty() );
249}
250
251void QgsVectorTileSourceSelect::cmbConnections_currentTextChanged( const QString &text )
252{
253 QgsVectorTileProviderConnection::setSelectedConnection( text );
254 emit enableButtons( !text.isEmpty() );
255}
256
257void QgsVectorTileSourceSelect::showHelp()
258{
259 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#using-vector-tiles-services" ) );
260}
261
@ 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.
Definition: qgsfilewidget.h:68
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:194
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:5776
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:5775