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