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