QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
21 #include "qgsbrowserdockwidget.h"
22 #include "qgssettings.h"
23 #include "qgsproviderregistry.h"
27 #include "qgsmapcanvas.h"
28 #include "qgsmessagelog.h"
29 #include "qgsmessagebar.h"
30 #include "qgsgui.h"
31 #include "qgsbrowserguimodel.h"
32 #include "qgsbrowserwidget.h"
33 
34 QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QgsBrowserGuiModel *browserModel, QWidget *parent, QgsMapCanvas *canvas, Qt::WindowFlags fl )
35  : QgsOptionsDialogBase( tr( "Data Source Manager" ), parent, fl )
36  , ui( new Ui::QgsDataSourceManagerDialog )
37  , mPreviousRow( -1 )
38  , mMapCanvas( canvas )
39  , mBrowserModel( browserModel )
40 {
41  ui->setupUi( this );
42  ui->verticalLayout_2->setSpacing( 6 );
43  ui->verticalLayout_2->setContentsMargins( 0, 0, 0, 0 );
44 
45  mMessageBar = new QgsMessageBar( this );
46  mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
47  static_cast<QVBoxLayout *>( layout() )->insertWidget( 0, mMessageBar );
48 
49  // QgsOptionsDialogBase handles saving/restoring of geometry, splitter and current tab states,
50  // switching vertical tabs between icon/text to icon-only modes (splitter collapsed to left),
51  // and connecting QDialogButtonBox's accepted/rejected signals to dialog's accept/reject slots
52  initOptionsBase( false );
53 
54  // Bind list index to the stacked dialogs
55  connect( ui->mOptionsListWidget, &QListWidget::currentRowChanged, this, &QgsDataSourceManagerDialog::setCurrentPage );
56 
57  // BROWSER Add the browser widget to the first stacked widget page
58  mBrowserWidget = new QgsBrowserDockWidget( QStringLiteral( "Browser" ), mBrowserModel, this );
59  mBrowserWidget->setFeatures( QDockWidget::NoDockWidgetFeatures );
60  ui->mOptionsStackedWidget->addWidget( mBrowserWidget );
61  mPageNames.append( QStringLiteral( "browser" ) );
62  // Forward all browser signals
64  connect( mBrowserWidget, &QgsBrowserDockWidget::openFile, this, &QgsDataSourceManagerDialog::openFile );
67 
68  // Add provider dialogs
69  const QList<QgsSourceSelectProvider *> sourceSelectProviders = QgsGui::sourceSelectProviderRegistry()->providers( );
70  for ( QgsSourceSelectProvider *provider : sourceSelectProviders )
71  {
72  QgsAbstractDataSourceWidget *dlg = provider->createDataSourceWidget( this );
73  if ( !dlg )
74  {
75  QgsMessageLog::logMessage( tr( "Cannot get %1 select dialog from source select provider %2." ).arg( provider->name(), provider->providerKey() ), QStringLiteral( "DataSourceManager" ), Qgis::MessageLevel::Critical );
76  continue;
77  }
78  addProviderDialog( dlg, provider->providerKey(), provider->text(), provider->icon( ), provider->toolTip( ) );
79  }
80 
81  restoreOptionsBaseUi( tr( "Data Source Manager" ) );
82 }
83 
85 {
86  delete ui;
87 }
88 
89 void QgsDataSourceManagerDialog::openPage( const QString &pageName )
90 {
91  const int pageIdx = mPageNames.indexOf( pageName );
92  if ( pageIdx != -1 )
93  {
94  QTimer::singleShot( 0, this, [ = ] { setCurrentPage( pageIdx ); } );
95  }
96 }
97 
99 {
100  return mMessageBar;
101 }
102 
104 {
105  mPreviousRow = ui->mOptionsStackedWidget->currentIndex();
106  ui->mOptionsStackedWidget->setCurrentIndex( index );
107  setWindowTitle( tr( "Data Source Manager | %1" ).arg( ui->mOptionsListWidget->currentItem()->text() ) );
108  resizeAlltabs( index );
109 }
110 
112 {
113  const int prevPage = mPreviousRow != -1 ? mPreviousRow : 0;
114  setCurrentPage( prevPage );
115 }
116 
118 {
119  mBrowserWidget->browserWidget()->refresh();
120  emit providerDialogsRefreshRequested();
121 }
122 
124 {
125  const int pageCount = ui->mOptionsStackedWidget->count();
126  for ( int i = 0; i < pageCount; ++i )
127  {
128  QWidget *widget = ui->mOptionsStackedWidget->widget( i );
129  QgsAbstractDataSourceWidget *dataSourceWidget = qobject_cast<QgsAbstractDataSourceWidget *>( widget );
130  if ( dataSourceWidget )
131  dataSourceWidget->reset();
132  }
133 }
134 
135 void QgsDataSourceManagerDialog::rasterLayerAdded( const QString &uri, const QString &baseName, const QString &providerKey )
136 {
137  emit addRasterLayer( uri, baseName, providerKey );
138 }
139 
140 void QgsDataSourceManagerDialog::rasterLayersAdded( const QStringList &layersList )
141 {
142  emit addRasterLayers( layersList );
143 }
144 
145 void QgsDataSourceManagerDialog::vectorLayerAdded( const QString &vectorLayerPath, const QString &baseName, const QString &providerKey )
146 {
147  emit addVectorLayer( vectorLayerPath, baseName, providerKey );
148 }
149 
150 void QgsDataSourceManagerDialog::vectorLayersAdded( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType )
151 {
152  emit addVectorLayers( layerQStringList, enc, dataSourceType );
153 }
154 
155 void QgsDataSourceManagerDialog::addProviderDialog( QgsAbstractDataSourceWidget *dlg, const QString &providerKey, const QString &providerName, const QIcon &icon, const QString &toolTip )
156 {
157  mPageNames.append( providerKey );
158  ui->mOptionsStackedWidget->addWidget( dlg );
159  QListWidgetItem *layerItem = new QListWidgetItem( providerName, ui->mOptionsListWidget );
160  layerItem->setToolTip( toolTip.isEmpty() ? tr( "Add %1 layer" ).arg( providerName ) : toolTip );
161  layerItem->setIcon( icon );
162  // Set crs and extent from canvas
163  if ( mMapCanvas )
164  {
165  dlg->setMapCanvas( mMapCanvas );
166  }
167  dlg->setBrowserModel( mBrowserModel );
168 
169  connect( dlg, &QgsAbstractDataSourceWidget::rejected, this, &QgsDataSourceManagerDialog::reject );
170  connect( dlg, &QgsAbstractDataSourceWidget::accepted, this, &QgsDataSourceManagerDialog::accept );
171  makeConnections( dlg, providerKey );
172 }
173 
174 void QgsDataSourceManagerDialog::makeConnections( QgsAbstractDataSourceWidget *dlg, const QString &providerKey )
175 {
176  // DB
181  // Vector
182  connect( dlg, &QgsAbstractDataSourceWidget::addVectorLayer, this, [ = ]( const QString & vectorLayerPath, const QString & baseName, const QString & specifiedProvider )
183  {
184  const QString key = specifiedProvider.isEmpty() ? providerKey : specifiedProvider;
185  this->vectorLayerAdded( vectorLayerPath, baseName, key );
186  }
187  );
191  // Raster
193  this, [ = ]( const QString & uri, const QString & baseName, const QString & providerKey )
194  {
195  addRasterLayer( uri, baseName, providerKey );
196  } );
199  // Mesh
201  // Vector tile
203  // Point Cloud
205  // Virtual
208  // Common
211 
212  // Message
213  connect( dlg, &QgsAbstractDataSourceWidget::pushMessage, this, [ = ]( const QString & title, const QString & message, const Qgis::MessageLevel level )
214  {
215  mMessageBar->pushMessage( title, message, level );
216  } );
217 }
218 
220 {
221  ui->mOptionsStackedWidget->currentWidget()->show();
223  resizeAlltabs( ui->mOptionsStackedWidget->currentIndex() );
224 }
QgsDataSourceManagerDialog::showEvent
void showEvent(QShowEvent *event) override
Definition: qgsdatasourcemanagerdialog.cpp:219
QgsDataSourceManagerDialog::vectorLayerAdded
void vectorLayerAdded(const QString &vectorLayerPath, const QString &baseName, const QString &providerKey)
A vector layer was added: for signal forwarding to QgisApp.
Definition: qgsdatasourcemanagerdialog.cpp:145
QgsDataSourceManagerDialog::openPage
void openPage(const QString &pageName)
Open a given page in the dialog.
Definition: qgsdatasourcemanagerdialog.cpp:89
QgsDataSourceManagerDialog::~QgsDataSourceManagerDialog
~QgsDataSourceManagerDialog() override
Definition: qgsdatasourcemanagerdialog.cpp:84
QgsAbstractDataSourceWidget::addDatabaseLayers
void addDatabaseLayers(const QStringList &paths, const QString &providerKey)
Emitted when a DB layer has been selected for addition.
qgssourceselectprovider.h
QgsAbstractDataSourceWidget::addPointCloudLayer
void addPointCloudLayer(const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a point cloud layer has been selected for addition.
QgsAbstractDataSourceWidget
Abstract base Data Source Widget to create connections and add layers This class provides common func...
Definition: qgsabstractdatasourcewidget.h:43
QgsDataSourceManagerDialog::setPreviousPage
void setPreviousPage()
Reset current page to previously selected page.
Definition: qgsdatasourcemanagerdialog.cpp:111
QgsOptionsDialogBase
A base dialog for options and properties dialogs that offers vertical tabs. It handles saving/restori...
Definition: qgsoptionsdialogbase.h:86
QgsDataSourceManagerDialog::showStatusMessage
void showStatusMessage(const QString &message)
Emitted when a status message needs to be shown: for signal forwarding to QgisApp.
qgsmapcanvas.h
QgsAbstractDataSourceWidget::addRasterLayer
void addRasterLayer(const QString &rasterLayerPath, const QString &baseName, const QString &providerKey)
Emitted when a raster layer has been selected for addition.
qgsgui.h
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsSourceSelectProvider
This is the interface for those who want to add entries to the QgsDataSourceManagerDialog.
Definition: qgssourceselectprovider.h:35
QgsDataSourceManagerDialog::addVectorTileLayer
void addVectorTileLayer(const QString &uri, const QString &baseName)
Emitted when a vector tile layer was selected for addition: for signal forwarding to QgisApp.
QgsAbstractDataSourceWidget::addRasterLayers
void addRasterLayers(const QStringList &layersList)
Emitted when one or more GDAL supported layers are selected for addition.
QgsAbstractDataSourceWidget::addVectorLayers
void addVectorLayers(const QStringList &layerList, const QString &encoding, const QString &dataSourceType)
Emitted when one or more OGR supported layers are selected for addition.
qgsbrowserdockwidget.h
qgsabstractdatasourcewidget.h
QgsDataSourceManagerDialog::connectionsChanged
void connectionsChanged()
Emitted when a connection has changed inside the provider dialogs This signal is normally forwarded t...
QgsDataSourceManagerDialog::addDatabaseLayers
void addDatabaseLayers(const QStringList &layerPathList, const QString &providerKey)
Emitted when a DB layer was selected for addition: for signal forwarding to QgisApp.
QgsAbstractDataSourceWidget::replaceVectorLayer
void replaceVectorLayer(const QString &oldId, const QString &source, const QString &name, const QString &provider)
Emitted when a layer needs to be replaced.
QgsAbstractDataSourceWidget::connectionsChanged
void connectionsChanged()
Emitted when the provider's connections have changed This signal is normally forwarded the app and us...
QgsDataSourceManagerDialog::vectorLayersAdded
void vectorLayersAdded(const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType)
One or more vector layer were added: for signal forwarding to QgisApp.
Definition: qgsdatasourcemanagerdialog.cpp:150
QgsDataSourceManagerDialog::replaceSelectedVectorLayer
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.
QgsDataSourceManagerDialog::refresh
void refresh()
Refresh the browser view.
Definition: qgsdatasourcemanagerdialog.cpp:117
qgssourceselectproviderregistry.h
QgsAbstractDataSourceWidget::progressMessage
void progressMessage(QString message)
Emitted when a progress dialog is shown by the provider dialog.
QgsBrowserWidget::updateProjectHome
void updateProjectHome()
Update project home directory.
Definition: qgsbrowserwidget.cpp:431
qgsdatasourcemanagerdialog.h
QgsDataSourceManagerDialog::handleDropUriList
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled from the browser.
qgsproviderregistry.h
QgsDataSourceManagerDialog::addPointCloudLayer
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.
QgsDataSourceManagerDialog::rasterLayerAdded
void rasterLayerAdded(QString const &uri, QString const &baseName, QString const &providerKey)
A raster layer was added: for signal forwarding to QgisApp.
Definition: qgsdatasourcemanagerdialog.cpp:135
QgsBrowserDockWidget::connectionsChanged
void connectionsChanged()
Connections changed in the browser.
QgsAbstractDataSourceWidget::addVectorLayer
void addVectorLayer(const QString &uri, const QString &layerName, const QString &providerKey=QString())
Emitted when a vector layer has been selected for addition.
QgsDataSourceManagerDialog::rasterLayersAdded
void rasterLayersAdded(const QStringList &layersList)
One or more raster layer were added: for signal forwarding to QgisApp.
Definition: qgsdatasourcemanagerdialog.cpp:140
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:60
QgsMessageLog::logMessage
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).
Definition: qgsmessagelog.cpp:27
QgsOptionsDialogBase::initOptionsBase
void initOptionsBase(bool restoreUi=true, const QString &title=QString())
Set up the base ui connections for vertical tabs.
Definition: qgsoptionsdialogbase.cpp:67
qgsmessagebar.h
QgsAbstractDataSourceWidget::addVectorTileLayer
void addVectorTileLayer(const QString &url, const QString &baseName)
Emitted when a vector tile layer has been selected for addition.
QgsDataSourceManagerDialog::updateProjectHome
void updateProjectHome()
Update project home directory.
QgsBrowserDockWidget::openFile
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
QgsDataSourceManagerDialog::providerDialogsRefreshRequested
void providerDialogsRefreshRequested()
One or more provider connections have changed and the dialogs should be refreshed.
QgsAbstractDataSourceWidget::setBrowserModel
virtual void setBrowserModel(QgsBrowserModel *model)
Sets a browser model to use with the widget.
Definition: qgsabstractdatasourcewidget.cpp:56
QgsSourceSelectProviderRegistry::providers
QList< QgsSourceSelectProvider * > providers()
Gets list of available providers.
Definition: qgssourceselectproviderregistry.cpp:29
qgsbrowserwidget.h
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:114
QgsBrowserDockWidget::browserWidget
QgsBrowserWidget * browserWidget()
Returns a pointer to the QgsBrowserWidget used by the dock widget.
Definition: qgsbrowserdockwidget.cpp:49
QgsAbstractDataSourceWidget::addMeshLayer
void addMeshLayer(const QString &url, const QString &baseName, const QString &providerKey)
Emitted when a mesh layer has been selected for addition.
qgsbrowserguimodel.h
QgsDataSourceManagerDialog::setCurrentPage
void setCurrentPage(int index)
Sync current page with the leftbar list.
Definition: qgsdatasourcemanagerdialog.cpp:103
qgssettings.h
QgsAbstractDataSourceWidget::setMapCanvas
virtual void setMapCanvas(QgsMapCanvas *mapCanvas)
Sets the dialog map canvas.
Definition: qgsabstractdatasourcewidget.h:68
QgsOptionsDialogBase::showEvent
void showEvent(QShowEvent *e) override
Definition: qgsoptionsdialogbase.cpp:688
QgsDataSourceManagerDialog::reset
void reset()
Resets the interface of the datasource manager after reopening the dialog.
Definition: qgsdatasourcemanagerdialog.cpp:123
QgsBrowserDockWidget
A dock widget containing a QgsBrowserWidget for navigating and managing data sources.
Definition: qgsbrowserdockwidget.h:31
QgsDataSourceManagerDialog::QgsDataSourceManagerDialog
QgsDataSourceManagerDialog(QgsBrowserGuiModel *browserModel, QWidget *parent=nullptr, QgsMapCanvas *canvas=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
QgsDataSourceManagerDialog constructor.
Definition: qgsdatasourcemanagerdialog.cpp:34
QgsBrowserDockWidget::handleDropUriList
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled.
QgsAbstractDataSourceWidget::pushMessage
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...
QgsAbstractDataSourceWidget::refresh
virtual void refresh()
Triggered when the provider's connections need to be refreshed The default implementation does nothin...
Definition: qgsabstractdatasourcewidget.h:76
QgsAbstractDataSourceWidget::reset
virtual void reset()
Called when this source select widget is being shown in a "new and clean" dialog.
Definition: qgsabstractdatasourcewidget.cpp:65
QgsDataSourceManagerDialog::messageBar
QgsMessageBar * messageBar() const
Returns the dialog's message bar.
Definition: qgsdatasourcemanagerdialog.cpp:98
QgsBrowserGuiModel
A model for showing available data sources and other items in a structured tree.
Definition: qgsbrowserguimodel.h:36
QgsDataSourceManagerDialog::addMeshLayer
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.
QgsDataSourceManagerDialog::openFile
void openFile(const QString &fileName, const QString &fileTypeHint=QString())
Emitted when a file needs to be opened.
QgsOptionsDialogBase::restoreOptionsBaseUi
void restoreOptionsBaseUi(const QString &title=QString())
Restore the base ui.
Definition: qgsoptionsdialogbase.cpp:206
QgsGui::sourceSelectProviderRegistry
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition: qgsgui.cpp:100
qgsmessagelog.h