QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 {
51 mStackedWidget->setCurrentIndex( 0 );
52
53 emit enableButtons( !mFileWidget->filePath().isEmpty() );
54 } );
55 connect( mRadioSourceService, &QRadioButton::toggled, this, [this]
56 {
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, [ = ]( const QString & path )
90 {
91 emit enableButtons( !path.isEmpty() );
92 } );
93}
94
95void QgsVectorTileSourceSelect::btnNew_clicked()
96{
97 QgsVectorTileConnectionDialog nc( this );
98 if ( nc.exec() )
99 {
100 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
101 populateConnectionList();
102 QgsVectorTileProviderConnection::setSelectedConnection( nc.connectionName() );
103 setConnectionListPosition();
104 emit connectionsChanged();
105 }
106}
107
108void QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection()
109{
110 QgsArcgisVectorTileConnectionDialog nc( this );
111 if ( nc.exec() )
112 {
113 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
114 populateConnectionList();
115 emit connectionsChanged();
116 }
117}
118
119void QgsVectorTileSourceSelect::btnEdit_clicked()
120{
121 const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( cmbConnections->currentText() );
122 const QString uri = QgsVectorTileProviderConnection::encodedUri( connection );
123
124 switch ( connection.serviceType )
125 {
126 case QgsVectorTileProviderConnection::Generic:
127 {
128 QgsVectorTileConnectionDialog nc( this );
129 nc.setConnection( cmbConnections->currentText(), uri );
130 if ( nc.exec() )
131 {
132 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
133 populateConnectionList();
134 emit connectionsChanged();
135 }
136 break;
137 }
138
139 case QgsVectorTileProviderConnection::ArcgisVectorTileService:
140 {
141 QgsArcgisVectorTileConnectionDialog nc( this );
142
143 nc.setConnection( cmbConnections->currentText(), uri );
144 if ( nc.exec() )
145 {
146 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
147 populateConnectionList();
148 emit connectionsChanged();
149 }
150 break;
151 }
152 }
153}
154
155void QgsVectorTileSourceSelect::btnDelete_clicked()
156{
157 const QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
158 .arg( cmbConnections->currentText() );
159 if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No ) )
160 return;
161
162 QgsVectorTileProviderConnection::deleteConnection( cmbConnections->currentText() );
163
164 populateConnectionList();
165 emit connectionsChanged();
166}
167
168void QgsVectorTileSourceSelect::btnSave_clicked()
169{
171 dlg.exec();
172}
173
174void QgsVectorTileSourceSelect::btnLoad_clicked()
175{
176 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(),
177 tr( "XML files (*.xml *.XML)" ) );
178 if ( fileName.isEmpty() )
179 {
180 return;
181 }
182
184 dlg.exec();
185 populateConnectionList();
186}
187
188void QgsVectorTileSourceSelect::addButtonClicked()
189{
190 if ( mRadioSourceService->isChecked() )
191 {
192 const QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( cmbConnections->currentText() ) );
194 emit addVectorTileLayer( uri, cmbConnections->currentText() );
196 emit addLayer( Qgis::LayerType::VectorTile, uri, cmbConnections->currentText(), QString() );
197 }
198 else if ( mRadioSourceFile->isChecked() )
199 {
200 const QString filePath = mFileWidget->filePath();
201 const QList< QgsProviderRegistry::ProviderCandidateDetails > providers = QgsProviderRegistry::instance()->preferredProvidersForUri( filePath );
202 QString providerKey;
203 for ( const QgsProviderRegistry::ProviderCandidateDetails &details : providers )
204 {
205 if ( details.layerTypes().contains( Qgis::LayerType::VectorTile ) )
206 {
207 providerKey = details.metadata()->key();
208 }
209 }
210
211 QVariantMap parts;
212 parts.insert( QStringLiteral( "path" ), filePath );
213 const QString uri = QgsProviderRegistry::instance()->encodeUri( providerKey, parts );
214
216 emit addVectorTileLayer( uri, QgsProviderUtils::suggestLayerNameFromFilePath( filePath ) );
218 emit addLayer( Qgis::LayerType::VectorTile, uri, cmbConnections->currentText(), QString() );
219 }
220}
221
222void QgsVectorTileSourceSelect::populateConnectionList()
223{
224 cmbConnections->blockSignals( true );
225 cmbConnections->clear();
226 cmbConnections->addItems( QgsVectorTileProviderConnection::connectionList() );
227 cmbConnections->blockSignals( false );
228
229 btnEdit->setDisabled( cmbConnections->count() == 0 );
230 btnDelete->setDisabled( cmbConnections->count() == 0 );
231 btnSave->setDisabled( cmbConnections->count() == 0 );
232 cmbConnections->setDisabled( cmbConnections->count() == 0 );
233
234 setConnectionListPosition();
235}
236
237void QgsVectorTileSourceSelect::setConnectionListPosition()
238{
239 const QString toSelect = QgsVectorTileProviderConnection::selectedConnection();
240
241 cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
242
243 if ( cmbConnections->currentIndex() < 0 )
244 {
245 if ( toSelect.isNull() )
246 cmbConnections->setCurrentIndex( 0 );
247 else
248 cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
249 }
250
251 emit enableButtons( !cmbConnections->currentText().isEmpty() );
252}
253
254void QgsVectorTileSourceSelect::cmbConnections_currentTextChanged( const QString &text )
255{
256 QgsVectorTileProviderConnection::setSelectedConnection( text );
257 emit enableButtons( !text.isEmpty() );
258}
259
260void QgsVectorTileSourceSelect::showHelp()
261{
262 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#using-vector-tiles-services" ) );
263}
264
@ 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:209
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:6494
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493