QGIS API Documentation 3.39.0-Master (47f7b3a4989)
Loading...
Searching...
No Matches
qgstiledscenesourceselect.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstiledscenesourceselect.cpp
3 ---------------------------------
4 begin : June 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson 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
20#include "qgsgui.h"
21#include "qgsprovidermetadata.h"
22#include "qgsproviderutils.h"
25#include "qgshelp.h"
26
27#include <QMenu>
28#include <QMessageBox>
29
31
32QgsTiledSceneSourceSelect::QgsTiledSceneSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
33 : QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
34{
35 setupUi( this );
36
38
39 setWindowTitle( tr( "Add Scene Layer" ) );
40
41 mRadioSourceService->setChecked( true );
42 mStackedWidget->setCurrentIndex( 1 );
43
44 connect( mRadioSourceFile, &QRadioButton::toggled, this, [this]
45 {
46 mStackedWidget->setCurrentIndex( 0 );
47
48 emit enableButtons( !mFileWidget->filePath().isEmpty() );
49 } );
50 connect( mRadioSourceService, &QRadioButton::toggled, this, [this]
51 {
52 mStackedWidget->setCurrentIndex( 1 );
53
54 emit enableButtons( !cmbConnections->currentText().isEmpty() );
55 } );
56
57 btnNew->setPopupMode( QToolButton::InstantPopup );
58 QMenu *newMenu = new QMenu( btnNew );
59
60 QAction *actionNew = new QAction( tr( "New Cesium 3D Tiles Connection…" ), this );
61 connect( actionNew, &QAction::triggered, this, &QgsTiledSceneSourceSelect::btnNewCesium3DTiles_clicked );
62 newMenu->addAction( actionNew );
63
64 btnNew->setMenu( newMenu );
65
66 connect( btnEdit, &QToolButton::clicked, this, &QgsTiledSceneSourceSelect::btnEdit_clicked );
67 connect( btnDelete, &QToolButton::clicked, this, &QgsTiledSceneSourceSelect::btnDelete_clicked );
68 connect( btnSave, &QToolButton::clicked, this, &QgsTiledSceneSourceSelect::btnSave_clicked );
69 connect( btnLoad, &QToolButton::clicked, this, &QgsTiledSceneSourceSelect::btnLoad_clicked );
70 connect( cmbConnections, &QComboBox::currentTextChanged, this, &QgsTiledSceneSourceSelect::cmbConnections_currentTextChanged );
71 setupButtons( buttonBox );
72 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsTiledSceneSourceSelect::showHelp );
73
74 populateConnectionList();
75
76 mFileWidget->setDialogTitle( tr( "Open Scene Dataset" ) );
77 mFileWidget->setFilter( QgsProviderRegistry::instance()->fileTiledSceneFilters() );
78 mFileWidget->setStorageMode( QgsFileWidget::GetFile );
79 mFileWidget->setOptions( QFileDialog::HideNameFilterDetails );
80 connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
81 {
82 emit enableButtons( !path.isEmpty() );
83 } );
84}
85
86void QgsTiledSceneSourceSelect::btnNewCesium3DTiles_clicked()
87{
88 QgsTiledSceneConnectionDialog nc( this );
89 if ( nc.exec() )
90 {
92 connectionData.provider = QStringLiteral( "cesiumtiles" );
93
94 QgsTiledSceneProviderConnection::addConnection( nc.connectionName(), connectionData );
95 populateConnectionList();
97 setConnectionListPosition();
98 emit connectionsChanged();
99 }
100}
101
102void QgsTiledSceneSourceSelect::btnEdit_clicked()
103{
104 const QgsTiledSceneProviderConnection::Data connection = QgsTiledSceneProviderConnection::connection( cmbConnections->currentText() );
105 const QString uri = QgsTiledSceneProviderConnection::encodedUri( connection );
106 const QString provider = connection.provider;
107
108 QgsTiledSceneConnectionDialog nc( this );
109 nc.setConnection( cmbConnections->currentText(), uri );
110 if ( nc.exec() )
111 {
113 connectionData.provider = provider;
114
115 QgsTiledSceneProviderConnection::addConnection( nc.connectionName(), connectionData );
116 populateConnectionList();
117 emit connectionsChanged();
118 }
119
120}
121
122void QgsTiledSceneSourceSelect::btnDelete_clicked()
123{
124 const QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
125 .arg( cmbConnections->currentText() );
126 if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No ) )
127 return;
128
129 QgsTiledSceneProviderConnection( QString() ).remove( cmbConnections->currentText() );
130
131 populateConnectionList();
132 emit connectionsChanged();
133}
134
135void QgsTiledSceneSourceSelect::btnSave_clicked()
136{
138 dlg.exec();
139}
140
141void QgsTiledSceneSourceSelect::btnLoad_clicked()
142{
143 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(),
144 tr( "XML files (*.xml *.XML)" ) );
145 if ( fileName.isEmpty() )
146 {
147 return;
148 }
149
151 dlg.exec();
152 populateConnectionList();
153}
154
155void QgsTiledSceneSourceSelect::addButtonClicked()
156{
157 if ( mRadioSourceService->isChecked() )
158 {
159 const QgsTiledSceneProviderConnection::Data connection = QgsTiledSceneProviderConnection::connection( cmbConnections->currentText() );
160 const QString uri = QgsTiledSceneProviderConnection::encodedUri( connection );
161 emit addLayer( Qgis::LayerType::TiledScene, uri, cmbConnections->currentText(), connection.provider );
162 }
163 else if ( mRadioSourceFile->isChecked() )
164 {
165 const QString filePath = mFileWidget->filePath();
166 const QList< QgsProviderRegistry::ProviderCandidateDetails > providers = QgsProviderRegistry::instance()->preferredProvidersForUri( filePath );
167 QString providerKey;
168 for ( const QgsProviderRegistry::ProviderCandidateDetails &details : providers )
169 {
170 if ( details.layerTypes().contains( Qgis::LayerType::TiledScene ) )
171 {
172 providerKey = details.metadata()->key();
173 }
174 }
175
176 QVariantMap parts;
177 parts.insert( QStringLiteral( "path" ), filePath );
178 const QString uri = QgsProviderRegistry::instance()->encodeUri( providerKey, parts );
179
180 emit addLayer( Qgis::LayerType::TiledScene, uri, QgsProviderUtils::suggestLayerNameFromFilePath( filePath ), providerKey );
181 }
182}
183
184void QgsTiledSceneSourceSelect::populateConnectionList()
185{
186 cmbConnections->blockSignals( true );
187 cmbConnections->clear();
188 cmbConnections->addItems( QgsTiledSceneProviderConnection::connectionList() );
189 cmbConnections->blockSignals( false );
190
191 btnEdit->setDisabled( cmbConnections->count() == 0 );
192 btnDelete->setDisabled( cmbConnections->count() == 0 );
193 btnSave->setDisabled( cmbConnections->count() == 0 );
194 cmbConnections->setDisabled( cmbConnections->count() == 0 );
195
196 setConnectionListPosition();
197}
198
199void QgsTiledSceneSourceSelect::setConnectionListPosition()
200{
202
203 cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
204
205 if ( cmbConnections->currentIndex() < 0 )
206 {
207 if ( toSelect.isNull() )
208 cmbConnections->setCurrentIndex( 0 );
209 else
210 cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
211 }
212
213 emit enableButtons( !cmbConnections->currentText().isEmpty() );
214}
215
216void QgsTiledSceneSourceSelect::cmbConnections_currentTextChanged( const QString &text )
217{
219 emit enableButtons( !text.isEmpty() );
220}
221
222void QgsTiledSceneSourceSelect::showHelp()
223{
224 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html" ) );
225}
226
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Abstract base Data Source Widget to create connections and add layers This class provides common func...
@ 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:194
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
@ TiledScene
Tiled scene connection (since QGIS 3.34)
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.
Represents connections to tiled scene data sources.
virtual void remove(const QString &name) const override
Deletes the connection from the settings.
static QString selectedConnection()
Returns the name of the last used connection.
static Data decodedUri(const QString &uri)
Returns a connection uri decoded to a data structure.
static Data connection(const QString &name)
Returns connection details for the stored connection with the specified name.
static void addConnection(const QString &name, const Data &connection)
Stores a new connection, under the specified connection name.
static void setSelectedConnection(const QString &name)
Stores the name of the last used connection.
static QStringList connectionList()
Returns a list of the stored connection names.
static QString encodedUri(const Data &data)
Returns connection data encoded as a string.
Represents decoded data of a tiled scene connection.