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