QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
33 QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QgsBrowserGuiModel *browserModel, QWidget *parent, QgsMapCanvas *canvas, Qt::WindowFlags fl )
34  : QgsOptionsDialogBase( QStringLiteral( "Data Source Manager" ), parent, fl )
35  , ui( new Ui::QgsDataSourceManagerDialog )
36  , mPreviousRow( -1 )
37  , mMapCanvas( canvas )
38 {
39  ui->setupUi( this );
40  ui->verticalLayout_2->setSpacing( 6 );
41  ui->verticalLayout_2->setMargin( 0 );
42  ui->verticalLayout_2->setContentsMargins( 0, 0, 0, 0 );
43 
44  mMessageBar = new QgsMessageBar( this );
45  mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
46  static_cast<QVBoxLayout *>( layout() )->insertWidget( 0, mMessageBar );
47 
48  // QgsOptionsDialogBase handles saving/restoring of geometry, splitter and current tab states,
49  // switching vertical tabs between icon/text to icon-only modes (splitter collapsed to left),
50  // and connecting QDialogButtonBox's accepted/rejected signals to dialog's accept/reject slots
51  initOptionsBase( false );
52 
53  // Bind list index to the stacked dialogs
54  connect( ui->mOptionsListWidget, &QListWidget::currentRowChanged, this, &QgsDataSourceManagerDialog::setCurrentPage );
55 
56  // BROWSER Add the browser widget to the first stacked widget page
57  mBrowserWidget = new QgsBrowserDockWidget( QStringLiteral( "Browser" ), browserModel, this );
58  mBrowserWidget->setFeatures( QDockWidget::NoDockWidgetFeatures );
59  ui->mOptionsStackedWidget->addWidget( mBrowserWidget );
60  mPageNames.append( QStringLiteral( "browser" ) );
61  // Forward all browser signals
63  connect( mBrowserWidget, &QgsBrowserDockWidget::openFile, this, &QgsDataSourceManagerDialog::openFile );
66 
67  // Add provider dialogs
68  const QList<QgsSourceSelectProvider *> sourceSelectProviders = QgsGui::sourceSelectProviderRegistry()->providers( );
69  for ( const auto &provider : sourceSelectProviders )
70  {
71  QgsAbstractDataSourceWidget *dlg = provider->createDataSourceWidget( this );
72  if ( !dlg )
73  {
74  QgsMessageLog::logMessage( tr( "Cannot get %1 select dialog from source select provider %2." ).arg( provider->name(), provider->providerKey() ), QStringLiteral( "DataSourceManager" ), Qgis::Critical );
75  continue;
76  }
77  addProviderDialog( dlg, provider->providerKey(), provider->text(), provider->icon( ), provider->toolTip( ) );
78  }
79 
80  restoreOptionsBaseUi( QStringLiteral( "Data Source Manager" ) );
81 }
82 
84 {
85  delete ui;
86 }
87 
88 void QgsDataSourceManagerDialog::openPage( const QString &pageName )
89 {
90  int pageIdx = mPageNames.indexOf( pageName );
91  if ( pageIdx != -1 )
92  {
93  QTimer::singleShot( 0, this, [ = ] { setCurrentPage( pageIdx ); } );
94  }
95 }
96 
98 {
99  return mMessageBar;
100 }
101 
103 {
104  mPreviousRow = ui->mOptionsStackedWidget->currentIndex();
105  ui->mOptionsStackedWidget->setCurrentIndex( index );
106  setWindowTitle( tr( "Data Source Manager | %1" ).arg( ui->mOptionsListWidget->currentItem()->text() ) );
107  resizeAlltabs( index );
108 }
109 
111 {
112  int prevPage = mPreviousRow != -1 ? mPreviousRow : 0;
113  setCurrentPage( prevPage );
114 }
115 
117 {
118  mBrowserWidget->refresh();
119  emit providerDialogsRefreshRequested();
120 }
121 
123 {
124  int pageCount = ui->mOptionsStackedWidget->count();
125  for ( int i = 0; i < pageCount; ++i )
126  {
127  QWidget *widget = ui->mOptionsStackedWidget->widget( i );
128  QgsAbstractDataSourceWidget *dataSourceWidget = qobject_cast<QgsAbstractDataSourceWidget *>( widget );
129  if ( dataSourceWidget )
130  dataSourceWidget->reset();
131  }
132 }
133 
134 void QgsDataSourceManagerDialog::rasterLayerAdded( const QString &uri, const QString &baseName, const QString &providerKey )
135 {
136  emit addRasterLayer( uri, baseName, providerKey );
137 }
138 
139 void QgsDataSourceManagerDialog::vectorLayerAdded( const QString &vectorLayerPath, const QString &baseName, const QString &providerKey )
140 {
141  emit addVectorLayer( vectorLayerPath, baseName, providerKey );
142 }
143 
144 void QgsDataSourceManagerDialog::vectorLayersAdded( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType )
145 {
146  emit addVectorLayers( layerQStringList, enc, dataSourceType );
147 }
148 
149 void QgsDataSourceManagerDialog::addProviderDialog( QgsAbstractDataSourceWidget *dlg, const QString &providerKey, const QString &providerName, const QIcon &icon, const QString &toolTip )
150 {
151  mPageNames.append( providerKey );
152  ui->mOptionsStackedWidget->addWidget( dlg );
153  QListWidgetItem *layerItem = new QListWidgetItem( providerName, ui->mOptionsListWidget );
154  layerItem->setToolTip( toolTip.isEmpty() ? tr( "Add %1 layer" ).arg( providerName ) : toolTip );
155  layerItem->setIcon( icon );
156  // Set crs and extent from canvas
157  if ( mMapCanvas )
158  {
159  dlg->setMapCanvas( mMapCanvas );
160  }
161  connect( dlg, &QgsAbstractDataSourceWidget::rejected, this, &QgsDataSourceManagerDialog::reject );
162  connect( dlg, &QgsAbstractDataSourceWidget::accepted, this, &QgsDataSourceManagerDialog::accept );
163  makeConnections( dlg, providerKey );
164 }
165 
166 void QgsDataSourceManagerDialog::makeConnections( QgsAbstractDataSourceWidget *dlg, const QString &providerKey )
167 {
168  // DB
173  // Vector
174  connect( dlg, &QgsAbstractDataSourceWidget::addVectorLayer, this, [ = ]( const QString & vectorLayerPath, const QString & baseName, const QString & specifiedProvider )
175  {
176  QString key = specifiedProvider.isEmpty() ? providerKey : specifiedProvider;
177  this->vectorLayerAdded( vectorLayerPath, baseName, key );
178  }
179  );
183  // Raster
185  this, [ = ]( const QString & uri, const QString & baseName, const QString & providerKey )
186  {
187  addRasterLayer( uri, baseName, providerKey );
188  } );
189  // Mesh
191  // Vector tile
193  // Virtual
196  // Common
199 
200  // Message
201  connect( dlg, &QgsAbstractDataSourceWidget::pushMessage, this, [ = ]( const QString & title, const QString & message, const Qgis::MessageLevel level )
202  {
203  mMessageBar->pushMessage( title, message, level );
204  } );
205 }
206 
208 {
209  ui->mOptionsStackedWidget->currentWidget()->show();
211  resizeAlltabs( ui->mOptionsStackedWidget->currentIndex() );
212 }
QgsDataSourceManagerDialog::showEvent
void showEvent(QShowEvent *event) override
Definition: qgsdatasourcemanagerdialog.cpp:207
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:139
QgsDataSourceManagerDialog::openPage
void openPage(const QString &pageName)
Open a given page in the dialog.
Definition: qgsdatasourcemanagerdialog.cpp:88
QgsDataSourceManagerDialog::~QgsDataSourceManagerDialog
~QgsDataSourceManagerDialog() override
Definition: qgsdatasourcemanagerdialog.cpp:83
QgsAbstractDataSourceWidget::addDatabaseLayers
void addDatabaseLayers(const QStringList &paths, const QString &providerKey)
Emitted when a DB layer has been selected for addition.
qgssourceselectprovider.h
QgsAbstractDataSourceWidget
Abstract base Data Source Widget to create connections and add layers This class provides common func...
Definition: qgsabstractdatasourcewidget.h:42
QgsDataSourceManagerDialog::setPreviousPage
void setPreviousPage()
Reset current page to previously selected page.
Definition: qgsdatasourcemanagerdialog.cpp:110
QgsOptionsDialogBase
Definition: qgsoptionsdialogbase.h:62
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
Definition: qgsmapcanvas.h:83
QgsAbstractDataSourceWidget::setMapCanvas
void setMapCanvas(const QgsMapCanvas *mapCanvas)
Store a pointer to the map canvas to retrieve extent and CRS Used to select an appropriate CRS and po...
Definition: qgsabstractdatasourcewidget.cpp:56
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::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
QgsBrowserDockWidget::updateProjectHome
void updateProjectHome()
Update project home directory.
Definition: qgsbrowserdockwidget.cpp:509
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:144
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:116
qgssourceselectproviderregistry.h
QgsAbstractDataSourceWidget::progressMessage
void progressMessage(QString message)
Emitted when a progress dialog is shown by the provider dialog.
qgsdatasourcemanagerdialog.h
QgsDataSourceManagerDialog::handleDropUriList
void handleDropUriList(const QgsMimeDataUtils::UriList &)
Emitted when drop uri list needs to be handled from the browser.
qgsproviderregistry.h
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:134
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.
QgsMessageBar
Definition: qgsmessagebar.h:60
QgsOptionsDialogBase::initOptionsBase
void initOptionsBase(bool restoreUi=true, const QString &title=QString())
Set up the base ui connections for vertical tabs.
Definition: qgsoptionsdialogbase.cpp:65
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.
QgsSourceSelectProviderRegistry::providers
QList< QgsSourceSelectProvider * > providers()
Gets list of available providers.
Definition: qgssourceselectproviderregistry.cpp:29
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:101
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:102
qgssettings.h
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
QgsOptionsDialogBase::showEvent
void showEvent(QShowEvent *e) override
Definition: qgsoptionsdialogbase.cpp:379
QgsDataSourceManagerDialog::reset
void reset()
Resets the interface of the datasource manager after reopening the dialog.
Definition: qgsdatasourcemanagerdialog.cpp:122
QgsBrowserDockWidget
Definition: qgsbrowserdockwidget.h:43
Qgis::Critical
@ Critical
Definition: qgis.h:105
QgsDataSourceManagerDialog::QgsDataSourceManagerDialog
QgsDataSourceManagerDialog(QgsBrowserGuiModel *browserModel, QWidget *parent=nullptr, QgsMapCanvas *canvas=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
QgsDataSourceManagerDialog constructor.
Definition: qgsdatasourcemanagerdialog.cpp:33
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:61
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:97
QgsBrowserGuiModel
Definition: qgsbrowserguimodel.h:37
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:161
QgsGui::sourceSelectProviderRegistry
static QgsSourceSelectProviderRegistry * sourceSelectProviderRegistry()
Returns the global source select provider registry, used for managing all known source select widget ...
Definition: qgsgui.cpp:78
qgsmessagelog.h