QGIS API Documentation 3.41.0-Master (57ec4277f5e)
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
19#include "moc_qgstiledscenesourceselect.cpp"
21#include "qgsgui.h"
22#include "qgsprovidermetadata.h"
23#include "qgsproviderutils.h"
26#include "qgshelp.h"
27
28#include <QMenu>
29#include <QMessageBox>
30
32
33QgsTiledSceneSourceSelect::QgsTiledSceneSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
34 : QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
35{
36 setupUi( this );
37
39
40 setWindowTitle( tr( "Add Scene Layer" ) );
41
42 mRadioSourceService->setChecked( true );
43 mStackedWidget->setCurrentIndex( 1 );
44
45 connect( mRadioSourceFile, &QRadioButton::toggled, this, [this] {
46 mStackedWidget->setCurrentIndex( 0 );
47
48 emit enableButtons( !mFileWidget->filePath().isEmpty() );
49 } );
50 connect( mRadioSourceService, &QRadioButton::toggled, this, [this] {
51 mStackedWidget->setCurrentIndex( 1 );
52
53 emit enableButtons( !cmbConnections->currentText().isEmpty() );
54 } );
55
56 btnNew->setPopupMode( QToolButton::InstantPopup );
57 QMenu *newMenu = new QMenu( btnNew );
58
59 QAction *actionNew = new QAction( tr( "New Cesium 3D Tiles Connection…" ), this );
60 connect( actionNew, &QAction::triggered, this, [this]() { newConnection( "cesiumtiles" ); } );
61 newMenu->addAction( actionNew );
62
63 actionNew = new QAction( tr( "New Quantized Mesh Connection…" ), this );
64 connect( actionNew, &QAction::triggered, this, [this]() { newConnection( "quantizedmesh" ); } );
65 newMenu->addAction( actionNew );
66
67 btnNew->setMenu( newMenu );
68
69 connect( btnEdit, &QToolButton::clicked, this, &QgsTiledSceneSourceSelect::btnEdit_clicked );
70 connect( btnDelete, &QToolButton::clicked, this, &QgsTiledSceneSourceSelect::btnDelete_clicked );
71 connect( btnSave, &QToolButton::clicked, this, &QgsTiledSceneSourceSelect::btnSave_clicked );
72 connect( btnLoad, &QToolButton::clicked, this, &QgsTiledSceneSourceSelect::btnLoad_clicked );
73 connect( cmbConnections, &QComboBox::currentTextChanged, this, &QgsTiledSceneSourceSelect::cmbConnections_currentTextChanged );
74 setupButtons( buttonBox );
75 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsTiledSceneSourceSelect::showHelp );
76
77 populateConnectionList();
78
79 mFileWidget->setDialogTitle( tr( "Open Scene Dataset" ) );
80 mFileWidget->setFilter( QgsProviderRegistry::instance()->fileTiledSceneFilters() );
81 mFileWidget->setStorageMode( QgsFileWidget::GetFile );
82 mFileWidget->setOptions( QFileDialog::HideNameFilterDetails );
83 connect( mFileWidget, &QgsFileWidget::fileChanged, this, [=]( const QString &path ) {
84 emit enableButtons( !path.isEmpty() );
85 } );
86}
87
88void QgsTiledSceneSourceSelect::btnEdit_clicked()
89{
90 const QgsTiledSceneProviderConnection::Data connection = QgsTiledSceneProviderConnection::connection( cmbConnections->currentText() );
91 const QString uri = QgsTiledSceneProviderConnection::encodedUri( connection );
92 const QString provider = connection.provider;
93
94 QgsTiledSceneConnectionDialog nc( this );
95 nc.setConnection( cmbConnections->currentText(), uri );
96 if ( nc.exec() )
97 {
99 connectionData.provider = provider;
100
101 QgsTiledSceneProviderConnection::addConnection( nc.connectionName(), connectionData );
102 populateConnectionList();
103 emit connectionsChanged();
104 }
105}
106
107void QgsTiledSceneSourceSelect::btnDelete_clicked()
108{
109 const QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
110 .arg( cmbConnections->currentText() );
111 if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Confirm Delete" ), msg, QMessageBox::Yes | QMessageBox::No ) )
112 return;
113
114 QgsTiledSceneProviderConnection( QString() ).remove( cmbConnections->currentText() );
115
116 populateConnectionList();
117 emit connectionsChanged();
118}
119
120void QgsTiledSceneSourceSelect::btnSave_clicked()
121{
123 dlg.exec();
124}
125
126void QgsTiledSceneSourceSelect::btnLoad_clicked()
127{
128 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Connections" ), QDir::homePath(), tr( "XML files (*.xml *.XML)" ) );
129 if ( fileName.isEmpty() )
130 {
131 return;
132 }
133
135 dlg.exec();
136 populateConnectionList();
137}
138
139void QgsTiledSceneSourceSelect::addButtonClicked()
140{
141 if ( mRadioSourceService->isChecked() )
142 {
143 const QgsTiledSceneProviderConnection::Data connection = QgsTiledSceneProviderConnection::connection( cmbConnections->currentText() );
144 const QString uri = QgsTiledSceneProviderConnection::encodedUri( connection );
145 emit addLayer( Qgis::LayerType::TiledScene, uri, cmbConnections->currentText(), connection.provider );
146 }
147 else if ( mRadioSourceFile->isChecked() )
148 {
149 const QString filePath = mFileWidget->filePath();
150 const QList<QgsProviderRegistry::ProviderCandidateDetails> providers = QgsProviderRegistry::instance()->preferredProvidersForUri( filePath );
151 QString providerKey;
152 for ( const QgsProviderRegistry::ProviderCandidateDetails &details : providers )
153 {
154 if ( details.layerTypes().contains( Qgis::LayerType::TiledScene ) )
155 {
156 providerKey = details.metadata()->key();
157 }
158 }
159
160 QVariantMap parts;
161 parts.insert( QStringLiteral( "path" ), filePath );
162 const QString uri = QgsProviderRegistry::instance()->encodeUri( providerKey, parts );
163
164 emit addLayer( Qgis::LayerType::TiledScene, uri, QgsProviderUtils::suggestLayerNameFromFilePath( filePath ), providerKey );
165 }
166}
167
168void QgsTiledSceneSourceSelect::newConnection( QString provider )
169{
170 QgsTiledSceneConnectionDialog nc( this );
171 if ( nc.exec() )
172 {
174 connectionData.provider = provider;
175
176 QgsTiledSceneProviderConnection::addConnection( nc.connectionName(), connectionData );
177 populateConnectionList();
179 setConnectionListPosition();
180 emit connectionsChanged();
181 }
182}
183
184
185void QgsTiledSceneSourceSelect::populateConnectionList()
186{
187 cmbConnections->blockSignals( true );
188 cmbConnections->clear();
189 cmbConnections->addItems( QgsTiledSceneProviderConnection::connectionList() );
190 cmbConnections->blockSignals( false );
191
192 btnEdit->setDisabled( cmbConnections->count() == 0 );
193 btnDelete->setDisabled( cmbConnections->count() == 0 );
194 btnSave->setDisabled( cmbConnections->count() == 0 );
195 cmbConnections->setDisabled( cmbConnections->count() == 0 );
196
197 setConnectionListPosition();
198}
199
200void QgsTiledSceneSourceSelect::setConnectionListPosition()
201{
203
204 cmbConnections->setCurrentIndex( cmbConnections->findText( toSelect ) );
205
206 if ( cmbConnections->currentIndex() < 0 )
207 {
208 if ( toSelect.isNull() )
209 cmbConnections->setCurrentIndex( 0 );
210 else
211 cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
212 }
213
214 emit enableButtons( !cmbConnections->currentText().isEmpty() );
215}
216
217void QgsTiledSceneSourceSelect::cmbConnections_currentTextChanged( const QString &text )
218{
220 emit enableButtons( !text.isEmpty() );
221}
222
223void QgsTiledSceneSourceSelect::showHelp()
224{
225 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html" ) );
226}
227
@ 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:210
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.
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.