QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 "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 ui->mOptionsStackedWidget->addWidget( mBrowserWidget );
62 mPageNames.append( QStringLiteral( "browser" ) );
63
64 // Forward all browser signals
69
70 // Add registered source select dialogs
71 const QList<QgsSourceSelectProvider *> sourceSelectProviders = QgsGui::sourceSelectProviderRegistry()->providers( );
72 for ( QgsSourceSelectProvider *provider : sourceSelectProviders )
73 {
74 QgsAbstractDataSourceWidget *dlg = provider->createDataSourceWidget( this );
75 if ( !dlg )
76 {
77 QgsMessageLog::logMessage( tr( "Cannot get %1 select dialog from source select provider %2." ).arg( provider->name(), provider->providerKey() ), QStringLiteral( "DataSourceManager" ), Qgis::MessageLevel::Critical );
78 continue;
79 }
80 addProviderDialog( dlg, provider->providerKey(), provider->text(), provider->icon( ), provider->toolTip( ) );
81 }
82
83 restoreOptionsBaseUi( tr( "Data Source Manager" ) );
84}
85
87{
88 delete ui;
89}
90
91void QgsDataSourceManagerDialog::openPage( const QString &pageName )
92{
93 const int pageIdx = mPageNames.indexOf( pageName );
94 if ( pageIdx != -1 )
95 {
96 QTimer::singleShot( 0, this, [ = ] { setCurrentPage( pageIdx ); } );
97 }
98}
99
101{
102 return mMessageBar;
103}
104
106{
107 raise();
108 setWindowState( windowState() & ~Qt::WindowMinimized );
109 activateWindow();
110}
111
113{
114 mPreviousRow = ui->mOptionsStackedWidget->currentIndex();
115 ui->mOptionsStackedWidget->setCurrentIndex( index );
116 setWindowTitle( tr( "Data Source Manager | %1" ).arg( ui->mOptionsListWidget->currentItem()->text() ) );
117 resizeAlltabs( index );
118}
119
121{
122 const int prevPage = mPreviousRow != -1 ? mPreviousRow : 0;
123 setCurrentPage( prevPage );
124}
125
127{
128 mBrowserWidget->browserWidget()->refresh();
130}
131
133{
134 const int pageCount = ui->mOptionsStackedWidget->count();
135 for ( int i = 0; i < pageCount; ++i )
136 {
137 QWidget *widget = ui->mOptionsStackedWidget->widget( i );
138 QgsAbstractDataSourceWidget *dataSourceWidget = qobject_cast<QgsAbstractDataSourceWidget *>( widget );
139 if ( dataSourceWidget )
140 dataSourceWidget->reset();
141 }
142}
143
144void QgsDataSourceManagerDialog::rasterLayerAdded( const QString &uri, const QString &baseName, const QString &providerKey )
145{
146 emit addRasterLayer( uri, baseName, providerKey );
147}
148
149void QgsDataSourceManagerDialog::rasterLayersAdded( const QStringList &layersList )
150{
151 emit addRasterLayers( layersList );
152}
153
154void QgsDataSourceManagerDialog::vectorLayerAdded( const QString &vectorLayerPath, const QString &baseName, const QString &providerKey )
155{
156 emit addVectorLayer( vectorLayerPath, baseName, providerKey );
157}
158
159void QgsDataSourceManagerDialog::vectorLayersAdded( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType )
160{
161 emit addVectorLayers( layerQStringList, enc, dataSourceType );
162}
163
164void QgsDataSourceManagerDialog::addProviderDialog( QgsAbstractDataSourceWidget *dlg, const QString &providerKey, const QString &providerName, const QIcon &icon, const QString &toolTip )
165{
166 mPageNames.append( providerKey );
167 ui->mOptionsStackedWidget->addWidget( dlg );
168 QListWidgetItem *layerItem = new QListWidgetItem( providerName, ui->mOptionsListWidget );
169 layerItem->setToolTip( toolTip.isEmpty() ? tr( "Add %1 layer" ).arg( providerName ) : toolTip );
170 layerItem->setIcon( icon );
171 // Set crs and extent from canvas
172 if ( mMapCanvas )
173 {
174 dlg->setMapCanvas( mMapCanvas );
175 }
176 dlg->setBrowserModel( mBrowserModel );
177
178 connect( dlg, &QgsAbstractDataSourceWidget::rejected, this, &QgsDataSourceManagerDialog::reject );
179 connect( dlg, &QgsAbstractDataSourceWidget::accepted, this, &QgsDataSourceManagerDialog::accept );
180 makeConnections( dlg, providerKey );
181}
182
183void QgsDataSourceManagerDialog::makeConnections( QgsAbstractDataSourceWidget *dlg, const QString &providerKey )
184{
185 // DB
190 // Vector
191 connect( dlg, &QgsAbstractDataSourceWidget::addVectorLayer, this, [ = ]( const QString & vectorLayerPath, const QString & baseName, const QString & specifiedProvider )
192 {
193 const QString key = specifiedProvider.isEmpty() ? providerKey : specifiedProvider;
194 this->vectorLayerAdded( vectorLayerPath, baseName, key );
195 }
196 );
200 // Raster
202 this, [ = ]( const QString & uri, const QString & baseName, const QString & providerKey )
203 {
204 addRasterLayer( uri, baseName, providerKey );
205 } );
208 // Mesh
210 // Vector tile
212 // Point Cloud
214 // Virtual
217 // Common
220
221 // Message
222 connect( dlg, &QgsAbstractDataSourceWidget::pushMessage, this, [ = ]( const QString & title, const QString & message, const Qgis::MessageLevel level )
223 {
224 mMessageBar->pushMessage( title, message, level );
225 } );
226}
227
229{
230 ui->mOptionsStackedWidget->currentWidget()->show();
232 resizeAlltabs( ui->mOptionsStackedWidget->currentIndex() );
233}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:115
Abstract base Data Source Widget to create connections and add layers This class provides common func...
void addPointCloudLayer(const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a point cloud layer has been selected for addition.
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...
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.
void addMeshLayer(const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a mesh layer has been selected for addition.
void addRasterLayer(const QString &rasterLayerPath, const QString &baseName, const QString &providerKey)
Emitted when a raster layer has been 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.
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 addVectorLayer(const QString &uri, const QString &layerName, const QString &providerKey=QString())
Emitted when a vector layer has been selected for addition.
void addDatabaseLayers(const QStringList &paths, const QString &providerKey)
Emitted when a DB layer has been selected for addition.
void addVectorTileLayer(const QString &url, const QString &baseName)
Emitted when a vector tile layer has been selected for addition.
virtual void reset()
Called when this source select widget is being shown in a "new and clean" dialog.
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 &)
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.
void addMeshLayer(const QString &uri, const QString &baseName, const QString &providerKey)
Emitted when a mesh layer was selected for addition: for signal forwarding to QgisApp.
QgsMessageBar * messageBar() const
Returns the dialog's message bar.
void addPointCloudLayer(const QString &pointCloudLayerPath, const QString &baseName, const QString &providerKey)
Emitted when a point cloud layer was selected for addition: for signal forwarding to QgisApp.
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 addVectorLayer(const QString &vectorLayerPath, const QString &baseName, const QString &providerKey)
Emitted when a vector layer was selected for addition: for signal forwarding to QgisApp.
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 addRasterLayer(const QString &uri, const QString &baseName, const QString &providerKey)
Emitted when a raster layer was selected for addition: for signal forwarding to QgisApp.
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 addVectorTileLayer(const QString &uri, const QString &baseName)
Emitted when a vector tile layer was selected for addition: for signal forwarding to QgisApp.
void rasterLayerAdded(QString const &uri, QString const &baseName, QString const &providerKey)
A raster layer was added: for signal forwarding to QgisApp.
void providerDialogsRefreshRequested()
One or more provider connections have changed and the dialogs should be refreshed.
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.
void vectorLayerAdded(const QString &vectorLayerPath, const QString &baseName, const QString &providerKey)
A vector layer was added: 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:98
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
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
QList< QgsSourceSelectProvider * > providers()
Gets list of available providers.
This is the interface for those who want to add entries to the QgsDataSourceManagerDialog.