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