QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
26#include <QFileDialog>
27#include <QMessageBox>
28#include <QPushButton>
29#include <QMenu>
30#include <QAction>
31
33
34QgsVectorTileSourceSelect::QgsVectorTileSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
35 : QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
36{
37 setupUi( this );
38
39 mConnectionDetailsGroupBox->hide();
40
41 setWindowTitle( tr( "Add Vector Tile Layer" ) );
42 mConnectionsGroupBox->setTitle( tr( "Vector Tile Connections" ) );
43
45
46 btnNew->setPopupMode( QToolButton::InstantPopup );
47 QMenu *newMenu = new QMenu( btnNew );
48
49 QAction *actionNew = new QAction( tr( "New Generic Connection…" ), this );
50 connect( actionNew, &QAction::triggered, this, &QgsVectorTileSourceSelect::btnNew_clicked );
51 newMenu->addAction( actionNew );
52
53 QAction *actionNewArcGISConnection = new QAction( tr( "New ArcGIS Vector Tile Service Connection…" ), this );
54 connect( actionNewArcGISConnection, &QAction::triggered, this, &QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection );
55 newMenu->addAction( actionNewArcGISConnection );
56
57 btnNew->setMenu( newMenu );
58
59 connect( btnEdit, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnEdit_clicked );
60 connect( btnDelete, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnDelete_clicked );
61 connect( btnSave, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnSave_clicked );
62 connect( btnLoad, &QToolButton::clicked, this, &QgsVectorTileSourceSelect::btnLoad_clicked );
63 connect( cmbConnections, &QComboBox::currentTextChanged, this, &QgsVectorTileSourceSelect::cmbConnections_currentTextChanged );
64 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorTileSourceSelect::showHelp );
65 setupButtons( buttonBox );
66
67 populateConnectionList();
68}
69
70void QgsVectorTileSourceSelect::btnNew_clicked()
71{
72 QgsVectorTileConnectionDialog nc( this );
73 if ( nc.exec() )
74 {
75 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
76 populateConnectionList();
77 emit connectionsChanged();
78 }
79}
80
81void QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection()
82{
83 QgsArcgisVectorTileConnectionDialog nc( this );
84 if ( nc.exec() )
85 {
86 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
87 populateConnectionList();
88 emit connectionsChanged();
89 }
90}
91
92void QgsVectorTileSourceSelect::btnEdit_clicked()
93{
94 const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( cmbConnections->currentText() );
95 const QString uri = QgsVectorTileProviderConnection::encodedUri( connection );
96
97 switch ( connection.serviceType )
98 {
99 case QgsVectorTileProviderConnection::Generic:
100 {
101 QgsVectorTileConnectionDialog nc( this );
102 nc.setConnection( cmbConnections->currentText(), uri );
103 if ( nc.exec() )
104 {
105 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
106 populateConnectionList();
107 emit connectionsChanged();
108 }
109 break;
110 }
111
112 case QgsVectorTileProviderConnection::ArcgisVectorTileService:
113 {
114 QgsArcgisVectorTileConnectionDialog nc( this );
115
116 nc.setConnection( cmbConnections->currentText(), uri );
117 if ( nc.exec() )
118 {
119 QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
120 populateConnectionList();
121 emit connectionsChanged();
122 }
123 break;
124 }
125 }
126}
127
128void QgsVectorTileSourceSelect::btnDelete_clicked()
129{
130 const QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
131 .arg( cmbConnections->currentText() );
132 if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No ) )
133 return;
134
135 QgsVectorTileProviderConnection::deleteConnection( cmbConnections->currentText() );
136
137 populateConnectionList();
138 emit connectionsChanged();
139}
140
141void QgsVectorTileSourceSelect::btnSave_clicked()
142{
144 dlg.exec();
145}
146
147void QgsVectorTileSourceSelect::btnLoad_clicked()
148{
149 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(),
150 tr( "XML files (*.xml *.XML)" ) );
151 if ( fileName.isEmpty() )
152 {
153 return;
154 }
155
157 dlg.exec();
158 populateConnectionList();
159}
160
161void QgsVectorTileSourceSelect::addButtonClicked()
162{
163 const QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( cmbConnections->currentText() ) );
164 emit addVectorTileLayer( uri, cmbConnections->currentText() );
165}
166
167void QgsVectorTileSourceSelect::populateConnectionList()
168{
169 cmbConnections->blockSignals( true );
170 cmbConnections->clear();
171 cmbConnections->addItems( QgsVectorTileProviderConnection::connectionList() );
172 cmbConnections->blockSignals( false );
173
174 btnEdit->setDisabled( cmbConnections->count() == 0 );
175 btnDelete->setDisabled( cmbConnections->count() == 0 );
176 btnSave->setDisabled( cmbConnections->count() == 0 );
177 cmbConnections->setDisabled( cmbConnections->count() == 0 );
178
179 setConnectionListPosition();
180}
181
182void QgsVectorTileSourceSelect::setConnectionListPosition()
183{
184 const QString toSelect = QgsVectorTileProviderConnection::selectedConnection();
185
186 cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
187
188 if ( cmbConnections->currentIndex() < 0 )
189 {
190 if ( toSelect.isNull() )
191 cmbConnections->setCurrentIndex( 0 );
192 else
193 cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
194 }
195
196 emit enableButtons( !cmbConnections->currentText().isEmpty() );
197}
198
199void QgsVectorTileSourceSelect::cmbConnections_currentTextChanged( const QString &text )
200{
201 QgsVectorTileProviderConnection::setSelectedConnection( text );
202 emit enableButtons( !text.isEmpty() );
203}
204
205void QgsVectorTileSourceSelect::showHelp()
206{
207 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#using-vector-tiles-services" ) );
208}
209
Abstract base Data Source Widget to create connections and add layers This class provides common func...
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:178
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:38
WidgetMode
Different ways a source select dialog can be used.