QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
19
21#include "qgsgui.h"
22#include "qgshelp.h"
24#include "qgsprovidermetadata.h"
25#include "qgsproviderutils.h"
28
29#include <QAction>
30#include <QFileDialog>
31#include <QMenu>
32#include <QMessageBox>
33#include <QPushButton>
34
35#include "moc_qgsvectortilesourceselect.cpp"
36
38
39QgsVectorTileSourceSelect::QgsVectorTileSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
40 : QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
41{
42 setupUi( this );
43
45
46 setWindowTitle( tr( "Add Vector Tile Layer" ) );
47
48 mRadioSourceService->setChecked( true );
49 mStackedWidget->setCurrentIndex( 1 );
50
51 connect( mRadioSourceFile, &QRadioButton::toggled, this, [this] {
52 mStackedWidget->setCurrentIndex( 0 );
53
54 emit enableButtons( !mFileWidget->filePath().isEmpty() );
55 } );
56 connect( mRadioSourceService, &QRadioButton::toggled, this, [this] {
57 mStackedWidget->setCurrentIndex( 1 );
58
59 emit enableButtons( !cmbConnections->currentText().isEmpty() );
60 } );
61
62 btnNew->setPopupMode( QToolButton::InstantPopup );
63 QMenu *newMenu = new QMenu( btnNew );
64
65 QAction *actionNew = new QAction( tr( "New Generic Connection…" ), this );
66 connect( actionNew, &QAction::triggered, this, &QgsVectorTileSourceSelect::btnNew_clicked );
67 newMenu->addAction( actionNew );
68
69 QAction *actionNewArcGISConnection = new QAction( tr( "New ArcGIS Vector Tile Service Connection…" ), this );
70 connect( actionNewArcGISConnection, &QAction::triggered, this, &QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection );
71 newMenu->addAction( actionNewArcGISConnection );
72
73 btnNew->setMenu( newMenu );
74
75 connect( btnEdit, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnEdit_clicked );
76 connect( btnDelete, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnDelete_clicked );
77 connect( btnSave, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnSave_clicked );
78 connect( btnLoad, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnLoad_clicked );
79 connect( cmbConnections, &QComboBox::currentTextChanged, this, &QgsVectorTileSourceSelect::cmbConnections_currentTextChanged );
80 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorTileSourceSelect::showHelp );
81 setupButtons( buttonBox );
82
83 populateConnectionList();
84
85 mFileWidget->setDialogTitle( tr( "Open Vector Tile Dataset" ) );
86 mFileWidget->setFilter( QgsProviderRegistry::instance()->fileVectorTileFilters() );
87 mFileWidget->setStorageMode( QgsFileWidget::GetFile );
88 mFileWidget->setOptions( QFileDialog::HideNameFilterDetails );
89 connect( mFileWidget, &QgsFileWidget::fileChanged, this, [this]( const QString &path ) {
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 QgsVectorTileProviderConnection::setSelectedConnection( nc.connectionName() );
102 setConnectionListPosition();
103 emit connectionsChanged();
104 }
105}
106
107void QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection()
108{
109 QgsArcgisVectorTileConnectionDialog nc( this );
110 if ( nc.exec() )
111 {
112 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
113 populateConnectionList();
114 emit connectionsChanged();
115 }
116}
117
118void QgsVectorTileSourceSelect::btnEdit_clicked()
119{
120 const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( cmbConnections->currentText() );
121 const QString uri = QgsVectorTileProviderConnection::encodedUri( connection );
122
123 switch ( connection.serviceType )
124 {
125 case QgsVectorTileProviderConnection::Generic:
126 {
127 QgsVectorTileConnectionDialog nc( this );
128 nc.setConnection( cmbConnections->currentText(), uri );
129 if ( nc.exec() )
130 {
131 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
132 populateConnectionList();
133 emit connectionsChanged();
134 }
135 break;
136 }
137
138 case QgsVectorTileProviderConnection::ArcgisVectorTileService:
139 {
140 QgsArcgisVectorTileConnectionDialog nc( this );
141
142 nc.setConnection( cmbConnections->currentText(), uri );
143 if ( nc.exec() )
144 {
145 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
146 populateConnectionList();
147 emit connectionsChanged();
148 }
149 break;
150 }
151 }
152}
153
154void QgsVectorTileSourceSelect::btnDelete_clicked()
155{
156 const QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
157 .arg( cmbConnections->currentText() );
158 if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No ) )
159 return;
160
161 QgsVectorTileProviderConnection::deleteConnection( cmbConnections->currentText() );
162
163 populateConnectionList();
164 emit connectionsChanged();
165}
166
167void QgsVectorTileSourceSelect::btnSave_clicked()
168{
170 dlg.exec();
171}
172
173void QgsVectorTileSourceSelect::btnLoad_clicked()
174{
175 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(), tr( "XML files (*.xml *.XML)" ) );
176 if ( fileName.isEmpty() )
177 {
178 return;
179 }
180
182 dlg.exec();
183 populateConnectionList();
184}
185
186void QgsVectorTileSourceSelect::addButtonClicked()
187{
188 if ( mRadioSourceService->isChecked() )
189 {
190 const QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( cmbConnections->currentText() ) );
192 emit addVectorTileLayer( uri, cmbConnections->currentText() );
194 emit addLayer( Qgis::LayerType::VectorTile, uri, cmbConnections->currentText(), QString() );
195 }
196 else if ( mRadioSourceFile->isChecked() )
197 {
198 const QString filePath = mFileWidget->filePath();
199 const QList<QgsProviderRegistry::ProviderCandidateDetails> providers = QgsProviderRegistry::instance()->preferredProvidersForUri( filePath );
200 QString providerKey;
201 for ( const QgsProviderRegistry::ProviderCandidateDetails &details : providers )
202 {
203 if ( details.layerTypes().contains( Qgis::LayerType::VectorTile ) )
204 {
205 providerKey = details.metadata()->key();
206 }
207 }
208
209 QVariantMap parts;
210 parts.insert( QStringLiteral( "path" ), filePath );
211 const QString uri = QgsProviderRegistry::instance()->encodeUri( providerKey, parts );
212
214 emit addVectorTileLayer( uri, QgsProviderUtils::suggestLayerNameFromFilePath( filePath ) );
216 emit addLayer( Qgis::LayerType::VectorTile, uri, cmbConnections->currentText(), QString() );
217 }
218}
219
220void QgsVectorTileSourceSelect::populateConnectionList()
221{
222 cmbConnections->blockSignals( true );
223 cmbConnections->clear();
224 cmbConnections->addItems( QgsVectorTileProviderConnection::connectionList() );
225 cmbConnections->blockSignals( false );
226
227 btnEdit->setDisabled( cmbConnections->count() == 0 );
228 btnDelete->setDisabled( cmbConnections->count() == 0 );
229 btnSave->setDisabled( cmbConnections->count() == 0 );
230 cmbConnections->setDisabled( cmbConnections->count() == 0 );
231
232 setConnectionListPosition();
233}
234
235void QgsVectorTileSourceSelect::setConnectionListPosition()
236{
237 const QString toSelect = QgsVectorTileProviderConnection::selectedConnection();
238
239 cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
240
241 if ( cmbConnections->currentIndex() < 0 )
242 {
243 if ( toSelect.isNull() )
244 cmbConnections->setCurrentIndex( 0 );
245 else
246 cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
247 }
248
249 emit enableButtons( !cmbConnections->currentText().isEmpty() );
250}
251
252void QgsVectorTileSourceSelect::cmbConnections_currentTextChanged( const QString &text )
253{
254 QgsVectorTileProviderConnection::setSelectedConnection( text );
255 emit enableButtons( !text.isEmpty() );
256}
257
258void QgsVectorTileSourceSelect::showHelp()
259{
260 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#using-vector-tiles-services" ) );
261}
262
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:195
Abstract base class for Data Source widgets to create connections and add layers.
@ 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:221
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
A dialog for importing or exporting stored connections.
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:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169