QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  // disable help button until we got an entry in the docs
66  buttonBox->button( QDialogButtonBox::Help )->setEnabled( false );
67 
68  populateConnectionList();
69 }
70 
71 void QgsVectorTileSourceSelect::btnNew_clicked()
72 {
73  QgsVectorTileConnectionDialog nc( this );
74  if ( nc.exec() )
75  {
76  QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
77  populateConnectionList();
78  emit connectionsChanged();
79  }
80 }
81 
82 void QgsVectorTileSourceSelect::newArcgisVectorTileServerConnection()
83 {
84  QgsArcgisVectorTileConnectionDialog nc( this );
85  if ( nc.exec() )
86  {
87  QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
88  populateConnectionList();
89  emit connectionsChanged();
90  }
91 }
92 
93 void QgsVectorTileSourceSelect::btnEdit_clicked()
94 {
95  const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( cmbConnections->currentText() );
96  QString uri = QgsVectorTileProviderConnection::encodedUri( connection );
97 
98  switch ( connection.serviceType )
99  {
100  case QgsVectorTileProviderConnection::Generic:
101  {
102  QgsVectorTileConnectionDialog nc( this );
103  nc.setConnection( cmbConnections->currentText(), uri );
104  if ( nc.exec() )
105  {
106  QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
107  populateConnectionList();
108  emit connectionsChanged();
109  }
110  break;
111  }
112 
113  case QgsVectorTileProviderConnection::ArcgisVectorTileService:
114  {
115  QgsArcgisVectorTileConnectionDialog nc( this );
116 
117  nc.setConnection( cmbConnections->currentText(), uri );
118  if ( nc.exec() )
119  {
120  QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
121  populateConnectionList();
122  emit connectionsChanged();
123  }
124  break;
125  }
126  }
127 }
128 
129 void QgsVectorTileSourceSelect::btnDelete_clicked()
130 {
131  QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
132  .arg( cmbConnections->currentText() );
133  if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No ) )
134  return;
135 
136  QgsVectorTileProviderConnection::deleteConnection( cmbConnections->currentText() );
137 
138  populateConnectionList();
139  emit connectionsChanged();
140 }
141 
142 void QgsVectorTileSourceSelect::btnSave_clicked()
143 {
145  dlg.exec();
146 }
147 
148 void QgsVectorTileSourceSelect::btnLoad_clicked()
149 {
150  QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(),
151  tr( "XML files (*.xml *.XML)" ) );
152  if ( fileName.isEmpty() )
153  {
154  return;
155  }
156 
158  dlg.exec();
159  populateConnectionList();
160 }
161 
162 void QgsVectorTileSourceSelect::addButtonClicked()
163 {
164  QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( cmbConnections->currentText() ) );
165  emit addVectorTileLayer( uri, cmbConnections->currentText() );
166 }
167 
168 void QgsVectorTileSourceSelect::populateConnectionList()
169 {
170  cmbConnections->blockSignals( true );
171  cmbConnections->clear();
172  cmbConnections->addItems( QgsVectorTileProviderConnection::connectionList() );
173  cmbConnections->blockSignals( false );
174 
175  btnEdit->setDisabled( cmbConnections->count() == 0 );
176  btnDelete->setDisabled( cmbConnections->count() == 0 );
177  btnSave->setDisabled( cmbConnections->count() == 0 );
178  cmbConnections->setDisabled( cmbConnections->count() == 0 );
179 
180  setConnectionListPosition();
181 }
182 
183 void QgsVectorTileSourceSelect::setConnectionListPosition()
184 {
185  QString toSelect = QgsVectorTileProviderConnection::selectedConnection();
186 
187  cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
188 
189  if ( cmbConnections->currentIndex() < 0 )
190  {
191  if ( toSelect.isNull() )
192  cmbConnections->setCurrentIndex( 0 );
193  else
194  cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
195  }
196 
197  emit enableButtons( !cmbConnections->currentText().isEmpty() );
198 }
199 
200 void QgsVectorTileSourceSelect::cmbConnections_currentTextChanged( const QString &text )
201 {
202  QgsVectorTileProviderConnection::setSelectedConnection( text );
203  emit enableButtons( !text.isEmpty() );
204 }
205 
206 void QgsVectorTileSourceSelect::showHelp()
207 {
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:156
WidgetMode
Different ways a source select dialog can be used.