QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsdatasourcemanagerdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatasourcemanagerdialog.cpp - datasource manager dialog
3
4 ---------------------
5 begin : May 19, 2017
6 copyright : (C) 2017 by Alessandro Pasotti
7 email : apasotti at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include <QListWidgetItem>
18
20#include "moc_qgsdatasourcemanagerdialog.cpp"
21#include "ui_qgsdatasourcemanagerdialog.h"
23#include "qgssettings.h"
24#include "qgsproviderregistry.h"
28#include "qgsmapcanvas.h"
29#include "qgsmessagelog.h"
30#include "qgsmessagebar.h"
31#include "qgsgui.h"
32#include "qgsbrowserguimodel.h"
33#include "qgsbrowserwidget.h"
34
35QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QgsBrowserGuiModel *browserModel, QWidget *parent, QgsMapCanvas *canvas, Qt::WindowFlags fl )
36 : QgsOptionsDialogBase( tr( "Data Source Manager" ), parent, fl )
37 , ui( new Ui::QgsDataSourceManagerDialog )
38 , mPreviousRow( -1 )
39 , mMapCanvas( canvas )
40 , mBrowserModel( browserModel )
41{
42 ui->setupUi( this );
43 ui->verticalLayout_2->setSpacing( 6 );
44 ui->verticalLayout_2->setContentsMargins( 0, 0, 0, 0 );
45
46 mMessageBar = new QgsMessageBar( this );
47 mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
48 static_cast<QVBoxLayout *>( layout() )->insertWidget( 0, mMessageBar );
49
50 // QgsOptionsDialogBase handles saving/restoring of geometry, splitter and current tab states,
51 // switching vertical tabs between icon/text to icon-only modes (splitter collapsed to left),
52 // and connecting QDialogButtonBox's accepted/rejected signals to dialog's accept/reject slots
53 initOptionsBase( false );
54
55 // Bind list index to the stacked dialogs
56 connect( ui->mOptionsListWidget, &QListWidget::currentRowChanged, this, &QgsDataSourceManagerDialog::setCurrentPage );
57
58 // BROWSER Add the browser widget to the first stacked widget page
59 mBrowserWidget = new QgsBrowserDockWidget( QStringLiteral( "Browser" ), mBrowserModel, this );
60 mBrowserWidget->setFeatures( QDockWidget::NoDockWidgetFeatures );
61 mBrowserWidget->setTitleBarWidget( new QWidget( mBrowserWidget ) );
62
63 QWidget *browserWidgetWrapper = new QWidget( this );
64 browserWidgetWrapper->setLayout( new QVBoxLayout( browserWidgetWrapper ) );
65 browserWidgetWrapper->layout()->addWidget( mBrowserWidget );
66 QDialogButtonBox *browserButtonBox = new QDialogButtonBox( QDialogButtonBox::StandardButton::Close | QDialogButtonBox::StandardButton::Help, browserWidgetWrapper );
67 browserWidgetWrapper->layout()->addWidget( browserButtonBox );
68
69 connect( browserButtonBox, &QDialogButtonBox::helpRequested, this, [ = ]
70 {
71 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#the-browser-panel" ) );
72 } );
73 connect( browserButtonBox, &QDialogButtonBox::rejected, this, &QgsDataSourceManagerDialog::reject );
74
75 ui->mOptionsStackedWidget->addWidget( browserWidgetWrapper );
76 mPageProviderKeys.append( QStringLiteral( "browser" ) );
77 mPageProviderNames.append( QStringLiteral( "browser" ) );
78
79 // Forward all browser signals
84
85 // Add registered source select dialogs
86 const QList<QgsSourceSelectProvider *> sourceSelectProviders = QgsGui::sourceSelectProviderRegistry()->providers( );
87 for ( QgsSourceSelectProvider *provider : sourceSelectProviders )
88 {
89 QgsAbstractDataSourceWidget *dlg = provider->createDataSourceWidget( this );
90 if ( !dlg )
91 {
92 QgsMessageLog::logMessage( tr( "Cannot get %1 select dialog from source select provider %2." ).arg( provider->name(), provider->providerKey() ), QStringLiteral( "DataSourceManager" ), Qgis::MessageLevel::Critical );
93 continue;
94 }
95 addProviderDialog( dlg, provider->providerKey(), provider->name(), provider->text(), provider->icon( ), provider->toolTip( ) );
96 }
97
98 connect( QgsGui::sourceSelectProviderRegistry(), &QgsSourceSelectProviderRegistry::providerAdded, this, [ = ]( const QString & name )
99 {
100 if ( QgsSourceSelectProvider *provider = QgsGui::sourceSelectProviderRegistry()->providerByName( name ) )
101 {
102 QgsAbstractDataSourceWidget *dlg = provider->createDataSourceWidget( this );
103 if ( !dlg )
104 {
105 QgsMessageLog::logMessage( tr( "Cannot get %1 select dialog from source select provider %2." ).arg( provider->name(), provider->providerKey() ), QStringLiteral( "DataSourceManager" ), Qgis::MessageLevel::Critical );
106 return;
107 }
108 addProviderDialog( dlg, provider->providerKey(), provider->name(), provider->text(), provider->icon( ), provider->toolTip( ) );
109 }
110 } );
111
112 connect( QgsGui::sourceSelectProviderRegistry(), &QgsSourceSelectProviderRegistry::providerRemoved, this, [ = ]( const QString & name )
113 {
114 removeProviderDialog( name );
115 } );
116
117 restoreOptionsBaseUi( tr( "Data Source Manager" ) );
118}
119
124
125void QgsDataSourceManagerDialog::openPage( const QString &pageName )
126{
127 // TODO -- this is actually using provider keys, not provider names!
128 const int pageIdx = mPageProviderKeys.indexOf( pageName );
129 if ( pageIdx != -1 )
130 {
131 QTimer::singleShot( 0, this, [ = ] { setCurrentPage( pageIdx ); } );
132 }
133}
134
136{
137 return mMessageBar;
138}
139
141{
142 raise();
143 setWindowState( windowState() & ~Qt::WindowMinimized );
144 activateWindow();
145}
146
148{
149 mPreviousRow = ui->mOptionsStackedWidget->currentIndex();
150 ui->mOptionsStackedWidget->setCurrentIndex( index );
151 setWindowTitle( tr( "Data Source Manager | %1" ).arg( ui->mOptionsListWidget->currentItem()->text() ) );
152 resizeAlltabs( index );
153}
154
156{
157 const int prevPage = mPreviousRow != -1 ? mPreviousRow : 0;
158 setCurrentPage( prevPage );
159}
160
166
168{
169 const int pageCount = ui->mOptionsStackedWidget->count();
170 for ( int i = 0; i < pageCount; ++i )
171 {
172 QWidget *widget = ui->mOptionsStackedWidget->widget( i );
173 QgsAbstractDataSourceWidget *dataSourceWidget = qobject_cast<QgsAbstractDataSourceWidget *>( widget );
174 if ( dataSourceWidget )
175 dataSourceWidget->reset();
176 }
177}
178
179void QgsDataSourceManagerDialog::configureFromUri( const QString &pageName, const QString &uri )
180{
181 const int pageIdx = mPageProviderNames.indexOf( pageName );
182 if ( pageIdx != -1 )
183 {
184 QTimer::singleShot( 0, this, [ = ]
185 {
186 setCurrentPage( pageIdx );
187 if ( QgsAbstractDataSourceWidget *dataSourceWidget = qobject_cast<QgsAbstractDataSourceWidget *>( ui->mOptionsStackedWidget->currentWidget() ) )
188 {
189 dataSourceWidget->configureFromUri( uri );
190 }
191 } );
192 }
193}
194
195void QgsDataSourceManagerDialog::rasterLayersAdded( const QStringList &layersList )
196{
197 emit addRasterLayers( layersList );
198}
199
200void QgsDataSourceManagerDialog::vectorLayersAdded( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType )
201{
202 emit addVectorLayers( layerQStringList, enc, dataSourceType );
203}
204
205void QgsDataSourceManagerDialog::addProviderDialog( QgsAbstractDataSourceWidget *dlg, const QString &providerKey, const QString &providerName, const QString &text, const QIcon &icon, const QString &toolTip )
206{
207 mPageProviderKeys.append( providerKey );
208 mPageProviderNames.append( providerName );
209 ui->mOptionsStackedWidget->addWidget( dlg );
210 QListWidgetItem *layerItem = new QListWidgetItem( text, ui->mOptionsListWidget );
211 layerItem->setData( Qt::UserRole, providerName );
212 layerItem->setToolTip( toolTip.isEmpty() ? tr( "Add %1 layer" ).arg( text ) : toolTip );
213 layerItem->setIcon( icon );
214 // Set crs and extent from canvas
215 if ( mMapCanvas )
216 {
217 dlg->setMapCanvas( mMapCanvas );
218 }
219 dlg->setBrowserModel( mBrowserModel );
220
221 connect( dlg, &QgsAbstractDataSourceWidget::rejected, this, &QgsDataSourceManagerDialog::reject );
222 connect( dlg, &QgsAbstractDataSourceWidget::accepted, this, &QgsDataSourceManagerDialog::accept );
223 makeConnections( dlg, providerKey );
224}
225
226void QgsDataSourceManagerDialog::removeProviderDialog( const QString &providerName )
227{
228 const int pageIdx = mPageProviderNames.indexOf( providerName );
229 if ( pageIdx != -1 )
230 {
231 ui->mOptionsStackedWidget->removeWidget( ui->mOptionsStackedWidget->widget( pageIdx ) );
232 mPageProviderKeys.removeAt( pageIdx );
233 mPageProviderNames.removeAt( pageIdx );
234 ui->mOptionsListWidget->removeItemWidget( ui->mOptionsListWidget->item( pageIdx ) );
235 }
236}
237
238void QgsDataSourceManagerDialog::makeConnections( QgsAbstractDataSourceWidget *dlg, const QString &providerKey )
239{
240 // DB
245
246 connect( dlg, &QgsAbstractDataSourceWidget::addLayer, this, [ = ]( Qgis::LayerType type, const QString & url, const QString & baseName, const QString & providerKey )
247 {
248 Q_UNUSED( url )
249 Q_UNUSED( baseName )
250 Q_UNUSED( providerKey )
251
252 switch ( type )
253 {
262 // for compatibility with older API, we ignore these signals and rely on the older granular signals (eg "addVectorLayer").
263 // otherwise we will be emitting double signals for the old/new signal for these layer types
264 break;
265
267 emit addLayer( type, url, baseName, providerKey );
268 break;
269 }
270 } );
271
272 // Vector
274 connect( dlg, &QgsAbstractDataSourceWidget::addVectorLayer, this, [ = ]( const QString & vectorLayerPath, const QString & baseName, const QString & specifiedProvider )
275 {
276 const QString key = specifiedProvider.isEmpty() ? providerKey : specifiedProvider;
277 emit addLayer( Qgis::LayerType::Vector, vectorLayerPath, baseName, key );
278 }
279 );
284
285 // Raster
287 connect( dlg, &QgsAbstractDataSourceWidget::addRasterLayer, this, [ = ]( const QString & rasterLayerPath, const QString & baseName, const QString & providerKey )
288 {
289 emit addLayer( Qgis::LayerType::Raster, rasterLayerPath, baseName, providerKey );
290 } );
294
295 // Mesh
297 connect( dlg, &QgsAbstractDataSourceWidget::addMeshLayer, this, [ = ]( const QString & url, const QString & baseName, const QString & providerKey )
298 {
299 emit addLayer( Qgis::LayerType::Mesh, url, baseName, providerKey );
300 } );
302 // Vector tile
304 connect( dlg, &QgsAbstractDataSourceWidget::addVectorTileLayer, this, [ = ]( const QString & url, const QString & baseName )
305 {
306 emit addLayer( Qgis::LayerType::VectorTile, url, baseName, QString() );
307 } );
309 // Point Cloud
311 connect( dlg, &QgsAbstractDataSourceWidget::addPointCloudLayer, this, [ = ]( const QString & url, const QString & baseName, const QString & providerKey )
312 {
313 emit addLayer( Qgis::LayerType::PointCloud, url, baseName, providerKey );
314 } );
316 // Virtual
319 // Common
322
323 // Message
324 connect( dlg, &QgsAbstractDataSourceWidget::pushMessage, this, [ = ]( const QString & title, const QString & message, const Qgis::MessageLevel level )
325 {
326 mMessageBar->pushMessage( title, message, level );
327 } );
328}
329
331{
332 ui->mOptionsStackedWidget->currentWidget()->show();
334 resizeAlltabs( ui->mOptionsStackedWidget->currentIndex() );
335}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ Critical
Critical/error message.
Definition qgis.h:157
LayerType
Types of layers that can be added to a map.
Definition qgis.h:169
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ Raster
Raster layer.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Abstract base Data Source Widget to create connections and add layers This class provides common func...
void pushMessage(const QString &title, const QString &message, const Qgis::MessageLevel level=Qgis::MessageLevel::Info)
Emitted when a message with title and level must be shown to the user using the parent visible messag...
Q_DECL_DEPRECATED void addPointCloudLayer(const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a point cloud layer has been selected for addition.
Q_DECL_DEPRECATED void addVectorTileLayer(const QString &url, const QString &baseName)
Emitted when a vector tile layer has been selected for addition.
Q_DECL_DEPRECATED void addRasterLayer(const QString &rasterLayerPath, const QString &baseName, const QString &providerKey)
Emitted when a raster layer has been selected for addition.
virtual void setMapCanvas(QgsMapCanvas *mapCanvas)
Sets the dialog map canvas.
void replaceVectorLayer(const QString &oldId, const QString &source, const QString &name, const QString &provider)
Emitted when a layer needs to be replaced.
void connectionsChanged()
Emitted when the provider's connections have changed This signal is normally forwarded the app and us...
void addRasterLayers(const QStringList &layersList)
Emitted when one or more GDAL supported layers are selected for addition.
virtual void refresh()
Triggered when the provider's connections need to be refreshed The default implementation does nothin...
virtual void setBrowserModel(QgsBrowserModel *model)
Sets a browser model to use with the widget.
Q_DECL_DEPRECATED void addMeshLayer(const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a mesh layer has been selected for addition.
void progressMessage(QString message)
Emitted when a progress dialog is shown by the provider dialog.
void addVectorLayers(const QStringList &layerList, const QString &encoding, const QString &dataSourceType)
Emitted when one or more OGR supported layers are selected for addition.
void addDatabaseLayers(const QStringList &paths, const QString &providerKey)
Emitted when a DB layer has been selected for addition.
Q_DECL_DEPRECATED void addVectorLayer(const QString &uri, const QString &layerName, const QString &providerKey=QString())
Emitted when a vector layer has been selected for addition.
virtual void reset()
Called when this source select widget is being shown in a "new and clean" dialog.
void addLayer(Qgis::LayerType type, const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a layer has been selected for addition.
A dock widget containing a QgsBrowserWidget for navigating and managing data sources.
void connectionsChanged()
Connections changed in the browser.
QgsBrowserWidget * browserWidget()
Returns a pointer to the QgsBrowserWidget used by the dock widget.
void handleDropUriList(const QgsMimeDataUtils::UriList &uris)
Emitted when drop uri list needs to be handled.
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
A model for showing available data sources and other items in a structured tree.
void updateProjectHome()
Update project home directory.
void refresh()
Refreshes the browser model and view.
The QgsDataSourceManagerDialog class embeds the browser panel and all the provider dialogs.
void reset()
Resets the interface of the datasource manager after reopening the dialog.
void refresh()
Refresh the browser view.
void addVectorLayers(const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType)
Emitted when a one or more layer were selected for addition: for signal forwarding to QgisApp.
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
QgsMessageBar * messageBar() const
Returns the dialog's message bar.
void rasterLayersAdded(const QStringList &layersList)
One or more raster layer were added: for signal forwarding to QgisApp.
void setPreviousPage()
Reset current page to previously selected page.
QgsDataSourceManagerDialog(QgsBrowserGuiModel *browserModel, QWidget *parent=nullptr, QgsMapCanvas *canvas=nullptr, Qt::WindowFlags fl=Qt::Window)
QgsDataSourceManagerDialog constructor.
void showStatusMessage(const QString &message)
Emitted when a status message needs to be shown: for signal forwarding to QgisApp.
void showEvent(QShowEvent *event) override
void replaceSelectedVectorLayer(const QString &oldId, const QString &uri, const QString &layerName, const QString &provider)
Replace the selected layer by a vector layer defined by uri, layer name, data source uri.
void configureFromUri(const QString &pageName, const QString &uri)
Shows the page pageName and configure the source select widget from the layer uri.
void openPage(const QString &pageName)
Open a given page in the dialog.
void activate()
Raise, unminimize and activate this window.
void connectionsChanged()
Emitted when a connection has changed inside the provider dialogs This signal is normally forwarded t...
void vectorLayersAdded(const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType)
One or more vector layer were added: for signal forwarding to QgisApp.
void addDatabaseLayers(const QStringList &layerPathList, const QString &providerKey)
Emitted when a DB layer was selected for addition: for signal forwarding to QgisApp.
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled from the browser.
void providerDialogsRefreshRequested()
One or more provider connections have changed and the dialogs should be refreshed.
void addLayer(Qgis::LayerType type, const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a layer has been selected for addition.
void updateProjectHome()
Update project home directory.
void setCurrentPage(int index)
Sync current page with the leftbar list.
void addRasterLayers(const QStringList &layersList)
Emitted when a one or more layer were selected for addition: for signal forwarding to QgisApp.
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition qgsgui.cpp:109
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
Map canvas is a class for displaying all GIS data types on a canvas.
A bar for displaying non-blocking messages to the user.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
A base dialog for options and properties dialogs that offers vertical tabs.
void resizeAlltabs(int index)
Resizes all tabs when the dialog is resized.
void restoreOptionsBaseUi(const QString &title=QString())
Restore the base ui.
void initOptionsBase(bool restoreUi=true, const QString &title=QString())
Set up the base ui connections for vertical tabs.
void showEvent(QShowEvent *e) override
void providerRemoved(const QString &name)
Emitted whenever a provider is removed from the registry.
void providerAdded(const QString &name)
Emitted whenever a provider is added to the registry.
QList< QgsSourceSelectProvider * > providers()
Gets list of available providers.
This is the interface for those who want to add entries to the QgsDataSourceManagerDialog.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534