QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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"
24 
25 #include <QFileDialog>
26 #include <QMessageBox>
27 
29 
30 QgsVectorTileSourceSelect::QgsVectorTileSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
31  : QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
32 {
33  setupUi( this );
34 
35  setWindowTitle( tr( "Add Vector Tile Layer" ) );
36  mConnectionsGroupBox->setTitle( tr( "Vector Tile Connections" ) );
37 
39 
40  connect( btnNew, &QPushButton::clicked, this, &QgsVectorTileSourceSelect::btnNew_clicked );
41  connect( btnEdit, &QPushButton::clicked, this, &QgsVectorTileSourceSelect::btnEdit_clicked );
42  connect( btnDelete, &QPushButton::clicked, this, &QgsVectorTileSourceSelect::btnDelete_clicked );
43  connect( btnSave, &QPushButton::clicked, this, &QgsVectorTileSourceSelect::btnSave_clicked );
44  connect( btnLoad, &QPushButton::clicked, this, &QgsVectorTileSourceSelect::btnLoad_clicked );
45  connect( cmbConnections, &QComboBox::currentTextChanged, this, &QgsVectorTileSourceSelect::cmbConnections_currentTextChanged );
46  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorTileSourceSelect::showHelp );
47  setupButtons( buttonBox );
48 
49  // disable help button until we got an entry in the docs
50  buttonBox->button( QDialogButtonBox::Help )->setEnabled( false );
51 
52  populateConnectionList();
53 }
54 
55 void QgsVectorTileSourceSelect::btnNew_clicked()
56 {
57  QgsVectorTileConnectionDialog nc( this );
58  if ( nc.exec() )
59  {
60  QgsVectorTileProviderConnection::addConnection( nc.connectionName(), QgsVectorTileProviderConnection::decodedUri( nc.connectionUri() ) );
61  populateConnectionList();
62  emit connectionsChanged();
63  }
64 }
65 
66 void QgsVectorTileSourceSelect::btnEdit_clicked()
67 {
68  QgsVectorTileConnectionDialog nc( this );
69  QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( cmbConnections->currentText() ) );
70  nc.setConnection( cmbConnections->currentText(), uri );
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::btnDelete_clicked()
80 {
81  QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
82  .arg( cmbConnections->currentText() );
83  if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No ) )
84  return;
85 
86  QgsVectorTileProviderConnection::deleteConnection( cmbConnections->currentText() );
87 
88  populateConnectionList();
89  emit connectionsChanged();
90 }
91 
92 void QgsVectorTileSourceSelect::btnSave_clicked()
93 {
95  dlg.exec();
96 }
97 
98 void QgsVectorTileSourceSelect::btnLoad_clicked()
99 {
100  QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(),
101  tr( "XML files (*.xml *.XML)" ) );
102  if ( fileName.isEmpty() )
103  {
104  return;
105  }
106 
108  dlg.exec();
109  populateConnectionList();
110 }
111 
112 void QgsVectorTileSourceSelect::addButtonClicked()
113 {
114  QString uri = QgsVectorTileProviderConnection::encodedUri( QgsVectorTileProviderConnection::connection( cmbConnections->currentText() ) );
115  emit addVectorTileLayer( uri, cmbConnections->currentText() );
116 }
117 
118 void QgsVectorTileSourceSelect::populateConnectionList()
119 {
120  cmbConnections->blockSignals( true );
121  cmbConnections->clear();
122  cmbConnections->addItems( QgsVectorTileProviderConnection::connectionList() );
123  cmbConnections->blockSignals( false );
124 
125  btnEdit->setDisabled( cmbConnections->count() == 0 );
126  btnDelete->setDisabled( cmbConnections->count() == 0 );
127  btnSave->setDisabled( cmbConnections->count() == 0 );
128  cmbConnections->setDisabled( cmbConnections->count() == 0 );
129 
130  setConnectionListPosition();
131 }
132 
133 void QgsVectorTileSourceSelect::setConnectionListPosition()
134 {
135  QString toSelect = QgsVectorTileProviderConnection::selectedConnection();
136 
137  cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
138 
139  if ( cmbConnections->currentIndex() < 0 )
140  {
141  if ( toSelect.isNull() )
142  cmbConnections->setCurrentIndex( 0 );
143  else
144  cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
145  }
146 
147  emit enableButtons( !cmbConnections->currentText().isEmpty() );
148 }
149 
150 void QgsVectorTileSourceSelect::cmbConnections_currentTextChanged( const QString &text )
151 {
152  QgsVectorTileProviderConnection::setSelectedConnection( text );
153  emit enableButtons( !text.isEmpty() );
154 }
155 
156 void QgsVectorTileSourceSelect::showHelp()
157 {
158 }
159 
qgsvectortilesourceselect.h
QgsManageConnectionsDialog
Definition: qgsmanageconnectionsdialog.h:31
QgsAbstractDataSourceWidget
Abstract base Data Source Widget to create connections and add layers This class provides common func...
Definition: qgsabstractdatasourcewidget.h:42
qgsmanageconnectionsdialog.h
qgsgui.h
qgsvectortileconnectiondialog.h
QgsGui::enableAutoGeometryRestore
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:133
QgsProviderRegistry::WidgetMode
WidgetMode
Different ways a source select dialog can be used.
Definition: qgsproviderregistry.h:63
QgsManageConnectionsDialog::Import
@ Import
Definition: qgsmanageconnectionsdialog.h:39
QgsManageConnectionsDialog::VectorTile
@ VectorTile
Definition: qgsmanageconnectionsdialog.h:55
qgsvectortileconnection.h
QgsManageConnectionsDialog::Export
@ Export
Definition: qgsmanageconnectionsdialog.h:38
qgshelp.h